View Javadoc

1   //
2   // $Revision: 5 $
3   // $LastChangedBy: mhanns $
4   // $Date: 2010-04-01 10:10:45 +0200 (Do, 01 Apr 2010) $
5   // $HeadURL:
6   // svn://localhost/winf-ps/trunk/repository/src/main/java/de/uni_leipzig/wifa/iwi/mr3/dao/impl/neo4j/NeoHelper.java
7   // $
8   //
9   
10  package de.uni_leipzig.wifa.iwi.mr3.dao.neo4j.impl;
11  
12  import java.util.Iterator;
13  
14  import org.eclipse.emf.ecore.EcorePackage;
15  import org.neo4j.api.core.Direction;
16  import org.neo4j.api.core.NeoService;
17  import org.neo4j.api.core.Node;
18  import org.neo4j.api.core.Relationship;
19  import org.neo4j.api.core.ReturnableEvaluator;
20  import org.neo4j.api.core.StopEvaluator;
21  import org.neo4j.api.core.Traverser.Order;
22  
23  import de.uni_leipzig.wifa.iwi.mr3.dao.Constants;
24  import de.uni_leipzig.wifa.iwi.mr3.dao.neo4j.EcoreRelationshipType;
25  
26  /**
27   * Helper class for DAO layer, specially for neo4j persistence.
28   */
29  public class NeoHelper
30  {
31    /** Neo4j service. */
32    private NeoService neo;
33  
34    /**
35     * Setter.
36     * 
37     * @param neoService
38     *          Neo4j service
39     */
40    public void setNeo(final NeoService neoService)
41    {
42      neo = neoService;
43    }
44  
45    /**
46     * Getter.
47     * 
48     * @return Neo4j service
49     */
50    public NeoService getNeo()
51    {
52      return neo;
53    }
54  
55    /**
56     * Determine all information from a given node.
57     *
58     * @param node
59     *          Node
60     * @return Informations
61     */
62    public String toString(final Node node)
63    {
64      final StringBuilder buf = new StringBuilder("[").append(node.getId());
65  
66      for (final Relationship rel : node.getRelationships(Direction.OUTGOING))
67      {
68        buf.append("|").append(rel.getType().toString()).append("(").append(rel.getId()).append(")").append("->").append(
69            rel.getEndNode().getId());
70      }
71      for (final String key : node.getPropertyKeys())
72      {
73        buf.append("|").append(key).append(":").append(node.getProperty(key));
74      }
75      return buf.append("]").toString();
76    }
77  
78    /**
79     * Determines the model node for the given namespace uri.
80     *
81     * @param nsUri
82     *          the model's namespace uri
83     * @return the model's node representation of <code>EPackage</code>
84     */
85    public Node getModelNode(final String nsUri)
86    {
87      // model node
88      final Iterable<Node> models = getSubrefNodeChildren(EcoreRelationshipType.RESOURCES);
89      for (final Node model : models)
90      {
91        if (model.getProperty(Constants.PROPERTY_NS_URI).equals(nsUri))
92        {
93          return model;
94        }
95      }
96      return null;
97    }
98  
99    /**
100    * Create a subref node that is bound to the reference node by the specified
101    * relationship type.
102    *
103    * @param relationshipType
104    *          the relationship type
105    * @return the subref node
106    */
107   private Node createSubrefNode(final EcoreRelationshipType relationshipType)
108   {
109     final Node subrefNode = neo.createNode();
110     neo.getReferenceNode().createRelationshipTo(subrefNode, relationshipType);
111     return subrefNode;
112   }
113 
114   /**
115    * Determines the one and only subref node bound to the reference node by the
116    * specified relationship type.
117    *
118    * @param relationshipType
119    *          the relationship type
120    * @return the subref node
121    */
122   public Node getSubrefNode(final EcoreRelationshipType relationshipType)
123   {
124     final Node root = neo.getReferenceNode();
125     final Iterator<Relationship> iter = root.getRelationships(relationshipType, Direction.OUTGOING).iterator();
126     if (iter.hasNext())
127     {
128       return iter.next().getEndNode();
129     }
130     else
131     {
132       return createSubrefNode(relationshipType);
133     }
134   }
135 
136   /**
137    * Determines the relationships from a subref node to its children.
138    *
139    * @param relationshipType
140    *          the relationship type
141    * @return Relationship Iterable
142    */
143   private Iterable<Relationship> getSubrefRelationships(
144       final EcoreRelationshipType relationshipType)
145   {
146     return getSubrefNode(relationshipType).getRelationships(Direction.OUTGOING);
147   }
148 
149   /**
150    * Determines the children of the subrefNode bound to the reference node by
151    * the specified relationship type.
152    *
153    * @param relationshipType
154    *          the relationship type from reference node to subreference node.
155    * @return subref node children Iterable
156    */
157   public Iterable<Node> getSubrefNodeChildren(final EcoreRelationshipType relationshipType)
158   {
159     return new Iterable<Node>()
160     {
161       /**
162        * @see java.lang.Iterable#iterator()
163        */
164       @Override
165       public Iterator<Node> iterator()
166       {
167         final Iterator<Relationship> iter = getSubrefRelationships(relationshipType).iterator();
168 
169         return new Iterator<Node>()
170         {
171           /**
172            * @see java.util.Iterator#hasNext()
173            */
174           @Override
175           public boolean hasNext()
176           {
177             return iter.hasNext();
178           }
179 
180           /**
181            * @see java.util.Iterator#next()
182            */
183           @Override
184           public Node next()
185           {
186             return iter.next().getEndNode();
187           }
188 
189           /**
190            * @see java.util.Iterator#remove()
191            */
192           @Override
193           public void remove()
194           {
195             throw new UnsupportedOperationException();
196           }
197         };
198       }
199     };
200   }
201 
202   /**
203    * Determine a node by a given name inside a model by a given nsURI.
204    * 
205    * @param elementName
206    *          Element name
207    * @return Node
208    */
209   public Node determineEcoreClassifierNode(final String elementName)
210   {
211     // find model
212     final Node model = getModelNode(EcorePackage.eNS_URI);
213     if (null == model)
214     {
215       return null;
216     }
217 
218     // traverse from model to all classifiers
219     for (final Node node : model.traverse(Order.BREADTH_FIRST, StopEvaluator.END_OF_GRAPH,
220         ReturnableEvaluator.ALL_BUT_START_NODE, EcoreRelationshipType.CONTAINS, Direction.OUTGOING))
221     {
222       if (node.hasProperty(Constants.PROPERTY_NAME))
223       {
224         final String name = (String) node.getProperty(Constants.PROPERTY_NAME);
225         if (name.equals(elementName))
226         {
227           return node;
228         }
229       }
230     }
231     return null;
232   }
233 }