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.scl.SCLUtil;
29 import org.simantics.plant3d.utils.Item.Type;
31 public class P3DUtil {
32 public static List<Item> getEquipments() throws DatabaseException {
33 return getEquipments(SCLUtil.getRequestProcessor());
36 public static List<Item> getEquipments(RequestProcessor session) throws DatabaseException {
37 return getEquipments(session, Plant3D.URIs.Builtin);
40 public static List<Item> getEquipments(final String libUri) throws DatabaseException {
41 return getEquipments(SCLUtil.getRequestProcessor(), libUri);
44 public static List<Item> getEquipments(RequestProcessor session, final String libUri) throws DatabaseException {
45 return session.syncRequest(new Read<List<Item>>() {
47 public List<Item> perform(ReadGraph graph) throws DatabaseException {
48 Plant3D p3d = Plant3D.getInstance(graph);
49 Resource project = Simantics.getProject().get();
50 Resource builtins = graph.getResource(libUri);
51 List<Item> actions = getItems(graph, project,p3d.Equipment);
52 actions.addAll(getItems(graph, builtins,p3d.Equipment));
60 public static List<Item> getNozzles(String libUri) throws DatabaseException {
61 return getNozzles(SCLUtil.getRequestProcessor(), libUri);
64 public static List<Item> getNozzles(RequestProcessor session, String libUri) throws DatabaseException {
65 return session.syncRequest(new Read<List<Item>>() {
67 public List<Item> perform(ReadGraph graph) throws DatabaseException {
68 Plant3D p3d = Plant3D.getInstance(graph);
69 ItemQuery query = new ItemQuery(p3d.Nozzle, libUri);
70 return graph.syncRequest(query);
75 public static class ItemQuery implements Read<List<Item>> {
76 private Resource type;
77 private String libUri;
78 public ItemQuery(Resource type, String libUri) {
84 public List<Item> perform(ReadGraph graph) throws DatabaseException {
85 // Resource project = Simantics.getProject().get();
86 Resource builtins = graph.getResource(libUri);
87 List<Item> actions = new ArrayList<>();
88 // actions.addAll(getItems(graph, project,type));
89 actions.addAll(getItems(graph, builtins,type));
94 public boolean equals(Object obj) {
95 if (obj.getClass() != this.getClass())
97 ItemQuery other = (ItemQuery)obj;
98 if (!type.equals(other.type))
100 return libUri.equals(other.libUri);
104 public int hashCode() {
105 return type.hashCode() + libUri.hashCode();
109 public static List<Item> getEnds(String libUri) throws DatabaseException {
110 return getEnds(SCLUtil.getRequestProcessor(), libUri);
113 public static List<Item> getEnds(RequestProcessor session, String libUri) throws DatabaseException {
114 return session.syncRequest(new Read<List<Item>>() {
116 public List<Item> perform(ReadGraph graph) throws DatabaseException {
117 Plant3D p3d = Plant3D.getInstance(graph);
118 ItemQuery query = new ItemQuery(p3d.EndComponent, libUri);
119 return graph.syncRequest(query);
124 public static List<Item> getTurns(String libUri) throws DatabaseException {
125 return getTurns(SCLUtil.getRequestProcessor(), libUri);
128 public static List<Item> getTurns(RequestProcessor session, String libUri) throws DatabaseException {
129 return session.syncRequest(new Read<List<Item>>() {
131 public List<Item> perform(ReadGraph graph) throws DatabaseException {
132 Plant3D p3d = Plant3D.getInstance(graph);
133 ItemQuery query = new ItemQuery(p3d.TurnComponent, libUri);
134 return graph.syncRequest(query);
139 public static List<Item> getInlines(String libUri) throws DatabaseException {
140 return getInlines(SCLUtil.getRequestProcessor(), libUri);
143 public static List<Item> getInlines(RequestProcessor session, String libUri) throws DatabaseException {
144 return session.syncRequest(new Read<List<Item>>() {
146 public List<Item> perform(ReadGraph graph) throws DatabaseException {
147 Plant3D p3d = Plant3D.getInstance(graph);
148 ItemQuery query = new ItemQuery(p3d.InlineComponent, libUri);
149 return graph.syncRequest(query);
154 public static List<Item> filterUserComponents(List<Item> items) {
155 List<Item> result = new ArrayList<Item>(items.size());
156 for (Item i : items) {
163 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
164 Plant3D p3d = Plant3D.getInstance(graph);
165 Layer0 l0 = Layer0.getInstance(graph);
166 List<Item> result = new ArrayList<Item>();
167 Set<Resource> processed = new HashSet<>();
168 Deque<Resource> stack = new ArrayDeque<Resource>();
169 stack.addAll(graph.getObjects(lib, l0.ConsistsOf));
170 stack.addAll(graph.getObjects(lib, p3d.ComponentLibrary_contains));
171 while (!stack.isEmpty()) {
172 Resource r = stack.pop();
173 if (processed.contains(r))
176 if (graph.isInstanceOf(r, type) ) {
177 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
178 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
180 result.add(createItem(graph, r));
184 if (graph.isInheritedFrom(r, type)) {
185 boolean asserts = false;
186 for (Resource a : graph.getObjects(r, l0.Asserts)) {
187 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
193 result.add(createItem(graph, r));
197 if (graph.isInstanceOf(r, p3d.ComponentLibrary)) {
198 stack.addAll(graph.getObjects(r, l0.ConsistsOf));
199 stack.addAll(graph.getObjects(r, p3d.ComponentLibrary_contains));
202 Collections.sort(result, new Comparator<Item>() {
204 public int compare(Item o1, Item o2) {
205 return o1.getName().compareTo(o2.getName());
211 public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
212 Layer0 l0 = Layer0.getInstance(graph);
213 Plant3D p3d = Plant3D.getInstance(graph);
214 String name = graph.getRelatedValue(r, l0.HasName);
215 String uri = graph.getURI(r);
216 String label = graph.getPossibleRelatedValue(r, l0.HasLabel);
217 Item item = new Item(uri, name, label);
218 if (graph.isInstanceOf(r, p3d.Equipment))
219 item.setType(Type.EQUIPMENT);
220 else if (graph.isInstanceOf(r, p3d.InlineComponent))
221 item.setType(Type.INLINE);
222 else if (graph.isInstanceOf(r, p3d.EndComponent))
223 item.setType(Type.END);
224 else if (graph.isInstanceOf(r, p3d.TurnComponent))
225 item.setType(Type.TURN);
226 else if (graph.isInstanceOf(r, p3d.Nozzle))
227 item.setType(Type.NOZZLE);
229 throw new RuntimeException("Cannot detect type for " + r);
231 if (graph.hasStatement(r, p3d.CodeComponent))
233 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
234 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
235 item.setVariable(true);
236 if (graph.hasStatement(r, p3d.AdjustableLengthInlineComponent))
237 item.setModifiable(true);
238 if (graph.hasStatement(r, p3d.SizeChangeComponent))
239 item.setSizeChange(true);
240 if (graph.hasStatement(r, p3d.RotateComponent))
241 item.setRotated(true);
245 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
246 Layer0 l0 = Layer0.getInstance(graph);
247 Plant3D p3d = Plant3D.getInstance(graph);
248 Resource model = graph.newResource();
249 graph.claim(model, l0.InstanceOf, p3d.Plant);
250 graph.claimLiteral(model, l0.HasName, name);
255 public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
256 for (INode node : rootNode.getChild()) {
257 if (node instanceof PipeRun) {
258 for (PipelineComponent pc : ((PipeRun) node).getChild()) {
260 pc.updateParameters();
262 } else if (node instanceof Equipment) {
263 for (PipelineComponent pc : ((Equipment) node).getChild()) {
265 pc.updateParameters();
270 for (INode node : rootNode.getChild()) {
271 if (node instanceof PipeRun) {
272 for (PipelineComponent pc : ((PipeRun) node).getChild())
274 } else if (node instanceof Equipment) {
275 for (PipelineComponent pc : ((Equipment) node).getChild())
279 for (INode node : rootNode.getChild()) {
280 if (node instanceof PipeRun) {
281 PipingRules.validate((PipeRun)node);
286 public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception {
287 PipingRules.setEnabled(true);
288 for (INode node : rootNode.getChild()) {
289 if (node instanceof PipeRun) {
290 PipeRun run = (PipeRun) node;
291 for (PipeControlPoint pcp : run.getControlPoints())
292 if (pcp.asPathLegEnd())
293 PipingRules.requestUpdate(pcp);
296 PipingRules.update();