package org.simantics.plant3d.utils; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.plant3d.ontology.Plant3D; import org.simantics.plant3d.utils.Item.Type; import org.simantics.ui.SimanticsUI; public class P3DUtil { public static List getEquipments() throws DatabaseException { return Simantics.getSession().syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); Resource project = Simantics.getProject().get(); Resource builtins = graph.getResource(Plant3D.URIs.Builtin); List actions = getItems(graph, project,p3d.Equipment); actions.addAll(getItems(graph, builtins,p3d.Equipment)); return actions; } }); } public static List getNozzles() throws DatabaseException { return Simantics.getSession().syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); ItemQuery query = new ItemQuery(p3d.Nozzle); return graph.syncRequest(query); } }); } private static class ItemQuery implements Read> { private Resource type; public ItemQuery(Resource type) { this.type = type; } @Override public List perform(ReadGraph graph) throws DatabaseException { Resource project = Simantics.getProject().get(); Resource builtins = graph.getResource(Plant3D.URIs.Builtin); List actions = getItems(graph, project,type); actions.addAll(getItems(graph, builtins,type)); return actions; } } public static List getEnds() throws DatabaseException { return Simantics.getSession().syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); ItemQuery query = new ItemQuery(p3d.EndComponent); return graph.syncRequest(query); } }); } public static List getTurns() throws DatabaseException { return Simantics.getSession().syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); ItemQuery query = new ItemQuery(p3d.TurnComponent); return graph.syncRequest(query); } }); } public static List getInlines() throws DatabaseException { return Simantics.getSession().syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); ItemQuery query = new ItemQuery(p3d.InlineComponent); return graph.syncRequest(query); } }); } public static List filterUserComponents(List items) { List result = new ArrayList(items.size()); for (Item i : items) { if (!i.isCode()) result.add(i); } return result; } private static List getItems(ReadGraph graph, Resource lib, Resource type) throws DatabaseException{ Plant3D p3d = Plant3D.getInstance(graph); Layer0 l0 = Layer0.getInstance(graph); List result = new ArrayList(); for (Resource r : graph.getObjects(lib, l0.ConsistsOf)) { if (graph.isInstanceOf(r, type) ) { Resource geom = graph.getPossibleObject(r,p3d.hasGeometry); if (geom != null || graph.hasStatement(r,p3d.NonVisibleComponent)) { result.add(createItem(graph, r)); } } if (graph.isInheritedFrom(r, type)) { boolean asserts = false; for (Resource a : graph.getObjects(r, l0.Asserts)) { if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) { asserts = true; break; } } if (asserts) { result.add(createItem(graph, r)); } } } Collections.sort(result, new Comparator() { @Override public int compare(Item o1, Item o2) { return o1.getName().compareTo(o2.getName()); } }); return result; } public static Item createItem(ReadGraph graph, Resource r) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Plant3D p3d = Plant3D.getInstance(graph); String name = graph.getRelatedValue(r, l0.HasName); String uri = graph.getURI(r); Item item = new Item(uri, name); if (graph.isInstanceOf(r, p3d.Equipment)) item.setType(Type.EQUIPMENT); else if (graph.isInstanceOf(r, p3d.InlineComponent)) item.setType(Type.INLINE); else if (graph.isInstanceOf(r, p3d.EndComponent)) item.setType(Type.END); else if (graph.isInstanceOf(r, p3d.TurnComponent)) item.setType(Type.TURN); else if (graph.isInstanceOf(r, p3d.Nozzle)) item.setType(Type.NOZZLE); else throw new RuntimeException("Cannot detect type for " + r); if (graph.hasStatement(r, p3d.CodeComponent)) item.setCode(true); if (graph.hasStatement(r, p3d.VariableAngleTurnComponent) || graph.hasStatement(r, p3d.VariableLengthInlineComponent)) item.setVariable(true); if (graph.hasStatement(r, p3d.SizeChangeComponent)) item.setSizeChange(true); return item; } public static Resource createModel(WriteGraph graph, String name) throws DatabaseException{ Layer0 l0 = Layer0.getInstance(graph); Plant3D p3d = Plant3D.getInstance(graph); Resource model = graph.newResource(); graph.claim(model, l0.InstanceOf, p3d.Plant); graph.claimLiteral(model, l0.HasName, name); return model; } }