1 package org.simantics.plant3d.utils;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
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.g3d.scenegraph.base.INode;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.plant3d.ontology.Plant3D;
17 import org.simantics.plant3d.scenegraph.Equipment;
18 import org.simantics.plant3d.scenegraph.P3DRootNode;
19 import org.simantics.plant3d.scenegraph.PipeRun;
20 import org.simantics.plant3d.scenegraph.PipelineComponent;
21 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
23 import org.simantics.plant3d.utils.Item.Type;
25 public class P3DUtil {
27 public static List<Item> getEquipments() throws DatabaseException {
28 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
30 public List<Item> perform(ReadGraph graph) throws DatabaseException {
31 Plant3D p3d = Plant3D.getInstance(graph);
32 Resource project = Simantics.getProject().get();
33 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
34 List<Item> actions = getItems(graph, project,p3d.Equipment);
35 actions.addAll(getItems(graph, builtins,p3d.Equipment));
43 public static List<Item> getNozzles() throws DatabaseException {
44 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
46 public List<Item> perform(ReadGraph graph) throws DatabaseException {
47 Plant3D p3d = Plant3D.getInstance(graph);
48 ItemQuery query = new ItemQuery(p3d.Nozzle);
49 return graph.syncRequest(query);
54 private static class ItemQuery implements Read<List<Item>> {
55 private Resource type;
56 public ItemQuery(Resource type) {
61 public List<Item> perform(ReadGraph graph) throws DatabaseException {
62 Resource project = Simantics.getProject().get();
63 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
64 List<Item> actions = getItems(graph, project,type);
65 actions.addAll(getItems(graph, builtins,type));
70 public static List<Item> getEnds() throws DatabaseException {
71 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
73 public List<Item> perform(ReadGraph graph) throws DatabaseException {
74 Plant3D p3d = Plant3D.getInstance(graph);
75 ItemQuery query = new ItemQuery(p3d.EndComponent);
76 return graph.syncRequest(query);
81 public static List<Item> getTurns() throws DatabaseException {
82 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
84 public List<Item> perform(ReadGraph graph) throws DatabaseException {
85 Plant3D p3d = Plant3D.getInstance(graph);
86 ItemQuery query = new ItemQuery(p3d.TurnComponent);
87 return graph.syncRequest(query);
92 public static List<Item> getInlines() throws DatabaseException {
93 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
95 public List<Item> perform(ReadGraph graph) throws DatabaseException {
96 Plant3D p3d = Plant3D.getInstance(graph);
97 ItemQuery query = new ItemQuery(p3d.InlineComponent);
98 return graph.syncRequest(query);
103 public static List<Item> filterUserComponents(List<Item> items) {
104 List<Item> result = new ArrayList<Item>(items.size());
105 for (Item i : items) {
112 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
113 Plant3D p3d = Plant3D.getInstance(graph);
114 Layer0 l0 = Layer0.getInstance(graph);
115 List<Item> result = new ArrayList<Item>();
116 for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
117 if (graph.isInstanceOf(r, type) ) {
118 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
119 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
121 result.add(createItem(graph, r));
124 if (graph.isInheritedFrom(r, type)) {
125 boolean asserts = false;
126 for (Resource a : graph.getObjects(r, l0.Asserts)) {
127 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
133 result.add(createItem(graph, r));
137 Collections.sort(result, new Comparator<Item>() {
139 public int compare(Item o1, Item o2) {
140 return o1.getName().compareTo(o2.getName());
146 public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
147 Layer0 l0 = Layer0.getInstance(graph);
148 Plant3D p3d = Plant3D.getInstance(graph);
149 String name = graph.getRelatedValue(r, l0.HasName);
150 String uri = graph.getURI(r);
151 Item item = new Item(uri, name);
152 if (graph.isInstanceOf(r, p3d.Equipment))
153 item.setType(Type.EQUIPMENT);
154 else if (graph.isInstanceOf(r, p3d.InlineComponent))
155 item.setType(Type.INLINE);
156 else if (graph.isInstanceOf(r, p3d.EndComponent))
157 item.setType(Type.END);
158 else if (graph.isInstanceOf(r, p3d.TurnComponent))
159 item.setType(Type.TURN);
160 else if (graph.isInstanceOf(r, p3d.Nozzle))
161 item.setType(Type.NOZZLE);
163 throw new RuntimeException("Cannot detect type for " + r);
165 if (graph.hasStatement(r, p3d.CodeComponent))
167 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
168 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
169 item.setVariable(true);
170 if (graph.hasStatement(r, p3d.SizeChangeComponent))
171 item.setSizeChange(true);
175 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
176 Layer0 l0 = Layer0.getInstance(graph);
177 Plant3D p3d = Plant3D.getInstance(graph);
178 Resource model = graph.newResource();
179 graph.claim(model, l0.InstanceOf, p3d.Plant);
180 graph.claimLiteral(model, l0.HasName, name);
185 public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
186 for (INode node : rootNode.getChild()) {
187 if (node instanceof PipeRun) {
188 for (PipelineComponent pc : ((PipeRun) node).getChild())
190 } else if (node instanceof Equipment) {
191 for (PipelineComponent pc : ((Equipment) node).getChild())
196 for (INode node : rootNode.getChild()) {
197 if (node instanceof PipeRun) {
198 for (PipelineComponent pc : ((PipeRun) node).getChild())
200 } else if (node instanceof Equipment) {
201 for (PipelineComponent pc : ((Equipment) node).getChild())
205 for (INode node : rootNode.getChild()) {
206 if (node instanceof PipeRun) {
207 PipingRules.validate((PipeRun)node);
210 PipingRules.setEnabled(true);
211 for (INode node : rootNode.getChild()) {
212 if (node instanceof PipeRun) {
213 PipeRun run = (PipeRun)node;
214 for (PipeControlPoint pcp : run.getControlPoints())
215 PipingRules.positionUpdate(pcp);