]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java
Enable model loading using an existing transaction.
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / P3DUtil.java
1 package org.simantics.plant3d.utils;
2
3 import java.util.ArrayDeque;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.Deque;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Set;
11
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.RequestProcessor;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.request.Read;
19 import org.simantics.g3d.scenegraph.base.INode;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.plant3d.ontology.Plant3D;
22 import org.simantics.plant3d.scenegraph.Equipment;
23 import org.simantics.plant3d.scenegraph.P3DRootNode;
24 import org.simantics.plant3d.scenegraph.PipeRun;
25 import org.simantics.plant3d.scenegraph.PipelineComponent;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
28 import org.simantics.plant3d.utils.Item.Type;
29
30 public class P3DUtil {
31         
32         public static List<Item> getEquipments(RequestProcessor session) throws DatabaseException {
33                 return getEquipments(session, Plant3D.URIs.Builtin);
34         }
35         
36         public static List<Item> getEquipments(RequestProcessor session, final String libUri) throws DatabaseException {
37                 return session.syncRequest(new Read<List<Item>>() {
38                         @Override
39                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
40                                 Plant3D p3d = Plant3D.getInstance(graph);
41                                 Resource project = Simantics.getProject().get();
42                                 Resource builtins = graph.getResource(libUri);
43                                 List<Item> actions = getItems(graph, project,p3d.Equipment);
44                                 actions.addAll(getItems(graph, builtins,p3d.Equipment));
45                                 return actions;
46                         }
47                         
48                         
49                 });
50         }
51         
52         public static List<Item> getNozzles(RequestProcessor session, String libUri) throws DatabaseException {
53                 return session.syncRequest(new Read<List<Item>>() {
54                         @Override
55                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
56                                 Plant3D p3d = Plant3D.getInstance(graph);
57                                 ItemQuery query = new ItemQuery(p3d.Nozzle, libUri);
58                                 return graph.syncRequest(query);
59                         }
60                 });
61         }
62         
63         public static class ItemQuery implements Read<List<Item>> {
64                 private Resource type;
65                 private String libUri;
66                 public ItemQuery(Resource type, String libUri) {
67                         this.type = type;
68                         this.libUri = libUri;
69                 }
70                 
71                 @Override
72                 public List<Item> perform(ReadGraph graph) throws DatabaseException {
73 //                      Resource project = Simantics.getProject().get();
74                         Resource builtins = graph.getResource(libUri);
75                         List<Item> actions = new ArrayList<>();
76 //                      actions.addAll(getItems(graph, project,type));
77                         actions.addAll(getItems(graph, builtins,type));
78                         return actions;
79                 }
80                 
81                 @Override
82                 public boolean equals(Object obj) {
83                         if (obj.getClass() != this.getClass())
84                                 return false;
85                         ItemQuery other = (ItemQuery)obj;
86                         if (!type.equals(other.type))
87                                 return false;
88                         return libUri.equals(other.libUri);
89                 }
90                 
91                 @Override
92                 public int hashCode() {
93                         return type.hashCode() + libUri.hashCode();
94                 }
95         }
96
97         public static List<Item> getEnds(RequestProcessor session, String libUri) throws DatabaseException {
98                 return session.syncRequest(new Read<List<Item>>() {
99                         @Override
100                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
101                                 Plant3D p3d = Plant3D.getInstance(graph);
102                                 ItemQuery query = new ItemQuery(p3d.EndComponent, libUri);
103                                 return graph.syncRequest(query);
104                         }
105                 });
106         }
107         
108         public static List<Item> getTurns(RequestProcessor session, String libUri) throws DatabaseException {
109                 return session.syncRequest(new Read<List<Item>>() {
110                         @Override
111                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
112                                 Plant3D p3d = Plant3D.getInstance(graph);
113                                 ItemQuery query = new ItemQuery(p3d.TurnComponent, libUri);
114                                 return graph.syncRequest(query);
115                         }
116                 });
117         }
118         
119         public static List<Item> getInlines(RequestProcessor session, String libUri) throws DatabaseException {
120                 return session.syncRequest(new Read<List<Item>>() {
121                         @Override
122                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
123                                 Plant3D p3d = Plant3D.getInstance(graph);
124                                 ItemQuery query = new ItemQuery(p3d.InlineComponent, libUri);
125                                 return graph.syncRequest(query);
126                         }
127                 });
128         }
129         
130         public static List<Item> filterUserComponents(List<Item> items) {
131                 List<Item> result = new ArrayList<Item>(items.size());
132                 for (Item i : items) {
133                         if (!i.isCode())
134                                 result.add(i);
135                 }
136                 return result;
137         }
138         
139         private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
140                 Plant3D p3d = Plant3D.getInstance(graph);
141                 Layer0 l0 = Layer0.getInstance(graph);
142                 List<Item> result = new ArrayList<Item>();
143                 Set<Resource> processed = new HashSet<>();
144                 Deque<Resource> stack = new ArrayDeque<Resource>();
145                 stack.addAll(graph.getObjects(lib, l0.ConsistsOf));
146                 stack.addAll(graph.getObjects(lib, p3d.ComponentLibrary_contains));
147                 while (!stack.isEmpty()) {
148                     Resource r = stack.pop();
149                     if (processed.contains(r))
150                         continue;
151                     processed.add(r);
152                         if (graph.isInstanceOf(r, type) ) {
153                                 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
154                                 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
155                                         
156                                         result.add(createItem(graph, r));
157                                         continue;
158                                 }
159                         } 
160                         if (graph.isInheritedFrom(r, type)) {
161                                 boolean asserts = false;
162                                 for (Resource a : graph.getObjects(r, l0.Asserts)) {
163                                         if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
164                                                 asserts = true;
165                                                 break;
166                                         }
167                                 }
168                                 if (asserts) {          
169                                         result.add(createItem(graph, r));
170                                         continue;
171                                 }
172                         }
173                         if (graph.isInstanceOf(r, p3d.ComponentLibrary)) {
174                             stack.addAll(graph.getObjects(r, l0.ConsistsOf));
175                         stack.addAll(graph.getObjects(r, p3d.ComponentLibrary_contains));
176                         }
177                 }
178                 Collections.sort(result, new Comparator<Item>() {
179                         @Override
180                         public int compare(Item o1, Item o2) {
181                                 return o1.getName().compareTo(o2.getName());
182                         }
183                 });
184                 return result;
185         }
186         
187         public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
188                 Layer0 l0 = Layer0.getInstance(graph);
189                 Plant3D p3d = Plant3D.getInstance(graph);
190                 String name = graph.getRelatedValue(r, l0.HasName);
191                 String uri = graph.getURI(r);
192                 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);
193                 Item item = new Item(uri, name, label);
194                 if (graph.isInstanceOf(r, p3d.Equipment))
195                         item.setType(Type.EQUIPMENT);
196                 else if (graph.isInstanceOf(r, p3d.InlineComponent))
197                         item.setType(Type.INLINE);
198                 else if (graph.isInstanceOf(r, p3d.EndComponent))
199                         item.setType(Type.END);
200                 else if (graph.isInstanceOf(r, p3d.TurnComponent))
201                         item.setType(Type.TURN);
202                 else if (graph.isInstanceOf(r, p3d.Nozzle))
203                         item.setType(Type.NOZZLE);
204                 else 
205                         throw new RuntimeException("Cannot detect type for " + r);
206                 
207                 if (graph.hasStatement(r, p3d.CodeComponent))
208                         item.setCode(true);
209                 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
210                         graph.hasStatement(r, p3d.VariableLengthInlineComponent))
211                         item.setVariable(true);
212                 if (graph.hasStatement(r, p3d.AdjustableLengthInlineComponent))
213                     item.setModifiable(true);
214                 if (graph.hasStatement(r, p3d.SizeChangeComponent))
215                         item.setSizeChange(true);
216                 if (graph.hasStatement(r, p3d.RotateComponent))
217             item.setRotated(true);
218                 return item;
219         }
220         
221         public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
222                 Layer0 l0 = Layer0.getInstance(graph);
223                 Plant3D p3d = Plant3D.getInstance(graph);
224                 Resource model = graph.newResource();
225                 graph.claim(model, l0.InstanceOf, p3d.Plant);
226                 graph.claimLiteral(model, l0.HasName, name);
227                 
228                 return model;
229         }
230         
231         public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
232                 for (INode node : rootNode.getChild()) {
233                         if (node instanceof PipeRun) {
234                                 for (PipelineComponent pc : ((PipeRun) node).getChild()) {
235                                         pc.sync();
236                                         pc.updateParameters();
237                                 }
238                         } else if (node instanceof Equipment) {
239                                 for (PipelineComponent pc : ((Equipment) node).getChild()) {
240                                         pc.sync();
241                                         pc.updateParameters();
242                                 }
243                         }
244                 }
245                 
246                 for (INode node : rootNode.getChild()) {
247                         if (node instanceof PipeRun) {
248                                 for (PipelineComponent pc : ((PipeRun) node).getChild())
249                                         pc.sync2();
250                         } else if (node instanceof Equipment) {
251                                 for (PipelineComponent pc : ((Equipment) node).getChild())
252                                         pc.sync2();
253                         }
254                 }
255                 for (INode node : rootNode.getChild()) {
256                         if (node instanceof PipeRun) {
257                                 PipingRules.validate((PipeRun)node);
258                         }
259                 }
260         }
261         
262     public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception {
263         PipingRules.setEnabled(true);
264         for (INode node : rootNode.getChild()) {
265             if (node instanceof PipeRun) {
266                 PipeRun run = (PipeRun) node;
267                 for (PipeControlPoint pcp : run.getControlPoints())
268                     if (pcp.asPathLegEnd())
269                         PipingRules.requestUpdate(pcp);
270             }
271         }
272         PipingRules.update();
273     }
274
275 }