blob: afc5142a39d6dd768435704102ef35a934bdf75d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath 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 Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.core.simple;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.simple.cache.TrickyCollection;
/**
* Class for representing VPM entities
*
* @author Andras Schmidt
*
*/
public class SimpleEntity extends SimpleModelElement implements IEntity {
/**
* The parent of this entity
*/
protected SimpleEntity parent = null;
/**
* The contained entities of this entity
*/
protected TreeMap<Long, IModelElement> containment;
/**
* The value stored in this entity
*/
protected String value;
/**
* Instantiate an entity with given id in the given modelspace
*
* @param ID
* @param ms
*/
public SimpleEntity(long ID, SimpleModelSpace ms) {
super(ID, ms);
containment = new TreeMap<Long, IModelElement>();
value = "";
}
/**
* Gets the entitys value
*
* @return the value
*/
public String getValue() {
try {
modelSpace.lock.readLock().lock();
return value;
} finally {
modelSpace.lock.readLock().unlock();
}
}
/**
* Retrives a model element that is contained by this entity and has a given
* name
*
* @param n
* name of element to return
* @return element ref, or null if not found
*/
public IModelElement getContentByName(String n) {
try {
modelSpace.lock.readLock().lock();
Long id = namespace.get(n);
if (id != null) {
IEntity me = (IEntity) containment.get(id);
return me;
}
return null;
} finally {
modelSpace.lock.readLock().unlock();
}
}
/**
* Retrives a model element that is contained by this entity and has a given
* type
*
* @param n
* type of element to return
* @return element ref, or null if not found
*/
public IModelElement getContentByType(IModelElement n) {
try {
modelSpace.lock.readLock().lock();
Iterator<IModelElement> it = containment.values().iterator();
while (it.hasNext()) {
SimpleModelElement me = (SimpleModelElement) it.next();
if (me.isInstanceOf(n))
return me;
}
return null;
} finally {
modelSpace.lock.readLock().unlock();
}
}
/**
* Returns nested elements of the Entity
*
* @return Set of contained elements.
*/
public Collection<IModelElement> getContents() {
try {
modelSpace.lock.readLock().lock();
return containment.values();
// TODO konstans
} finally {
modelSpace.lock.readLock().unlock();
}
}
public Collection<IModelElement> getAllComponents() {
return new TrickyCollection(null, this);
// return _getAllComponents();
}
/**
* Returns the components of the entity.
*
* @return set of elements contained by the entity. (recoursive)
*/
public Collection<IModelElement> _getAllComponents() {
try {
modelSpace.lock.readLock().lock();
ArrayList<IModelElement> ret = new ArrayList<IModelElement>();
ret.addAll(getContents());
for (IModelElement elem : getContents()) {
if (elem instanceof IEntity) {
SimpleEntity e = (SimpleEntity) elem;
ret.addAll(e._getAllComponents());
}
}
return ret;
} finally {
modelSpace.lock.readLock().unlock();
}
}
public IEntity getParent() {
try {
modelSpace.lock.readLock().lock();
return parent;
} finally {
modelSpace.lock.readLock().unlock();
}
}
/**
* Returns one of the element's fully qualified name
*
* @return name
*/
@Override
public String getFullyQualifiedName() {
try {
modelSpace.lock.readLock().lock();
String name = getName();
IEntity root = modelSpace.getModelManager().getRoot();
if (root == this)
return "";
if (!(root == parent) && !(parent == null))
name = parent.getFullyQualifiedName() + "." + name;
return name;
} finally {
modelSpace.lock.readLock().unlock();
}
}
/*
* (non-Javadoc)
*
* @see hu.bme.mit.viatra.core.IModelElement#getNamespace()
*/
public IModelElement getNamespace() {
try {
modelSpace.lock.readLock().lock();
return parent;
} finally {
modelSpace.lock.readLock().unlock();
}
}
public boolean isEntity() {
return true;
}
public boolean isRelation() {
return false;
}
}