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 getEquipments(Plant3D.URIs.Builtin);
31 public static List<Item> getEquipments(final String libUri) throws DatabaseException {
32 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
34 public List<Item> perform(ReadGraph graph) throws DatabaseException {
35 Plant3D p3d = Plant3D.getInstance(graph);
36 Resource project = Simantics.getProject().get();
37 Resource builtins = graph.getResource(libUri);
38 List<Item> actions = getItems(graph, project,p3d.Equipment);
39 actions.addAll(getItems(graph, builtins,p3d.Equipment));
47 public static List<Item> getNozzles() throws DatabaseException {
48 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
50 public List<Item> perform(ReadGraph graph) throws DatabaseException {
51 Plant3D p3d = Plant3D.getInstance(graph);
52 ItemQuery query = new ItemQuery(p3d.Nozzle, Plant3D.URIs.Builtin);
53 return graph.syncRequest(query);
58 public static class ItemQuery implements Read<List<Item>> {
59 private Resource type;
60 private String libUri;
61 public ItemQuery(Resource type, String libUri) {
67 public List<Item> perform(ReadGraph graph) throws DatabaseException {
68 Resource project = Simantics.getProject().get();
69 Resource builtins = graph.getResource(libUri);
70 List<Item> actions = getItems(graph, project,type);
71 actions.addAll(getItems(graph, builtins,type));
76 public boolean equals(Object obj) {
77 if (obj.getClass() != this.getClass())
79 ItemQuery other = (ItemQuery)obj;
80 if (!type.equals(other.type))
82 return libUri.equals(other.libUri);
86 public int hashCode() {
87 return type.hashCode() + libUri.hashCode();
91 public static List<Item> getEnds() throws DatabaseException {
92 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
94 public List<Item> perform(ReadGraph graph) throws DatabaseException {
95 Plant3D p3d = Plant3D.getInstance(graph);
96 ItemQuery query = new ItemQuery(p3d.EndComponent, Plant3D.URIs.Builtin);
97 return graph.syncRequest(query);
102 public static List<Item> getTurns() throws DatabaseException {
103 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
105 public List<Item> perform(ReadGraph graph) throws DatabaseException {
106 Plant3D p3d = Plant3D.getInstance(graph);
107 ItemQuery query = new ItemQuery(p3d.TurnComponent, Plant3D.URIs.Builtin);
108 return graph.syncRequest(query);
113 public static List<Item> getInlines() throws DatabaseException {
114 return Simantics.getSession().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.InlineComponent, Plant3D.URIs.Builtin);
119 return graph.syncRequest(query);
124 public static List<Item> filterUserComponents(List<Item> items) {
125 List<Item> result = new ArrayList<Item>(items.size());
126 for (Item i : items) {
133 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
134 Plant3D p3d = Plant3D.getInstance(graph);
135 Layer0 l0 = Layer0.getInstance(graph);
136 List<Item> result = new ArrayList<Item>();
137 for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
138 if (graph.isInstanceOf(r, type) ) {
139 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
140 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
142 result.add(createItem(graph, r));
145 if (graph.isInheritedFrom(r, type)) {
146 boolean asserts = false;
147 for (Resource a : graph.getObjects(r, l0.Asserts)) {
148 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
154 result.add(createItem(graph, r));
158 Collections.sort(result, new Comparator<Item>() {
160 public int compare(Item o1, Item o2) {
161 return o1.getName().compareTo(o2.getName());
167 public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
168 Layer0 l0 = Layer0.getInstance(graph);
169 Plant3D p3d = Plant3D.getInstance(graph);
170 String name = graph.getRelatedValue(r, l0.HasName);
171 String uri = graph.getURI(r);
172 Item item = new Item(uri, name);
173 if (graph.isInstanceOf(r, p3d.Equipment))
174 item.setType(Type.EQUIPMENT);
175 else if (graph.isInstanceOf(r, p3d.InlineComponent))
176 item.setType(Type.INLINE);
177 else if (graph.isInstanceOf(r, p3d.EndComponent))
178 item.setType(Type.END);
179 else if (graph.isInstanceOf(r, p3d.TurnComponent))
180 item.setType(Type.TURN);
181 else if (graph.isInstanceOf(r, p3d.Nozzle))
182 item.setType(Type.NOZZLE);
184 throw new RuntimeException("Cannot detect type for " + r);
186 if (graph.hasStatement(r, p3d.CodeComponent))
188 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
189 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
190 item.setVariable(true);
191 if (graph.hasStatement(r, p3d.SizeChangeComponent))
192 item.setSizeChange(true);
196 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
197 Layer0 l0 = Layer0.getInstance(graph);
198 Plant3D p3d = Plant3D.getInstance(graph);
199 Resource model = graph.newResource();
200 graph.claim(model, l0.InstanceOf, p3d.Plant);
201 graph.claimLiteral(model, l0.HasName, name);
206 public static void finalizeDBLoad(P3DRootNode rootNode) throws Exception{
207 for (INode node : rootNode.getChild()) {
208 if (node instanceof PipeRun) {
209 for (PipelineComponent pc : ((PipeRun) node).getChild())
211 } else if (node instanceof Equipment) {
212 for (PipelineComponent pc : ((Equipment) node).getChild())
217 for (INode node : rootNode.getChild()) {
218 if (node instanceof PipeRun) {
219 for (PipelineComponent pc : ((PipeRun) node).getChild())
221 } else if (node instanceof Equipment) {
222 for (PipelineComponent pc : ((Equipment) node).getChild())
226 for (INode node : rootNode.getChild()) {
227 if (node instanceof PipeRun) {
228 PipingRules.validate((PipeRun)node);
233 public static void finalizeDBLoad2(P3DRootNode rootNode) throws Exception{
234 PipingRules.setEnabled(true);
235 for (INode node : rootNode.getChild()) {
236 if (node instanceof PipeRun) {
237 PipeRun run = (PipeRun)node;
238 for (PipeControlPoint pcp : run.getControlPoints())
239 PipingRules.positionUpdate(pcp);
240 //PipingRules.requestUpdate(pcp);