Search in sources :

Example 1 with BasicEObjectImpl

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;
}
Also used : ResourceEnvironment(org.palladiosimulator.pcm.resourceenvironment.ResourceEnvironment) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) URI(org.eclipse.emf.common.util.URI) BasicEObjectImpl(org.eclipse.emf.ecore.impl.BasicEObjectImpl) EAttribute(org.eclipse.emf.ecore.EAttribute) PrimitiveDataType(org.palladiosimulator.pcm.repository.PrimitiveDataType) EList(org.eclipse.emf.common.util.EList) UsageModel(org.palladiosimulator.pcm.usagemodel.UsageModel) Relationship(org.neo4j.graphdb.Relationship) EObject(org.eclipse.emf.ecore.EObject) EReference(org.eclipse.emf.ecore.EReference)

Example 2 with BasicEObjectImpl

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());
    }
}
Also used : Link(org.iobserve.model.test.storage.two.Link) BasicEObjectImpl(org.eclipse.emf.ecore.impl.BasicEObjectImpl) Other(org.iobserve.model.test.storage.one.Other)

Example 3 with BasicEObjectImpl

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;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Label(org.neo4j.graphdb.Label) BasicEObjectImpl(org.eclipse.emf.ecore.impl.BasicEObjectImpl)

Example 4 with BasicEObjectImpl

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());
                }
            }
        }
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) Two(org.iobserve.model.test.storage.two.Two) Link(org.iobserve.model.test.storage.two.Link) BasicEObjectImpl(org.eclipse.emf.ecore.impl.BasicEObjectImpl) Other(org.iobserve.model.test.storage.one.Other)

Example 5 with BasicEObjectImpl

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;
}
Also used : HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) NotFoundException(org.neo4j.graphdb.NotFoundException) URI(org.eclipse.emf.common.util.URI) BasicEObjectImpl(org.eclipse.emf.ecore.impl.BasicEObjectImpl) LinkedList(java.util.LinkedList) EAttribute(org.eclipse.emf.ecore.EAttribute) Entry(java.util.Map.Entry) EList(org.eclipse.emf.common.util.EList) Relationship(org.neo4j.graphdb.Relationship) EObject(org.eclipse.emf.ecore.EObject) EObject(org.eclipse.emf.ecore.EObject) EReference(org.eclipse.emf.ecore.EReference)

Aggregations

BasicEObjectImpl (org.eclipse.emf.ecore.impl.BasicEObjectImpl)6 EObject (org.eclipse.emf.ecore.EObject)5 EList (org.eclipse.emf.common.util.EList)3 URI (org.eclipse.emf.common.util.URI)3 EAttribute (org.eclipse.emf.ecore.EAttribute)3 EReference (org.eclipse.emf.ecore.EReference)3 Label (org.neo4j.graphdb.Label)3 Node (org.neo4j.graphdb.Node)3 Relationship (org.neo4j.graphdb.Relationship)3 Entry (java.util.Map.Entry)2 Other (org.iobserve.model.test.storage.one.Other)2 Link (org.iobserve.model.test.storage.two.Link)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Two (org.iobserve.model.test.storage.two.Two)1 NotFoundException (org.neo4j.graphdb.NotFoundException)1 PrimitiveDataType (org.palladiosimulator.pcm.repository.PrimitiveDataType)1 ResourceEnvironment (org.palladiosimulator.pcm.resourceenvironment.ResourceEnvironment)1 UsageModel (org.palladiosimulator.pcm.usagemodel.UsageModel)1