1 package org.simantics.plant3d.utils;
3 import java.util.ArrayDeque;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.Deque;
8 import java.util.HashSet;
12 import org.simantics.Simantics;
13 import org.simantics.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.exception.DatabaseException;
17 import org.simantics.db.request.Read;
18 import org.simantics.g3d.scenegraph.base.INode;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.plant3d.ontology.Plant3D;
21 import org.simantics.plant3d.scenegraph.Equipment;
22 import org.simantics.plant3d.scenegraph.P3DRootNode;
23 import org.simantics.plant3d.scenegraph.PipeRun;
24 import org.simantics.plant3d.scenegraph.PipelineComponent;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
27 import org.simantics.plant3d.utils.Item.Type;
29 public class P3DUtil {
31 public static List<Item> getEquipments() throws DatabaseException {
32 return getEquipments(Plant3D.URIs.Builtin);
35 public static List<Item> getEquipments(final String libUri) throws DatabaseException {
36 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
38 public List<Item> perform(ReadGraph graph) throws DatabaseException {
39 Plant3D p3d = Plant3D.getInstance(graph);
40 Resource project = Simantics.getProject().get();
41 Resource builtins = graph.getResource(libUri);
42 List<Item> actions = getItems(graph, project,p3d.Equipment);
43 actions.addAll(getItems(graph, builtins,p3d.Equipment));
51 public static List<Item> getNozzles(String libUri) throws DatabaseException {
52 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
54 public List<Item> perform(ReadGraph graph) throws DatabaseException {
55 Plant3D p3d = Plant3D.getInstance(graph);
56 ItemQuery query = new ItemQuery(p3d.Nozzle, libUri);
57 return graph.syncRequest(query);
62 public static class ItemQuery implements Read<List<Item>> {
63 private Resource type;
64 private String libUri;
65 public ItemQuery(Resource type, String libUri) {
71 public List<Item> perform(ReadGraph graph) throws DatabaseException {
72 // Resource project = Simantics.getProject().get();
73 Resource builtins = graph.getResource(libUri);
74 List<Item> actions = new ArrayList<>();
75 // actions.addAll(getItems(graph, project,type));
76 actions.addAll(getItems(graph, builtins,type));
81 public boolean equals(Object obj) {
82 if (obj.getClass() != this.getClass())
84 ItemQuery other = (ItemQuery)obj;
85 if (!type.equals(other.type))
87 return libUri.equals(other.libUri);
91 public int hashCode() {
92 return type.hashCode() + libUri.hashCode();
96 public static List<Item> getEnds(String libUri) throws DatabaseException {
97 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
99 public List<Item> perform(ReadGraph graph) throws DatabaseException {
100 Plant3D p3d = Plant3D.getInstance(graph);
101 ItemQuery query = new ItemQuery(p3d.EndComponent, libUri);
102 return graph.syncRequest(query);
107 public static List<Item> getTurns(String libUri) throws DatabaseException {
108 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
110 public List<Item> perform(ReadGraph graph) throws DatabaseException {
111 Plant3D p3d = Plant3D.getInstance(graph);
112 ItemQuery query = new ItemQuery(p3d.TurnComponent, libUri);
113 return graph.syncRequest(query);
118 public static List<Item> getInlines(String libUri) throws DatabaseException {
119 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
121 public List<Item> perform(ReadGraph graph) throws DatabaseException {
122 Plant3D p3d = Plant3D.getInstance(graph);
123 ItemQuery query = new ItemQuery(p3d.InlineComponent, libUri);
124 return graph.syncRequest(query);
129 public static List<Item> filterUserComponents(List<Item> items) {
130 List<Item> result = new ArrayList<Item>(items.size());
131 for (Item i : items) {
138 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
139 Plant3D p3d = Plant3D.getInstance(graph);
140 Layer0 l0 = Layer0.getInstance(graph);
141 List<Item> result = new ArrayList<Item>();
142 Set<Resource> processed = new HashSet<>();
143 Deque<Resource> stack = new ArrayDeque<Resource>();
144 stack.addAll(graph.getObjects(lib, l0.ConsistsOf));
145 stack.addAll(graph.getObjects(lib, p3d.ComponentLibrary_contains));
146 while (!stack.isEmpty()) {
147 Resource r = stack.pop();
148 if (processed.contains(r))
151 if (graph.isInstanceOf(r, type) ) {
152 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
153 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
155 result.add(createItem(graph, r));
159 if (graph.isInheritedFrom(r, type)) {
160 boolean asserts = false;
161 for (Resource a : graph.getObjects(r, l0.Asserts)) {
162 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
168 result.add(createItem(graph, r));
172 if (graph.isInstanceOf(r, p3d.ComponentLibrary)) {
173 stack.addAll(graph.getObjects(r, l0.ConsistsOf));
174 stack.addAll(graph.getObjects(r, p3d.ComponentLibrary_contains));
177 Collections.sort(result, new Comparator<Item>() {
179 public int compare(Item o1, Item o2) {
180 return o1.getName().compareTo(o2.getName());
186 public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
187 Layer0 l0 = Layer0.getInstance(graph);
188 Plant3D p3d = Plant3D.getInstance(graph);
189 String name = graph.getRelatedValue(r, l0.HasName);
190 String uri = graph.getURI(r);
191 Item item = new Item(uri, name);
192 if (graph.isInstanceOf(r, p3d.Equipment))
193 item.setType(Type.EQUIPMENT);
194 else if (graph.isInstanceOf(r, p3d.InlineComponent))
195 item.setType(Type.INLINE);
196 else if (graph.isInstanceOf(r, p3d.EndComponent))
197 item.setType(Type.END);
198 else if (graph.isInstanceOf(r, p3d.TurnComponent))
199 item.setType(Type.TURN);
200 else if (graph.isInstanceOf(r, p3d.Nozzle))
201 item.setType(Type.NOZZLE);
203 throw new RuntimeException("Cannot detect type for " + r);
205 if (graph.hasStatement(r, p3d.CodeComponent))
207 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
208 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
209 item.setVariable(true);
210 if (graph.hasStatement(r, p3d.AdjustableLengthInlineComponent))
211 item.setModifiable(true);
212 if (graph.hasStatement(r, p3d.SizeChangeComponent))
213 item.setSizeChange(true);
214 if (graph.hasStatement(r, p3d.RotateComponent))
215 item.setRotated(true);
219 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
220 Layer0 l0 = Layer0.getInstance(graph);
221 Plant3D p3d = Plant3D.getInstance(graph);
222 Resource model = graph.newResource();
223 graph.claim(model, l0.InstanceOf, p3d.Plant);
224 graph.claimLiteral(model, l0.HasName, name);
229 public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
230 for (INode node : rootNode.getChild()) {
231 if (node instanceof PipeRun) {
232 for (PipelineComponent pc : ((PipeRun) node).getChild())
234 } else if (node instanceof Equipment) {
235 for (PipelineComponent pc : ((Equipment) node).getChild())
240 for (INode node : rootNode.getChild()) {
241 if (node instanceof PipeRun) {
242 for (PipelineComponent pc : ((PipeRun) node).getChild())
244 } else if (node instanceof Equipment) {
245 for (PipelineComponent pc : ((Equipment) node).getChild())
249 for (INode node : rootNode.getChild()) {
250 if (node instanceof PipeRun) {
251 PipingRules.validate((PipeRun)node);
256 public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception {
257 PipingRules.setEnabled(true);
258 for (INode node : rootNode.getChild()) {
259 if (node instanceof PipeRun) {
260 PipeRun run = (PipeRun) node;
261 for (PipeControlPoint pcp : run.getControlPoints())
262 if (pcp.asPathLegEnd())
263 PipingRules.requestUpdate(pcp);
266 PipingRules.update();