1
2
3
4
5
6
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
28
29 public class NeoHelper
30 {
31
32 private NeoService neo;
33
34
35
36
37
38
39
40 public void setNeo(final NeoService neoService)
41 {
42 neo = neoService;
43 }
44
45
46
47
48
49
50 public NeoService getNeo()
51 {
52 return neo;
53 }
54
55
56
57
58
59
60
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
80
81
82
83
84
85 public Node getModelNode(final String nsUri)
86 {
87
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
101
102
103
104
105
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
116
117
118
119
120
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
138
139
140
141
142
143 private Iterable<Relationship> getSubrefRelationships(
144 final EcoreRelationshipType relationshipType)
145 {
146 return getSubrefNode(relationshipType).getRelationships(Direction.OUTGOING);
147 }
148
149
150
151
152
153
154
155
156
157 public Iterable<Node> getSubrefNodeChildren(final EcoreRelationshipType relationshipType)
158 {
159 return new Iterable<Node>()
160 {
161
162
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
173
174 @Override
175 public boolean hasNext()
176 {
177 return iter.hasNext();
178 }
179
180
181
182
183 @Override
184 public Node next()
185 {
186 return iter.next().getEndNode();
187 }
188
189
190
191
192 @Override
193 public void remove()
194 {
195 throw new UnsupportedOperationException();
196 }
197 };
198 }
199 };
200 }
201
202
203
204
205
206
207
208
209 public Node determineEcoreClassifierNode(final String elementName)
210 {
211
212 final Node model = getModelNode(EcorePackage.eNS_URI);
213 if (null == model)
214 {
215 return null;
216 }
217
218
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 }