]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
967bd04bbc6b9d146a23d491b398bc95d7c01c4c
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.modelImport.model.element;\r
2 \r
3 import org.simantics.db.ReadGraph;\r
4 import org.simantics.db.Resource;\r
5 import org.simantics.db.WriteGraph;\r
6 import org.simantics.db.common.utils.OrderedSetUtils;\r
7 import org.simantics.db.exception.DatabaseException;\r
8 import org.simantics.diagram.stubs.DiagramResource;\r
9 import org.simantics.layer0.Layer0;\r
10 import org.simantics.layer0.utils.direct.GraphUtils;\r
11 import org.simantics.modeling.ModelingResources;\r
12 import org.simantics.structural.stubs.StructuralResource2;\r
13 import org.simantics.sysdyn.SysdynResource;\r
14 import org.simantics.sysdyn.modelImport.model.IWriteableObject;\r
15 import org.simantics.sysdyn.modelImport.model.WriteContext;\r
16 \r
17 public abstract class Connection implements IWriteableObject {\r
18 \r
19         protected Symbol head;\r
20         protected Symbol tail;\r
21         \r
22         private Resource connection;\r
23         private Resource diagramConnection;\r
24 \r
25         public Connection(Symbol tail, Symbol head) {\r
26                 this.tail = tail;\r
27                 this.head = head;\r
28         }\r
29         \r
30         @Override\r
31         public Resource write(WriteGraph graph, Resource parent, WriteContext context) throws DatabaseException {\r
32                 DiagramResource dr = DiagramResource.getInstance(graph);\r
33                 Layer0 l0 = Layer0.getInstance(graph);\r
34                 ModelingResources mr = ModelingResources.getInstance(graph);\r
35                 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
36                 SysdynResource sr = SysdynResource.getInstance(graph);\r
37                 \r
38                 connection = GraphUtils.create2(graph, getConnectionType(graph),\r
39                                 sr.Variable_HasHead, head.getResource(),\r
40                                 sr.Variable_HasTail, tail.getResource(),\r
41                                 l0.PartOf, parent);\r
42                 graph.claim(connection, mr.Mapped, connection);\r
43                 \r
44                 Resource diagram = graph.getSingleObject(parent, mr.CompositeToDiagram);\r
45                 \r
46                 Resource tailConnector = GraphUtils.create2(graph, dr.Connector,\r
47                                 sr.HasTailTerminal, tail.getDiagramResource());\r
48 \r
49                 Resource headConnector = GraphUtils.create2(graph, dr.Connector,\r
50                                 sr.HasHeadTerminal, head.getDiagramResource(),\r
51                                 dr.AreConnected, tailConnector);\r
52 \r
53                 diagramConnection = GraphUtils.create2(graph, getDiagramConnectionType(graph),\r
54                                 l0.HasName, context.getNextObject(),\r
55                                 l0.PartOf, diagram,\r
56                                 sr2.HasConnectionType, sr.SysdynConnectionType,\r
57                                 mr.DiagramConnectionToConnection, connection,\r
58                                 dr.HasArrowConnector, headConnector,\r
59                                 dr.HasPlainConnector, tailConnector);\r
60 \r
61                 OrderedSetUtils.add(graph, diagram, diagramConnection);\r
62                 \r
63                 return connection;\r
64         }\r
65         \r
66         public abstract Resource getConnectionType(ReadGraph graph);\r
67         public abstract Resource getDiagramConnectionType(ReadGraph graph);\r
68         \r
69         @Override\r
70         public Resource getResource() {\r
71                 return connection;\r
72         }\r
73         \r
74         public Resource getDiagramResource() {\r
75                 return diagramConnection;\r
76         }\r
77 \r
78 }\r