]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java
vtk 8.2.0 API changes
[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.List;
5
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.request.Read;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.plant3d.ontology.Plant3D;
14 import org.simantics.plant3d.utils.Item.Type;
15 import org.simantics.ui.SimanticsUI;
16
17 public class P3DUtil {
18         
19         public static List<Item> getEquipments() throws DatabaseException {
20                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
21                         @Override
22                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
23                                 Plant3D p3d = Plant3D.getInstance(graph);
24                                 Resource project = Simantics.getProject().get();
25                                 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
26                                 List<Item> actions = getItems(graph, project,p3d.Equipment);
27                                 actions.addAll(getItems(graph, builtins,p3d.Equipment));
28                                 return actions;
29                         }
30                         
31                         
32                 });
33         }
34         
35         public static List<Item> getNozzles() 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                                 ItemQuery query = new ItemQuery(p3d.Nozzle);
41                                 return graph.syncRequest(query);
42                         }
43                 });
44         }
45         
46         private static class ItemQuery implements Read<List<Item>> {
47                 private Resource type;
48                 public ItemQuery(Resource type) {
49                         this.type = type;
50                 }
51                 
52                 @Override
53                 public List<Item> perform(ReadGraph graph) throws DatabaseException {
54                         Resource project = Simantics.getProject().get();
55                         Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
56                         List<Item> actions = getItems(graph, project,type);
57                         actions.addAll(getItems(graph, builtins,type));
58                         return actions;
59                 }
60         }
61         
62         public static List<Item> getEnds() throws DatabaseException {
63                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
64                         @Override
65                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
66                                 Plant3D p3d = Plant3D.getInstance(graph);
67                                 ItemQuery query = new ItemQuery(p3d.EndComponent);
68                                 return graph.syncRequest(query);
69                         }
70                 });
71         }
72         
73         public static List<Item> getTurns() throws DatabaseException {
74                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
75                         @Override
76                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
77                                 Plant3D p3d = Plant3D.getInstance(graph);
78                                 ItemQuery query = new ItemQuery(p3d.TurnComponent);
79                                 return graph.syncRequest(query);
80                         }
81                 });
82         }
83         
84         public static List<Item> getInlines() throws DatabaseException {
85                 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
86                         @Override
87                         public List<Item> perform(ReadGraph graph) throws DatabaseException {
88                                 Plant3D p3d = Plant3D.getInstance(graph);
89                                 ItemQuery query = new ItemQuery(p3d.InlineComponent);
90                                 return graph.syncRequest(query);
91                         }
92                 });
93         }
94         
95         private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
96                 Plant3D p3d = Plant3D.getInstance(graph);
97                 Layer0 l0 = Layer0.getInstance(graph);
98                 List<Item> result = new ArrayList<Item>();
99                 for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
100                         if (graph.isInstanceOf(r, type) ) {
101                                 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
102                                 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
103                                         
104                                         result.add(createItem(graph, r));
105                                 }
106                         } 
107                         if (graph.isInheritedFrom(r, type)) {
108                                 boolean asserts = false;
109                                 for (Resource a : graph.getObjects(r, l0.Asserts)) {
110                                         if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
111                                                 asserts = true;
112                                                 break;
113                                         }
114                                 }
115                                 if (asserts) {          
116                                         result.add(createItem(graph, r));
117                                 }
118                         }
119                 }
120                 return result;
121         }
122         
123         private static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
124                 Layer0 l0 = Layer0.getInstance(graph);
125                 Plant3D p3d = Plant3D.getInstance(graph);
126                 String name = graph.getRelatedValue(r, l0.HasName);
127                 String uri = graph.getURI(r);
128                 Item item = new Item(uri, name);
129                 if (graph.isInstanceOf(r, p3d.Equipment))
130                         item.setType(Type.EQUIPMENT);
131                 else if (graph.isInstanceOf(r, p3d.InlineComponent))
132                         item.setType(Type.INLINE);
133                 else if (graph.isInstanceOf(r, p3d.EndComponent))
134                         item.setType(Type.END);
135                 else if (graph.isInstanceOf(r, p3d.TurnComponent))
136                         item.setType(Type.TURN);
137                 else if (graph.isInstanceOf(r, p3d.Nozzle))
138                         item.setType(Type.NOZZLE);
139                 else 
140                         throw new RuntimeException("Cannot detect type for " + r);
141                 
142                 if (graph.hasStatement(r, p3d.CodeComponent))
143                         item.setCode(true);
144                 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
145                     graph.hasStatement(r, p3d.VariableLengthInlineComponent))
146                         item.setVariable(true);
147                 if (graph.hasStatement(r, p3d.SizeChangeComponent))
148                         item.setSizeChange(true);
149                 return item;
150         }
151         
152         public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
153                 Layer0 l0 = Layer0.getInstance(graph);
154                 Plant3D p3d = Plant3D.getInstance(graph);
155                 Resource model = graph.newResource();
156                 graph.claim(model, l0.InstanceOf, p3d.Plant);
157                 graph.claimLiteral(model, l0.HasName, name);
158                 
159                 return model;
160         }
161
162 }