]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
Creating equipment with typeURI
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / utils / ComponentUtils.java
1 package org.simantics.plant3d.utils;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.simantics.Simantics;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.request.ReadRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.g3d.scenegraph.GeometryProvider;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.scenegraph.EndComponent;
17 import org.simantics.plant3d.scenegraph.Equipment;
18 import org.simantics.plant3d.scenegraph.InlineComponent;
19 import org.simantics.plant3d.scenegraph.Nozzle;
20 import org.simantics.plant3d.scenegraph.P3DRootNode;
21 import org.simantics.plant3d.scenegraph.PipeRun;
22 import org.simantics.plant3d.scenegraph.PipelineComponent;
23 import org.simantics.plant3d.scenegraph.TurnComponent;
24
25 public class ComponentUtils {
26
27         
28         private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
29         private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
30         
31         public static void preloadCache() {
32                 Simantics.getSession().asyncRequest(new ReadRequest() {
33                         
34                         @Override
35                         public void run(ReadGraph graph) throws DatabaseException {
36                                 List<String> types = new ArrayList<String>();
37                                 types.add(Plant3D.URIs.Builtin_Straight);
38                                 types.add(Plant3D.URIs.Builtin_Elbow);
39                                 types.add(Plant3D.URIs.Builtin_ConcentricReducer);
40                                 types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
41                                 types.add(Plant3D.URIs.Builtin_EccentricReducer);
42                                 
43                                 for (String typeURI : types) {
44                                         load(graph, typeURI);
45                                 }
46                         }
47                 });
48         }
49         
50         private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
51                 
52                 Layer0 l0 = Layer0.getInstance(graph);
53                 Plant3D p3d = Plant3D.getInstance(graph);
54                 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
55                 if (geom == null) {
56                         for (Resource a : graph.getObjects(type, l0.Asserts)) {
57                                 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
58                                         geom = graph.getPossibleObject(a, l0.HasObject);
59                                         break;
60                                 }
61                         }
62                 }
63                 if (geom != null) {
64                         GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
65                         return provider;
66                 }
67                 return null;
68         }
69         
70         private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
71                 Plant3D p3d = Plant3D.getInstance(graph);
72                 if (graph.isInheritedFrom(type, p3d.InlineComponent))
73                         return InlineComponent.class;
74                 if (graph.isInheritedFrom(type, p3d.TurnComponent))
75                         return TurnComponent.class;
76                 if (graph.isInheritedFrom(type, p3d.EndComponent))
77                         return EndComponent.class;
78                 if (graph.isInheritedFrom(type, p3d.Nozzle))
79                         return Nozzle.class;
80                 return null;
81         }
82         
83         private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
84                 Plant3D p3d = Plant3D.getInstance(graph);
85                 Resource type = graph.getResource(typeURI);
86                 
87                 GeometryProvider provider = getProvider(graph, type);
88                 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
89                         providers.put(typeURI, provider);
90                         if (graph.isInheritedFrom(type, p3d.PipelineComponent))
91                             clazzes.put(typeURI,getClazz(graph, type));
92                         return;
93                 }
94                 throw new DatabaseException("Cannot find component for " + typeURI);
95         }
96         
97         private static void load(final String typeURI) throws DatabaseException {
98                 Simantics.getSession().syncRequest(new ReadRequest() {
99                         
100                         @Override
101                         public void run(ReadGraph graph) throws DatabaseException {
102                                 load(graph,typeURI);    
103                         }
104                 });
105         }
106         
107         /**
108          * Creates a component
109          * 
110          * Does not set the name or add the component to a piperun.
111          * @param root
112          * @param typeURI
113          * @return
114          * @throws Exception
115          */
116         public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
117                 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
118                 GeometryProvider provider = providers.get(typeURI);
119                 if (type == null || provider == null) {
120                         load(typeURI);
121                         type = clazzes.get(typeURI);
122                         provider = providers.get(typeURI);
123                 }
124                 //PipelineComponent component = type.newInstance();
125                 PipelineComponent component = null;
126                 if (type == InlineComponent.class) {
127                         component = root.createInline();
128                 } else if (type == TurnComponent.class) {
129                         component = root.createTurn();
130                 } else if (type == EndComponent.class) {
131                         component = root.createTurn();
132                 } else if (type == Nozzle.class) {
133                         component = root.createNozzle();
134                 }
135                 component.setType(typeURI);
136                 component.setGeometry(provider);
137                 return component;
138         }
139         
140         /**
141          * Creates a equipment
142          * 
143          * Does not set the name
144          * 
145          * @param root
146          * @param typeURI
147          * @return
148          * @throws Exception
149          */
150         
151         public static Equipment createEquipment(P3DRootNode root, String typeURI) throws Exception {
152             GeometryProvider provider = providers.get(typeURI);
153         if (provider == null) {
154             load(typeURI);
155             provider = providers.get(typeURI);
156         }
157         Equipment equipment = root.createEquipment();
158         equipment.setType(typeURI);
159         equipment.setGeometry(provider);
160         root.addChild(equipment);
161         return equipment;
162     }
163         
164         public static InlineComponent createStraight(P3DRootNode root) throws Exception{
165                 InlineComponent component = root.createInline();
166                 component.setType(Plant3D.URIs.Builtin_Straight);
167                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
168                 return component;
169         }
170         
171         public static TurnComponent createTurn(P3DRootNode root) throws Exception {
172                 TurnComponent elbow = root.createTurn();
173                 elbow.setType(Plant3D.URIs.Builtin_Elbow);
174                 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
175                 return elbow;
176         }
177         
178         public static InlineComponent createReducer(P3DRootNode root) throws Exception {
179                 InlineComponent component = root.createInline();
180                 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
181                 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
182                 return component;
183         }
184         
185         public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
186                 InlineComponent component = root.createInline();
187                 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
188                 return component;
189         }
190         
191         public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception {
192             Equipment equipment = root.createEquipment();
193         equipment.setType(equipmentType.getUri());
194         String n = root.getUniqueName(equipmentType.getName());
195         equipment.setName(n);
196         root.addChild(equipment);
197         return equipment;
198         }
199         
200         
201         
202         public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
203             return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));
204         }
205         
206         public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception {
207             Nozzle nozzle = root.createNozzle();
208         nozzle.setType(nozzleType.getUri());
209         String n = root.getUniqueName(nozzleType.getName());
210         nozzle.setName(n);
211         PipeRun pipeRun = new PipeRun();
212         n = root.getUniqueName("PipeRun");
213         pipeRun.setName(n);
214         nozzle.setPipeRun(pipeRun);
215         
216         equipment.addChild(nozzle);
217         root.addChild(pipeRun);
218         // root.getNodeMap().commit("Add nozzle " + n);
219         return nozzle;
220         }
221 }