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