1 package org.simantics.interop.diagram;
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;
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.NameUtils;
17 import org.simantics.db.common.utils.OrderedSetUtils;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
20 import org.simantics.db.exception.NoSingleResultException;
21 import org.simantics.db.exception.ServiceException;
22 import org.simantics.db.exception.ValidationException;
23 import org.simantics.diagram.stubs.DiagramResource;
24 import org.simantics.diagram.stubs.G2DResource;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.modeling.ModelingResources;
27 import org.simantics.structural.stubs.StructuralResource2;
28 import org.simantics.structural2.utils.StructuralUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
36 * @author Marko Luukkainen
38 public abstract class Symbol {
40 private static Logger Logger = LoggerFactory.getLogger(Symbol.class);
42 private static boolean USE_UNRELIABLE_CONNECT = false;
43 private static boolean PREVENT_SELF_CONNECTIONS = false;
45 private Diagram diagram;
50 * Use Diagram.getSymbol() to get Symbols.
55 public Symbol(Diagram diagram, Resource element, Resource component) {
56 assert(diagram != null);
57 assert(element != null);
58 this.diagram = diagram;
59 this.element = element;
60 this.component = component;
63 public Diagram getDiagram() {
67 public Resource getComponent() {
71 public Resource getElement() {
76 * Connects two Symbols, chooses connection type automatically.
78 * @param symbolConf2 other symbol.
79 * @param componentConRel1 symbol's terminal relation.
80 * @param componentConRel2 other symbol's terminal relation.
81 * @return SymbolConnetionData instance describing the connection. Return value is never null.
82 * @throws DatabaseException on database failure.
84 public SymbolConnectionData connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2) throws DatabaseException {
85 return connect(g, symbolConf2, componentConRel1, componentConRel2, false);
89 protected Collection<Resource> getConnectionRel(ReadGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Collection<Resource> matching) throws DatabaseException {
93 protected SymbolConnectionData customConnect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connType) throws DatabaseException {
97 protected Set<Resource> getConnectionTypes(ReadGraph g, Resource componentConRel1) throws DatabaseException {
98 StructuralResource2 sr = StructuralResource2.getInstance(g);
99 Set<Resource> set = new HashSet<Resource>();
100 set.addAll(g.getObjects(componentConRel1, sr.AllowsConnectionType));
104 protected Set<Resource> getNonAssertedConnectionTypes(ReadGraph g, Resource componentConRel1) throws DatabaseException {
105 StructuralResource2 sr = StructuralResource2.getInstance(g);
106 Set<Resource> set = new HashSet<Resource>();
107 for (Statement s : g.getStatements(componentConRel1, sr.AllowsConnectionType)) {
108 if (s.isAsserted(componentConRel1))
110 set.add(s.getObject());
116 * Connects two Symbols, chooses connection type automatically.
118 * @param symbolConf2 other symbol.
119 * @param componentConRel1 symbol's terminal relation.
120 * @param componentConRel2 other symbol's terminal relation.
121 * @param forceFlag creates flag connection even if symbols are on the same diagram.
122 * @return SymbolConnetionData instance describing the connection. Return value is never null.
123 * @throws DatabaseException on database failure.
125 public SymbolConnectionData connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, boolean forceFlag) throws DatabaseException {
126 if (this.equals(symbolConf2)) {
127 if (PREVENT_SELF_CONNECTIONS) {
128 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
130 return new ConnectionErrorImpl(err);
132 if (componentConRel1.equals(componentConRel2)) {
133 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
135 return new ConnectionErrorImpl(err);
138 StructuralResource2 sr = StructuralResource2.getInstance(g);
139 Collection<Resource> connType1 = getConnectionTypes(g, componentConRel1);
140 Collection<Resource> connType2 = getConnectionTypes(g, componentConRel2);
141 Collection<Resource> matching = new HashSet<Resource>();
142 for (Resource r : connType2) {
143 if (connType1.contains(r))
146 if (matching.size() > 1) {
147 matching = getConnectionRel(g, symbolConf2, componentConRel1, componentConRel2, matching);
149 Resource usedConnType = null;
150 if (matching.size() != 1) {
152 if (USE_UNRELIABLE_CONNECT) {
153 err = "Unreliable connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " cannot find proper connection type, using Name Reference";
155 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;
158 return new ConnectionErrorImpl(err);
160 usedConnType = matching.iterator().next();
163 SymbolConnectionData data = customConnect(g, symbolConf2, componentConRel1, componentConRel2, usedConnType);
165 if (!forceFlag && diagram.equals(symbolConf2.diagram))
166 return _connect(g, symbolConf2, componentConRel1, componentConRel2,usedConnType);
168 return connectWithFlag(g, symbolConf2, componentConRel1, componentConRel2,usedConnType);
174 public SymbolConnectionData connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2) throws DatabaseException {
176 StructuralResource2 sr = StructuralResource2.getInstance(g);
177 ModelingResources mod = ModelingResources.getInstance(g);
179 Resource componentConRel1 = g.getPossibleObject(elementConRel1, mod.DiagramConnectionRelationToConnectionRelation);
180 Resource componentConRel2 = g.getPossibleObject(elementConRel2, mod.DiagramConnectionRelationToConnectionRelation);
182 if (componentConRel1 != null && componentConRel2 != null) {
183 return connect(g, symbolConf2, componentConRel1, componentConRel2);
186 Collection<Resource> connType1 = Collections.EMPTY_LIST;
187 Collection<Resource> connType2 = Collections.EMPTY_LIST;
188 if (componentConRel1 != null) {
189 connType1 = getConnectionTypes(g, componentConRel1);
190 if (connType1.size() > 1)
191 connType1 = getNonAssertedConnectionTypes(g,componentConRel1);
192 } else if (componentConRel2 != null) {
193 connType2 = getConnectionTypes(g, componentConRel2);
194 if (connType2.size() > 1)
195 connType2 = getNonAssertedConnectionTypes(g,componentConRel2);
197 Resource usedConnType = null;
198 if (connType1.size() == 1)
199 usedConnType = connType1.iterator().next();
200 else if (connType2.size() == 1)
201 usedConnType = connType2.iterator().next();
203 String err = "Cannot locate connection type for: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
205 return new ConnectionErrorImpl(err);
207 if (!diagram.equals(symbolConf2.diagram)) {
208 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;
210 return new ConnectionErrorImpl(err);
212 return connectElement(g, symbolConf2, elementConRel1, elementConRel2, usedConnType);
215 public SymbolConnectionData connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2, Resource usedConnType) throws DatabaseException {
216 if (this.equals(symbolConf2)) {
217 if (PREVENT_SELF_CONNECTIONS) {
218 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
220 return new ConnectionErrorImpl(err);
222 if (elementConRel1.equals(elementConRel2)) {
223 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
225 return new ConnectionErrorImpl(err);
228 // TODO : should we have separate customConnect method for element level connections?
229 SymbolConnectionData data = customConnect(g, symbolConf2, elementConRel1, elementConRel2, usedConnType);
231 return _connectElement(g, symbolConf2, elementConRel1, elementConRel2,usedConnType);
238 * Connects two symbols with chosen connection type. Does not work with signal connections.
240 * @param symbolConf2 other symbol.
241 * @param componentConRel1 symbol's terminal relation.
242 * @param componentConRel2 other symbol's terminal relation.
243 * @param connectionType used connection type.
244 * @return SymbolConnetionData instance describing the connection. Return value is never null.
245 * @throws DatabaseException on database failure.
247 public SymbolConnectionData connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectionType) throws DatabaseException {
248 if (diagram.equals(symbolConf2.diagram))
249 return _connect(g, symbolConf2, componentConRel1, componentConRel2, connectionType);
251 return connectWithFlag(g, symbolConf2, componentConRel1, componentConRel2, connectionType);
254 protected SymbolConnectionData connectToExisting(WriteGraph g, Symbol symbolConf2, Symbol current, Resource componentConRel1, Resource componentConRel2, Resource connectorType, boolean firstSame) throws DatabaseException {
255 return new ConnectionErrorImpl("Symbol is already connected");
258 protected Resource componentConnectionType(ReadGraph g, Resource componentConRel1, Resource componentConRel2) throws DatabaseException {
259 StructuralResource2 s = StructuralResource2.getInstance(g);
263 protected Resource diagramConnectionType(ReadGraph g, Resource componentConnectionType) throws DatabaseException {
264 ModelingResources MOD = ModelingResources.getInstance(g);
265 if (componentConnectionType != null) {
266 Resource ct = g.getPossibleObject(componentConnectionType, MOD.ConnectionTypeToDiagramConnectionType);
270 DiagramResource d = DiagramResource.getInstance(g);
271 return d.RouteGraphConnection;
274 private SymbolConnectionData _connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectorType) throws DatabaseException {
275 Layer0 b = Layer0.getInstance(g);
276 StructuralResource2 s = StructuralResource2.getInstance(g);
277 DiagramResource d = DiagramResource.getInstance(g);
278 ModelingResources m = ModelingResources.getInstance(g);
281 if (g.isInstanceOf(componentConRel1, b.FunctionalRelation) && getSingleConnected(g, componentConRel1) != null) {
282 Symbol current = getSingleConnected(g, componentConRel1);
283 if (current.component.equals(symbolConf2.component))
284 return new SymbolExistsImpl();
286 return connectToExisting(g, symbolConf2, current, componentConRel1, componentConRel2, connectorType, true);
288 if (g.isInstanceOf(componentConRel2, b.FunctionalRelation) && symbolConf2.getSingleConnected(g, componentConRel2) != null) {
289 Symbol current = symbolConf2.getSingleConnected(g, componentConRel2);
290 if (current.component.equals(component))
291 return new SymbolExistsImpl();
292 return connectToExisting(g, symbolConf2, current, componentConRel1, componentConRel2, connectorType, false);
296 // connection object in module level
297 Resource moduleConnection = g.newResource();
298 Resource moduleConnectionType = componentConnectionType(g, componentConRel1, componentConRel2);
299 g.claim(moduleConnection, b.InstanceOf, moduleConnectionType);
301 // connect the modules
303 g.claim(component, componentConRel1, moduleConnection);
304 g.claim(symbolConf2.component, componentConRel2, moduleConnection);
306 // connection object in diagram level
307 Resource diagramConnection = g.newResource();
308 g.claim(diagramConnection, b.InstanceOf, diagramConnectionType(g, moduleConnectionType));
309 DiagramUtils.addElementFirst(g, diagram, diagramConnection);
311 g.claim(diagramConnection, s.HasConnectionType, connectorType);
313 // Relation from element1 to connector1
314 Resource elementConRel1 = getDiagramConnectionRelation(g, element, componentConRel1);
315 Resource connectorRel1 = g.getPossibleObject(componentConRel1, s.HasAttachmentRelation);
316 if (connectorRel1 == null)
317 connectorRel1 = d.HasPlainConnector;
320 Resource connector1 = g.newResource();
321 g.claim(connector1, b.InstanceOf, d.Connector);
322 g.claim(element, elementConRel1, connector1);
323 g.claim(diagramConnection, connectorRel1, connector1);
325 // Relation from element2 to connector2
326 Resource elementConRel2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
327 Resource connectorRel2 = g.getPossibleObject(componentConRel2, s.HasAttachmentRelation);
328 if (connectorRel2 == null)
329 connectorRel2 = d.HasArrowConnector;
331 Resource connector2 = g.newResource();
332 g.claim(connector2, b.InstanceOf, d.Connector);
333 g.claim(symbolConf2.element, elementConRel2, connector2);
334 g.claim(diagramConnection, connectorRel2, connector2);
336 // connect connectors
337 g.claim(connector1, d.AreConnected, connector2);
339 g.claim(moduleConnection, m.ConnectionToDiagramConnection, diagramConnection);
341 return new ConnectorDataImpl(diagramConnection);
344 private SymbolConnectionData _connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2, Resource connectorType) throws DatabaseException {
345 Layer0 b = Layer0.getInstance(g);
346 StructuralResource2 s = StructuralResource2.getInstance(g);
347 DiagramResource d = DiagramResource.getInstance(g);
348 ModelingResources m = ModelingResources.getInstance(g);
351 if (g.isInstanceOf(elementConRel1, b.FunctionalRelation) && getDiagramSingleConnected(g, elementConRel1) != null) {
352 Symbol current = getDiagramSingleConnected(g, elementConRel1);
353 if (current.component.equals(symbolConf2.component))
354 return new SymbolExistsImpl();
355 String err = "Cannot connect, terminal 1 is reserved: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
357 return new ConnectionErrorImpl(err);
359 if (g.isInstanceOf(elementConRel2, b.FunctionalRelation) && symbolConf2.getDiagramSingleConnected(g, elementConRel2) != null) {
360 Symbol current = symbolConf2.getDiagramSingleConnected(g, elementConRel2);
361 if (current.component.equals(component))
362 return new SymbolExistsImpl();
363 String err = "Cannot connect, terminal 2 is reserved: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
365 return new ConnectionErrorImpl(err);
368 // // connection object in module level
369 // Resource moduleConnection = g.newResource();
370 // g.claim(moduleConnection, b.InstanceOf, s.Connection);
372 // // connect the modules
374 // g.claim(component, componentConRel1, moduleConnection);
375 // g.claim(symbolConf2.component, componentConRel2, moduleConnection);
377 // connection object in diagram level
378 Resource diagramConnection = g.newResource();
379 g.claim(diagramConnection, b.InstanceOf, diagramConnectionType(g, null));
380 DiagramUtils.addElementFirst(g, diagram, diagramConnection);
382 g.claim(diagramConnection, s.HasConnectionType, connectorType);
384 // Relation from element1 to connector1
385 // Resource elementConRel1 = getDiagramConnectionRelation(g, element, componentConRel1);
386 // Resource connectorRel1 = g.getPossibleObject(componentConRel1, s.HasAttachmentRelation);
387 // if (connectorRel1 == null)
388 // connectorRel1 = d.HasPlainConnector;
389 Resource connectorRel1 = d.HasPlainConnector;
392 Resource connector1 = g.newResource();
393 g.claim(connector1, b.InstanceOf, d.Connector);
394 g.claim(element, elementConRel1, connector1);
395 g.claim(diagramConnection, connectorRel1, connector1);
397 // Relation from element2 to connector2
398 // Resource elementConRel2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
399 // Resource connectorRel2 = g.getPossibleObject(componentConRel2, s.HasAttachmentRelation);
400 // if (connectorRel2 == null)
401 // connectorRel2 = d.HasArrowConnector;
403 Resource connectorRel2 = d.HasArrowConnector;
405 Resource connector2 = g.newResource();
406 g.claim(connector2, b.InstanceOf, d.Connector);
407 g.claim(symbolConf2.element, elementConRel2, connector2);
408 g.claim(diagramConnection, connectorRel2, connector2);
410 // connect connectors
411 g.claim(connector1, d.AreConnected, connector2);
413 // g.claim(moduleConnection, m.ConnectionToDiagramConnection, diagramConnection);
415 return new ConnectorDataImpl(diagramConnection);
419 private SymbolConnectionData connectWithFlag(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectionType) throws DatabaseException {
421 Diagram diagram2 = symbolConf2.diagram;
422 Layer0 l0 = Layer0.getInstance(g);
423 StructuralResource2 s = StructuralResource2.getInstance(g);
424 DiagramResource d = DiagramResource.getInstance(g);
425 ModelingResources m = ModelingResources.getInstance(g);
427 if (g.isInstanceOf(componentConRel1, l0.FunctionalRelation) && getSingleConnected(g, componentConRel1) != null) {
428 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";
430 return new ConnectionErrorImpl(err);
432 if (g.isInstanceOf(componentConRel2, l0.FunctionalRelation) && symbolConf2.getSingleConnected(g, componentConRel2) != null) {
433 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";
435 return new ConnectionErrorImpl(err);
439 double[] t1 = getSymbolTranslation(g);
440 double[] t2 = symbolConf2.getSymbolTranslation(g);
442 Symbol flag1 = diagram.addElement(g, d.Flag, t1[0]+t, t1[1]);
443 Symbol flag2 = diagram2.addElement(g, d.Flag, t2[0]-t, t2[1]);
445 Resource connectionJoin = g.newResource();
446 g.claim(connectionJoin, l0.InstanceOf, s.ConnectionJoin);
447 g.claim(diagram.getComposite(), s.HasConnectionJoin, connectionJoin);
448 g.claim(diagram2.getComposite(), s.HasConnectionJoin, connectionJoin);
450 Resource connection1 = g.newResource();
451 Resource ct = componentConnectionType(g, componentConRel1, componentConRel2);
452 g.claim(connection1, l0.InstanceOf, ct);
454 Resource connection2 = g.newResource();
455 g.claim(connection2, l0.InstanceOf, ct);
457 g.claim(component, componentConRel1, connection1);
458 g.claim(symbolConf2.component, componentConRel2, connection2);
459 g.claim(connection1, s.IsJoinedBy, connectionJoin);
460 g.claim(connection2, s.IsJoinedBy, connectionJoin);
462 g.claim(flag1.element, d.FlagIsJoinedBy, connectionJoin);
463 g.claim(flag2.element, d.FlagIsJoinedBy, connectionJoin);
465 Resource diagConnection1 = g.newResource();
466 Resource dct = diagramConnectionType(g, ct);
467 g.claim(diagConnection1, l0.InstanceOf, dct);
468 DiagramUtils.addElementFirst(g, diagram, diagConnection1);
469 g.claim(diagConnection1, s.HasConnectionType, connectionType);
471 Resource diagConnection2 = g.newResource();
472 g.claim(diagConnection2, l0.InstanceOf, dct);
473 DiagramUtils.addElementFirst(g, diagram2, diagConnection2);
474 g.claim(diagConnection2, s.HasConnectionType, connectionType);
476 g.claim(connection1, m.ConnectionToDiagramConnection, diagConnection1);
477 g.claim(connection2, m.ConnectionToDiagramConnection, diagConnection2);
479 // Relation from element1 to connector1
480 Resource isConnected1 = getDiagramConnectionRelation(g, element, componentConRel1);
483 Resource connector1_1 = g.newResource();
484 g.claim(connector1_1, l0.InstanceOf, d.Connector);
485 g.claim(element, isConnected1, connector1_1);
486 g.claim(diagConnection1, d.HasPlainConnector, connector1_1);
488 Resource connector1_2 = g.newResource();
489 g.claim(connector1_2, l0.InstanceOf, d.Connector);
490 g.claim(flag1.element, d.Flag_ConnectionPoint, connector1_2);
491 g.claim(diagConnection1, d.HasArrowConnector, connector1_2);
493 g.claim(connector1_1, d.AreConnected, connector1_2);
495 // Relation from element2 to connector2
496 Resource isConnected2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
499 Resource connector2_1 = g.newResource();
500 g.claim(connector2_1, l0.InstanceOf, d.Connector);
501 g.claim(flag2.element, d.Flag_ConnectionPoint, connector2_1);
502 g.claim(diagConnection2, d.HasPlainConnector, connector2_1);
504 Resource connector2_2 = g.newResource();
505 g.claim(connector2_2, l0.InstanceOf, d.Connector);
506 g.claim(symbolConf2.element, isConnected2, connector2_2);
507 g.claim(diagConnection2, d.HasArrowConnector, connector2_2);
509 g.claim(connector2_1, d.AreConnected, connector2_2);
511 return new FlagConnectionDataImpl(flag1, flag2, diagConnection1, diagConnection2);
515 * Returns diagram connection relation for an element and its component connection relation
518 * @param componentConnRel
520 * @throws ServiceException
521 * @throws NoSingleResultException
522 * @throws ValidationException
524 public static Resource getDiagramConnectionRelation(ReadGraph g, Resource element, Resource componentConnRel) throws DatabaseException {
525 ModelingResources m = ModelingResources.getInstance(g);
526 Collection<Resource> diagramConnectionRels = g.getObjects(componentConnRel, m.ConnectionRelationToDiagramConnectionRelation);
527 if (diagramConnectionRels.size() == 1)
528 return diagramConnectionRels.iterator().next();
529 if (diagramConnectionRels.size() > 1) {
530 List<Resource> matching = new ArrayList<Resource>();
531 for (Resource r : diagramConnectionRels) {
532 if (g.hasStatement(element, r))
535 if (matching.size() == 1)
536 return matching.get(0);
538 Layer0 b = Layer0.getInstance(g);
539 Collection<Resource> elementTypes = g.getObjects(element, b.InstanceOf);
540 if (elementTypes.size() != 1)
541 throw new RuntimeException("Cannot handle multi-instances " + element + " " + NameUtils.getSafeName(g, element));
542 Resource elementType = elementTypes.iterator().next();
543 Collection<Resource> rels = StructuralUtils.getConnectionRelations(g, elementType);
544 if (rels.size() == 1)
545 return rels.iterator().next();
546 for (Resource rel : rels) {
547 if (diagramConnectionRels.contains(rel))
550 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 );
553 public static Resource getComponentConnectionRelation(ReadGraph g, Resource diagraromConnRel) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
554 ModelingResources mr = ModelingResources.getInstance(g);
555 return g.getSingleObject(diagraromConnRel, mr.DiagramConnectionRelationToConnectionRelation);
558 // public static Resource getSymbolRelation(ReadGraph g, Symbol conf, Resource genericRel) throws DatabaseException {
559 // Layer0 b = Layer0.getInstance(g);
560 // if (g.isInstanceOf(conf.element, ab.CalculationLevelReferenceElement)) {
561 // ModelingResources m = ModelingResources.getInstance(g);
562 // //return g.getInverse(g.getSingleObject(conf.element, m.HasReferenceRelation));
563 // return g.getSingleObject(conf.element, m.HasReferenceRelation);
566 // Resource compType = g.getSingleObject(conf.getComponent(), b.InstanceOf);
568 // for (Resource rel : StructuralUtils.getConnectionRelations(g, compType))
569 // if (g.isSubrelationOf(rel, genericRel))
575 * Returns symbol where given symbol is connected using given relation. Returns null if the symbol is not connected using the relation.
577 * @param componentConRel
579 * @throws DatabaseException
581 public Symbol getSingleConnected(ReadGraph g, Resource componentConRel) throws DatabaseException {
582 Collection<Symbol> symbols = getConnected(g, componentConRel);
583 if (symbols.size() > 1)
584 throw new NoSingleResultException("Symbol " + component + " is connected more than once using relation " + componentConRel);
585 if (symbols.size() == 1)
586 return symbols.iterator().next();
590 public Symbol getSingleConnected(ReadGraph g, Resource componentConRel, Resource otherComponentConRel) throws DatabaseException {
591 Collection<Symbol> symbols = getConnected(g, componentConRel, otherComponentConRel);
592 if (symbols.size() > 1)
593 throw new NoSingleResultException("Symbol " + component + " is connected more than once using relation " + componentConRel);
594 if (symbols.size() == 1)
595 return symbols.iterator().next();
599 public Collection<Symbol> getConnected(ReadGraph g, Resource componentConRel, Resource otherComponentConRel) throws DatabaseException {
600 assert(componentConRel != null);
601 Layer0 l0 = Layer0.getInstance(g);
602 StructuralResource2 sr = StructuralResource2.getInstance(g);
603 ModelingResources mr = ModelingResources.getInstance(g);
604 Collection<Symbol> result = new ArrayList<Symbol>();
605 Collection<Resource> modConn1s = g.getObjects(this.component, componentConRel);
606 for (Resource modConn1 : modConn1s) {
608 Collection<Statement> connected = g.getStatements(modConn1, sr.Connects);
609 Collection<Resource> connectionJoins = g.getObjects(modConn1, sr.IsJoinedBy);
610 Collection<Resource> connectedComponents = new ArrayList<Resource>();
612 for (Statement stm : connected) {
613 if (!stm.getObject().equals(this.component)) {
614 if (g.getInverse(stm.getPredicate()).equals(otherComponentConRel)) {
615 connectedComponents.add(stm.getObject());
620 for (Resource connectionJoin : connectionJoins) {
621 Collection<Resource> joinedConns = g.getObjects(connectionJoin, sr.Joins);
622 for (Resource conn : joinedConns) {
623 if (!conn.equals(modConn1)) {
624 // TODO : check ConnectionJoins?
625 for (Statement stm : g.getStatements(conn, sr.Connects)) {
626 if (g.getInverse(stm.getPredicate()).equals(otherComponentConRel))
627 connectedComponents.add(stm.getObject());
632 for (Resource connectedComponent : connectedComponents) {
634 Resource pointElem = g.getSingleObject(connectedComponent, mr.ComponentToElement);
636 Diagram diag = diagram.fromExisting(g, g.getSingleObject(connectedComponent, l0.PartOf));
637 result.add(diag.getSymbol(g, pointElem,connectedComponent));
644 public Collection<Symbol> getConnected(ReadGraph g, Resource componentConRel) throws DatabaseException {
645 assert(componentConRel != null);
646 Layer0 l0 = Layer0.getInstance(g);
647 StructuralResource2 sr = StructuralResource2.getInstance(g);
648 ModelingResources mr = ModelingResources.getInstance(g);
650 Collection<Resource> modConn1s = g.getObjects(this.component, componentConRel);
651 Collection<Symbol> result = new ArrayList<Symbol>();
652 for (Resource modConn1 : modConn1s) {
654 Collection<Resource> connected = g.getObjects(modConn1, sr.Connects);
655 Collection<Resource> connectionJoins = g.getObjects(modConn1, sr.IsJoinedBy);
656 Collection<Resource> connectedComponents = new ArrayList<Resource>();
658 for (Resource r : connected) {
659 if (!r.equals(this.component)) {
660 connectedComponents.add(r);
663 for (Resource connectionJoin : connectionJoins) {
664 Collection<Resource> joinedConns = g.getObjects(connectionJoin, sr.Joins);
665 for (Resource conn : joinedConns) {
666 if (!conn.equals(modConn1)) {
667 // TODO : check ConnectionJoins?
668 for (Resource obj : g.getObjects(conn, sr.Connects))
669 connectedComponents.add(obj);
673 for (Resource connectedComponent : connectedComponents) {
675 Resource pointElem = g.getSingleObject(connectedComponent, mr.ComponentToElement);
677 Diagram diag = diagram.fromExisting(g, g.getSingleObject(connectedComponent, l0.PartOf));
678 result.add(diag.getSymbol(g, pointElem,connectedComponent));
686 * Returns connected symbols on diagram level. Searches through branchpoints.
688 * @param diagramConnRel
689 * @param otherConnRel
691 * @throws DatabaseException
693 public Collection<Symbol> getDiagramConnected(ReadGraph g, Resource diagramConnRel, Resource otherConnRel) throws DatabaseException {
694 DiagramResource dr = DiagramResource.getInstance(g);
695 Collection<Resource> connectors = g.getObjects(element, diagramConnRel);
696 Collection<Symbol> result = new ArrayList<Symbol>();
697 for (Resource connector : connectors) {
698 Resource otherConnector = g.getSingleObject(connector, dr.AreConnected);
699 if (g.isInstanceOf(otherConnector, dr.Connector)) {
700 getConnectorConnectors(g, otherConnector, otherConnRel, result);
701 } else if (g.isInstanceOf(otherConnector, dr.RouteLine)) {
702 getBranchPointConnectors(g, connector, otherConnector, otherConnRel, result);
704 throw new DatabaseException("Connector " + g.getPossibleURI(otherConnector) + " " + otherConnector + " han unknown type");
710 private void getConnectorConnectors(ReadGraph g, Resource connector, Resource otherConnRel, Collection<Symbol> result) throws DatabaseException{
711 StructuralResource2 sr = StructuralResource2.getInstance(g);
712 DiagramResource dr = DiagramResource.getInstance(g);
713 Collection<Statement> stms = g.getStatements(connector, sr.Connects);
714 Statement stm = null;
715 for (Statement s : stms) {
716 if (!g.isInstanceOf(s.getObject(), dr.Connection)) {
722 if (otherConnRel != null) {
723 Resource rel = g.getInverse(stm.getPredicate());
724 if (!rel.equals(otherConnRel))
727 Resource element = stm.getObject();
728 result.add(diagram.getSymbol(g, element));
731 private void getBranchPointConnectors(ReadGraph g, Resource origin, Resource branchPoint, Resource otherConnRel, Collection<Symbol> result) throws DatabaseException {
732 DiagramResource dr = DiagramResource.getInstance(g);
734 Collection<Resource> branchConnected = g.getObjects(branchPoint, dr.AreConnected);
735 for (Resource branchConnector : branchConnected) {
736 if (branchConnector.equals(origin))
738 if (g.isInstanceOf(branchConnector, dr.Connector)) {
739 getConnectorConnectors(g, branchConnector, otherConnRel, result);
740 } else if (g.isInstanceOf(branchConnector, dr.RouteLine)) {
741 getBranchPointConnectors(g, branchPoint, branchConnector, otherConnRel, result);
743 throw new DatabaseException("Connector " + g.getPossibleURI(branchConnector) + " " + branchConnector + " han unknown type");
748 public Collection<Symbol> getDiagramConnected(ReadGraph g, Resource diagramConnRel) throws DatabaseException {
749 return getDiagramConnected(g, diagramConnRel,null);
752 public Collection<Symbol> getDiagramInverseConnected(ReadGraph g, Resource otherConnRel) throws DatabaseException {
753 StructuralResource2 sr = StructuralResource2.getInstance(g);
754 return getDiagramConnected(g, sr.IsConnectedTo, otherConnRel);
758 public Collection<Symbol> getDiagramConnected(ReadGraph g) throws DatabaseException {
759 StructuralResource2 sr = StructuralResource2.getInstance(g);
760 return getDiagramConnected(g, sr.IsConnectedTo, null);
763 public Symbol getDiagramSingleConnected(ReadGraph g, Resource diagramConnRel) throws DatabaseException {
764 Collection<Symbol> symbols = getDiagramConnected(g, diagramConnRel);
765 if (symbols.size() > 1) {
766 throw new NoSingleResultException("Symbol " + element + " has more than one connection with " + diagramConnRel);
767 } else if (symbols.size() == 1) {
768 return symbols.iterator().next();
773 public Symbol getOtherFlag(ReadGraph g) throws DatabaseException {
774 DiagramResource dr = DiagramResource.getInstance(g);
775 Resource connectionJoin = g.getPossibleObject(element, dr.FlagIsJoinedBy);
776 if (connectionJoin == null)
778 Collection<Resource> flags = g.getObjects(connectionJoin, dr.JoinsFlag);
779 Resource otherFlag = null;
780 for (Resource f : flags) {
781 if (!f.equals(element)) {
787 if (otherFlag == null) {
790 Resource otherDiagramR = OrderedSetUtils.getSingleOwnerList(g, otherFlag);
791 Diagram otherDiagram = diagram.fromExisting(g, otherDiagramR);
792 return otherDiagram.getSymbol(g, otherFlag, null);
797 * Returns concatenated translation of the symbol.
800 * @throws DatabaseException
802 public double[] getSymbolTranslation(ReadGraph g) throws DatabaseException {
803 DiagramResource d = DiagramResource.getInstance(g);
804 ModelingResources mr = ModelingResources.getInstance(g);
806 Resource e = element;
807 AffineTransform at = new AffineTransform();
809 double transform[] = g.getPossibleRelatedValue(e, d.HasTransform);
810 if (transform == null)
812 AffineTransform at2 = new AffineTransform(transform);
813 at.preConcatenate(at2);
814 Resource component = g.getPossibleObject(e, mr.HasParentComponent);
815 if (component != null) {
816 e = g.getPossibleObject(component, mr.ComponentToElement);
822 return new double[]{at.getTranslateX(),at.getTranslateY()};
826 * Returns transformation of the symbol.
829 * @throws DatabaseException
831 public double[] getSymbolTransformation(ReadGraph g) throws DatabaseException{
832 DiagramResource d = DiagramResource.getInstance(g);
833 double transform[] = g.getPossibleRelatedValue(element, d.HasTransform);
838 * Sets translation of symbol. The scale is set to 1.0.
842 * @throws DatabaseException
844 public void setSymbolTranslation(WriteGraph g, double x, double y) throws DatabaseException {
845 DiagramResource d = DiagramResource.getInstance(g);
846 G2DResource g2d = G2DResource.getInstance(g);
847 g.claimLiteral(element, d.HasTransform, g2d.Transform, new double[]{1.0,0.0,0.0,1.0,x,y});
850 public void setSymbolTransformation(WriteGraph g, AffineTransform at) throws DatabaseException {
851 DiagramResource d = DiagramResource.getInstance(g);
852 G2DResource g2d = G2DResource.getInstance(g);
853 double arr[] = new double[6];
855 g.claimLiteral(element, d.HasTransform, g2d.Transform, arr);
859 * Set the scale of symbol.
862 * @throws DatabaseException
864 public void setScale(WriteGraph g, double scale) throws DatabaseException {
865 DiagramResource d = DiagramResource.getInstance(g);
866 double transform[] = g.getPossibleRelatedValue(element, d.HasTransform);
867 g.claimLiteral(element, d.HasTransform, new double[]{scale,0.0,0.0,scale,transform[4],transform[5]});
871 public boolean equals(Object o) {
874 if (this.getClass() != o.getClass())
876 Symbol other = (Symbol)o;
877 return element.equals(other.element);
881 public int hashCode() {
882 return element.hashCode();
888 * Returns component type from symbol type
892 * @throws NoSingleResultException
893 * @throws ManyObjectsForFunctionalRelationException
894 * @throws ServiceException
896 public static Resource getComponentTypeFromSymbolType(ReadGraph g, Resource symbolType) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
897 ModelingResources m = ModelingResources.getInstance(g);
898 Resource component = g.getPossibleObject(symbolType, m.SymbolToComponentType);
902 public static Resource getSymbolTypeFromComponentType(ReadGraph g, Resource componentType) throws NoSingleResultException, ServiceException, ManyObjectsForFunctionalRelationException {
903 ModelingResources m = ModelingResources.getInstance(g);
904 Collection<Resource> symbols = g.getObjects(componentType, m.ComponentTypeToSymbol);
905 if (symbols.size() == 0)
907 return symbols.iterator().next();
910 public void remove(WriteGraph g) throws DatabaseException{
911 Layer0 L0 = Layer0.getInstance(g);
912 StructuralResource2 STR = StructuralResource2.getInstance(g);
913 DiagramResource DIA = DiagramResource.getInstance(g);
914 OrderedSetUtils.remove(g, diagram.getDiagram(), element);
915 if (component != null)
916 g.deny(component, L0.PartOf);
917 Collection<Statement> statements = g.getStatements(element, STR.IsConnectedTo);
918 for (Statement s : statements) {
919 if (g.isInstanceOf(s.getObject(),DIA.Connector)) {
920 Resource connectedConnector = g.getPossibleObject(s.getObject(), DIA.AreConnected);
921 if (connectedConnector != null)
922 g.deny(connectedConnector);
923 g.deny(s.getObject());
926 if (component != null) {
927 statements = g.getStatements(component, STR.IsConnectedTo);
928 for (Statement s : statements) {
929 if (g.isInstanceOf(s.getObject(),STR.Connection)) {
930 g.deny(s.getObject());
935 if (component != null)
939 public void dispose() {
946 * Common interface for Symbol.connect return value.
949 public interface SymbolConnectionData {
954 * Interface for returning diagram level connectors.
957 public interface DiagramConnectionData extends SymbolConnectionData {
958 public int getConnectionCount();
959 public Resource getConnection(int index);
963 * Interface for flag connections.
966 public interface FlagConnectionData extends SymbolConnectionData {
967 public Symbol getFirstFlag();
968 public Symbol getSecondFlag();
972 * Interface for "connection" that merged the connected symbols into one symbol.
975 public interface MergeSymbolData extends SymbolConnectionData {
976 Symbol getResultSymbol();
980 * Return value for a connection that already exists.
983 public interface SymbolExists extends SymbolConnectionData {
988 * Return value for an error.
991 public interface ConnectionError extends SymbolConnectionData {
995 private static class ConnectionErrorImpl implements ConnectionError {
996 private String error;
998 public ConnectionErrorImpl(String error) {
1003 public String getReason() {
1008 private static class SymbolExistsImpl implements SymbolExists {
1012 private static class ConnectorDataImpl implements DiagramConnectionData {
1014 private Resource[] connectors;
1016 public ConnectorDataImpl(Resource... connectors) {
1017 this.connectors = connectors;
1021 public Resource getConnection(int index) {
1022 return connectors[index];
1026 public int getConnectionCount() {
1027 return connectors.length;
1031 private static class FlagConnectionDataImpl implements FlagConnectionData, DiagramConnectionData {
1033 private Symbol firstFlag;
1034 private Symbol secondFlag;
1035 private Resource[] connectors;
1037 public FlagConnectionDataImpl(Symbol firstFlag, Symbol secondFlag, Resource... connectors) {
1038 this.firstFlag = firstFlag;
1039 this.secondFlag = secondFlag;
1040 this.connectors = connectors;
1044 public Symbol getFirstFlag() {
1049 public Symbol getSecondFlag() {
1054 public Resource getConnection(int index) {
1055 return connectors[index];
1059 public int getConnectionCount() {
1060 return connectors.length;
1064 public void setComponentValue(WriteGraph graph, Resource property, double value) throws DatabaseException{
1065 setValue(graph, component, property, value);
1068 public void setComponentValue(WriteGraph graph, Resource property, float value) throws DatabaseException{
1069 setValue(graph, component, property, value);
1072 public void setComponentValue(WriteGraph graph, Resource property, boolean value) throws DatabaseException{
1073 setValue(graph, component, property, value);
1076 public void setComponentValue(WriteGraph graph, Resource property, int value) throws DatabaseException{
1077 setValue(graph, component, property, value);
1080 public void setComponentValue(WriteGraph graph, Resource property, String value) throws DatabaseException{
1081 setValue(graph, component, property, value);
1084 public void setComponentValue(WriteGraph graph, Resource property, double value[]) throws DatabaseException{
1085 setValue(graph, component, property, value);
1088 public void setComponentValue(WriteGraph graph, Resource property, float value[]) throws DatabaseException{
1089 setValue(graph, component, property, value);
1092 public void setComponentValue(WriteGraph graph, Resource property, int value[]) throws DatabaseException{
1093 setValue(graph, component, property, value);
1096 public void setComponentValue(WriteGraph graph, Resource property, boolean value[]) throws DatabaseException{
1097 setValue(graph, component, property, value);
1100 public void setElementValue(WriteGraph graph, Resource property, double value) throws DatabaseException{
1101 setValue(graph, element, property, value);
1104 public void setElementValue(WriteGraph graph, Resource property, float value) throws DatabaseException{
1105 setValue(graph, element, property, value);
1108 public void setElementValue(WriteGraph graph, Resource property, boolean value) throws DatabaseException{
1109 setValue(graph, element, property, value);
1112 public void setElementValue(WriteGraph graph, Resource property, int value) throws DatabaseException{
1113 setValue(graph, element, property, value);
1116 public void setElementValue(WriteGraph graph, Resource property, double value[]) throws DatabaseException{
1117 setValue(graph, element, property, value);
1120 public void setElementValue(WriteGraph graph, Resource property, float value[]) throws DatabaseException{
1121 setValue(graph, element, property, value);
1124 public void setElementValue(WriteGraph graph, Resource property, int value[]) throws DatabaseException{
1125 setValue(graph, element, property, value);
1128 public void setElementValue(WriteGraph graph, Resource property, boolean value[]) throws DatabaseException{
1129 setValue(graph, element, property, value);
1133 * Sets literal value. Does data conversion if required.
1135 * Note: This method support only some basic L0 data types.
1141 * @throws DatabaseException
1143 public static void setValue(WriteGraph graph, Resource object, Resource property, double value) throws DatabaseException{
1144 Layer0 l0 = Layer0.getInstance(graph);
1145 Resource range = graph.getPossibleObject(property, l0.HasRange);
1146 if (l0.Double.equals(range)) {
1147 graph.claimLiteral(object, property, value, Bindings.DOUBLE);
1148 } else if (l0.Float.equals(range)) {
1149 graph.claimLiteral(object, property, (float)value, Bindings.FLOAT);
1150 } else if (l0.Integer.equals(range)) {
1151 graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1152 } else if (l0.Long.equals(range)) {
1153 graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1154 } else if (l0.Boolean.equals(range)) {
1155 graph.claimLiteral(object, property, value > 0.0, Bindings.BOOLEAN);
1156 } else if (l0.String.equals(range)) {
1157 graph.claimLiteral(object, property, Double.toString(value), Bindings.STRING);
1159 graph.claimLiteral(object, property, value, Bindings.DOUBLE);
1164 * Sets literal value. Does data conversion if required.
1166 * Note: This method support only some basic L0 data types.
1172 * @throws DatabaseException
1174 public static void setValue(WriteGraph graph, Resource object, Resource property, float value) throws DatabaseException{
1175 Layer0 l0 = Layer0.getInstance(graph);
1176 Resource range = graph.getPossibleObject(property, l0.HasRange);
1177 if (l0.Double.equals(range)) {
1178 graph.claimLiteral(object, property, (double)value, Bindings.DOUBLE);
1179 } else if (l0.Float.equals(range)) {
1180 graph.claimLiteral(object, property, value, Bindings.FLOAT);
1181 } else if (l0.Integer.equals(range)) {
1182 graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1183 } else if (l0.Long.equals(range)) {
1184 graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1185 } else if (l0.Boolean.equals(range)) {
1186 graph.claimLiteral(object, property, value > 0.f, Bindings.BOOLEAN);
1187 } else if (l0.String.equals(range)) {
1188 graph.claimLiteral(object, property, Float.toString(value), Bindings.STRING);
1190 graph.claimLiteral(object, property, value, Bindings.FLOAT);
1195 * Sets literal value. Does data conversion if required.
1197 * Note: This method support only some basic L0 data types.
1203 * @throws DatabaseException
1205 public static void setValue(WriteGraph graph, Resource object, Resource property, int value) throws DatabaseException{
1206 Layer0 l0 = Layer0.getInstance(graph);
1207 Resource range = graph.getPossibleObject(property, l0.HasRange);
1208 if (l0.Double.equals(range)) {
1209 graph.claimLiteral(object, property, (double)value, Bindings.DOUBLE);
1210 } else if (l0.Float.equals(range)) {
1211 graph.claimLiteral(object, property, (float) value, Bindings.FLOAT);
1212 } else if (l0.Integer.equals(range)) {
1213 graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1214 } else if (l0.Long.equals(range)) {
1215 graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1216 } else if (l0.Boolean.equals(range)) {
1217 graph.claimLiteral(object, property, value > 0, Bindings.BOOLEAN);
1218 } else if (l0.String.equals(range)) {
1219 graph.claimLiteral(object, property, Integer.toString(value), Bindings.STRING);
1221 graph.claimLiteral(object, property, value, Bindings.INTEGER);
1226 * Sets literal value. Does data conversion if required.
1232 * @throws DatabaseException
1234 public static void setValue(WriteGraph graph, Resource object, Resource property, String value) throws DatabaseException{
1235 Layer0 l0 = Layer0.getInstance(graph);
1236 Resource range = graph.getPossibleObject(property, l0.HasRange);
1237 if (range == null) {
1238 graph.claimLiteral(object, property, value, Bindings.STRING);
1240 if (graph.isInheritedFrom(range, l0.String)) {
1241 graph.claimLiteral(object, property, value, Bindings.STRING);
1242 } else if (graph.isInheritedFrom(range, l0.Double)) {
1243 graph.claimLiteral(object, property, Double.parseDouble(value), Bindings.DOUBLE);
1244 } else if (graph.isInheritedFrom(range, l0.Float)) {
1245 graph.claimLiteral(object, property, Float.parseFloat(value), Bindings.FLOAT);
1246 } else if (graph.isInheritedFrom(range, l0.Integer)) {
1247 graph.claimLiteral(object, property, Integer.parseInt(value), Bindings.INTEGER);
1248 } else if (graph.isInheritedFrom(range, l0.Long)) {
1249 graph.claimLiteral(object, property, Long.parseLong(value), Bindings.LONG);
1250 } else if (graph.isInheritedFrom(range, l0.Boolean)) {
1251 graph.claimLiteral(object, property, Boolean.parseBoolean(value), Bindings.BOOLEAN);
1253 graph.claimLiteral(object, property, value, Bindings.STRING);
1259 * Sets literal value. Does data conversion if required.
1261 * Note: This method support only some basic L0 data types.
1267 * @throws DatabaseException
1269 public static void setValue(WriteGraph graph, Resource object, Resource property, boolean value) throws DatabaseException{
1270 Layer0 l0 = Layer0.getInstance(graph);
1271 Resource range = graph.getPossibleObject(property, l0.HasRange);
1272 if (l0.Boolean.equals(range)) {
1273 graph.claimLiteral(object, property, value , Bindings.BOOLEAN);
1274 } else if (l0.String.equals(range)) {
1275 graph.claimLiteral(object, property, Boolean.toString(value), Bindings.STRING);
1276 } else if (l0.Integer.equals(range)) {
1277 graph.claimLiteral(object, property, value ? 1 : 0, Bindings.INTEGER);
1279 graph.claimLiteral(object, property, value, Bindings.BOOLEAN);
1284 * Sets literal value. Does data conversion if required.
1286 * Note: This method support only some basic L0 data types.
1292 * @throws DatabaseException
1294 public static void setValue(WriteGraph graph, Resource object, Resource property, double[] value) throws DatabaseException{
1295 Layer0 l0 = Layer0.getInstance(graph);
1296 Resource range = graph.getPossibleObject(property, l0.HasRange);
1297 if (l0.DoubleArray.equals(range)) {
1298 graph.claimLiteral(object, property, value, Bindings.DOUBLE_ARRAY);
1299 } else if (l0.FloatArray.equals(range)) {
1300 float arr[] = new float[value.length];
1301 for (int i = 0; i < value.length; i++)
1302 arr[i] = (float) value[i];
1303 graph.claimLiteral(object, property, arr, Bindings.FLOAT_ARRAY);
1304 } else if (l0.IntegerArray.equals(range)) {
1305 int arr[] = new int[value.length];
1306 for (int i = 0; i < value.length; i++)
1307 arr[i] = (int) value[i];
1308 graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1310 graph.claimLiteral(object, property, value, Bindings.DOUBLE_ARRAY);
1315 * Sets literal value. Does data conversion if required.
1317 * Note: This method support only some basic L0 data types.
1323 * @throws DatabaseException
1325 public static void setValue(WriteGraph graph, Resource object, Resource property, float[] value) throws DatabaseException{
1326 Layer0 l0 = Layer0.getInstance(graph);
1327 Resource range = graph.getPossibleObject(property, l0.HasRange);
1328 if (l0.FloatArray.equals(range)) {
1329 graph.claimLiteral(object, property, value, Bindings.FLOAT_ARRAY);
1330 } else if (l0.DoubleArray.equals(range)) {
1331 double arr[] = new double[value.length];
1332 for (int i = 0; i < value.length; i++)
1333 arr[i] = (double) value[i];
1334 graph.claimLiteral(object, property, arr, Bindings.DOUBLE_ARRAY);
1335 } else if (l0.IntegerArray.equals(range)) {
1336 int arr[] = new int[value.length];
1337 for (int i = 0; i < value.length; i++)
1338 arr[i] = (int) value[i];
1339 graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1341 graph.claimLiteral(object, property, value, Bindings.FLOAT_ARRAY);
1346 * Sets literal value. Does data conversion if required.
1348 * Note: This method support only some basic L0 data types.
1354 * @throws DatabaseException
1356 public static void setValue(WriteGraph graph, Resource object, Resource property, int[] value) throws DatabaseException{
1357 Layer0 l0 = Layer0.getInstance(graph);
1358 Resource range = graph.getPossibleObject(property, l0.HasRange);
1359 if (l0.IntegerArray.equals(range)) {
1360 graph.claimLiteral(object, property, value, Bindings.INT_ARRAY);
1361 } else if (l0.DoubleArray.equals(range)) {
1362 double arr[] = new double[value.length];
1363 for (int i = 0; i < value.length; i++)
1364 arr[i] = (double) value[i];
1365 graph.claimLiteral(object, property, arr, Bindings.DOUBLE_ARRAY);
1366 } else if (l0.FloatArray.equals(range)) {
1367 float arr[] = new float[value.length];
1368 for (int i = 0; i < value.length; i++)
1369 arr[i] = (float) value[i];
1370 graph.claimLiteral(object, property, arr, Bindings.FLOAT_ARRAY);
1372 graph.claimLiteral(object, property, value, Bindings.INT_ARRAY);
1377 * Sets literal value. Does data conversion if required.
1379 * Note: This method support only some basic L0 data types.
1385 * @throws DatabaseException
1387 public static void setValue(WriteGraph graph, Resource object, Resource property, boolean[] value) throws DatabaseException{
1388 Layer0 l0 = Layer0.getInstance(graph);
1389 Resource range = graph.getPossibleObject(property, l0.HasRange);
1390 if (l0.BooleanArray.equals(range)) {
1391 graph.claimLiteral(object, property, value, Bindings.BOOLEAN_ARRAY);
1392 } else if (l0.IntegerArray.equals(range)) {
1393 int arr[] = new int[value.length];
1394 for (int i = 0; i < value.length; i++)
1395 arr[i] = value[i] ? 1 : 0;
1396 graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1398 graph.claimLiteral(object, property, value, Bindings.BOOLEAN_ARRAY);