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.Logger;
17 import org.simantics.db.common.utils.NameUtils;
18 import org.simantics.db.common.utils.OrderedSetUtils;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
21 import org.simantics.db.exception.NoSingleResultException;
22 import org.simantics.db.exception.ServiceException;
23 import org.simantics.db.exception.ValidationException;
24 import org.simantics.diagram.stubs.DiagramResource;
25 import org.simantics.diagram.stubs.G2DResource;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.structural.stubs.StructuralResource2;
29 import org.simantics.structural2.utils.StructuralUtils;
34 * @author Marko Luukkainen
36 public abstract class Symbol {
38 private static boolean USE_UNRELIABLE_CONNECT = false;
39 private static boolean PREVENT_SELF_CONNECTIONS = false;
41 private Diagram diagram;
46 * Use Diagram.getSymbol() to get Symbols.
51 public Symbol(Diagram diagram, Resource element, Resource component) {
52 assert(diagram != null);
53 assert(element != null);
54 this.diagram = diagram;
55 this.element = element;
56 this.component = component;
59 public Diagram getDiagram() {
63 public Resource getComponent() {
67 public Resource getElement() {
72 * Connects two Symbols, chooses connection type automatically.
74 * @param symbolConf2 other symbol.
75 * @param componentConRel1 symbol's terminal relation.
76 * @param componentConRel2 other symbol's terminal relation.
77 * @return SymbolConnetionData instance describing the connection. Return value is never null.
78 * @throws DatabaseException on database failure.
80 public SymbolConnectionData connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2) throws DatabaseException {
81 return connect(g, symbolConf2, componentConRel1, componentConRel2, false);
85 protected Collection<Resource> getConnectionRel(ReadGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Collection<Resource> matching) throws DatabaseException {
89 protected SymbolConnectionData customConnect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connType) throws DatabaseException {
93 protected Set<Resource> getConnectionTypes(ReadGraph g, Resource componentConRel1) throws DatabaseException {
94 StructuralResource2 sr = StructuralResource2.getInstance(g);
95 Set<Resource> set = new HashSet<Resource>();
96 set.addAll(g.getObjects(componentConRel1, sr.AllowsConnectionType));
100 protected Set<Resource> getNonAssertedConnectionTypes(ReadGraph g, Resource componentConRel1) throws DatabaseException {
101 StructuralResource2 sr = StructuralResource2.getInstance(g);
102 Set<Resource> set = new HashSet<Resource>();
103 for (Statement s : g.getStatements(componentConRel1, sr.AllowsConnectionType)) {
104 if (s.isAsserted(componentConRel1))
106 set.add(s.getObject());
112 * Connects two Symbols, chooses connection type automatically.
114 * @param symbolConf2 other symbol.
115 * @param componentConRel1 symbol's terminal relation.
116 * @param componentConRel2 other symbol's terminal relation.
117 * @param forceFlag creates flag connection even if symbols are on the same diagram.
118 * @return SymbolConnetionData instance describing the connection. Return value is never null.
119 * @throws DatabaseException on database failure.
121 public SymbolConnectionData connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, boolean forceFlag) throws DatabaseException {
122 if (this.equals(symbolConf2)) {
123 if (PREVENT_SELF_CONNECTIONS) {
124 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
125 Logger.defaultLogError(err);
126 return new ConnectionErrorImpl(err);
128 if (componentConRel1.equals(componentConRel2)) {
129 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
130 Logger.defaultLogError(err);
131 return new ConnectionErrorImpl(err);
134 StructuralResource2 sr = StructuralResource2.getInstance(g);
135 Collection<Resource> connType1 = getConnectionTypes(g, componentConRel1);
136 Collection<Resource> connType2 = getConnectionTypes(g, componentConRel2);
137 Collection<Resource> matching = new HashSet<Resource>();
138 for (Resource r : connType2) {
139 if (connType1.contains(r))
142 if (matching.size() > 1) {
143 matching = getConnectionRel(g, symbolConf2, componentConRel1, componentConRel2, matching);
145 Resource usedConnType = null;
146 if (matching.size() != 1) {
148 if (USE_UNRELIABLE_CONNECT) {
149 err = "Unreliable connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " cannot find proper connection type, using Name Reference";
151 err = "Unable to connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " cannot find proper connection type for terminals " + g.getPossibleURI(componentConRel1) + " "+ componentConRel1 + " " + g.getPossibleURI(componentConRel2) + " "+ componentConRel2;
153 Logger.defaultLogInfo(err);
154 return new ConnectionErrorImpl(err);
156 usedConnType = matching.iterator().next();
159 SymbolConnectionData data = customConnect(g, symbolConf2, componentConRel1, componentConRel2, usedConnType);
161 if (!forceFlag && diagram.equals(symbolConf2.diagram))
162 return _connect(g, symbolConf2, componentConRel1, componentConRel2,usedConnType);
164 return connectWithFlag(g, symbolConf2, componentConRel1, componentConRel2,usedConnType);
170 public SymbolConnectionData connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2) throws DatabaseException {
172 StructuralResource2 sr = StructuralResource2.getInstance(g);
173 ModelingResources mod = ModelingResources.getInstance(g);
175 Resource componentConRel1 = g.getPossibleObject(elementConRel1, mod.DiagramConnectionRelationToConnectionRelation);
176 Resource componentConRel2 = g.getPossibleObject(elementConRel2, mod.DiagramConnectionRelationToConnectionRelation);
178 if (componentConRel1 != null && componentConRel2 != null) {
179 return connect(g, symbolConf2, componentConRel1, componentConRel2);
182 Collection<Resource> connType1 = Collections.EMPTY_LIST;
183 Collection<Resource> connType2 = Collections.EMPTY_LIST;
184 if (componentConRel1 != null) {
185 connType1 = getConnectionTypes(g, componentConRel1);
186 if (connType1.size() > 1)
187 connType1 = getNonAssertedConnectionTypes(g,componentConRel1);
188 } else if (componentConRel2 != null) {
189 connType2 = getConnectionTypes(g, componentConRel2);
190 if (connType2.size() > 1)
191 connType2 = getNonAssertedConnectionTypes(g,componentConRel2);
193 Resource usedConnType = null;
194 if (connType1.size() == 1)
195 usedConnType = connType1.iterator().next();
196 else if (connType2.size() == 1)
197 usedConnType = connType2.iterator().next();
199 String err = "Cannot locate connection type for: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
200 Logger.defaultLogError(err);
201 return new ConnectionErrorImpl(err);
203 if (!diagram.equals(symbolConf2.diagram)) {
204 String err = "Element connections must be done on the same diagram: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
205 Logger.defaultLogError(err);
206 return new ConnectionErrorImpl(err);
208 return connectElement(g, symbolConf2, elementConRel1, elementConRel2, usedConnType);
211 public SymbolConnectionData connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2, Resource usedConnType) throws DatabaseException {
212 if (this.equals(symbolConf2)) {
213 if (PREVENT_SELF_CONNECTIONS) {
214 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
215 Logger.defaultLogError(err);
216 return new ConnectionErrorImpl(err);
218 if (elementConRel1.equals(elementConRel2)) {
219 String err = "Preventing connection to self: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
220 Logger.defaultLogError(err);
221 return new ConnectionErrorImpl(err);
224 // TODO : should we have separate customConnect method for element level connections?
225 SymbolConnectionData data = customConnect(g, symbolConf2, elementConRel1, elementConRel2, usedConnType);
227 return _connectElement(g, symbolConf2, elementConRel1, elementConRel2,usedConnType);
234 * Connects two symbols with chosen connection type. Does not work with signal connections.
236 * @param symbolConf2 other symbol.
237 * @param componentConRel1 symbol's terminal relation.
238 * @param componentConRel2 other symbol's terminal relation.
239 * @param connectionType used connection type.
240 * @return SymbolConnetionData instance describing the connection. Return value is never null.
241 * @throws DatabaseException on database failure.
243 public SymbolConnectionData connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectionType) throws DatabaseException {
244 if (diagram.equals(symbolConf2.diagram))
245 return _connect(g, symbolConf2, componentConRel1, componentConRel2, connectionType);
247 return connectWithFlag(g, symbolConf2, componentConRel1, componentConRel2, connectionType);
250 protected SymbolConnectionData connectToExisting(WriteGraph g, Symbol symbolConf2, Symbol current, Resource componentConRel1, Resource componentConRel2, Resource connectorType, boolean firstSame) throws DatabaseException {
251 return new ConnectionErrorImpl("Symbol is already connected");
254 private SymbolConnectionData _connect(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectorType) throws DatabaseException {
255 Layer0 b = Layer0.getInstance(g);
256 StructuralResource2 s = StructuralResource2.getInstance(g);
257 DiagramResource d = DiagramResource.getInstance(g);
258 ModelingResources m = ModelingResources.getInstance(g);
261 if (g.isInstanceOf(componentConRel1, b.FunctionalRelation) && getSingleConnected(g, componentConRel1) != null) {
262 Symbol current = getSingleConnected(g, componentConRel1);
263 if (current.component.equals(symbolConf2.component))
264 return new SymbolExistsImpl();
266 return connectToExisting(g, symbolConf2, current, componentConRel1, componentConRel2, connectorType, true);
268 if (g.isInstanceOf(componentConRel2, b.FunctionalRelation) && symbolConf2.getSingleConnected(g, componentConRel2) != null) {
269 Symbol current = symbolConf2.getSingleConnected(g, componentConRel2);
270 if (current.component.equals(component))
271 return new SymbolExistsImpl();
272 return connectToExisting(g, symbolConf2, current, componentConRel1, componentConRel2, connectorType, false);
276 // connection object in module level
277 Resource moduleConnection = g.newResource();
278 g.claim(moduleConnection, b.InstanceOf, s.Connection);
280 // connect the modules
282 g.claim(component, componentConRel1, moduleConnection);
283 g.claim(symbolConf2.component, componentConRel2, moduleConnection);
285 // connection object in diagram level
286 Resource diagramConnection = g.newResource();
287 g.claim(diagramConnection, b.InstanceOf, d.RouteGraphConnection);
288 DiagramUtils.addElementFirst(g, diagram, diagramConnection);
290 g.claim(diagramConnection, s.HasConnectionType, connectorType);
292 // Relation from element1 to connector1
293 Resource elementConRel1 = getDiagramConnectionRelation(g, element, componentConRel1);
294 Resource connectorRel1 = g.getPossibleObject(componentConRel1, s.HasAttachmentRelation);
295 if (connectorRel1 == null)
296 connectorRel1 = d.HasPlainConnector;
299 Resource connector1 = g.newResource();
300 g.claim(connector1, b.InstanceOf, d.Connector);
301 g.claim(element, elementConRel1, connector1);
302 g.claim(diagramConnection, connectorRel1, connector1);
304 // Relation from element2 to connector2
305 Resource elementConRel2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
306 Resource connectorRel2 = g.getPossibleObject(componentConRel2, s.HasAttachmentRelation);
307 if (connectorRel2 == null)
308 connectorRel2 = d.HasArrowConnector;
310 Resource connector2 = g.newResource();
311 g.claim(connector2, b.InstanceOf, d.Connector);
312 g.claim(symbolConf2.element, elementConRel2, connector2);
313 g.claim(diagramConnection, connectorRel2, connector2);
315 // connect connectors
316 g.claim(connector1, d.AreConnected, connector2);
318 g.claim(moduleConnection, m.ConnectionToDiagramConnection, diagramConnection);
320 return new ConnectorDataImpl(diagramConnection);
323 private SymbolConnectionData _connectElement(WriteGraph g, Symbol symbolConf2, Resource elementConRel1, Resource elementConRel2, Resource connectorType) throws DatabaseException {
324 Layer0 b = Layer0.getInstance(g);
325 StructuralResource2 s = StructuralResource2.getInstance(g);
326 DiagramResource d = DiagramResource.getInstance(g);
327 ModelingResources m = ModelingResources.getInstance(g);
330 if (g.isInstanceOf(elementConRel1, b.FunctionalRelation) && getDiagramSingleConnected(g, elementConRel1) != null) {
331 Symbol current = getDiagramSingleConnected(g, elementConRel1);
332 if (current.component.equals(symbolConf2.component))
333 return new SymbolExistsImpl();
334 String err = "Cannot connect, terminal 1 is reserved: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
335 Logger.defaultLogError(err);
336 return new ConnectionErrorImpl(err);
338 if (g.isInstanceOf(elementConRel2, b.FunctionalRelation) && symbolConf2.getDiagramSingleConnected(g, elementConRel2) != null) {
339 Symbol current = symbolConf2.getDiagramSingleConnected(g, elementConRel2);
340 if (current.component.equals(component))
341 return new SymbolExistsImpl();
342 String err = "Cannot connect, terminal 2 is reserved: " + component + " " + NameUtils.getSafeName(g, component) + " terminals: " + g.getPossibleURI(elementConRel1) + " "+ elementConRel1 + " " + g.getPossibleURI(elementConRel2) + " "+ elementConRel2;
343 Logger.defaultLogError(err);
344 return new ConnectionErrorImpl(err);
347 // // connection object in module level
348 // Resource moduleConnection = g.newResource();
349 // g.claim(moduleConnection, b.InstanceOf, s.Connection);
351 // // connect the modules
353 // g.claim(component, componentConRel1, moduleConnection);
354 // g.claim(symbolConf2.component, componentConRel2, moduleConnection);
356 // connection object in diagram level
357 Resource diagramConnection = g.newResource();
358 g.claim(diagramConnection, b.InstanceOf, d.RouteGraphConnection);
359 DiagramUtils.addElementFirst(g, diagram, diagramConnection);
361 g.claim(diagramConnection, s.HasConnectionType, connectorType);
363 // Relation from element1 to connector1
364 // Resource elementConRel1 = getDiagramConnectionRelation(g, element, componentConRel1);
365 // Resource connectorRel1 = g.getPossibleObject(componentConRel1, s.HasAttachmentRelation);
366 // if (connectorRel1 == null)
367 // connectorRel1 = d.HasPlainConnector;
368 Resource connectorRel1 = d.HasPlainConnector;
371 Resource connector1 = g.newResource();
372 g.claim(connector1, b.InstanceOf, d.Connector);
373 g.claim(element, elementConRel1, connector1);
374 g.claim(diagramConnection, connectorRel1, connector1);
376 // Relation from element2 to connector2
377 // Resource elementConRel2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
378 // Resource connectorRel2 = g.getPossibleObject(componentConRel2, s.HasAttachmentRelation);
379 // if (connectorRel2 == null)
380 // connectorRel2 = d.HasArrowConnector;
382 Resource connectorRel2 = d.HasArrowConnector;
384 Resource connector2 = g.newResource();
385 g.claim(connector2, b.InstanceOf, d.Connector);
386 g.claim(symbolConf2.element, elementConRel2, connector2);
387 g.claim(diagramConnection, connectorRel2, connector2);
389 // connect connectors
390 g.claim(connector1, d.AreConnected, connector2);
392 // g.claim(moduleConnection, m.ConnectionToDiagramConnection, diagramConnection);
394 return new ConnectorDataImpl(diagramConnection);
398 private SymbolConnectionData connectWithFlag(WriteGraph g, Symbol symbolConf2, Resource componentConRel1, Resource componentConRel2, Resource connectionType) throws DatabaseException {
400 Diagram diagram2 = symbolConf2.diagram;
401 Layer0 l0 = Layer0.getInstance(g);
402 StructuralResource2 s = StructuralResource2.getInstance(g);
403 DiagramResource d = DiagramResource.getInstance(g);
404 ModelingResources m = ModelingResources.getInstance(g);
406 if (g.isInstanceOf(componentConRel1, l0.FunctionalRelation) && getSingleConnected(g, componentConRel1) != null) {
407 String err = "Cannot flag connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " since the first terminal is already reserved";
408 Logger.defaultLogError(err);
409 return new ConnectionErrorImpl(err);
411 if (g.isInstanceOf(componentConRel2, l0.FunctionalRelation) && symbolConf2.getSingleConnected(g, componentConRel2) != null) {
412 String err = "Cannot flag connect " + component + " " + NameUtils.getSafeName(g, component) + " to " + symbolConf2.component + " " + NameUtils.getSafeName(g, symbolConf2.component) + " since the second terminal is already reserved";
413 Logger.defaultLogError(err);
414 return new ConnectionErrorImpl(err);
418 double[] t1 = getSymbolTranslation(g);
419 double[] t2 = symbolConf2.getSymbolTranslation(g);
421 Symbol flag1 = diagram.addElement(g, d.Flag, t1[0]+t, t1[1]);
422 Symbol flag2 = diagram2.addElement(g, d.Flag, t2[0]-t, t2[1]);
424 Resource connectionJoin = g.newResource();
425 g.claim(connectionJoin, l0.InstanceOf, s.ConnectionJoin);
426 g.claim(diagram.getComposite(), s.HasConnectionJoin, connectionJoin);
427 g.claim(diagram2.getComposite(), s.HasConnectionJoin, connectionJoin);
429 Resource connection1 = g.newResource();
430 g.claim(connection1, l0.InstanceOf, s.Connection);
432 Resource connection2 = g.newResource();
433 g.claim(connection2, l0.InstanceOf, s.Connection);
435 g.claim(component, componentConRel1, connection1);
436 g.claim(symbolConf2.component, componentConRel2, connection2);
437 g.claim(connection1, s.IsJoinedBy, connectionJoin);
438 g.claim(connection2, s.IsJoinedBy, connectionJoin);
440 g.claim(flag1.element, d.FlagIsJoinedBy, connectionJoin);
441 g.claim(flag2.element, d.FlagIsJoinedBy, connectionJoin);
443 Resource diagConnection1 = g.newResource();
444 g.claim(diagConnection1, l0.InstanceOf, d.RouteGraphConnection);
445 DiagramUtils.addElementFirst(g, diagram, diagConnection1);
446 g.claim(diagConnection1, s.HasConnectionType, connectionType);
448 Resource diagConnection2 = g.newResource();
449 g.claim(diagConnection2, l0.InstanceOf, d.RouteGraphConnection);
450 DiagramUtils.addElementFirst(g, diagram2, diagConnection2);
451 g.claim(diagConnection2, s.HasConnectionType, connectionType);
453 g.claim(connection1, m.ConnectionToDiagramConnection, diagConnection1);
454 g.claim(connection2, m.ConnectionToDiagramConnection, diagConnection2);
456 // Relation from element1 to connector1
457 Resource isConnected1 = getDiagramConnectionRelation(g, element, componentConRel1);
460 Resource connector1_1 = g.newResource();
461 g.claim(connector1_1, l0.InstanceOf, d.Connector);
462 g.claim(element, isConnected1, connector1_1);
463 g.claim(diagConnection1, d.HasPlainConnector, connector1_1);
465 Resource connector1_2 = g.newResource();
466 g.claim(connector1_2, l0.InstanceOf, d.Connector);
467 g.claim(flag1.element, d.Flag_ConnectionPoint, connector1_2);
468 g.claim(diagConnection1, d.HasArrowConnector, connector1_2);
470 g.claim(connector1_1, d.AreConnected, connector1_2);
472 // Relation from element2 to connector2
473 Resource isConnected2 = getDiagramConnectionRelation(g, symbolConf2.element, componentConRel2);
476 Resource connector2_1 = g.newResource();
477 g.claim(connector2_1, l0.InstanceOf, d.Connector);
478 g.claim(flag2.element, d.Flag_ConnectionPoint, connector2_1);
479 g.claim(diagConnection2, d.HasPlainConnector, connector2_1);
481 Resource connector2_2 = g.newResource();
482 g.claim(connector2_2, l0.InstanceOf, d.Connector);
483 g.claim(symbolConf2.element, isConnected2, connector2_2);
484 g.claim(diagConnection2, d.HasArrowConnector, connector2_2);
486 g.claim(connector2_1, d.AreConnected, connector2_2);
488 return new FlagConnectionDataImpl(flag1, flag2, diagConnection1, diagConnection2);
492 * Returns diagram connection relation for an element and its component connection relation
495 * @param componentConnRel
497 * @throws ServiceException
498 * @throws NoSingleResultException
499 * @throws ValidationException
501 public static Resource getDiagramConnectionRelation(ReadGraph g, Resource element, Resource componentConnRel) throws DatabaseException {
502 ModelingResources m = ModelingResources.getInstance(g);
503 Collection<Resource> diagramConnectionRels = g.getObjects(componentConnRel, m.ConnectionRelationToDiagramConnectionRelation);
504 if (diagramConnectionRels.size() == 1)
505 return diagramConnectionRels.iterator().next();
506 if (diagramConnectionRels.size() > 1) {
507 List<Resource> matching = new ArrayList<Resource>();
508 for (Resource r : diagramConnectionRels) {
509 if (g.hasStatement(element, r))
512 if (matching.size() == 1)
513 return matching.get(0);
515 Layer0 b = Layer0.getInstance(g);
516 Collection<Resource> elementTypes = g.getObjects(element, b.InstanceOf);
517 if (elementTypes.size() != 1)
518 throw new RuntimeException("Cannot handle multi-instances " + element + " " + NameUtils.getSafeName(g, element));
519 Resource elementType = elementTypes.iterator().next();
520 Collection<Resource> rels = StructuralUtils.getConnectionRelations(g, elementType);
521 if (rels.size() == 1)
522 return rels.iterator().next();
523 for (Resource rel : rels) {
524 if (diagramConnectionRels.contains(rel))
527 throw new RuntimeException("Cannot find diagram level relation for relation " + NameUtils.getSafeName(g, componentConnRel) + " " + componentConnRel + " , element " + NameUtils.getSafeName(g, element) + " " + element + " , elementType " + NameUtils.getSafeName(g, elementType) + " " + elementType );
530 public static Resource getComponentConnectionRelation(ReadGraph g, Resource diagraromConnRel) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
531 ModelingResources mr = ModelingResources.getInstance(g);
532 return g.getSingleObject(diagraromConnRel, mr.DiagramConnectionRelationToConnectionRelation);
535 // public static Resource getSymbolRelation(ReadGraph g, Symbol conf, Resource genericRel) throws DatabaseException {
536 // Layer0 b = Layer0.getInstance(g);
537 // if (g.isInstanceOf(conf.element, ab.CalculationLevelReferenceElement)) {
538 // ModelingResources m = ModelingResources.getInstance(g);
539 // //return g.getInverse(g.getSingleObject(conf.element, m.HasReferenceRelation));
540 // return g.getSingleObject(conf.element, m.HasReferenceRelation);
543 // Resource compType = g.getSingleObject(conf.getComponent(), b.InstanceOf);
545 // for (Resource rel : StructuralUtils.getConnectionRelations(g, compType))
546 // if (g.isSubrelationOf(rel, genericRel))
552 * Returns symbol where given symbol is connected using given relation. Returns null if the symbol is not connected using the relation.
554 * @param componentConRel
556 * @throws DatabaseException
558 public Symbol getSingleConnected(ReadGraph g, Resource componentConRel) throws DatabaseException {
559 Collection<Symbol> symbols = getConnected(g, componentConRel);
560 if (symbols.size() > 1)
561 throw new NoSingleResultException("Symbol " + component + " is connected more than once using relation " + componentConRel);
562 if (symbols.size() == 1)
563 return symbols.iterator().next();
567 public Symbol getSingleConnected(ReadGraph g, Resource componentConRel, Resource otherComponentConRel) throws DatabaseException {
568 Collection<Symbol> symbols = getConnected(g, componentConRel, otherComponentConRel);
569 if (symbols.size() > 1)
570 throw new NoSingleResultException("Symbol " + component + " is connected more than once using relation " + componentConRel);
571 if (symbols.size() == 1)
572 return symbols.iterator().next();
576 public Collection<Symbol> getConnected(ReadGraph g, Resource componentConRel, Resource otherComponentConRel) throws DatabaseException {
577 assert(componentConRel != null);
578 Layer0 l0 = Layer0.getInstance(g);
579 StructuralResource2 sr = StructuralResource2.getInstance(g);
580 ModelingResources mr = ModelingResources.getInstance(g);
581 Collection<Symbol> result = new ArrayList<Symbol>();
582 Collection<Resource> modConn1s = g.getObjects(this.component, componentConRel);
583 for (Resource modConn1 : modConn1s) {
585 Collection<Statement> connected = g.getStatements(modConn1, sr.Connects);
586 Collection<Resource> connectionJoins = g.getObjects(modConn1, sr.IsJoinedBy);
587 Collection<Resource> connectedComponents = new ArrayList<Resource>();
589 for (Statement stm : connected) {
590 if (!stm.getObject().equals(this.component)) {
591 if (g.getInverse(stm.getPredicate()).equals(otherComponentConRel)) {
592 connectedComponents.add(stm.getObject());
597 for (Resource connectionJoin : connectionJoins) {
598 Collection<Resource> joinedConns = g.getObjects(connectionJoin, sr.Joins);
599 for (Resource conn : joinedConns) {
600 if (!conn.equals(modConn1)) {
601 // TODO : check ConnectionJoins?
602 for (Statement stm : g.getStatements(conn, sr.Connects)) {
603 if (g.getInverse(stm.getPredicate()).equals(otherComponentConRel))
604 connectedComponents.add(stm.getObject());
609 for (Resource connectedComponent : connectedComponents) {
611 Resource pointElem = g.getSingleObject(connectedComponent, mr.ComponentToElement);
613 Diagram diag = diagram.fromExisting(g, g.getSingleObject(connectedComponent, l0.PartOf));
614 result.add(diag.getSymbol(g, pointElem,connectedComponent));
621 public Collection<Symbol> getConnected(ReadGraph g, Resource componentConRel) throws DatabaseException {
622 assert(componentConRel != null);
623 Layer0 l0 = Layer0.getInstance(g);
624 StructuralResource2 sr = StructuralResource2.getInstance(g);
625 ModelingResources mr = ModelingResources.getInstance(g);
627 Collection<Resource> modConn1s = g.getObjects(this.component, componentConRel);
628 Collection<Symbol> result = new ArrayList<Symbol>();
629 for (Resource modConn1 : modConn1s) {
631 Collection<Resource> connected = g.getObjects(modConn1, sr.Connects);
632 Collection<Resource> connectionJoins = g.getObjects(modConn1, sr.IsJoinedBy);
633 Collection<Resource> connectedComponents = new ArrayList<Resource>();
635 for (Resource r : connected) {
636 if (!r.equals(this.component)) {
637 connectedComponents.add(r);
640 for (Resource connectionJoin : connectionJoins) {
641 Collection<Resource> joinedConns = g.getObjects(connectionJoin, sr.Joins);
642 for (Resource conn : joinedConns) {
643 if (!conn.equals(modConn1)) {
644 // TODO : check ConnectionJoins?
645 for (Resource obj : g.getObjects(conn, sr.Connects))
646 connectedComponents.add(obj);
650 for (Resource connectedComponent : connectedComponents) {
652 Resource pointElem = g.getSingleObject(connectedComponent, mr.ComponentToElement);
654 Diagram diag = diagram.fromExisting(g, g.getSingleObject(connectedComponent, l0.PartOf));
655 result.add(diag.getSymbol(g, pointElem,connectedComponent));
663 * Returns connected symbols on diagram level. Searches through branchpoints.
665 * @param diagramConnRel
666 * @param otherConnRel
668 * @throws DatabaseException
670 public Collection<Symbol> getDiagramConnected(ReadGraph g, Resource diagramConnRel, Resource otherConnRel) throws DatabaseException {
671 DiagramResource dr = DiagramResource.getInstance(g);
672 Collection<Resource> connectors = g.getObjects(element, diagramConnRel);
673 Collection<Symbol> result = new ArrayList<Symbol>();
674 for (Resource connector : connectors) {
675 Resource otherConnector = g.getSingleObject(connector, dr.AreConnected);
676 if (g.isInstanceOf(otherConnector, dr.Connector)) {
677 getConnectorConnectors(g, otherConnector, otherConnRel, result);
678 } else if (g.isInstanceOf(otherConnector, dr.RouteLine)) {
679 getBranchPointConnectors(g, connector, otherConnector, otherConnRel, result);
681 throw new DatabaseException("Connector " + g.getPossibleURI(otherConnector) + " " + otherConnector + " han unknown type");
687 private void getConnectorConnectors(ReadGraph g, Resource connector, Resource otherConnRel, Collection<Symbol> result) throws DatabaseException{
688 StructuralResource2 sr = StructuralResource2.getInstance(g);
689 DiagramResource dr = DiagramResource.getInstance(g);
690 Collection<Statement> stms = g.getStatements(connector, sr.Connects);
691 Statement stm = null;
692 for (Statement s : stms) {
693 if (!g.isInstanceOf(s.getObject(), dr.Connection)) {
699 if (otherConnRel != null) {
700 Resource rel = g.getInverse(stm.getPredicate());
701 if (!rel.equals(otherConnRel))
704 Resource element = stm.getObject();
705 result.add(diagram.getSymbol(g, element));
708 private void getBranchPointConnectors(ReadGraph g, Resource origin, Resource branchPoint, Resource otherConnRel, Collection<Symbol> result) throws DatabaseException {
709 DiagramResource dr = DiagramResource.getInstance(g);
711 Collection<Resource> branchConnected = g.getObjects(branchPoint, dr.AreConnected);
712 for (Resource branchConnector : branchConnected) {
713 if (branchConnector.equals(origin))
715 if (g.isInstanceOf(branchConnector, dr.Connector)) {
716 getConnectorConnectors(g, branchConnector, otherConnRel, result);
717 } else if (g.isInstanceOf(branchConnector, dr.RouteLine)) {
718 getBranchPointConnectors(g, branchPoint, branchConnector, otherConnRel, result);
720 throw new DatabaseException("Connector " + g.getPossibleURI(branchConnector) + " " + branchConnector + " han unknown type");
725 public Collection<Symbol> getDiagramConnected(ReadGraph g, Resource diagramConnRel) throws DatabaseException {
726 return getDiagramConnected(g, diagramConnRel,null);
729 public Collection<Symbol> getDiagramInverseConnected(ReadGraph g, Resource otherConnRel) throws DatabaseException {
730 StructuralResource2 sr = StructuralResource2.getInstance(g);
731 return getDiagramConnected(g, sr.IsConnectedTo, otherConnRel);
735 public Collection<Symbol> getDiagramConnected(ReadGraph g) throws DatabaseException {
736 StructuralResource2 sr = StructuralResource2.getInstance(g);
737 return getDiagramConnected(g, sr.IsConnectedTo, null);
740 public Symbol getDiagramSingleConnected(ReadGraph g, Resource diagramConnRel) throws DatabaseException {
741 Collection<Symbol> symbols = getDiagramConnected(g, diagramConnRel);
742 if (symbols.size() > 1) {
743 throw new NoSingleResultException("Symbol " + element + " has more than one connection with " + diagramConnRel);
744 } else if (symbols.size() == 1) {
745 return symbols.iterator().next();
750 public Symbol getOtherFlag(ReadGraph g) throws DatabaseException {
751 DiagramResource dr = DiagramResource.getInstance(g);
752 Resource connectionJoin = g.getPossibleObject(element, dr.FlagIsJoinedBy);
753 if (connectionJoin == null)
755 Collection<Resource> flags = g.getObjects(connectionJoin, dr.JoinsFlag);
756 Resource otherFlag = null;
757 for (Resource f : flags) {
758 if (!f.equals(element)) {
764 if (otherFlag == null) {
767 Resource otherDiagramR = OrderedSetUtils.getSingleOwnerList(g, otherFlag);
768 Diagram otherDiagram = diagram.fromExisting(g, otherDiagramR);
769 return otherDiagram.getSymbol(g, otherFlag, null);
774 * Returns concatenated translation of the symbol.
777 * @throws DatabaseException
779 public double[] getSymbolTranslation(ReadGraph g) throws DatabaseException {
780 DiagramResource d = DiagramResource.getInstance(g);
781 ModelingResources mr = ModelingResources.getInstance(g);
783 Resource e = element;
784 AffineTransform at = new AffineTransform();
786 double transform[] = g.getPossibleRelatedValue(e, d.HasTransform);
787 if (transform == null)
789 AffineTransform at2 = new AffineTransform(transform);
790 at.preConcatenate(at2);
791 Resource component = g.getPossibleObject(e, mr.HasParentComponent);
792 if (component != null) {
793 e = g.getPossibleObject(component, mr.ComponentToElement);
799 return new double[]{at.getTranslateX(),at.getTranslateY()};
803 * Returns transformation of the symbol.
806 * @throws DatabaseException
808 public double[] getSymbolTransformation(ReadGraph g) throws DatabaseException{
809 DiagramResource d = DiagramResource.getInstance(g);
810 double transform[] = g.getPossibleRelatedValue(element, d.HasTransform);
815 * Sets translation of symbol. The scale is set to 1.0.
819 * @throws DatabaseException
821 public void setSymbolTranslation(WriteGraph g, double x, double y) throws DatabaseException {
822 DiagramResource d = DiagramResource.getInstance(g);
823 G2DResource g2d = G2DResource.getInstance(g);
824 g.claimLiteral(element, d.HasTransform, g2d.Transform, new double[]{1.0,0.0,0.0,1.0,x,y});
827 public void setSymbolTransformation(WriteGraph g, AffineTransform at) throws DatabaseException {
828 DiagramResource d = DiagramResource.getInstance(g);
829 G2DResource g2d = G2DResource.getInstance(g);
830 double arr[] = new double[6];
832 g.claimLiteral(element, d.HasTransform, g2d.Transform, arr);
836 * Set the scale of symbol.
839 * @throws DatabaseException
841 public void setScale(WriteGraph g, double scale) throws DatabaseException {
842 DiagramResource d = DiagramResource.getInstance(g);
843 double transform[] = g.getPossibleRelatedValue(element, d.HasTransform);
844 g.claimLiteral(element, d.HasTransform, new double[]{scale,0.0,0.0,scale,transform[4],transform[5]});
848 public boolean equals(Object o) {
851 if (this.getClass() != o.getClass())
853 Symbol other = (Symbol)o;
854 return element.equals(other.element);
858 public int hashCode() {
859 return element.hashCode();
865 * Returns component type from symbol type
869 * @throws NoSingleResultException
870 * @throws ManyObjectsForFunctionalRelationException
871 * @throws ServiceException
873 public static Resource getComponentTypeFromSymbolType(ReadGraph g, Resource symbolType) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
874 ModelingResources m = ModelingResources.getInstance(g);
875 Resource component = g.getPossibleObject(symbolType, m.SymbolToComponentType);
879 public static Resource getSymbolTypeFromComponentType(ReadGraph g, Resource componentType) throws NoSingleResultException, ServiceException, ManyObjectsForFunctionalRelationException {
880 ModelingResources m = ModelingResources.getInstance(g);
881 Collection<Resource> symbols = g.getObjects(componentType, m.ComponentTypeToSymbol);
882 if (symbols.size() == 0)
884 return symbols.iterator().next();
887 public void remove(WriteGraph g) throws DatabaseException{
888 Layer0 L0 = Layer0.getInstance(g);
889 StructuralResource2 STR = StructuralResource2.getInstance(g);
890 DiagramResource DIA = DiagramResource.getInstance(g);
891 OrderedSetUtils.remove(g, diagram.getDiagram(), element);
892 if (component != null)
893 g.deny(component, L0.PartOf);
894 Collection<Statement> statements = g.getStatements(element, STR.IsConnectedTo);
895 for (Statement s : statements) {
896 if (g.isInstanceOf(s.getObject(),DIA.Connector)) {
897 Resource connectedConnector = g.getPossibleObject(s.getObject(), DIA.AreConnected);
898 if (connectedConnector != null)
899 g.deny(connectedConnector);
900 g.deny(s.getObject());
903 if (component != null) {
904 statements = g.getStatements(component, STR.IsConnectedTo);
905 for (Statement s : statements) {
906 if (g.isInstanceOf(s.getObject(),STR.Connection)) {
907 g.deny(s.getObject());
912 if (component != null)
916 public void dispose() {
923 * Common interface for Symbol.connect return value.
926 public interface SymbolConnectionData {
931 * Interface for returning diagram level connectors.
934 public interface DiagramConnectionData extends SymbolConnectionData {
935 public int getConnectionCount();
936 public Resource getConnection(int index);
940 * Interface for flag connections.
943 public interface FlagConnectionData extends SymbolConnectionData {
944 public Symbol getFirstFlag();
945 public Symbol getSecondFlag();
950 * Interface for signal connections.
953 public interface SignalConnectionData extends SymbolConnectionData {
954 public Resource getSignalComponent();
958 * Interface for Point connections.
961 public interface PointConnectionData extends SymbolConnectionData {
966 * Interface for "connection" that merged the connected symbols into one symbol.
969 public interface MergeSymbolData extends SymbolConnectionData {
970 Symbol getResultSymbol();
974 * Return value for a connection that already exists.
977 public interface SymbolExists extends SymbolConnectionData {
982 * Return value for an error.
985 public interface ConnectionError extends SymbolConnectionData {
989 private static class ConnectionErrorImpl implements ConnectionError {
990 private String error;
992 public ConnectionErrorImpl(String error) {
997 public String getReason() {
1002 private static class SymbolExistsImpl implements SymbolExists {
1006 private static class ConnectorDataImpl implements DiagramConnectionData {
1008 private Resource[] connectors;
1010 public ConnectorDataImpl(Resource... connectors) {
1011 this.connectors = connectors;
1015 public Resource getConnection(int index) {
1016 return connectors[index];
1020 public int getConnectionCount() {
1021 return connectors.length;
1025 private static class FlagConnectionDataImpl implements FlagConnectionData, DiagramConnectionData {
1027 private Symbol firstFlag;
1028 private Symbol secondFlag;
1029 private Resource[] connectors;
1031 public FlagConnectionDataImpl(Symbol firstFlag, Symbol secondFlag, Resource... connectors) {
1032 this.firstFlag = firstFlag;
1033 this.secondFlag = secondFlag;
1034 this.connectors = connectors;
1038 public Symbol getFirstFlag() {
1043 public Symbol getSecondFlag() {
1048 public Resource getConnection(int index) {
1049 return connectors[index];
1053 public int getConnectionCount() {
1054 return connectors.length;
1058 public void setComponentValue(WriteGraph graph, Resource property, double value) throws DatabaseException{
1059 setValue(graph, component, property, value);
1062 public void setComponentValue(WriteGraph graph, Resource property, float value) throws DatabaseException{
1063 setValue(graph, component, property, value);
1066 public void setComponentValue(WriteGraph graph, Resource property, boolean value) throws DatabaseException{
1067 setValue(graph, component, property, value);
1070 public void setComponentValue(WriteGraph graph, Resource property, int value) throws DatabaseException{
1071 setValue(graph, component, property, value);
1074 public void setComponentValue(WriteGraph graph, Resource property, String value) throws DatabaseException{
1075 setValue(graph, component, property, value);
1078 public void setComponentValue(WriteGraph graph, Resource property, double value[]) throws DatabaseException{
1079 setValue(graph, component, property, value);
1082 public void setComponentValue(WriteGraph graph, Resource property, float value[]) throws DatabaseException{
1083 setValue(graph, component, property, value);
1086 public void setComponentValue(WriteGraph graph, Resource property, int value[]) throws DatabaseException{
1087 setValue(graph, component, property, value);
1090 public void setComponentValue(WriteGraph graph, Resource property, boolean value[]) throws DatabaseException{
1091 setValue(graph, component, property, value);
1094 public void setElementValue(WriteGraph graph, Resource property, double value) throws DatabaseException{
1095 setValue(graph, element, property, value);
1098 public void setElementValue(WriteGraph graph, Resource property, float value) throws DatabaseException{
1099 setValue(graph, element, property, value);
1102 public void setElementValue(WriteGraph graph, Resource property, boolean value) throws DatabaseException{
1103 setValue(graph, element, property, value);
1106 public void setElementValue(WriteGraph graph, Resource property, int value) throws DatabaseException{
1107 setValue(graph, element, property, value);
1110 public void setElementValue(WriteGraph graph, Resource property, double value[]) throws DatabaseException{
1111 setValue(graph, element, property, value);
1114 public void setElementValue(WriteGraph graph, Resource property, float value[]) throws DatabaseException{
1115 setValue(graph, element, property, value);
1118 public void setElementValue(WriteGraph graph, Resource property, int value[]) throws DatabaseException{
1119 setValue(graph, element, property, value);
1122 public void setElementValue(WriteGraph graph, Resource property, boolean value[]) throws DatabaseException{
1123 setValue(graph, element, property, value);
1127 * Sets literal value. Does data conversion if required.
1129 * Note: This method support only some basic L0 data types.
1135 * @throws DatabaseException
1137 public static void setValue(WriteGraph graph, Resource object, Resource property, double value) throws DatabaseException{
1138 Layer0 l0 = Layer0.getInstance(graph);
1139 Resource range = graph.getPossibleObject(property, l0.HasRange);
1140 if (l0.Double.equals(range)) {
1141 graph.claimLiteral(object, property, value, Bindings.DOUBLE);
1142 } else if (l0.Float.equals(range)) {
1143 graph.claimLiteral(object, property, (float)value, Bindings.FLOAT);
1144 } else if (l0.Integer.equals(range)) {
1145 graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1146 } else if (l0.Long.equals(range)) {
1147 graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1148 } else if (l0.Boolean.equals(range)) {
1149 graph.claimLiteral(object, property, value > 0.0, Bindings.BOOLEAN);
1150 } else if (l0.String.equals(range)) {
1151 graph.claimLiteral(object, property, Double.toString(value), Bindings.STRING);
1153 graph.claimLiteral(object, property, value, Bindings.DOUBLE);
1158 * Sets literal value. Does data conversion if required.
1160 * Note: This method support only some basic L0 data types.
1166 * @throws DatabaseException
1168 public static void setValue(WriteGraph graph, Resource object, Resource property, float value) throws DatabaseException{
1169 Layer0 l0 = Layer0.getInstance(graph);
1170 Resource range = graph.getPossibleObject(property, l0.HasRange);
1171 if (l0.Double.equals(range)) {
1172 graph.claimLiteral(object, property, (double)value, Bindings.DOUBLE);
1173 } else if (l0.Float.equals(range)) {
1174 graph.claimLiteral(object, property, value, Bindings.FLOAT);
1175 } else if (l0.Integer.equals(range)) {
1176 graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1177 } else if (l0.Long.equals(range)) {
1178 graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1179 } else if (l0.Boolean.equals(range)) {
1180 graph.claimLiteral(object, property, value > 0.f, Bindings.BOOLEAN);
1181 } else if (l0.String.equals(range)) {
1182 graph.claimLiteral(object, property, Float.toString(value), Bindings.STRING);
1184 graph.claimLiteral(object, property, value, Bindings.FLOAT);
1189 * Sets literal value. Does data conversion if required.
1191 * Note: This method support only some basic L0 data types.
1197 * @throws DatabaseException
1199 public static void setValue(WriteGraph graph, Resource object, Resource property, int value) throws DatabaseException{
1200 Layer0 l0 = Layer0.getInstance(graph);
1201 Resource range = graph.getPossibleObject(property, l0.HasRange);
1202 if (l0.Double.equals(range)) {
1203 graph.claimLiteral(object, property, (double)value, Bindings.DOUBLE);
1204 } else if (l0.Float.equals(range)) {
1205 graph.claimLiteral(object, property, (float) value, Bindings.FLOAT);
1206 } else if (l0.Integer.equals(range)) {
1207 graph.claimLiteral(object, property, (int)value, Bindings.INTEGER);
1208 } else if (l0.Long.equals(range)) {
1209 graph.claimLiteral(object, property, (long)value, Bindings.LONG);
1210 } else if (l0.Boolean.equals(range)) {
1211 graph.claimLiteral(object, property, value > 0, Bindings.BOOLEAN);
1212 } else if (l0.String.equals(range)) {
1213 graph.claimLiteral(object, property, Integer.toString(value), Bindings.STRING);
1215 graph.claimLiteral(object, property, value, Bindings.INTEGER);
1220 * Sets literal value. Does data conversion if required.
1226 * @throws DatabaseException
1228 public static void setValue(WriteGraph graph, Resource object, Resource property, String value) throws DatabaseException{
1229 Layer0 l0 = Layer0.getInstance(graph);
1230 Resource range = graph.getPossibleObject(property, l0.HasRange);
1231 if (range == null) {
1232 graph.claimLiteral(object, property, value, Bindings.STRING);
1234 if (graph.isInheritedFrom(range, l0.String)) {
1235 graph.claimLiteral(object, property, value, Bindings.STRING);
1236 } else if (graph.isInheritedFrom(range, l0.Double)) {
1237 graph.claimLiteral(object, property, Double.parseDouble(value), Bindings.DOUBLE);
1238 } else if (graph.isInheritedFrom(range, l0.Float)) {
1239 graph.claimLiteral(object, property, Float.parseFloat(value), Bindings.FLOAT);
1240 } else if (graph.isInheritedFrom(range, l0.Integer)) {
1241 graph.claimLiteral(object, property, Integer.parseInt(value), Bindings.INTEGER);
1242 } else if (graph.isInheritedFrom(range, l0.Long)) {
1243 graph.claimLiteral(object, property, Long.parseLong(value), Bindings.LONG);
1244 } else if (graph.isInheritedFrom(range, l0.Boolean)) {
1245 graph.claimLiteral(object, property, Boolean.parseBoolean(value), Bindings.BOOLEAN);
1247 graph.claimLiteral(object, property, value, Bindings.STRING);
1253 * Sets literal value. Does data conversion if required.
1255 * Note: This method support only some basic L0 data types.
1261 * @throws DatabaseException
1263 public static void setValue(WriteGraph graph, Resource object, Resource property, boolean value) throws DatabaseException{
1264 Layer0 l0 = Layer0.getInstance(graph);
1265 Resource range = graph.getPossibleObject(property, l0.HasRange);
1266 if (l0.Boolean.equals(range)) {
1267 graph.claimLiteral(object, property, value , Bindings.BOOLEAN);
1268 } else if (l0.String.equals(range)) {
1269 graph.claimLiteral(object, property, Boolean.toString(value), Bindings.STRING);
1270 } else if (l0.Integer.equals(range)) {
1271 graph.claimLiteral(object, property, value ? 1 : 0, Bindings.INTEGER);
1273 graph.claimLiteral(object, property, value, Bindings.BOOLEAN);
1278 * Sets literal value. Does data conversion if required.
1280 * Note: This method support only some basic L0 data types.
1286 * @throws DatabaseException
1288 public static void setValue(WriteGraph graph, Resource object, Resource property, double[] value) throws DatabaseException{
1289 Layer0 l0 = Layer0.getInstance(graph);
1290 Resource range = graph.getPossibleObject(property, l0.HasRange);
1291 if (l0.DoubleArray.equals(range)) {
1292 graph.claimLiteral(object, property, value, Bindings.DOUBLE_ARRAY);
1293 } else if (l0.FloatArray.equals(range)) {
1294 float arr[] = new float[value.length];
1295 for (int i = 0; i < value.length; i++)
1296 arr[i] = (float) value[i];
1297 graph.claimLiteral(object, property, arr, Bindings.FLOAT_ARRAY);
1298 } else if (l0.IntegerArray.equals(range)) {
1299 int arr[] = new int[value.length];
1300 for (int i = 0; i < value.length; i++)
1301 arr[i] = (int) value[i];
1302 graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1304 graph.claimLiteral(object, property, value, Bindings.DOUBLE_ARRAY);
1309 * Sets literal value. Does data conversion if required.
1311 * Note: This method support only some basic L0 data types.
1317 * @throws DatabaseException
1319 public static void setValue(WriteGraph graph, Resource object, Resource property, float[] value) throws DatabaseException{
1320 Layer0 l0 = Layer0.getInstance(graph);
1321 Resource range = graph.getPossibleObject(property, l0.HasRange);
1322 if (l0.FloatArray.equals(range)) {
1323 graph.claimLiteral(object, property, value, Bindings.FLOAT_ARRAY);
1324 } else if (l0.DoubleArray.equals(range)) {
1325 double arr[] = new double[value.length];
1326 for (int i = 0; i < value.length; i++)
1327 arr[i] = (double) value[i];
1328 graph.claimLiteral(object, property, arr, Bindings.DOUBLE_ARRAY);
1329 } else if (l0.IntegerArray.equals(range)) {
1330 int arr[] = new int[value.length];
1331 for (int i = 0; i < value.length; i++)
1332 arr[i] = (int) value[i];
1333 graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1335 graph.claimLiteral(object, property, value, Bindings.FLOAT_ARRAY);
1340 * Sets literal value. Does data conversion if required.
1342 * Note: This method support only some basic L0 data types.
1348 * @throws DatabaseException
1350 public static void setValue(WriteGraph graph, Resource object, Resource property, int[] value) throws DatabaseException{
1351 Layer0 l0 = Layer0.getInstance(graph);
1352 Resource range = graph.getPossibleObject(property, l0.HasRange);
1353 if (l0.IntegerArray.equals(range)) {
1354 graph.claimLiteral(object, property, value, Bindings.INT_ARRAY);
1355 } else if (l0.DoubleArray.equals(range)) {
1356 double arr[] = new double[value.length];
1357 for (int i = 0; i < value.length; i++)
1358 arr[i] = (double) value[i];
1359 graph.claimLiteral(object, property, arr, Bindings.DOUBLE_ARRAY);
1360 } else if (l0.FloatArray.equals(range)) {
1361 float arr[] = new float[value.length];
1362 for (int i = 0; i < value.length; i++)
1363 arr[i] = (float) value[i];
1364 graph.claimLiteral(object, property, arr, Bindings.FLOAT_ARRAY);
1366 graph.claimLiteral(object, property, value, Bindings.INT_ARRAY);
1371 * Sets literal value. Does data conversion if required.
1373 * Note: This method support only some basic L0 data types.
1379 * @throws DatabaseException
1381 public static void setValue(WriteGraph graph, Resource object, Resource property, boolean[] value) throws DatabaseException{
1382 Layer0 l0 = Layer0.getInstance(graph);
1383 Resource range = graph.getPossibleObject(property, l0.HasRange);
1384 if (l0.BooleanArray.equals(range)) {
1385 graph.claimLiteral(object, property, value, Bindings.BOOLEAN_ARRAY);
1386 } else if (l0.IntegerArray.equals(range)) {
1387 int arr[] = new int[value.length];
1388 for (int i = 0; i < value.length; i++)
1389 arr[i] = value[i] ? 1 : 0;
1390 graph.claimLiteral(object, property, arr, Bindings.INT_ARRAY);
1392 graph.claimLiteral(object, property, value, Bindings.BOOLEAN_ARRAY);