]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.diagram/src/org/simantics/interop/diagram/Symbol.java
c86ca7ab4159fca1c6d973efa7af1fbcf74a6884
[simantics/interop.git] / org.simantics.interop.diagram / src / org / simantics / interop / diagram / Symbol.java
1 package org.simantics.interop.diagram;
2
3 import java.awt.geom.AffineTransform;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Set;
10
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Statement;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.utils.Logger;
17 import org.simantics.db.common.utils.NameUtils;
18 import org.simantics.db.common.utils.OrderedSetUtils;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
21 import org.simantics.db.exception.NoSingleResultException;
22 import org.simantics.db.exception.ServiceException;
23 import org.simantics.db.exception.ValidationException;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.diagram.stubs.G2DResource;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.structural2.utils.StructuralUtils;
30
31
32
33 /**
34  * @author Marko Luukkainen
35  */
36 public abstract class Symbol {
37         
38         private static boolean USE_UNRELIABLE_CONNECT = false;
39         private static boolean PREVENT_SELF_CONNECTIONS = false;
40         
41         private Diagram diagram;
42         Resource element;
43         Resource component;
44         
45         /**
46          * Use Diagram.getSymbol() to get Symbols.
47          * @param diagram
48          * @param element
49          * @param component
50          */
51         public Symbol(Diagram diagram, Resource element, Resource component) {
52                 assert(diagram != null);
53                 assert(element != null);
54                 this.diagram = diagram;
55                 this.element = element;
56                 this.component = component;
57         }
58         
59         public Diagram getDiagram() {
60                 return diagram;
61         }
62         
63         public Resource getComponent() {
64                 return component;
65         }
66         
67         public Resource getElement() {
68                 return element;
69         }
70         
71         /**
72          * Connects two Symbols, chooses connection type automatically.
73          * @param g
74          * @param symbolConf2 other symbol.
75          * @param componentConRel1 symbol's terminal relation.
76          * @param componentConRel2 other symbol's terminal relation.
77          * @return SymbolConnetionData instance describing the connection. Return value is never null.
78          * @throws DatabaseException on database failure.
79          */
80         public SymbolConnectionData connect(WriteGraph g,  Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2) throws DatabaseException {
81                 return connect(g, symbolConf2, componentConRel1, componentConRel2, false);
82         }
83         
84         
85         protected Collection<Resource> getConnectionRel(ReadGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Collection<Resource> matching) throws DatabaseException {
86                 return matching;
87         }
88         
89         protected  SymbolConnectionData customConnect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connType) throws DatabaseException {
90                 return null;
91         }
92         
93         protected Set<Resource> getConnectionTypes(ReadGraph g, Resource componentConRel1) throws DatabaseException {
94                 StructuralResource2 sr = StructuralResource2.getInstance(g);
95                 Set<Resource> set = new HashSet<Resource>();
96                 set.addAll(g.getObjects(componentConRel1, sr.AllowsConnectionType));
97                 return set;
98         }
99         
100         protected Set<Resource> getNonAssertedConnectionTypes(ReadGraph g, Resource componentConRel1) throws DatabaseException {
101                 StructuralResource2 sr = StructuralResource2.getInstance(g);
102                 Set<Resource> set = new HashSet<Resource>();
103                 for (Statement s : g.getStatements(componentConRel1, sr.AllowsConnectionType)) {
104                         if (s.isAsserted(componentConRel1))
105                                 continue;
106                         set.add(s.getObject());
107                 }
108                 return set;
109         }
110         
111         /**
112          * Connects two Symbols, chooses connection type automatically.
113          * @param g
114          * @param symbolConf2 other symbol.
115          * @param componentConRel1 symbol's terminal relation.
116          * @param componentConRel2 other symbol's terminal relation.
117          * @param forceFlag creates flag connection even if symbols are on the same diagram.
118          * @return SymbolConnetionData instance describing the connection. Return value is never null.
119          * @throws DatabaseException on database failure.
120          */
121         public SymbolConnectionData connect(WriteGraph g,  Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, boolean forceFlag) throws DatabaseException {
122                 if (this.equals(symbolConf2)) {
123                         if (PREVENT_SELF_CONNECTIONS) {
124                                 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
125                                 Logger.defaultLogError(err);
126                                 return new ConnectionErrorImpl(err);
127                         }
128                         if (componentConRel1.equals(componentConRel2)) {
129                                 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
130                                 Logger.defaultLogError(err);
131                                 return new ConnectionErrorImpl(err);
132                         }
133                 }
134                 StructuralResource2 sr = StructuralResource2.getInstance(g);
135                 Collection<Resource> connType1 = getConnectionTypes(g, componentConRel1);
136                 Collection<Resource> connType2 = getConnectionTypes(g, componentConRel2);
137                 Collection<Resource> matching = new HashSet<Resource>();
138                 for (Resource r : connType2) {
139                         if (connType1.contains(r))
140                                 matching.add(r);
141                 }
142                 if (matching.size() > 1) {
143                         matching = getConnectionRel(g, symbolConf2, componentConRel1, componentConRel2, matching);
144                 }
145                 Resource usedConnType = null;
146                 if (matching.size() != 1) {
147                         String err;
148                         if (USE_UNRELIABLE_CONNECT) {
149                                 err = "Unreliable connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " cannot find proper connection type, using Name Reference";
150                         } else {
151                                 err = "Unable to connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " cannot find proper connection type for terminals " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
152                         }
153                         Logger.defaultLogInfo(err);
154                         return new ConnectionErrorImpl(err);
155                 } else {
156                         usedConnType = matching.iterator().next();
157                 }
158                 
159                 SymbolConnectionData data = customConnect(g, symbolConf2, componentConRel1, componentConRel2, usedConnType);
160                 if (data == null) {
161                         if (!forceFlag && diagram.equals(symbolConf2.diagram))
162                                 return _connect(g, symbolConf2, componentConRel1, componentConRel2,usedConnType);
163                         else
164                                 return connectWithFlag(g,  symbolConf2, componentConRel1, componentConRel2,usedConnType);
165                 } else {
166                         return data;
167                 }
168         }
169         
170         public SymbolConnectionData connectElement(WriteGraph g,  Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2) throws DatabaseException {
171                 
172                 StructuralResource2 sr = StructuralResource2.getInstance(g);
173                 ModelingResources mod = ModelingResources.getInstance(g);
174                 
175                 Resource componentConRel1 = g.getPossibleObject(elementConRel1, mod.DiagramConnectionRelationToConnectionRelation);
176                 Resource componentConRel2 = g.getPossibleObject(elementConRel2, mod.DiagramConnectionRelationToConnectionRelation);
177                 
178                 if (componentConRel1 != null && componentConRel2 != null) {
179                         return connect(g, symbolConf2, componentConRel1, componentConRel2);
180                 }
181
182                 Collection<Resource> connType1 = Collections.EMPTY_LIST;
183                 Collection<Resource> connType2 = Collections.EMPTY_LIST;
184                 if (componentConRel1 != null) {
185                         connType1 = getConnectionTypes(g, componentConRel1);
186                         if (connType1.size() > 1)
187                                 connType1 = getNonAssertedConnectionTypes(g,componentConRel1);
188                 } else if (componentConRel2 != null) {
189                         connType2 = getConnectionTypes(g, componentConRel2);
190                         if (connType2.size() > 1)
191                                 connType2 = getNonAssertedConnectionTypes(g,componentConRel2);
192                 }
193                 Resource usedConnType = null;
194                 if (connType1.size() == 1)
195                         usedConnType = connType1.iterator().next();
196                 else if (connType2.size() == 1)
197                         usedConnType = connType2.iterator().next();
198                 else {
199                         String err = "Cannot locate connection type for: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
200                         Logger.defaultLogError(err);
201                         return new ConnectionErrorImpl(err);
202                 }
203                 if (!diagram.equals(symbolConf2.diagram)) {
204                         String err = "Element connections must be done on the same diagram: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
205                         Logger.defaultLogError(err);
206                         return new ConnectionErrorImpl(err);
207                 }
208                 return connectElement(g, symbolConf2, elementConRel1, elementConRel2, usedConnType);
209         }
210         
211         public SymbolConnectionData connectElement(WriteGraph g,  Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2, Resource usedConnType) throws DatabaseException {
212                 if (this.equals(symbolConf2)) {
213                         if (PREVENT_SELF_CONNECTIONS) {
214                                 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
215                                 Logger.defaultLogError(err);
216                                 return new ConnectionErrorImpl(err);
217                         }
218                         if (elementConRel1.equals(elementConRel2)) {
219                                 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
220                                 Logger.defaultLogError(err);
221                                 return new ConnectionErrorImpl(err);
222                         }
223                 }
224                 // TODO : should we have separate customConnect method for element level connections?
225                 SymbolConnectionData data = customConnect(g, symbolConf2, elementConRel1, elementConRel2, usedConnType);
226                 if (data == null) {
227                         return _connectElement(g, symbolConf2, elementConRel1, elementConRel2,usedConnType);
228                 } else {
229                         return data;
230                 }
231         }
232         
233         /**
234          * Connects two symbols with chosen connection type. Does not work with signal connections. 
235          * @param g
236          * @param symbolConf2 other symbol.
237          * @param componentConRel1 symbol's terminal relation.
238          * @param componentConRel2 other symbol's terminal relation.
239          * @param connectionType used connection type.
240          * @return SymbolConnetionData instance describing the connection. Return value is never null.
241          * @throws DatabaseException on database failure.
242          */
243         public SymbolConnectionData connect(WriteGraph g,  Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectionType) throws DatabaseException {
244                 if (diagram.equals(symbolConf2.diagram))
245                         return _connect(g, symbolConf2, componentConRel1, componentConRel2, connectionType);
246                 else
247                         return connectWithFlag(g,  symbolConf2, componentConRel1, componentConRel2, connectionType);
248         }
249         
250         protected SymbolConnectionData connectToExisting(WriteGraph g, Symbol symbolConf2, Symbol current, Resource componentConRel1, Resource componentConRel2, Resource connectorType, boolean firstSame) throws DatabaseException {
251                 return new ConnectionErrorImpl("Symbol is already connected");
252         }
253         
254         private SymbolConnectionData _connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectorType) throws DatabaseException {
255                 Layer0 b = Layer0.getInstance(g);
256                 StructuralResource2 s = StructuralResource2.getInstance(g);
257                 DiagramResource d = DiagramResource.getInstance(g);
258                 ModelingResources m = ModelingResources.getInstance(g);
259                 
260                 
261                 if (g.isInstanceOf(componentConRel1, b.FunctionalRelation) && getSingleConnected(g, componentConRel1) != null) {
262                         Symbol current = getSingleConnected(g, componentConRel1);
263                         if (current.component.equals(symbolConf2.component))
264                                 return new SymbolExistsImpl();
265                         
266                         return connectToExisting(g, symbolConf2, current, componentConRel1, componentConRel2, connectorType, true);
267                 }
268                 if (g.isInstanceOf(componentConRel2, b.FunctionalRelation) && symbolConf2.getSingleConnected(g, componentConRel2) != null) {
269                         Symbol current = symbolConf2.getSingleConnected(g, componentConRel2);
270                         if (current.component.equals(component))
271                                 return new SymbolExistsImpl();
272                         return connectToExisting(g, symbolConf2, current, componentConRel1, componentConRel2, connectorType, false);
273                         
274                 }
275                 
276                 // connection object in module level
277                 Resource moduleConnection = g.newResource();
278                 g.claim(moduleConnection, b.InstanceOf, s.Connection);
279                 
280                 // connect the modules
281                 
282                 g.claim(component, componentConRel1, moduleConnection);
283                 g.claim(symbolConf2.component, componentConRel2, moduleConnection);
284                 
285                 // connection object in diagram level
286                 Resource diagramConnection = g.newResource();
287                 g.claim(diagramConnection, b.InstanceOf, d.RouteGraphConnection);
288                 DiagramUtils.addElementFirst(g, diagram, diagramConnection);
289                 
290                 g.claim(diagramConnection, s.HasConnectionType, connectorType);
291                 
292                 // Relation from element1 to connector1
293                 Resource elementConRel1 = getDiagramConnectionRelation(g, element, componentConRel1);
294                 Resource connectorRel1 = g.getPossibleObject(componentConRel1, s.HasAttachmentRelation);
295                 if (connectorRel1 == null)
296                         connectorRel1 = d.HasPlainConnector;
297                                 
298                 // connector1
299                 Resource connector1 = g.newResource();
300                 g.claim(connector1, b.InstanceOf, d.Connector);
301                 g.claim(element, elementConRel1, connector1);
302                 g.claim(diagramConnection, connectorRel1, connector1);
303                 
304                 // Relation from element2 to connector2
305                 Resource elementConRel2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
306                 Resource connectorRel2 = g.getPossibleObject(componentConRel2, s.HasAttachmentRelation);
307                 if (connectorRel2 == null)
308                         connectorRel2 = d.HasArrowConnector;
309                 // connector2
310                 Resource connector2 = g.newResource();
311                 g.claim(connector2, b.InstanceOf, d.Connector);
312                 g.claim(symbolConf2.element, elementConRel2, connector2);
313                 g.claim(diagramConnection, connectorRel2, connector2);
314                 
315                 // connect connectors
316                 g.claim(connector1, d.AreConnected, connector2);
317
318                 g.claim(moduleConnection, m.ConnectionToDiagramConnection, diagramConnection);
319                 
320                 return new ConnectorDataImpl(diagramConnection);
321         }
322         
323         private SymbolConnectionData _connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2, Resource connectorType) throws DatabaseException {
324                 Layer0 b = Layer0.getInstance(g);
325                 StructuralResource2 s = StructuralResource2.getInstance(g);
326                 DiagramResource d = DiagramResource.getInstance(g);
327                 ModelingResources m = ModelingResources.getInstance(g);
328                 
329                 
330                 if (g.isInstanceOf(elementConRel1, b.FunctionalRelation) && getDiagramSingleConnected(g, elementConRel1) != null) {
331                         Symbol current = getDiagramSingleConnected(g, elementConRel1);
332                         if (current.component.equals(symbolConf2.component))
333                                 return new SymbolExistsImpl();
334                         String err = "Cannot connect, terminal 1 is reserved: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
335                         Logger.defaultLogError(err);
336                         return new ConnectionErrorImpl(err);
337                 }
338                 if (g.isInstanceOf(elementConRel2, b.FunctionalRelation) && symbolConf2.getDiagramSingleConnected(g, elementConRel2) != null) {
339                         Symbol current = symbolConf2.getDiagramSingleConnected(g, elementConRel2);
340                         if (current.component.equals(component))
341                                 return new SymbolExistsImpl();
342                         String err = "Cannot connect, terminal 2 is reserved: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
343                         Logger.defaultLogError(err);
344                         return new ConnectionErrorImpl(err);
345                 }
346                 
347 //              // connection object in module level
348 //              Resource moduleConnection = g.newResource();
349 //              g.claim(moduleConnection, b.InstanceOf, s.Connection);
350 //              
351 //              // connect the modules
352 //              
353 //              g.claim(component, componentConRel1, moduleConnection);
354 //              g.claim(symbolConf2.component, componentConRel2, moduleConnection);
355                 
356                 // connection object in diagram level
357                 Resource diagramConnection = g.newResource();
358                 g.claim(diagramConnection, b.InstanceOf, d.RouteGraphConnection);
359                 DiagramUtils.addElementFirst(g, diagram, diagramConnection);
360                 
361                 g.claim(diagramConnection, s.HasConnectionType, connectorType);
362                 
363                 // Relation from element1 to connector1
364 //              Resource elementConRel1 = getDiagramConnectionRelation(g, element, componentConRel1);
365 //              Resource connectorRel1 = g.getPossibleObject(componentConRel1, s.HasAttachmentRelation);
366 //              if (connectorRel1 == null)
367 //                      connectorRel1 = d.HasPlainConnector;
368                 Resource connectorRel1 = d.HasPlainConnector;
369                                 
370                 // connector1
371                 Resource connector1 = g.newResource();
372                 g.claim(connector1, b.InstanceOf, d.Connector);
373                 g.claim(element, elementConRel1, connector1);
374                 g.claim(diagramConnection, connectorRel1, connector1);
375                 
376                 // Relation from element2 to connector2
377 //              Resource elementConRel2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
378 //              Resource connectorRel2 = g.getPossibleObject(componentConRel2, s.HasAttachmentRelation);
379 //              if (connectorRel2 == null)
380 //                      connectorRel2 = d.HasArrowConnector;
381                 
382                 Resource connectorRel2 = d.HasArrowConnector;
383                 // connector2
384                 Resource connector2 = g.newResource();
385                 g.claim(connector2, b.InstanceOf, d.Connector);
386                 g.claim(symbolConf2.element, elementConRel2, connector2);
387                 g.claim(diagramConnection, connectorRel2, connector2);
388                 
389                 // connect connectors
390                 g.claim(connector1, d.AreConnected, connector2);
391
392 //              g.claim(moduleConnection, m.ConnectionToDiagramConnection, diagramConnection);
393                 
394                 return new ConnectorDataImpl(diagramConnection);
395         }
396         
397         
398         private SymbolConnectionData connectWithFlag(WriteGraph g,  Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectionType) throws DatabaseException {
399                 
400                 Diagram diagram2 = symbolConf2.diagram;
401                 Layer0 l0 = Layer0.getInstance(g);
402                 StructuralResource2 s = StructuralResource2.getInstance(g);
403                 DiagramResource d = DiagramResource.getInstance(g);
404                 ModelingResources m = ModelingResources.getInstance(g);
405                 
406                 if (g.isInstanceOf(componentConRel1, l0.FunctionalRelation) && getSingleConnected(g, componentConRel1) != null) {
407                         String err = "Cannot flag connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " since the first terminal is already reserved";
408                         Logger.defaultLogError(err);
409                         return new ConnectionErrorImpl(err);
410                 }
411                 if (g.isInstanceOf(componentConRel2, l0.FunctionalRelation) && symbolConf2.getSingleConnected(g, componentConRel2) != null) {
412                         String err = "Cannot flag connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " since the second terminal is already reserved";
413                         Logger.defaultLogError(err);
414                         return new ConnectionErrorImpl(err);
415                 }
416         
417                 double t = 20.0;
418                 double[] t1 =  getSymbolTranslation(g);
419                 double[] t2 =  symbolConf2.getSymbolTranslation(g);
420                 
421                 Symbol flag1 = diagram.addElement(g, d.Flag, t1[0]+t, t1[1]);
422                 Symbol flag2 = diagram2.addElement(g, d.Flag, t2[0]-t, t2[1]);
423                 
424                 Resource connectionJoin = g.newResource();
425                 g.claim(connectionJoin, l0.InstanceOf, s.ConnectionJoin);
426                 g.claim(diagram.getComposite(), s.HasConnectionJoin, connectionJoin);
427                 g.claim(diagram2.getComposite(), s.HasConnectionJoin, connectionJoin);
428                 
429                 Resource connection1 = g.newResource();
430                 g.claim(connection1, l0.InstanceOf, s.Connection);
431                 
432                 Resource connection2 = g.newResource();
433                 g.claim(connection2, l0.InstanceOf, s.Connection);
434                 
435                 g.claim(component, componentConRel1, connection1);
436                 g.claim(symbolConf2.component, componentConRel2, connection2);
437                 g.claim(connection1, s.IsJoinedBy, connectionJoin);
438                 g.claim(connection2, s.IsJoinedBy, connectionJoin);
439                 
440                 g.claim(flag1.element, d.FlagIsJoinedBy, connectionJoin);
441                 g.claim(flag2.element, d.FlagIsJoinedBy, connectionJoin);
442                 
443                 Resource diagConnection1 = g.newResource();
444                 g.claim(diagConnection1, l0.InstanceOf, d.RouteGraphConnection);
445                 DiagramUtils.addElementFirst(g, diagram, diagConnection1);
446                 g.claim(diagConnection1, s.HasConnectionType, connectionType);
447
448                 Resource diagConnection2 = g.newResource();
449                 g.claim(diagConnection2, l0.InstanceOf, d.RouteGraphConnection);
450                 DiagramUtils.addElementFirst(g, diagram2, diagConnection2);
451                 g.claim(diagConnection2, s.HasConnectionType, connectionType);
452                 
453                 g.claim(connection1, m.ConnectionToDiagramConnection, diagConnection1);
454                 g.claim(connection2, m.ConnectionToDiagramConnection, diagConnection2);
455                 
456                 // Relation from element1 to connector1
457                 Resource isConnected1 = getDiagramConnectionRelation(g, element, componentConRel1);
458                 
459                 // connector1
460                 Resource connector1_1 = g.newResource();
461                 g.claim(connector1_1, l0.InstanceOf, d.Connector);
462                 g.claim(element, isConnected1, connector1_1);
463                 g.claim(diagConnection1, d.HasPlainConnector, connector1_1);
464                 
465                 Resource connector1_2 = g.newResource();
466                 g.claim(connector1_2, l0.InstanceOf, d.Connector);
467                 g.claim(flag1.element, d.Flag_ConnectionPoint, connector1_2);
468                 g.claim(diagConnection1, d.HasArrowConnector, connector1_2);
469                 
470                 g.claim(connector1_1, d.AreConnected, connector1_2);
471                 
472                 // Relation from element2 to connector2
473                 Resource isConnected2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
474                 
475                 // connector1
476                 Resource connector2_1 = g.newResource();
477                 g.claim(connector2_1, l0.InstanceOf, d.Connector);
478                 g.claim(flag2.element, d.Flag_ConnectionPoint, connector2_1);
479                 g.claim(diagConnection2, d.HasPlainConnector, connector2_1);
480                 
481                 Resource connector2_2 = g.newResource();
482                 g.claim(connector2_2, l0.InstanceOf, d.Connector);
483                 g.claim(symbolConf2.element, isConnected2, connector2_2);
484                 g.claim(diagConnection2, d.HasArrowConnector, connector2_2);
485                 
486                 g.claim(connector2_1, d.AreConnected, connector2_2);
487                 
488                 return new FlagConnectionDataImpl(flag1, flag2, diagConnection1, diagConnection2);
489         }
490         
491         /**
492          * Returns diagram connection relation for an element and its component connection relation
493          * @param g
494          * @param element
495          * @param componentConnRel
496          * @return
497          * @throws ServiceException
498          * @throws NoSingleResultException
499          * @throws ValidationException 
500          */
501         public static Resource getDiagramConnectionRelation(ReadGraph g, Resource element, Resource componentConnRel) throws DatabaseException {
502                 ModelingResources m = ModelingResources.getInstance(g);
503                 Collection<Resource> diagramConnectionRels = g.getObjects(componentConnRel, m.ConnectionRelationToDiagramConnectionRelation);
504                 if (diagramConnectionRels.size() == 1)
505                         return diagramConnectionRels.iterator().next();
506                 if (diagramConnectionRels.size() > 1) {
507                         List<Resource> matching = new ArrayList<Resource>();
508                         for (Resource r : diagramConnectionRels) {
509                                 if (g.hasStatement(element, r))
510                                         matching.add(r);
511                         }
512                         if (matching.size() == 1)
513                                 return matching.get(0);
514                 }
515                 Layer0 b = Layer0.getInstance(g);
516                 Collection<Resource> elementTypes = g.getObjects(element, b.InstanceOf);
517                 if (elementTypes.size() != 1)
518                         throw new RuntimeException("Cannot handle multi-instances " + element + " " + NameUtils.getSafeName(g, element));
519                 Resource elementType = elementTypes.iterator().next();
520                 Collection<Resource> rels = StructuralUtils.getConnectionRelations(g, elementType);
521                 if (rels.size() == 1)
522                         return rels.iterator().next();
523                 for (Resource rel : rels) {
524                         if (diagramConnectionRels.contains(rel))
525                                 return rel;
526                 }
527                 throw new RuntimeException("Cannot find diagram level relation for relation " + NameUtils.getSafeName(g, componentConnRel) + " " + componentConnRel + " , element " + NameUtils.getSafeName(g, element) + " " + element + " , elementType " + NameUtils.getSafeName(g, elementType) + " " + elementType );
528         }
529         
530         public static Resource getComponentConnectionRelation(ReadGraph g, Resource diagraromConnRel) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
531                 ModelingResources mr = ModelingResources.getInstance(g);
532                 return g.getSingleObject(diagraromConnRel, mr.DiagramConnectionRelationToConnectionRelation);
533         }
534         
535 //      public static Resource getSymbolRelation(ReadGraph g, Symbol conf, Resource genericRel) throws DatabaseException {
536 //              Layer0 b = Layer0.getInstance(g);
537 //              if (g.isInstanceOf(conf.element, ab.CalculationLevelReferenceElement)) {
538 //                      ModelingResources m = ModelingResources.getInstance(g);
539 //                      //return g.getInverse(g.getSingleObject(conf.element, m.HasReferenceRelation));
540 //                      return g.getSingleObject(conf.element, m.HasReferenceRelation);
541 //              }
542 //              
543 //              Resource compType = g.getSingleObject(conf.getComponent(), b.InstanceOf);
544 //              
545 //              for (Resource rel : StructuralUtils.getConnectionRelations(g, compType))
546 //                  if (g.isSubrelationOf(rel, genericRel))
547 //                      return rel;
548 //              return null;
549 //      }
550         
551         /**
552          * Returns symbol where given symbol is connected using given relation. Returns null if the symbol is not connected using the relation.
553          * @param g
554          * @param componentConRel
555          * @return
556          * @throws DatabaseException 
557          */
558         public Symbol getSingleConnected(ReadGraph g, Resource componentConRel) throws DatabaseException {
559                 Collection<Symbol> symbols = getConnected(g, componentConRel);
560                 if (symbols.size() > 1)
561                         throw new NoSingleResultException("Symbol " + component + " is connected more than once using relation " + componentConRel);
562                 if (symbols.size() == 1)
563                         return symbols.iterator().next();
564                 return null;
565         }
566         
567         public Symbol getSingleConnected(ReadGraph g, Resource componentConRel, Resource otherComponentConRel) throws DatabaseException {
568                 Collection<Symbol> symbols = getConnected(g, componentConRel, otherComponentConRel);
569                 if (symbols.size() > 1)
570                         throw new NoSingleResultException("Symbol " + component + " is connected more than once using relation " + componentConRel);
571                 if (symbols.size() == 1)
572                         return symbols.iterator().next();
573                 return null;
574         }
575         
576         public Collection<Symbol> getConnected(ReadGraph g, Resource componentConRel, Resource otherComponentConRel) throws DatabaseException {
577                 assert(componentConRel != null);
578                 Layer0 l0 = Layer0.getInstance(g);
579                 StructuralResource2 sr = StructuralResource2.getInstance(g);
580                 ModelingResources mr = ModelingResources.getInstance(g);
581                 Collection<Symbol> result = new ArrayList<Symbol>();
582                 Collection<Resource> modConn1s = g.getObjects(this.component, componentConRel);
583                 for (Resource modConn1 : modConn1s) {
584                 
585                         Collection<Statement> connected = g.getStatements(modConn1, sr.Connects);
586                         Collection<Resource> connectionJoins = g.getObjects(modConn1, sr.IsJoinedBy);
587                         Collection<Resource> connectedComponents = new ArrayList<Resource>();
588                         
589                         for (Statement stm : connected) {
590                                 if (!stm.getObject().equals(this.component)) {
591                                         if (g.getInverse(stm.getPredicate()).equals(otherComponentConRel)) {
592                                                 connectedComponents.add(stm.getObject());
593                                         }
594                                 }
595                         }
596                         
597                         for (Resource connectionJoin : connectionJoins) {
598                                 Collection<Resource> joinedConns = g.getObjects(connectionJoin, sr.Joins);
599                                 for (Resource conn : joinedConns) {
600                                         if (!conn.equals(modConn1)) {
601                                                 // TODO : check ConnectionJoins?
602                                                 for (Statement stm : g.getStatements(conn, sr.Connects)) {
603                                                         if (g.getInverse(stm.getPredicate()).equals(otherComponentConRel))
604                                                                 connectedComponents.add(stm.getObject());
605                                                 }
606                                         }
607                                 }
608                         }
609                         for (Resource connectedComponent : connectedComponents) {
610                         
611                                 Resource pointElem = g.getSingleObject(connectedComponent, mr.ComponentToElement);
612                                 
613                                 Diagram diag = diagram.fromExisting(g, g.getSingleObject(connectedComponent, l0.PartOf));
614                                 result.add(diag.getSymbol(g, pointElem,connectedComponent));
615                         }
616                         
617                 }
618                 return result;  
619         }
620         
621         public Collection<Symbol> getConnected(ReadGraph g, Resource componentConRel) throws DatabaseException {
622                 assert(componentConRel != null);
623                 Layer0 l0 = Layer0.getInstance(g);
624                 StructuralResource2 sr = StructuralResource2.getInstance(g);
625                 ModelingResources mr = ModelingResources.getInstance(g);
626                 
627                 Collection<Resource> modConn1s = g.getObjects(this.component, componentConRel);
628                 Collection<Symbol> result = new ArrayList<Symbol>();
629                 for (Resource modConn1 : modConn1s) {
630                 
631                         Collection<Resource> connected = g.getObjects(modConn1, sr.Connects);
632                         Collection<Resource> connectionJoins = g.getObjects(modConn1, sr.IsJoinedBy);
633                         Collection<Resource> connectedComponents = new ArrayList<Resource>();
634                         
635                         for (Resource r : connected) {
636                                 if (!r.equals(this.component)) {
637                                         connectedComponents.add(r);
638                                 }
639                         }
640                         for (Resource connectionJoin : connectionJoins) {
641                                 Collection<Resource> joinedConns = g.getObjects(connectionJoin, sr.Joins);
642                                 for (Resource conn : joinedConns) {
643                                         if (!conn.equals(modConn1)) {
644                                                 // TODO : check ConnectionJoins?
645                                                 for (Resource obj : g.getObjects(conn, sr.Connects))
646                                                         connectedComponents.add(obj);
647                                         }
648                                 }
649                         }
650                         for (Resource connectedComponent : connectedComponents) {
651                         
652                                 Resource pointElem = g.getSingleObject(connectedComponent, mr.ComponentToElement);
653                                 
654                                 Diagram diag = diagram.fromExisting(g, g.getSingleObject(connectedComponent, l0.PartOf));
655                                 result.add(diag.getSymbol(g, pointElem,connectedComponent));
656                         }
657                 }
658                 return result;
659                 
660         }
661         
662         /**
663          * Returns connected symbols on diagram level. Searches through branchpoints.
664          * @param g
665          * @param diagramConnRel
666          * @param otherConnRel
667          * @return
668          * @throws DatabaseException
669          */
670         public Collection<Symbol> getDiagramConnected(ReadGraph g, Resource diagramConnRel, Resource otherConnRel) throws DatabaseException {
671                 DiagramResource dr = DiagramResource.getInstance(g);
672                 Collection<Resource> connectors = g.getObjects(element, diagramConnRel);
673                 Collection<Symbol> result = new ArrayList<Symbol>();
674                 for (Resource connector : connectors) {
675                         Resource otherConnector = g.getSingleObject(connector, dr.AreConnected);
676                         if (g.isInstanceOf(otherConnector, dr.Connector)) {
677                                 getConnectorConnectors(g, otherConnector, otherConnRel, result);
678                         } else if (g.isInstanceOf(otherConnector, dr.RouteLine)) {
679                                 getBranchPointConnectors(g, connector, otherConnector, otherConnRel, result);
680                         } else {
681                                 throw new DatabaseException("Connector " + g.getPossibleURI(otherConnector) + " " + otherConnector + " han unknown type");
682                         }
683                 }
684                 return result;
685         }
686         
687         private void getConnectorConnectors(ReadGraph g, Resource connector, Resource otherConnRel, Collection<Symbol> result) throws DatabaseException{
688                 StructuralResource2 sr = StructuralResource2.getInstance(g);
689                 DiagramResource dr = DiagramResource.getInstance(g);
690                 Collection<Statement> stms = g.getStatements(connector, sr.Connects);
691                 Statement stm = null;
692                 for (Statement s : stms) {
693                         if (!g.isInstanceOf(s.getObject(), dr.Connection)) {
694                                 stm = s;
695                         }
696                 }
697                 if (stm == null)
698                         return;
699                 if (otherConnRel != null) {
700                         Resource rel = g.getInverse(stm.getPredicate());
701                         if (!rel.equals(otherConnRel))
702                                 return;
703                 }
704                 Resource element = stm.getObject();
705                 result.add(diagram.getSymbol(g, element));
706         }
707         
708         private void getBranchPointConnectors(ReadGraph g, Resource origin, Resource branchPoint, Resource otherConnRel, Collection<Symbol> result) throws DatabaseException {
709                 DiagramResource dr = DiagramResource.getInstance(g);
710                 
711                 Collection<Resource> branchConnected = g.getObjects(branchPoint, dr.AreConnected);
712                 for (Resource branchConnector : branchConnected) {
713                         if (branchConnector.equals(origin))
714                                 continue;
715                         if (g.isInstanceOf(branchConnector, dr.Connector)) {
716                                 getConnectorConnectors(g, branchConnector, otherConnRel, result);
717                         } else if (g.isInstanceOf(branchConnector, dr.RouteLine)) {
718                                 getBranchPointConnectors(g, branchPoint, branchConnector, otherConnRel, result);
719                         } else {
720                                 throw new DatabaseException("Connector " + g.getPossibleURI(branchConnector) + " " + branchConnector + " han unknown type");
721                         }
722                 }
723         }
724
725         public Collection<Symbol> getDiagramConnected(ReadGraph g, Resource diagramConnRel) throws DatabaseException {
726                 return getDiagramConnected(g, diagramConnRel,null);
727         }
728         
729         public Collection<Symbol> getDiagramInverseConnected(ReadGraph g,  Resource otherConnRel) throws DatabaseException {
730                 StructuralResource2 sr = StructuralResource2.getInstance(g);
731                 return getDiagramConnected(g, sr.IsConnectedTo, otherConnRel);
732                 
733         }
734         
735         public Collection<Symbol> getDiagramConnected(ReadGraph g) throws DatabaseException {
736                 StructuralResource2 sr = StructuralResource2.getInstance(g);
737                 return getDiagramConnected(g, sr.IsConnectedTo, null);
738         }
739         
740         public Symbol getDiagramSingleConnected(ReadGraph g, Resource diagramConnRel) throws DatabaseException {
741                 Collection<Symbol> symbols = getDiagramConnected(g, diagramConnRel);
742                 if (symbols.size() > 1) {
743                         throw new NoSingleResultException("Symbol " + element + " has more than one connection with " + diagramConnRel);
744                 } else if (symbols.size() == 1) {
745                         return symbols.iterator().next();
746                 }
747                 return null;
748         }
749         
750         public Symbol getOtherFlag(ReadGraph g) throws DatabaseException {
751                 DiagramResource dr = DiagramResource.getInstance(g);
752                 Resource connectionJoin = g.getPossibleObject(element, dr.FlagIsJoinedBy);
753                 if (connectionJoin == null)
754                         return null;
755                 Collection<Resource> flags = g.getObjects(connectionJoin, dr.JoinsFlag);
756                 Resource otherFlag = null;
757                 for (Resource f : flags) {
758                         if (!f.equals(element)) {
759                                 otherFlag = f;
760                                 break;
761                         }
762                 }
763                 
764                 if (otherFlag == null) {
765                         return null;
766                 }
767                 Resource otherDiagramR = OrderedSetUtils.getSingleOwnerList(g, otherFlag);
768                 Diagram otherDiagram = diagram.fromExisting(g, otherDiagramR);
769                 return otherDiagram.getSymbol(g, otherFlag, null);
770                 
771         }
772
773         /**
774          * Returns concatenated translation of the symbol.
775          * @param g
776          * @return
777          * @throws DatabaseException
778          */
779         public double[] getSymbolTranslation(ReadGraph g) throws DatabaseException {
780                 DiagramResource d = DiagramResource.getInstance(g);
781                 ModelingResources mr = ModelingResources.getInstance(g);
782                 
783                 Resource e = element;
784                 AffineTransform at = new AffineTransform();
785                 while (e != null) {
786                         double transform[] = g.getPossibleRelatedValue(e, d.HasTransform);
787                         if (transform == null)
788                                 break;
789                         AffineTransform at2 = new AffineTransform(transform);
790                         at.preConcatenate(at2);
791                         Resource component = g.getPossibleObject(e, mr.HasParentComponent);
792                         if (component != null) {
793                                 e = g.getPossibleObject(component, mr.ComponentToElement);
794                         } else {
795                                 e = null;
796                         }
797                 }
798
799                 return new double[]{at.getTranslateX(),at.getTranslateY()};
800         }
801         
802         /**
803          * Returns transformation of the symbol.
804          * @param g
805          * @return
806          * @throws DatabaseException
807          */
808         public double[] getSymbolTransformation(ReadGraph g) throws DatabaseException{
809                 DiagramResource d = DiagramResource.getInstance(g);
810                 double transform[] = g.getPossibleRelatedValue(element, d.HasTransform);
811                 return transform;
812         }
813         
814         /**
815          * Sets translation of symbol. The scale is set to 1.0.
816          * @param g
817          * @param x
818          * @param y
819          * @throws DatabaseException
820          */
821         public void setSymbolTranslation(WriteGraph g, double x, double y) throws DatabaseException {
822                 DiagramResource d = DiagramResource.getInstance(g);
823                 G2DResource g2d = G2DResource.getInstance(g);
824                 g.claimLiteral(element, d.HasTransform, g2d.Transform, new double[]{1.0,0.0,0.0,1.0,x,y});
825         }
826         
827         public void setSymbolTransformation(WriteGraph g, AffineTransform at) throws DatabaseException {
828                 DiagramResource d = DiagramResource.getInstance(g);
829                 G2DResource g2d = G2DResource.getInstance(g);
830                 double arr[] = new double[6];
831                 at.getMatrix(arr);
832                 g.claimLiteral(element, d.HasTransform, g2d.Transform, arr);
833         }
834         
835         /**
836          * Set the scale of symbol.
837          * @param g
838          * @param scale
839          * @throws DatabaseException
840          */
841         public void setScale(WriteGraph g, double scale) throws DatabaseException {
842                 DiagramResource d = DiagramResource.getInstance(g);
843                 double transform[] = g.getPossibleRelatedValue(element, d.HasTransform);
844                 g.claimLiteral(element, d.HasTransform, new double[]{scale,0.0,0.0,scale,transform[4],transform[5]});
845         }
846         
847         @Override
848         public boolean equals(Object o) {
849                 if (o == null)
850                         return false;
851                 if (this.getClass() != o.getClass())
852                         return false;
853                 Symbol other = (Symbol)o;
854                 return element.equals(other.element);
855         }
856         
857         @Override
858         public int hashCode() {
859                 return element.hashCode();
860         }
861         
862         
863         
864         /**
865          * Returns component type from symbol type
866          * @param g
867          * @param symbolType
868          * @return
869          * @throws NoSingleResultException
870          * @throws ManyObjectsForFunctionalRelationException
871          * @throws ServiceException
872          */
873         public static Resource getComponentTypeFromSymbolType(ReadGraph g, Resource symbolType) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
874                 ModelingResources m = ModelingResources.getInstance(g);
875                 Resource component = g.getPossibleObject(symbolType, m.SymbolToComponentType);
876                 return component;
877         }
878         
879         public static Resource getSymbolTypeFromComponentType(ReadGraph g, Resource componentType) throws NoSingleResultException, ServiceException, ManyObjectsForFunctionalRelationException {
880                 ModelingResources m = ModelingResources.getInstance(g);
881                 Collection<Resource> symbols = g.getObjects(componentType, m.ComponentTypeToSymbol);
882                 if (symbols.size() == 0)
883                         return null;
884                 return symbols.iterator().next();
885         }
886         
887         public void remove(WriteGraph g) throws DatabaseException{
888                 Layer0 L0 = Layer0.getInstance(g);
889                 StructuralResource2 STR = StructuralResource2.getInstance(g);
890                 DiagramResource DIA = DiagramResource.getInstance(g);
891                 OrderedSetUtils.remove(g, diagram.getDiagram(), element);
892                 if (component != null)
893                         g.deny(component, L0.PartOf);
894                 Collection<Statement> statements = g.getStatements(element, STR.IsConnectedTo);
895                 for (Statement s : statements) {
896                         if (g.isInstanceOf(s.getObject(),DIA.Connector)) {
897                                 Resource connectedConnector = g.getPossibleObject(s.getObject(), DIA.AreConnected);
898                                 if (connectedConnector != null)
899                                         g.deny(connectedConnector);
900                                 g.deny(s.getObject());
901                         }
902                 }
903                 if (component != null) {
904                         statements = g.getStatements(component, STR.IsConnectedTo);
905                         for (Statement s : statements) {
906                                 if (g.isInstanceOf(s.getObject(),STR.Connection)) {
907                                         g.deny(s.getObject());
908                                 }
909                         }
910                 }
911                 g.deny(element);
912                 if (component != null)
913                         g.deny(component);
914         }
915         
916         public void dispose() {
917                 diagram = null;
918                 component = null;
919                 element = null;
920         }
921
922         /**
923          * Common interface for Symbol.connect return value.
924          *
925          */
926         public interface SymbolConnectionData {
927                 
928         }
929         
930         /**
931          * Interface for returning diagram level connectors.
932          *
933          */
934         public interface DiagramConnectionData extends SymbolConnectionData {
935                 public int getConnectionCount();
936                 public Resource getConnection(int index);
937         }
938         
939         /**
940          * Interface for flag connections. 
941          *
942          */
943         public interface FlagConnectionData extends SymbolConnectionData {
944                 public Symbol getFirstFlag();
945                 public Symbol getSecondFlag();
946         }
947         
948         
949         /**
950          * Interface for signal connections. 
951          *
952          */
953         public interface SignalConnectionData extends SymbolConnectionData {
954                 public Resource getSignalComponent();
955         }
956         
957         /**
958          * Interface for Point connections. 
959          *
960          */
961         public interface PointConnectionData extends SymbolConnectionData {
962                 Symbol getPoint();
963         }
964         
965         /**
966          * Interface for "connection" that merged the connected symbols into one symbol. 
967          *
968          */
969         public interface MergeSymbolData extends SymbolConnectionData {
970                 Symbol getResultSymbol();
971         }
972         
973         /**
974          * Return value for a connection that already exists. 
975          *
976          */
977         public interface SymbolExists extends SymbolConnectionData {
978                 
979         }
980         
981         /**
982          * Return value for an error. 
983          *
984          */
985         public interface ConnectionError extends SymbolConnectionData {
986                 String getReason();
987         }
988         
989         private static class ConnectionErrorImpl implements ConnectionError {
990                 private String error;
991                 
992                 public ConnectionErrorImpl(String error) {
993                         this.error = error;
994                 }
995                 
996                 @Override
997                 public String getReason() {
998                         return error;
999                 }
1000         }
1001         
1002         private static class SymbolExistsImpl implements SymbolExists {
1003                 
1004         }
1005         
1006         private static class ConnectorDataImpl implements DiagramConnectionData {
1007                 
1008                 private Resource[] connectors;
1009                 
1010                 public ConnectorDataImpl(Resource... connectors) {
1011                         this.connectors = connectors;
1012                 }
1013                 
1014                 @Override
1015                 public Resource getConnection(int index) {
1016                         return connectors[index];
1017                 }
1018                 
1019                 @Override
1020                 public int getConnectionCount() {
1021                         return connectors.length;
1022                 }
1023         }
1024         
1025         private static class FlagConnectionDataImpl implements FlagConnectionData, DiagramConnectionData {
1026                 
1027                 private Symbol firstFlag;
1028                 private Symbol secondFlag;
1029                 private Resource[] connectors;
1030                 
1031                 public FlagConnectionDataImpl(Symbol firstFlag, Symbol secondFlag, Resource... connectors) {
1032                         this.firstFlag = firstFlag;
1033                         this.secondFlag = secondFlag;
1034                         this.connectors = connectors;
1035                 }
1036                 
1037                 @Override
1038                 public Symbol getFirstFlag() {
1039                         return firstFlag;
1040                 }
1041                 
1042                 @Override
1043                 public Symbol getSecondFlag() {
1044                         return secondFlag;
1045                 }
1046                 
1047                 @Override
1048                 public Resource getConnection(int index) {
1049                         return connectors[index];
1050                 }
1051                 
1052                 @Override
1053                 public int getConnectionCount() {
1054                         return connectors.length;
1055                 }
1056         }
1057
1058         public void setComponentValue(WriteGraph graph, Resource property, double value) throws DatabaseException{
1059                 setValue(graph, component, property, value);
1060         }
1061         
1062         public void setComponentValue(WriteGraph graph, Resource property, float value) throws DatabaseException{
1063                 setValue(graph, component, property, value);
1064         }
1065         
1066         public void setComponentValue(WriteGraph graph, Resource property, boolean value) throws DatabaseException{
1067                 setValue(graph, component, property, value);
1068         }
1069         
1070         public void setComponentValue(WriteGraph graph, Resource property, int value) throws DatabaseException{
1071                 setValue(graph, component, property, value);
1072         }
1073         
1074         public void setComponentValue(WriteGraph graph, Resource property, String value) throws DatabaseException{
1075                 setValue(graph, component, property, value);
1076         }
1077         
1078         public void setComponentValue(WriteGraph graph, Resource property, double value[]) throws DatabaseException{
1079                 setValue(graph, component, property, value);
1080         }
1081         
1082         public void setComponentValue(WriteGraph graph, Resource property, float value[]) throws DatabaseException{
1083                 setValue(graph, component, property, value);
1084         }
1085         
1086         public void setComponentValue(WriteGraph graph, Resource property, int value[]) throws DatabaseException{
1087                 setValue(graph, component, property, value);
1088         }
1089         
1090         public void setComponentValue(WriteGraph graph, Resource property, boolean value[]) throws DatabaseException{
1091                 setValue(graph, component, property, value);
1092         }
1093         
1094         public void setElementValue(WriteGraph graph, Resource property, double value) throws DatabaseException{
1095                 setValue(graph, element, property, value);
1096         }
1097         
1098         public void setElementValue(WriteGraph graph, Resource property, float value) throws DatabaseException{
1099                 setValue(graph, element, property, value);
1100         }
1101         
1102         public void setElementValue(WriteGraph graph, Resource property, boolean value) throws DatabaseException{
1103                 setValue(graph, element, property, value);
1104         }
1105         
1106         public void setElementValue(WriteGraph graph, Resource property, int value) throws DatabaseException{
1107                 setValue(graph, element, property, value);
1108         }
1109         
1110         public void setElementValue(WriteGraph graph, Resource property, double value[]) throws DatabaseException{
1111                 setValue(graph, element, property, value);
1112         }
1113         
1114         public void setElementValue(WriteGraph graph, Resource property, float value[]) throws DatabaseException{
1115                 setValue(graph, element, property, value);
1116         }
1117         
1118         public void setElementValue(WriteGraph graph, Resource property, int value[]) throws DatabaseException{
1119                 setValue(graph, element, property, value);
1120         }
1121         
1122         public void setElementValue(WriteGraph graph, Resource property, boolean value[]) throws DatabaseException{
1123                 setValue(graph, element, property, value);
1124         }
1125         
1126         /**
1127          * Sets literal value. Does data conversion if required.
1128          * 
1129          * Note: This method support only some basic L0 data types.
1130          * 
1131          * @param graph
1132          * @param object
1133          * @param property
1134          * @param value
1135          * @throws DatabaseException
1136          */
1137         public static void setValue(WriteGraph graph, Resource object, Resource property, double value) throws DatabaseException{
1138                 Layer0 l0 = Layer0.getInstance(graph);
1139                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1140                 if (l0.Double.equals(range)) {
1141                         graph.claimLiteral(object, property, value, Bindings.DOUBLE);
1142                 } else if (l0.Float.equals(range)) {
1143                         graph.claimLiteral(object, property, (float)value, Bindings.FLOAT);
1144                 } else if (l0.Integer.equals(range)) {
1145                         graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1146                 } else if (l0.Long.equals(range)) {
1147                         graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1148                 } else if (l0.Boolean.equals(range)) {
1149                         graph.claimLiteral(object, property, value > 0.0, Bindings.BOOLEAN);
1150                 } else if (l0.String.equals(range)) {
1151                         graph.claimLiteral(object, property, Double.toString(value), Bindings.STRING);
1152                 } else {
1153                         graph.claimLiteral(object, property, value, Bindings.DOUBLE);
1154                 }
1155         }
1156
1157         /**
1158          * Sets literal value. Does data conversion if required.
1159          * 
1160          * Note: This method support only some basic L0 data types.
1161          * 
1162          * @param graph
1163          * @param object
1164          * @param property
1165          * @param value
1166          * @throws DatabaseException
1167          */
1168         public static void setValue(WriteGraph graph, Resource object, Resource property, float value) throws DatabaseException{
1169                 Layer0 l0 = Layer0.getInstance(graph);
1170                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1171                 if (l0.Double.equals(range)) {
1172                         graph.claimLiteral(object, property, (double)value, Bindings.DOUBLE);
1173                 } else if (l0.Float.equals(range)) {
1174                         graph.claimLiteral(object, property, value, Bindings.FLOAT);
1175                 } else if (l0.Integer.equals(range)) {
1176                         graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1177                 } else if (l0.Long.equals(range)) {
1178                         graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1179                 } else if (l0.Boolean.equals(range)) {
1180                         graph.claimLiteral(object, property, value > 0.f, Bindings.BOOLEAN);
1181                 } else if (l0.String.equals(range)) {
1182                         graph.claimLiteral(object, property, Float.toString(value), Bindings.STRING);
1183                 } else {
1184                         graph.claimLiteral(object, property, value, Bindings.FLOAT);
1185                 }
1186         }
1187         
1188         /**
1189          * Sets literal value. Does data conversion if required.
1190          * 
1191          * Note: This method support only some basic L0 data types.
1192          * 
1193          * @param graph
1194          * @param object
1195          * @param property
1196          * @param value
1197          * @throws DatabaseException
1198          */
1199         public static void setValue(WriteGraph graph, Resource object, Resource property, int value) throws DatabaseException{
1200                 Layer0 l0 = Layer0.getInstance(graph);
1201                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1202                 if (l0.Double.equals(range)) {
1203                         graph.claimLiteral(object, property, (double)value, Bindings.DOUBLE);
1204                 } else if (l0.Float.equals(range)) {
1205                         graph.claimLiteral(object, property, (float) value, Bindings.FLOAT);
1206                 } else if (l0.Integer.equals(range)) {
1207                         graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1208                 } else if (l0.Long.equals(range)) {
1209                         graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1210                 } else if (l0.Boolean.equals(range)) {
1211                         graph.claimLiteral(object, property, value > 0, Bindings.BOOLEAN);
1212                 } else if (l0.String.equals(range)) {
1213                         graph.claimLiteral(object, property, Integer.toString(value), Bindings.STRING);
1214                 } else {
1215                         graph.claimLiteral(object, property, value, Bindings.INTEGER);
1216                 }
1217         }
1218         
1219         /**
1220          * Sets literal value. Does data conversion if required.
1221          * 
1222          * @param graph
1223          * @param object
1224          * @param property
1225          * @param value
1226          * @throws DatabaseException
1227          */
1228         public static void setValue(WriteGraph graph, Resource object, Resource property, String value) throws DatabaseException{
1229                 Layer0 l0 = Layer0.getInstance(graph);
1230                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1231                 if (range == null) {
1232                         graph.claimLiteral(object, property, value, Bindings.STRING);
1233                 } else {
1234                         if (graph.isInheritedFrom(range, l0.String)) {
1235                                 graph.claimLiteral(object, property, value, Bindings.STRING);
1236                         } else if (graph.isInheritedFrom(range, l0.Double)) {
1237                                 graph.claimLiteral(object, property, Double.parseDouble(value), Bindings.DOUBLE);
1238                         } else if (graph.isInheritedFrom(range, l0.Float)) {
1239                                 graph.claimLiteral(object, property, Float.parseFloat(value), Bindings.FLOAT);
1240                         } else if (graph.isInheritedFrom(range, l0.Integer)) {
1241                                 graph.claimLiteral(object, property, Integer.parseInt(value), Bindings.INTEGER);
1242                         } else if (graph.isInheritedFrom(range, l0.Long)) {
1243                                 graph.claimLiteral(object, property, Long.parseLong(value), Bindings.LONG);
1244                         } else if (graph.isInheritedFrom(range, l0.Boolean)) {
1245                                 graph.claimLiteral(object, property, Boolean.parseBoolean(value), Bindings.BOOLEAN);
1246                         } else {
1247                                 graph.claimLiteral(object, property, value, Bindings.STRING);
1248                         } 
1249                 }
1250         }
1251
1252         /**
1253          * Sets literal value. Does data conversion if required.
1254          * 
1255          * Note: This method support only some basic L0 data types.
1256          * 
1257          * @param graph
1258          * @param object
1259          * @param property
1260          * @param value
1261          * @throws DatabaseException
1262          */
1263         public static void setValue(WriteGraph graph, Resource object, Resource property, boolean value) throws DatabaseException{
1264                 Layer0 l0 = Layer0.getInstance(graph);
1265                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1266                 if (l0.Boolean.equals(range)) {
1267                         graph.claimLiteral(object, property, value , Bindings.BOOLEAN);
1268                 } else if (l0.String.equals(range)) {
1269                         graph.claimLiteral(object, property, Boolean.toString(value), Bindings.STRING);
1270                 } else if (l0.Integer.equals(range)) {
1271                         graph.claimLiteral(object, property, value ? 1 : 0, Bindings.INTEGER);
1272                 } else {
1273                         graph.claimLiteral(object, property, value, Bindings.BOOLEAN);
1274                 }
1275         }
1276         
1277         /**
1278          * Sets literal value. Does data conversion if required.
1279          * 
1280          * Note: This method support only some basic L0 data types.
1281          * 
1282          * @param graph
1283          * @param object
1284          * @param property
1285          * @param value
1286          * @throws DatabaseException
1287          */
1288         public static void setValue(WriteGraph graph, Resource object, Resource property, double[] value) throws DatabaseException{
1289                 Layer0 l0 = Layer0.getInstance(graph);
1290                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1291                 if (l0.DoubleArray.equals(range)) {
1292                         graph.claimLiteral(object, property, value, Bindings.DOUBLE_ARRAY);
1293                 } else if (l0.FloatArray.equals(range)) {
1294                         float arr[] = new float[value.length];
1295                         for (int i = 0; i < value.length; i++)
1296                                 arr[i] = (float) value[i];
1297                         graph.claimLiteral(object, property, arr, Bindings.FLOAT_ARRAY);
1298                 } else if (l0.IntegerArray.equals(range)) {
1299                         int arr[] = new int[value.length];
1300                         for (int i = 0; i < value.length; i++)
1301                                 arr[i] = (int) value[i];
1302                         graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1303                 } else {
1304                         graph.claimLiteral(object, property, value, Bindings.DOUBLE_ARRAY);
1305                 }
1306         }
1307         
1308         /**
1309          * Sets literal value. Does data conversion if required.
1310          * 
1311          * Note: This method support only some basic L0 data types.
1312          * 
1313          * @param graph
1314          * @param object
1315          * @param property
1316          * @param value
1317          * @throws DatabaseException
1318          */
1319         public static void setValue(WriteGraph graph, Resource object, Resource property, float[] value) throws DatabaseException{
1320                 Layer0 l0 = Layer0.getInstance(graph);
1321                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1322                 if (l0.FloatArray.equals(range)) {
1323                         graph.claimLiteral(object, property, value, Bindings.FLOAT_ARRAY);
1324                 } else if (l0.DoubleArray.equals(range)) {
1325                         double arr[] = new double[value.length];
1326                         for (int i = 0; i < value.length; i++)
1327                                 arr[i] = (double) value[i];
1328                         graph.claimLiteral(object, property, arr, Bindings.DOUBLE_ARRAY);
1329                 } else if (l0.IntegerArray.equals(range)) {
1330                         int arr[] = new int[value.length];
1331                         for (int i = 0; i < value.length; i++)
1332                                 arr[i] = (int) value[i];
1333                         graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1334                 } else {
1335                         graph.claimLiteral(object, property, value, Bindings.FLOAT_ARRAY);
1336                 }
1337         }
1338         
1339         /**
1340          * Sets literal value. Does data conversion if required.
1341          * 
1342          * Note: This method support only some basic L0 data types.
1343          * 
1344          * @param graph
1345          * @param object
1346          * @param property
1347          * @param value
1348          * @throws DatabaseException
1349          */
1350         public static void setValue(WriteGraph graph, Resource object, Resource property, int[] value) throws DatabaseException{
1351                 Layer0 l0 = Layer0.getInstance(graph);
1352                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1353                 if (l0.IntegerArray.equals(range)) {
1354                         graph.claimLiteral(object, property, value, Bindings.INT_ARRAY);
1355                 } else if (l0.DoubleArray.equals(range)) {
1356                         double arr[] = new double[value.length];
1357                         for (int i = 0; i < value.length; i++)
1358                                 arr[i] = (double) value[i];
1359                         graph.claimLiteral(object, property, arr, Bindings.DOUBLE_ARRAY);
1360                 } else if (l0.FloatArray.equals(range)) {
1361                         float arr[] = new float[value.length];
1362                         for (int i = 0; i < value.length; i++)
1363                                 arr[i] = (float) value[i];
1364                         graph.claimLiteral(object, property, arr, Bindings.FLOAT_ARRAY);
1365                 } else {
1366                         graph.claimLiteral(object, property, value, Bindings.INT_ARRAY);
1367                 }
1368         }
1369         
1370         /**
1371          * Sets literal value. Does data conversion if required.
1372          * 
1373          * Note: This method support only some basic L0 data types.
1374          * 
1375          * @param graph
1376          * @param object
1377          * @param property
1378          * @param value
1379          * @throws DatabaseException
1380          */
1381         public static void setValue(WriteGraph graph, Resource object, Resource property, boolean[] value) throws DatabaseException{
1382                 Layer0 l0 = Layer0.getInstance(graph);
1383                 Resource range = graph.getPossibleObject(property, l0.HasRange);
1384                 if (l0.BooleanArray.equals(range)) {
1385                         graph.claimLiteral(object, property, value, Bindings.BOOLEAN_ARRAY);
1386                 } else if (l0.IntegerArray.equals(range)) {
1387                         int arr[] = new int[value.length];
1388                         for (int i = 0; i < value.length; i++)
1389                                 arr[i] =  value[i] ? 1 : 0;
1390                         graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1391                 } else {
1392                         graph.claimLiteral(object, property, value, Bindings.BOOLEAN_ARRAY);
1393                 }
1394         }
1395         
1396 }