package org.simantics.plant3d.scenegraph.controlpoint; import java.util.HashMap; import java.util.List; import java.util.Map; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Read; import org.simantics.plant3d.ontology.Plant3D; import org.simantics.plant3d.scenegraph.PipelineComponent; import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PointType; import org.simantics.plant3d.utils.Item; import org.simantics.plant3d.utils.P3DUtil; public class ControlPointFactory { private static Map cache = new HashMap(); public static void preloadCache(RequestProcessor session, String libUri) throws Exception { List items = P3DUtil.getEnds(session, libUri); items.addAll(P3DUtil.getInlines(session, libUri)); items.addAll(P3DUtil.getNozzles(session, libUri)); items.addAll(P3DUtil.getTurns(session, libUri)); for (Item item : items) { Instruction inst = createInstruction(session, item.getUri()); cache.put(item.getUri(), inst); } } public static PipeControlPoint create(PipelineComponent component) throws Exception { Instruction inst = cache.get(component.getType()); if (inst == null) { inst = createInstruction(Simantics.getSession(), component.getType()); cache.put(component.getType(), inst); } if (inst == null) { return null; } PipeControlPoint pcp = new PipeControlPoint(component); pcp.setType(inst.type); pcp.setFixed(inst.fixed); pcp.setMod(inst.mod); pcp.setRotate(inst.isRotate); pcp.setReverse(inst.isReverse); switch(inst.type) { case END: break; case INLINE: if (inst.isOffset || inst.isSizeChange) { PipeControlPoint sub = new PipeControlPoint(component); pcp.children.add(sub); sub.parent = pcp; sub.setType(inst.type); sub.setFixed(inst.fixed); sub.setMod(inst.mod); sub.setSub(true); sub.setDeletable(false); if (inst.isOffset) pcp.setOffset(0.0); if (inst.isSizeChange) pcp.setSizeChange(true); } break; case TURN: break; } return pcp; } private static class Instruction { PointType type; boolean fixed; boolean mod; boolean isOffset; boolean isSizeChange; boolean isRotate; boolean isReverse; } private static Instruction createInstruction(RequestProcessor session, final String type) throws Exception { return session.syncRequest(new Read() { @Override public Instruction perform(ReadGraph graph) throws DatabaseException { Resource res = graph.getResource(type); Plant3D p3d = Plant3D.getInstance(graph); Instruction i = new Instruction(); i.fixed = false; i.isOffset = false; i.isSizeChange = false; i.isRotate = false; i.isReverse = false; i.type = PointType.INLINE; i.mod = false; if (graph.isInheritedFrom(res, p3d.Nozzle)) { i.fixed = true; i.isOffset = false; i.isSizeChange = false; i.type = PointType.END; } else if (graph.isInheritedFrom(res, p3d.InlineComponent)){ i.type = PointType.INLINE; if (graph.hasStatement(res,p3d.VariableLengthInlineComponent)) { i.fixed = false; } else if (graph.hasStatement(res,p3d.FixedLengthInlineComponent)) { i.fixed = true; } else if (graph.hasStatement(res,p3d.AdjustableLengthInlineComponent)) { i.fixed = true; i.mod = true; } else { throw new DatabaseException("Inline component type " + res + " does not have length configuration."); } if (graph.hasStatement(res,p3d.SizeChangeComponent)) { i.isSizeChange = true; } if (graph.hasStatement(res,p3d.OffsetComponent)) { i.isOffset = true; } if (graph.hasStatement(res,p3d.RotateComponent)) { i.isRotate = true; } if (graph.hasStatement(res,p3d.ReverseComponent)) { i.isReverse = true; } } else if (graph.isInheritedFrom(res, p3d.TurnComponent)) { i.type = PointType.TURN; if (graph.hasStatement(res,p3d.VariableAngleTurnComponent)) { i.fixed = false; } else if (graph.hasStatement(res,p3d.FixedAngleTurnComponent)) { i.fixed = true; } else { throw new DatabaseException("Turn component type " + res + " does not have angle configuration."); } } else if (graph.isInheritedFrom(res, p3d.EndComponent)) { i.fixed = false; i.type = PointType.END; } else { return null; } return i; } }); } }