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.layer0.Layer0;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.utils.Item.Type;
17 import org.simantics.ui.SimanticsUI;
19 public class P3DUtil {
21 public static List<Item> getEquipments() throws DatabaseException {
22 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
24 public List<Item> perform(ReadGraph graph) throws DatabaseException {
25 Plant3D p3d = Plant3D.getInstance(graph);
26 Resource project = Simantics.getProject().get();
27 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
28 List<Item> actions = getItems(graph, project,p3d.Equipment);
29 actions.addAll(getItems(graph, builtins,p3d.Equipment));
37 public static List<Item> getNozzles() throws DatabaseException {
38 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
40 public List<Item> perform(ReadGraph graph) throws DatabaseException {
41 Plant3D p3d = Plant3D.getInstance(graph);
42 ItemQuery query = new ItemQuery(p3d.Nozzle);
43 return graph.syncRequest(query);
48 private static class ItemQuery implements Read<List<Item>> {
49 private Resource type;
50 public ItemQuery(Resource type) {
55 public List<Item> perform(ReadGraph graph) throws DatabaseException {
56 Resource project = Simantics.getProject().get();
57 Resource builtins = graph.getResource(Plant3D.URIs.Builtin);
58 List<Item> actions = getItems(graph, project,type);
59 actions.addAll(getItems(graph, builtins,type));
64 public static List<Item> getEnds() throws DatabaseException {
65 return Simantics.getSession().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.EndComponent);
70 return graph.syncRequest(query);
75 public static List<Item> getTurns() throws DatabaseException {
76 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
78 public List<Item> perform(ReadGraph graph) throws DatabaseException {
79 Plant3D p3d = Plant3D.getInstance(graph);
80 ItemQuery query = new ItemQuery(p3d.TurnComponent);
81 return graph.syncRequest(query);
86 public static List<Item> getInlines() throws DatabaseException {
87 return Simantics.getSession().syncRequest(new Read<List<Item>>() {
89 public List<Item> perform(ReadGraph graph) throws DatabaseException {
90 Plant3D p3d = Plant3D.getInstance(graph);
91 ItemQuery query = new ItemQuery(p3d.InlineComponent);
92 return graph.syncRequest(query);
97 public static List<Item> filterUserComponents(List<Item> items) {
98 List<Item> result = new ArrayList<Item>(items.size());
99 for (Item i : items) {
106 private static List<Item> getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{
107 Plant3D p3d = Plant3D.getInstance(graph);
108 Layer0 l0 = Layer0.getInstance(graph);
109 List<Item> result = new ArrayList<Item>();
110 for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) {
111 if (graph.isInstanceOf(r, type) ) {
112 Resource geom = graph.getPossibleObject(r,p3d.hasGeometry);
113 if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) {
115 result.add(createItem(graph, r));
118 if (graph.isInheritedFrom(r, type)) {
119 boolean asserts = false;
120 for (Resource a : graph.getObjects(r, l0.Asserts)) {
121 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
127 result.add(createItem(graph, r));
131 Collections.sort(result, new Comparator<Item>() {
133 public int compare(Item o1, Item o2) {
134 return o1.getName().compareTo(o2.getName());
140 public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException {
141 Layer0 l0 = Layer0.getInstance(graph);
142 Plant3D p3d = Plant3D.getInstance(graph);
143 String name = graph.getRelatedValue(r, l0.HasName);
144 String uri = graph.getURI(r);
145 Item item = new Item(uri, name);
146 if (graph.isInstanceOf(r, p3d.Equipment))
147 item.setType(Type.EQUIPMENT);
148 else if (graph.isInstanceOf(r, p3d.InlineComponent))
149 item.setType(Type.INLINE);
150 else if (graph.isInstanceOf(r, p3d.EndComponent))
151 item.setType(Type.END);
152 else if (graph.isInstanceOf(r, p3d.TurnComponent))
153 item.setType(Type.TURN);
154 else if (graph.isInstanceOf(r, p3d.Nozzle))
155 item.setType(Type.NOZZLE);
157 throw new RuntimeException("Cannot detect type for " + r);
159 if (graph.hasStatement(r, p3d.CodeComponent))
161 if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) ||
162 graph.hasStatement(r, p3d.VariableLengthInlineComponent))
163 item.setVariable(true);
164 if (graph.hasStatement(r, p3d.SizeChangeComponent))
165 item.setSizeChange(true);
169 public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{
170 Layer0 l0 = Layer0.getInstance(graph);
171 Plant3D p3d = Plant3D.getInstance(graph);
172 Resource model = graph.newResource();
173 graph.claim(model, l0.InstanceOf, p3d.Plant);
174 graph.claimLiteral(model, l0.HasName, name);