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.RequestProcessor;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.request.Read;
19 import org.simantics.g3d.scenegraph.base.INode;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.plant3d.ontology.Plant3D;
22 import org.simantics.plant3d.scenegraph.Equipment;
23 import org.simantics.plant3d.scenegraph.P3DRootNode;
24 import org.simantics.plant3d.scenegraph.PipeRun;
25 import org.simantics.plant3d.scenegraph.PipelineComponent;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
28 import org.simantics.plant3d.utils.Item.Type;
30 public class P3DUtil {
32 public static List<Item> getEquipments(RequestProcessor session) throws DatabaseException {
33 return getEquipments(session, Plant3D.URIs.Builtin);
36 public static List<Item> getEquipments(RequestProcessor session, final String libUri) throws DatabaseException {
37 return session.syncRequest(new Read<List<Item>>() {
39 public List<Item> perform(ReadGraph graph) throws DatabaseException {
40 Plant3D p3d = Plant3D.getInstance(graph);
41 Resource project = Simantics.getProject().get();
42 Resource builtins = graph.getResource(libUri);
43 List<Item> actions = getItems(graph, project,p3d.Equipment);
44 actions.addAll(getItems(graph, builtins,p3d.Equipment));
52 public static List<Item> getNozzles(RequestProcessor session, String libUri) throws DatabaseException {
53 return session.syncRequest(new Read<List<Item>>() {
55 public List<Item> perform(ReadGraph graph) throws DatabaseException {
56 Plant3D p3d = Plant3D.getInstance(graph);
57 ItemQuery query = new ItemQuery(p3d.Nozzle, libUri);
58 return graph.syncRequest(query);
63 public static class ItemQuery implements Read<List<Item>> {
64 private Resource type;
65 private String libUri;
66 public ItemQuery(Resource type, String libUri) {
72 public List<Item> perform(ReadGraph graph) throws DatabaseException {
73 // Resource project = Simantics.getProject().get();
74 Resource builtins = graph.getResource(libUri);
75 List<Item> actions = new ArrayList<>();
76 // actions.addAll(getItems(graph, project,type));
77 actions.addAll(getItems(graph, builtins,type));
82 public boolean equals(Object obj) {
83 if (obj.getClass() != this.getClass())
85 ItemQuery other = (ItemQuery)obj;
86 if (!type.equals(other.type))
88 return libUri.equals(other.libUri);
92 public int hashCode() {
93 return type.hashCode() + libUri.hashCode();
97 public static List<Item> getEnds(RequestProcessor session, String libUri) throws DatabaseException {
98 return session.syncRequest(new Read<List<Item>>() {
100 public List<Item> perform(ReadGraph graph) throws DatabaseException {
101 Plant3D p3d = Plant3D.getInstance(graph);
102 ItemQuery query = new ItemQuery(p3d.EndComponent, libUri);
103 return graph.syncRequest(query);
108 public static List<Item> getTurns(RequestProcessor session, String libUri) throws DatabaseException {
109 return session.syncRequest(new Read<List<Item>>() {
111 public List<Item> perform(ReadGraph graph) throws DatabaseException {
112 Plant3D p3d = Plant3D.getInstance(graph);
113 ItemQuery query = new ItemQuery(p3d.TurnComponent, libUri);
114 return graph.syncRequest(query);
119 public static List<Item> getInlines(RequestProcessor session, String libUri) throws DatabaseException {
120 return session.syncRequest(new Read<List<Item>>() {
122 public List<Item> perform(ReadGraph graph) throws DatabaseException {
123 Plant3D p3d = Plant3D.getInstance(graph);
124 ItemQuery query = new ItemQuery(p3d.InlineComponent, libUri);
125 return graph.syncRequest(query);
130 public static List<Item> filterUserComponents(List<Item> items) {
131 List<Item> result = new ArrayList<Item>(items.size());
132 for (Item i : items) {
139 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
140 Plant3D p3d = Plant3D.getInstance(graph);
141 Layer0 l0 = Layer0.getInstance(graph);
142 List<Item> result = new ArrayList<Item>();
143 Set<Resource> processed = new HashSet<>();
144 Deque<Resource> stack = new ArrayDeque<Resource>();
145 stack.addAll(graph.getObjects(lib, l0.ConsistsOf));
146 stack.addAll(graph.getObjects(lib, p3d.ComponentLibrary_contains));
147 while (!stack.isEmpty()) {
148 Resource r = stack.pop();
149 if (processed.contains(r))
152 if (graph.isInstanceOf(r, type) ) {
153 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
154 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
156 result.add(createItem(graph, r));
160 if (graph.isInheritedFrom(r, type)) {
161 boolean asserts = false;
162 for (Resource a : graph.getObjects(r, l0.Asserts)) {
163 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
169 result.add(createItem(graph, r));
173 if (graph.isInstanceOf(r, p3d.ComponentLibrary)) {
174 stack.addAll(graph.getObjects(r, l0.ConsistsOf));
175 stack.addAll(graph.getObjects(r, p3d.ComponentLibrary_contains));
178 Collections.sort(result, new Comparator<Item>() {
180 public int compare(Item o1, Item o2) {
181 return o1.getName().compareTo(o2.getName());
187 public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
188 Layer0 l0 = Layer0.getInstance(graph);
189 Plant3D p3d = Plant3D.getInstance(graph);
190 String name = graph.getRelatedValue(r, l0.HasName);
191 String uri = graph.getURI(r);
192 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);
193 Item item = new Item(uri, name, label);
194 if (graph.isInstanceOf(r, p3d.Equipment))
195 item.setType(Type.EQUIPMENT);
196 else if (graph.isInstanceOf(r, p3d.InlineComponent))
197 item.setType(Type.INLINE);
198 else if (graph.isInstanceOf(r, p3d.EndComponent))
199 item.setType(Type.END);
200 else if (graph.isInstanceOf(r, p3d.TurnComponent))
201 item.setType(Type.TURN);
202 else if (graph.isInstanceOf(r, p3d.Nozzle))
203 item.setType(Type.NOZZLE);
205 throw new RuntimeException("Cannot detect type for " + r);
207 if (graph.hasStatement(r, p3d.CodeComponent))
209 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
210 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
211 item.setVariable(true);
212 if (graph.hasStatement(r, p3d.AdjustableLengthInlineComponent))
213 item.setModifiable(true);
214 if (graph.hasStatement(r, p3d.SizeChangeComponent))
215 item.setSizeChange(true);
216 if (graph.hasStatement(r, p3d.RotateComponent))
217 item.setRotated(true);
221 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
222 Layer0 l0 = Layer0.getInstance(graph);
223 Plant3D p3d = Plant3D.getInstance(graph);
224 Resource model = graph.newResource();
225 graph.claim(model, l0.InstanceOf, p3d.Plant);
226 graph.claimLiteral(model, l0.HasName, name);
231 public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
232 for (INode node : rootNode.getChild()) {
233 if (node instanceof PipeRun) {
234 for (PipelineComponent pc : ((PipeRun) node).getChild()) {
236 pc.updateParameters();
238 } else if (node instanceof Equipment) {
239 for (PipelineComponent pc : ((Equipment) node).getChild()) {
241 pc.updateParameters();
246 for (INode node : rootNode.getChild()) {
247 if (node instanceof PipeRun) {
248 for (PipelineComponent pc : ((PipeRun) node).getChild())
250 } else if (node instanceof Equipment) {
251 for (PipelineComponent pc : ((Equipment) node).getChild())
255 for (INode node : rootNode.getChild()) {
256 if (node instanceof PipeRun) {
257 PipingRules.validate((PipeRun)node);
262 public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception {
263 PipingRules.setEnabled(true);
264 for (INode node : rootNode.getChild()) {
265 if (node instanceof PipeRun) {
266 PipeRun run = (PipeRun) node;
267 for (PipeControlPoint pcp : run.getControlPoints())
268 if (pcp.asPathLegEnd())
269 PipingRules.requestUpdate(pcp);
272 PipingRules.update();