blob: 6f4443d3daa84cdb7f0099414c611af70f7eda98 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Balogh and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andras Balogh - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.imports.vtml;
import java.util.HashSet;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IModelManager;
import org.eclipse.viatra2.core.IModelSpace;
import org.eclipse.viatra2.errors.VPMCoreException;
/**
* @author babo
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class EntityDefinition extends ElementDefinition {
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
IEntity element = null;
public IModelElement getElement()
{
if (element==null)
{
elemGen(VTMLMetaModelHelper.getInstance().getMSpace());
}
return element;
}
public String toString() {
// TODO Auto-generated method stub
return "entity name : "+name+" type : "+type+" namespace : "+namespace+" value : "+value;
}
String namespace;
String type;
String name;
String value;
IEntity ns;
IModelElement typ;
HashSet<EntityDefinition> content = new HashSet<EntityDefinition>();
public void addContent(EntityDefinition e)
{
content.add(e);
}
public boolean isNative = false;
public EntityDefinition(IEntity e)
{
element = e;
name = e.getFullyQualifiedName();
namespace = "";
isNative = true;
}
public EntityDefinition(int l, String ns, String n, String t, String v)
{
setLine(l);
namespace = ns;
name = n;
type = t;
value = v;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the namespace.
*/
public String getNamespace() {
return namespace;
}
/**
* @param namespace The namespace to set.
*/
public void setNamespace(String namespace) {
this.namespace = namespace;
}
/**
* @return Returns the type.
*/
public String getType() {
return type;
}
/**
* @param type The type to set.
*/
public void setType(String type) {
this.type = type;
}
/**
* @return Returns the value.
*/
public String getValue() {
return value;
}
public String getFQN()
{
return fqn(namespace,name);
}
/**
* @param value The value to set.
*/
public void setValue(String value) {
this.value = value;
}
EntityDefinition _type =null;
EntityDefinition _namespace =null;
public void normalizeName(ASTBuilder ab)
{
if (name.indexOf('.')>=0)
{
namespace = ab.parentName(name);
name = ab.localName(name);
}
}
/* (non-Javadoc)
* @see org.eclipse.viatra2.imports.vtcl.ASTNode#checkDefintion(org.eclipse.viatra2.imports.vtcl.ASTBuilder, org.eclipse.viatra2.core.IModelSpace)
*/
public boolean checkDefintion(ASTBuilder ab, IModelSpace m) {
if (!isNative)
{
String tmp = null;
tmp = ab.fqn(namespace,name);
namespace = ab.parentName(tmp);
name = ab.localName(tmp);
_namespace = ab.getEntityWithLookup("",namespace);
_type = ab.getEntityWithLookup(namespace,type);
if ((_type==null) && ((type==null) || (!type.equals("entity"))))
{
ab.log.error("type "+type+" for entity "+name+" not found. Error in line "+getLine());
return false;
}
if (ab.lookupDuplicateEntity(this)!=null)
{
ab.log.error("Entity "+name+" in namespace "+namespace+" already defined. Error in line "+getLine());
return false;
}
}
for (EntityDefinition e : ((HashSet<EntityDefinition>)content.clone()))
{
boolean b = e.checkDefintion(ab,m);
if (!b) return b;
}
for (RelationDefinition r : relations)
{
boolean btm = r.checkDefintion(ab,m);
if (!btm) return false;
}
return true;
}
IEntity me,tmp;
String tmpName;
IModelManager mm;
public void generateElements(ASTBuilder ab, IModelSpace m,IEntity temp) throws VPMCoreException
{
mm=m.getModelManager();tmp=temp;
tmpName = ab.nameGenerator();
if (value!=null)
me = m.getModelManager().newEntity(tmpName,value,temp);
else
me = m.getModelManager().newEntity(tmpName,temp);
}
/* (non-Javadoc)
* @see org.eclipse.viatra2.imports.vtcl.ASTNode#resolveReferences(org.eclipse.viatra2.imports.vtcl.ASTBuilder)
*/
public void resolveReferences(ASTBuilder ab) throws VPMCoreException {
}
/* (non-Javadoc)
* @see org.eclipse.viatra2.imports.vtcl.ASTNode#updateModelspace(org.eclipse.viatra2.imports.vtcl.ASTBuilder, org.eclipse.viatra2.core.IModelSpace)
*/
public void updateModelspace(ASTBuilder ab, IModelSpace m) throws VPMCoreException {
elemGen(m);
for (EntityDefinition e : content)
{
e.updateModelspace(ab,m);
}
for (RelationDefinition r : relations)
{
r.updateModelspace(ab,m);
}
}
public void elemGen(IModelSpace m)
{
try
{
if (element==null)
{
if (_namespace!=null)
{
element = m.getModelManager().newEntity(name,(IEntity)_namespace.getElement());
}
else
{
element = m.getModelManager().newEntity(name);
}
if (value!=null) m.getModelManager().setValue(element,value);
if( _type!=null) m.getModelManager().newInstanceOf(_type.getElement(),element);
}
}
catch (Exception e)
{
//TODO
e.printStackTrace();
}
}
/**
* @return Returns the content.
*/
public HashSet<EntityDefinition> getContent() {
return content;
}
public void printDebug(int i)
{
System.out.println(DBUG.substring(0,i)+getFQN());
for (EntityDefinition e : content)
{
e.printDebug(i+3);
}
for (RelationDefinition e : relations)
{
e.printDebug(i+3);
}
}
}