use of org.eclipse.emf.ecore.impl.BasicEObjectImpl in project iobserve-analysis by research-iobserve.
the class ModelProvider method createNodes.
/**
* Helper method for writing: Writes the given component into the provider's {@link #graph}
* recursively. Calls to this method have to be performed from inside a {@link Transaction}.
*
* @param component
* Component to save
* @param containmentsAndDatatypes
* Set of EObjects contained in the root preferably created by
* {@link #getAllContainmentsAndDatatypes(EObject, HashSet)}
* @param objectsToCreatedNodes
* Initially empty map of EObjects to already created correspondent nodes to make
* sure nodes are written just once
* @return Root node of the component's graph
*/
private Node createNodes(final EObject component, final Set<EObject> containmentsAndDatatypes, final Map<EObject, Node> objectsToCreatedNodes) {
// Create a label representing the type of the component
final Label label = Label.label(ModelProviderUtil.getTypeName(component.eClass()));
Node node = null;
// Check if node has already been created
final EAttribute idAttr = component.eClass().getEIDAttribute();
if (idAttr != null) {
node = this.graph.getGraphDatabaseService().findNode(label, ModelProvider.ID, component.eGet(idAttr));
} else if (component instanceof PrimitiveDataType) {
node = this.graph.getGraphDatabaseService().findNode(label, ModelProvider.TYPE, ((PrimitiveDataType) component).getType().name());
} else if (component instanceof UsageModel || component instanceof ResourceEnvironment) {
final ResourceIterator<Node> nodes = this.graph.getGraphDatabaseService().findNodes(Label.label(component.eClass().getName()));
if (nodes.hasNext()) {
node = nodes.next();
}
} else {
// For components that cannot be found in the graph (e.g. due to missing id) but have
// been created in this recursion
node = objectsToCreatedNodes.get(component);
}
// If there is no node yet, create one
if (node == null) {
node = this.graph.getGraphDatabaseService().createNode(label);
objectsToCreatedNodes.put(component, node);
// Create a URI to enable proxy resolving
final URI uri = ((BasicEObjectImpl) component).eProxyURI();
if (uri == null) {
node.setProperty(ModelProvider.EMF_URI, ModelProviderUtil.getUriString(component));
} else {
node.setProperty(ModelProvider.EMF_URI, uri.toString());
}
// Save attributes as node properties
for (final EAttribute attr : component.eClass().getEAllAttributes()) {
final Object value = component.eGet(attr);
if (value != null) {
node.setProperty(attr.getName(), value.toString());
}
}
// otherwise we just store the blank node as a proxy
if (containmentsAndDatatypes.contains(component)) {
for (final EReference ref : component.eClass().getEAllReferences()) {
final Object refReprensation = component.eGet(ref);
// 0..* refs are represented as a list and 1 refs are represented directly
if (refReprensation instanceof EList<?>) {
final EList<?> refs = (EList<?>) component.eGet(ref);
for (int i = 0; i < refs.size(); i++) {
final Object o = refs.get(i);
final Node refNode = this.createNodes((EObject) o, containmentsAndDatatypes, objectsToCreatedNodes);
final Relationship rel = node.createRelationshipTo(refNode, ModelProviderUtil.getRelationshipType(ref, o));
rel.setProperty(ModelProvider.REF_NAME, ref.getName());
rel.setProperty(ModelProvider.REF_POS, i);
}
} else {
if (refReprensation != null) {
final Node refNode = this.createNodes((EObject) refReprensation, containmentsAndDatatypes, objectsToCreatedNodes);
final Relationship rel = node.createRelationshipTo(refNode, ModelProviderUtil.getRelationshipType(ref, refReprensation));
rel.setProperty(ModelProvider.REF_NAME, ref.getName());
rel.setProperty(ModelProvider.REF_POS, 0);
}
}
}
}
}
return node;
}
use of org.eclipse.emf.ecore.impl.BasicEObjectImpl in project iobserve-analysis by research-iobserve.
the class EvalProxyDBTest method checkProxies.
private void checkProxies(final String label, final Two retrievedModelTwo) {
System.out.println("Step " + label);
for (final Link l : retrievedModelTwo.getLinks()) {
final Other ref = l.getReference();
System.out.println("Link " + ref.getName() + " " + ref.eIsProxy() + " -- " + ((BasicEObjectImpl) ref).eProxyURI());
}
}
use of org.eclipse.emf.ecore.impl.BasicEObjectImpl in project iobserve-analysis by research-iobserve.
the class ModelObjectFactory method createProxyObject.
/**
* Create a proxy object from node.
*
* @param node
* the node
* @param factories
* collection of classes which belong to the partition or metamodel
* @return returns the proxy object
*/
public static EObject createProxyObject(final Node node, final Set<EFactory> factories) {
final Label label = ModelObjectFactory.getEMFTypeLabel(node.getLabels());
final EObject object = ModelObjectFactory.instantiateEObject(factories, label.name());
((BasicEObjectImpl) object).eSetProxyURI(ModelGraphFactory.getUri(node));
return object;
}
use of org.eclipse.emf.ecore.impl.BasicEObjectImpl in project iobserve-analysis by research-iobserve.
the class EvalProxyTest method checkProxies.
private void checkProxies(final String label, final Resource resource) {
System.out.println("Step " + label);
for (final EObject o : resource.getContents()) {
if (o instanceof Two) {
for (final EObject l : ((Two) o).getLinks()) {
if (l instanceof Link) {
final Other ref = ((Link) l).getReference();
System.out.println("Link " + ref.getName() + " " + ref.eIsProxy() + " -- " + ((BasicEObjectImpl) ref).eProxyURI());
}
}
}
}
}
use of org.eclipse.emf.ecore.impl.BasicEObjectImpl in project iobserve-analysis by research-iobserve.
the class ModelProvider method updateNodes.
private Node updateNodes(final EObject component, final Node node, final Set<EObject> containmentsAndDatatypes, final Set<EObject> updatedComponents) {
if (!updatedComponents.contains(component)) {
updatedComponents.add(component);
// Update node properties
final Map<String, Object> nodeProperties = node.getAllProperties();
for (final EAttribute attr : component.eClass().getEAllAttributes()) {
final String key = attr.getName();
final Object value = component.eGet(attr);
if (value != null) {
node.setProperty(key, value.toString());
nodeProperties.remove(key);
}
}
// Remove possibly removed properties
final Iterator<Entry<String, Object>> iter = nodeProperties.entrySet().iterator();
while (iter.hasNext()) {
node.removeProperty(iter.next().getKey());
}
// Create a URI to enable proxy resolving
final URI uri = ((BasicEObjectImpl) component).eProxyURI();
if (uri == null) {
node.setProperty(ModelProvider.EMF_URI, ModelProviderUtil.getUriString(component));
} else {
node.setProperty(ModelProvider.EMF_URI, uri.toString());
}
// otherwise we just store the blank node as a proxy
if (containmentsAndDatatypes.contains(component)) {
// Create list of node's outgoing relationships
final List<Relationship> outRels = new LinkedList<>();
node.getRelationships(Direction.OUTGOING).forEach(outRels::add);
for (final EReference ref : component.eClass().getEAllReferences()) {
final Object refReprensation = component.eGet(ref);
// 0..* refs are represented as a list and 1 refs are represented directly
if (refReprensation instanceof EList<?>) {
final EList<?> refs = (EList<?>) component.eGet(ref);
for (int i = 0; i < refs.size(); i++) {
final Object o = refs.get(i);
// Find node matching o
Node endNode = ModelProviderUtil.findMatchingNode(ModelProviderUtil.getUriString((EObject) o), outRels);
if (endNode == null) {
// Create a non existing node
endNode = this.createNodes((EObject) o, containmentsAndDatatypes, new HashMap<>());
final Relationship rel = node.createRelationshipTo(endNode, ModelProviderUtil.getRelationshipType(ref, o));
rel.setProperty(ModelProvider.REF_NAME, ref.getName());
rel.setProperty(ModelProvider.REF_POS, i);
}
this.updateNodes((EObject) o, endNode, containmentsAndDatatypes, updatedComponents);
}
} else {
if (refReprensation != null) {
// Find node matching refRepresentation
Node endNode = ModelProviderUtil.findMatchingNode(ModelProviderUtil.getUriString((EObject) refReprensation), outRels);
if (endNode == null) {
// Create a non existing node
endNode = this.createNodes((EObject) refReprensation, containmentsAndDatatypes, new HashMap<>());
final Relationship rel = node.createRelationshipTo(endNode, ModelProviderUtil.getRelationshipType(ref, refReprensation));
rel.setProperty(ModelProvider.REF_NAME, ref.getName());
rel.setProperty(ModelProvider.REF_POS, 0);
}
this.updateNodes((EObject) refReprensation, endNode, containmentsAndDatatypes, updatedComponents);
}
}
}
// Delete nodes that are not referenced anymore
for (final Relationship r : outRels) {
try {
final Node endNode = r.getEndNode();
r.delete();
this.deleteComponentAndDatatypeNodes(endNode, false);
} catch (final NotFoundException e) {
// relation has already been deleted
}
}
}
}
return node;
}
Aggregations