]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java
Enable model loading using an existing transaction.
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / controlpoint / ControlPointFactory.java
1 package org.simantics.plant3d.scenegraph.controlpoint;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.simantics.Simantics;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.RequestProcessor;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.request.Read;
13 import org.simantics.plant3d.ontology.Plant3D;
14 import org.simantics.plant3d.scenegraph.PipelineComponent;
15 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PointType;
16 import org.simantics.plant3d.utils.Item;
17 import org.simantics.plant3d.utils.P3DUtil;
18
19 public class ControlPointFactory {
20         
21         private static Map<String,Instruction> cache = new HashMap<String, Instruction>();
22         
23         
24         public static void preloadCache(RequestProcessor session, String libUri) throws Exception {
25                 List<Item> items = P3DUtil.getEnds(session, libUri);
26                 items.addAll(P3DUtil.getInlines(session, libUri));
27                 items.addAll(P3DUtil.getNozzles(session, libUri));
28                 items.addAll(P3DUtil.getTurns(session, libUri));
29                 
30                 for (Item item : items) {
31                         Instruction inst = createInstruction(session, item.getUri());
32                         cache.put(item.getUri(), inst);
33                 }
34         }
35         
36         public static PipeControlPoint create(PipelineComponent component) throws Exception {
37                 Instruction inst = cache.get(component.getType());
38                 if (inst == null) {
39                         inst = createInstruction(Simantics.getSession(), component.getType());
40                         cache.put(component.getType(), inst);
41                 }
42                 if (inst == null) {
43                         return null;
44                 }
45                 
46                 PipeControlPoint pcp = new PipeControlPoint(component);
47                 pcp.setType(inst.type);
48                 pcp.setFixed(inst.fixed);
49                 pcp.setMod(inst.mod);
50                 pcp.setRotate(inst.isRotate);
51                 pcp.setReverse(inst.isReverse);
52                 switch(inst.type) {
53                 case END:
54                         
55                         break;
56                 case INLINE:
57                         if (inst.isOffset || inst.isSizeChange) {
58                                 PipeControlPoint sub = new PipeControlPoint(component);
59                                 pcp.children.add(sub);
60                                 sub.parent = pcp;
61                                 sub.setType(inst.type);
62                                 sub.setFixed(inst.fixed);
63                                 sub.setMod(inst.mod);
64                                 sub.setSub(true);
65                                 sub.setDeletable(false);
66                                 if (inst.isOffset)
67                                         pcp.setOffset(0.0);
68                                 if (inst.isSizeChange)
69                                         pcp.setSizeChange(true);
70                         }
71                         
72                         break;
73                 case TURN:
74                         
75                         break;
76                 }
77                 return pcp;
78         }
79         
80         
81         private static class Instruction {
82                 PointType type;
83                 boolean fixed;
84                 boolean mod;
85                 boolean isOffset;
86                 boolean isSizeChange;
87                 boolean isRotate;
88                 boolean isReverse;
89                 
90         }
91         
92         private static Instruction createInstruction(RequestProcessor session, final String type) throws Exception {
93                 return session.syncRequest(new Read<Instruction>() {
94                         @Override
95                         public Instruction perform(ReadGraph graph) throws DatabaseException {
96                                 Resource res = graph.getResource(type);
97                                 Plant3D p3d = Plant3D.getInstance(graph);
98                                 Instruction i = new Instruction();
99                                 i.fixed = false;
100                                 i.isOffset = false;
101                                 i.isSizeChange = false;
102                                 i.isRotate = false;
103                                 i.isReverse = false;
104                                 i.type = PointType.INLINE;
105                                 i.mod = false;
106                                 if (graph.isInheritedFrom(res, p3d.Nozzle)) {
107                                         i.fixed = true;
108                                         i.isOffset = false;
109                                         i.isSizeChange = false;
110                                         i.type = PointType.END;
111                                 } else if (graph.isInheritedFrom(res, p3d.InlineComponent)){
112                                         
113                                         i.type = PointType.INLINE;
114                                         if (graph.hasStatement(res,p3d.VariableLengthInlineComponent)) {
115                                                 i.fixed = false;
116                                         } else if (graph.hasStatement(res,p3d.FixedLengthInlineComponent)) {
117                                                 i.fixed = true;
118                                         } else if (graph.hasStatement(res,p3d.AdjustableLengthInlineComponent)) {
119                                             i.fixed = true;
120                                             i.mod = true;
121                                         } else {
122                                             throw new DatabaseException("Inline component type " + res + " does not have length configuration.");
123                                         }
124                                         
125                                         if (graph.hasStatement(res,p3d.SizeChangeComponent)) {
126                                                 i.isSizeChange = true;
127                                         }
128                                         
129                                         if (graph.hasStatement(res,p3d.OffsetComponent)) {
130                                                 i.isOffset = true;
131                                         }
132                                         if (graph.hasStatement(res,p3d.RotateComponent)) {
133                                                 i.isRotate = true;
134                                         }
135                                         
136                                         if (graph.hasStatement(res,p3d.ReverseComponent)) {
137                                                 i.isReverse = true;
138                                         }
139                                         
140                                 } else if (graph.isInheritedFrom(res, p3d.TurnComponent)) {
141                                         i.type = PointType.TURN;
142                                         if (graph.hasStatement(res,p3d.VariableAngleTurnComponent)) {
143                                                 i.fixed = false;
144                                         } else if (graph.hasStatement(res,p3d.FixedAngleTurnComponent)) {
145                                                 i.fixed = true; 
146                                         } else {
147                                             throw new DatabaseException("Turn component type " + res + " does not have angle configuration.");
148                                         }
149                                 } else if (graph.isInheritedFrom(res, p3d.EndComponent)) {
150                                         i.fixed = false;
151                                         i.type = PointType.END;
152                                 } else {
153                                         return null;
154                                 }
155                                 return i;
156                         }
157                 });
158         }
159
160 }