1 package org.simantics.plant3d.utils;
3 import java.util.ArrayList;
6 import org.simantics.Simantics;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.request.Read;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.plant3d.ontology.Plant3D;
14 import org.simantics.plant3d.utils.Item.Type;
15 import org.simantics.ui.SimanticsUI;
17 public class P3DUtil {
19 public static List<Item> getEquipments() throws DatabaseException {
20 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
22 public List<Item> perform(ReadGraph graph) throws DatabaseException {
23 Plant3D p3d = Plant3D.getInstance(graph);
24 Resource project = Simantics.getProject().get();
25 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
26 List<Item> actions = getItems(graph, project,p3d.Equipment);
27 actions.addAll(getItems(graph, builtins,p3d.Equipment));
35 public static List<Item> getNozzles() 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 ItemQuery query = new ItemQuery(p3d.Nozzle);
41 return graph.syncRequest(query);
46 private static class ItemQuery implements Read<List<Item>> {
47 private Resource type;
48 public ItemQuery(Resource type) {
53 public List<Item> perform(ReadGraph graph) throws DatabaseException {
54 Resource project = Simantics.getProject().get();
55 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
56 List<Item> actions = getItems(graph, project,type);
57 actions.addAll(getItems(graph, builtins,type));
62 public static List<Item> getEnds() throws DatabaseException {
63 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
65 public List<Item> perform(ReadGraph graph) throws DatabaseException {
66 Plant3D p3d = Plant3D.getInstance(graph);
67 ItemQuery query = new ItemQuery(p3d.EndComponent);
68 return graph.syncRequest(query);
73 public static List<Item> getTurns() throws DatabaseException {
74 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
76 public List<Item> perform(ReadGraph graph) throws DatabaseException {
77 Plant3D p3d = Plant3D.getInstance(graph);
78 ItemQuery query = new ItemQuery(p3d.TurnComponent);
79 return graph.syncRequest(query);
84 public static List<Item> getInlines() throws DatabaseException {
85 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
87 public List<Item> perform(ReadGraph graph) throws DatabaseException {
88 Plant3D p3d = Plant3D.getInstance(graph);
89 ItemQuery query = new ItemQuery(p3d.InlineComponent);
90 return graph.syncRequest(query);
95 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
96 Plant3D p3d = Plant3D.getInstance(graph);
97 Layer0 l0 = Layer0.getInstance(graph);
98 List<Item> result = new ArrayList<Item>();
99 for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
100 if (graph.isInstanceOf(r, type) ) {
101 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
102 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
104 result.add(createItem(graph, r));
107 if (graph.isInheritedFrom(r, type)) {
108 boolean asserts = false;
109 for (Resource a : graph.getObjects(r, l0.Asserts)) {
110 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
116 result.add(createItem(graph, r));
123 private static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
124 Layer0 l0 = Layer0.getInstance(graph);
125 Plant3D p3d = Plant3D.getInstance(graph);
126 String name = graph.getRelatedValue(r, l0.HasName);
127 String uri = graph.getURI(r);
128 Item item = new Item(uri, name);
129 if (graph.isInstanceOf(r, p3d.Equipment))
130 item.setType(Type.EQUIPMENT);
131 else if (graph.isInstanceOf(r, p3d.InlineComponent))
132 item.setType(Type.INLINE);
133 else if (graph.isInstanceOf(r, p3d.EndComponent))
134 item.setType(Type.END);
135 else if (graph.isInstanceOf(r, p3d.TurnComponent))
136 item.setType(Type.TURN);
137 else if (graph.isInstanceOf(r, p3d.Nozzle))
138 item.setType(Type.NOZZLE);
140 throw new RuntimeException("Cannot detect type for " + r);
142 if (graph.hasStatement(r, p3d.CodeComponent))
144 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
145 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
146 item.setVariable(true);
147 if (graph.hasStatement(r, p3d.SizeChangeComponent))
148 item.setSizeChange(true);
152 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
153 Layer0 l0 = Layer0.getInstance(graph);
154 Plant3D p3d = Plant3D.getInstance(graph);
155 Resource model = graph.newResource();
156 graph.claim(model, l0.InstanceOf, p3d.Plant);
157 graph.claimLiteral(model, l0.HasName, name);