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