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