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