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