From d207cfa3235339e7d7b894494be400160b121bd5 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Mon, 24 Feb 2020 16:56:00 +0200 Subject: [PATCH] Enable model loading using an existing transaction. gitlab #88 Change-Id: I1c594b284d716a9e428c04f0f3076afcd0001806 --- .../org/simantics/g3d/scl/ScriptNodeMap.java | 6 +-- org.simantics.plant3d/META-INF/MANIFEST.MF | 1 + .../dialog/ComponentSelectionDialog.java | 9 ++-- .../dialog/EquipmentSelectionDialog.java | 20 +++----- .../plant3d/editor/Plant3DEditor.java | 7 +-- .../controlpoint/ControlPointFactory.java | 19 +++---- .../plant3d/scl/P3DScriptNodeMap.java | 4 +- .../org/simantics/plant3d/scl/SCLUtil.java | 49 +++++++++++++++---- .../plant3d/utils/ComponentUtils.java | 42 +++++++++------- .../org/simantics/plant3d/utils/P3DUtil.java | 27 +++++----- 10 files changed, 110 insertions(+), 74 deletions(-) diff --git a/org.simantics.g3d/src/org/simantics/g3d/scl/ScriptNodeMap.java b/org.simantics.g3d/src/org/simantics/g3d/scl/ScriptNodeMap.java index 3c1cd9f2..b7a7de03 100644 --- a/org.simantics.g3d/src/org/simantics/g3d/scl/ScriptNodeMap.java +++ b/org.simantics.g3d/src/org/simantics/g3d/scl/ScriptNodeMap.java @@ -11,7 +11,7 @@ import java.util.Set; import java.util.Stack; import org.simantics.db.ReadGraph; -import org.simantics.db.Session; +import org.simantics.db.RequestProcessor; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; @@ -46,7 +46,7 @@ public abstract class ScriptNodeMap implements NodeMap private static final boolean DEBUG = false; - protected Session session; + protected RequestProcessor session; protected IMapping mapping; protected ParentNode rootNode; @@ -55,7 +55,7 @@ public abstract class ScriptNodeMap implements NodeMap private boolean dirty = false; - public ScriptNodeMap(Session session, IMapping mapping, ParentNode rootNode) { + public ScriptNodeMap(RequestProcessor session, IMapping mapping, ParentNode rootNode) { this.session = session; this.mapping = mapping; this.rootNode = rootNode; diff --git a/org.simantics.plant3d/META-INF/MANIFEST.MF b/org.simantics.plant3d/META-INF/MANIFEST.MF index 7b8a3b0d..aa6bfd98 100644 --- a/org.simantics.plant3d/META-INF/MANIFEST.MF +++ b/org.simantics.plant3d/META-INF/MANIFEST.MF @@ -50,3 +50,4 @@ Export-Package: org.simantics.plant3d.actions, org.simantics.plant3d.scl, org.simantics.plant3d.utils Automatic-Module-Name: org.simantics.plant3d +Import-Package: org.slf4j diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java index 35e761cc..030cce9f 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java @@ -34,6 +34,8 @@ import org.eclipse.swt.widgets.ExpandItem; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; +import org.simantics.Simantics; +import org.simantics.db.Session; import org.simantics.db.exception.DatabaseException; import org.simantics.plant3d.Activator; import org.simantics.plant3d.ontology.Plant3D; @@ -149,12 +151,13 @@ public class ComponentSelectionDialog extends Dialog implements ISelectionChange } protected List getItems(Class c, String libUri) throws DatabaseException { + Session session = Simantics.getSession(); if (InlineComponent.class.equals(c)) { - return P3DUtil.getInlines(libUri); + return P3DUtil.getInlines(session, libUri); } else if (TurnComponent.class.equals(c)) { - return P3DUtil.getTurns(libUri); + return P3DUtil.getTurns(session, libUri); } else if (EndComponent.class.equals(c)) { - return P3DUtil.getEnds(libUri); + return P3DUtil.getEnds(session, libUri); } else { return null; } diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java index 99ef9e7c..9b1bdee9 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java @@ -1,15 +1,12 @@ package org.simantics.plant3d.dialog; -import java.util.HashSet; import java.util.List; -import java.util.Set; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.resource.LocalResourceManager; -import org.eclipse.jface.resource.ResourceManager; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.viewers.ListViewer; @@ -23,23 +20,20 @@ import org.eclipse.swt.widgets.ExpandBar; import org.eclipse.swt.widgets.ExpandItem; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; +import org.simantics.Simantics; +import org.simantics.db.Session; import org.simantics.db.exception.DatabaseException; import org.simantics.plant3d.ontology.Plant3D; import org.simantics.plant3d.scenegraph.Equipment; -import org.simantics.plant3d.scenegraph.InlineComponent; import org.simantics.plant3d.scenegraph.Nozzle; import org.simantics.plant3d.scenegraph.P3DRootNode; -import org.simantics.plant3d.scenegraph.PipelineComponent; -import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType; import org.simantics.plant3d.utils.Item; import org.simantics.plant3d.utils.P3DUtil; import org.simantics.utils.ui.ExceptionUtils; public class EquipmentSelectionDialog extends Dialog implements ISelectionChangedListener{ - private ResourceManager resourceManager; private String libUri; - private P3DRootNode root; private Item selected; private Item selectedNozzle; @@ -53,23 +47,21 @@ public class EquipmentSelectionDialog extends Dialog implements ISelectionChange public EquipmentSelectionDialog(Shell parentShell, P3DRootNode root, String libUri){ super(parentShell); - this.root = root; this.libUri = libUri; } - protected List getItems(Class c, String libUri) throws DatabaseException{ + protected List getItems(Class c, String libUri) throws DatabaseException{ + Session session = Simantics.getSession(); if (Equipment.class.equals(c)) { - return P3DUtil.getEquipments(libUri); + return P3DUtil.getEquipments(session, libUri); } else if (Nozzle.class.equals(c)) { - return P3DUtil.getNozzles(libUri); + return P3DUtil.getNozzles(session, libUri); } return null; } @Override protected Control createDialogArea(Composite parent) { - resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent); - Composite composite = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(2,false); layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java index 2c626c95..a7559f80 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java @@ -19,6 +19,7 @@ import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Menu; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; +import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -183,8 +184,8 @@ public class Plant3DEditor extends ResourceEditorPart { panel.setPickType(4); try { - ControlPointFactory.preloadCache(getLibraryUri()); - ComponentUtils.preloadCache(); + ControlPointFactory.preloadCache(Simantics.getSession(), getLibraryUri()); + ComponentUtils.preloadCache(Simantics.getSession()); } catch (Exception e) { ExceptionUtils.logAndShowError("Cannot open Plant3D editor",e); return; @@ -449,7 +450,7 @@ public class Plant3DEditor extends ResourceEditorPart { if (node instanceof Equipment) { m.add(translateAction); m.add(rotateAction); - for (Item eq : P3DUtil.getNozzles(getLibraryUri())) { + for (Item eq : P3DUtil.getNozzles(Simantics.getSession(), getLibraryUri())) { AddNozzleAction add = new AddNozzleAction(rootNode, eq); add.setEquipment((Equipment)node); m.add(add); diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java index ff49b93a..441d1276 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java @@ -6,6 +6,7 @@ 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; @@ -20,14 +21,14 @@ public class ControlPointFactory { private static Map cache = new HashMap(); - public static void preloadCache(String libUri) throws Exception { - List items = P3DUtil.getEnds(libUri); - items.addAll(P3DUtil.getInlines(libUri)); - items.addAll(P3DUtil.getNozzles(libUri)); - items.addAll(P3DUtil.getTurns(libUri)); + 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(item.getUri()); + Instruction inst = createInstruction(session, item.getUri()); cache.put(item.getUri(), inst); } } @@ -35,7 +36,7 @@ public class ControlPointFactory { public static PipeControlPoint create(PipelineComponent component) throws Exception { Instruction inst = cache.get(component.getType()); if (inst == null) { - inst = createInstruction(component.getType()); + inst = createInstruction(Simantics.getSession(), component.getType()); cache.put(component.getType(), inst); } if (inst == null) { @@ -88,8 +89,8 @@ public class ControlPointFactory { } - private static Instruction createInstruction(final String type) throws Exception { - return Simantics.getSession().syncRequest(new Read() { + 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); diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scl/P3DScriptNodeMap.java b/org.simantics.plant3d/src/org/simantics/plant3d/scl/P3DScriptNodeMap.java index 21a77a6e..d38af171 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scl/P3DScriptNodeMap.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scl/P3DScriptNodeMap.java @@ -4,8 +4,8 @@ import java.util.HashSet; import java.util.Set; import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; -import org.simantics.db.Session; import org.simantics.db.exception.DatabaseException; import org.simantics.g3d.ontology.G3D; import org.simantics.g3d.scenegraph.base.INode; @@ -26,7 +26,7 @@ public class P3DScriptNodeMap extends ScriptNodeMap { private static final boolean DEBUG = false; - public P3DScriptNodeMap(Session session, IMapping mapping, P3DRootNode rootNode) { + public P3DScriptNodeMap(RequestProcessor session, IMapping mapping, P3DRootNode rootNode) { super(session,mapping,rootNode); rootNode.setMapping(mapping); } diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java b/org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java index e892106d..6b1947d4 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java @@ -2,6 +2,7 @@ package org.simantics.plant3d.scl; 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; @@ -16,6 +17,7 @@ import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory; import org.simantics.plant3d.scenegraph.controlpoint.PipingRules; import org.simantics.plant3d.utils.ComponentUtils; import org.simantics.plant3d.utils.P3DUtil; +import org.simantics.scl.runtime.SCLContext; public class SCLUtil { @@ -28,31 +30,43 @@ public class SCLUtil { } public static P3DScriptNodeMap load(final Resource root) throws DatabaseException { - preload(Plant3D.URIs.Builtin); + return load(getRequestProcessor(), root); + } + + public static P3DScriptNodeMap load(RequestProcessor session, final Resource root) throws DatabaseException { + preload(Simantics.getSession(), Plant3D.URIs.Builtin); SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g); NodeMapProvider mapProvider = (mapping, rootNode) -> new P3DScriptNodeMap(Simantics.getSession(), mapping, rootNode); - return load(root, schemaProvider, mapProvider); + return load(session, root, schemaProvider, mapProvider); } public static P3DRootNode loadReadOnly(final Resource root) throws DatabaseException { - preload(Plant3D.URIs.Builtin); + return loadReadOnly(getRequestProcessor(), root); + } + + public static P3DRootNode loadReadOnly(RequestProcessor session, final Resource root) throws DatabaseException { + preload(session, Plant3D.URIs.Builtin); SchemaProvider schemaProvider = g -> SchemaBuilder.getSchema(g); - return loadReadOnly(root, schemaProvider); + return loadReadOnly(session, root, schemaProvider); } - public static void preload(String symbolSet) throws DatabaseException { + public static void preload(RequestProcessor session, String symbolSet) throws DatabaseException { try { - ControlPointFactory.preloadCache(symbolSet); - ComponentUtils.preloadCache(); + ControlPointFactory.preloadCache(session, symbolSet); + ComponentUtils.preloadCache(session); } catch (Exception e) { throw new DatabaseException(e); } } public static NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read() { + return load(getRequestProcessor(), root, schemaProvider, mapProvider); + } + + public static NodeMap load(RequestProcessor session, final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException { + return session.syncRequest(new Read() { @Override public NodeMap perform(ReadGraph graph) throws DatabaseException { PipingRules.setEnabled(false); @@ -75,9 +89,9 @@ public class SCLUtil { }); } - public static Root loadReadOnly(final Resource root, SchemaProvider schemaProvider) + public static Root loadReadOnly(RequestProcessor session, final Resource root, SchemaProvider schemaProvider) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read() { + return session.syncRequest(new Read() { @Override public Root perform(ReadGraph graph) throws DatabaseException { PipingRules.setEnabled(false); @@ -98,4 +112,19 @@ public class SCLUtil { }); } + /** + * Load a read-only Java object representation of a Plant3d model. + * + * This method can be called from SCL either as a <Proc> or a <ReadGraph> function. + */ + public static Root loadReadOnly(final Resource root, SchemaProvider schemaProvider) + throws DatabaseException { + return loadReadOnly(getRequestProcessor(), root, schemaProvider); + } + + public static RequestProcessor getRequestProcessor() { + RequestProcessor rp = (RequestProcessor) SCLContext.getCurrent().get("graph"); + return rp != null ? rp : Simantics.getSession(); + } + } diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java index c20cf921..2cef7060 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java @@ -11,6 +11,7 @@ import javax.vecmath.Vector3d; import org.simantics.Simantics; import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.utils.NameUtils; @@ -32,34 +33,41 @@ import org.simantics.plant3d.scenegraph.TurnComponent; import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint; import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction; import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.simantics.plant3d.scenegraph.controlpoint.PipingRules; public class ComponentUtils { + private final static Logger LOGGER = LoggerFactory.getLogger(ComponentUtils.class); private static Map> clazzes = new HashMap>(); private static Map providers = new HashMap(); private static Map names = new HashMap(); - public static void preloadCache() { - Simantics.getSession().asyncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - List types = new ArrayList(); - types.add(Plant3D.URIs.Builtin_Straight); - types.add(Plant3D.URIs.Builtin_Elbow); - types.add(Plant3D.URIs.Builtin_ConcentricReducer); - types.add(Plant3D.URIs.Builtin_BranchSplitComponent); - types.add(Plant3D.URIs.Builtin_EccentricReducer); - types.add(Plant3D.URIs.Builtin_Elbow45); - types.add(Plant3D.URIs.Builtin_Elbow90); + public static void preloadCache(RequestProcessor session) { + try { + session.syncRequest(new ReadRequest() { - for (String typeURI : types) { - load(graph, typeURI); + @Override + public void run(ReadGraph graph) throws DatabaseException { + List types = new ArrayList(); + types.add(Plant3D.URIs.Builtin_Straight); + types.add(Plant3D.URIs.Builtin_Elbow); + types.add(Plant3D.URIs.Builtin_ConcentricReducer); + types.add(Plant3D.URIs.Builtin_BranchSplitComponent); + types.add(Plant3D.URIs.Builtin_EccentricReducer); + types.add(Plant3D.URIs.Builtin_Elbow45); + types.add(Plant3D.URIs.Builtin_Elbow90); + + for (String typeURI : types) { + load(graph, typeURI); + } } - } - }); + }); + } catch (DatabaseException e) { + LOGGER.error("ComponentUtils.preloadCache() failed unexpectedly", e); + } } private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException { diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java b/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java index c125f105..2f5972a8 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java @@ -11,6 +11,7 @@ import java.util.Set; import org.simantics.Simantics; import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; @@ -28,12 +29,12 @@ import org.simantics.plant3d.utils.Item.Type; public class P3DUtil { - public static List getEquipments() throws DatabaseException { - return getEquipments(Plant3D.URIs.Builtin); + public static List getEquipments(RequestProcessor session) throws DatabaseException { + return getEquipments(session, Plant3D.URIs.Builtin); } - public static List getEquipments(final String libUri) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getEquipments(RequestProcessor session, final String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); @@ -48,8 +49,8 @@ public class P3DUtil { }); } - public static List getNozzles(String libUri) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getNozzles(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); @@ -92,9 +93,9 @@ public class P3DUtil { return type.hashCode() + libUri.hashCode(); } } - - public static List getEnds(String libUri) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + + public static List getEnds(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); @@ -104,8 +105,8 @@ public class P3DUtil { }); } - public static List getTurns(String libUri) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getTurns(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); @@ -115,8 +116,8 @@ public class P3DUtil { }); } - public static List getInlines(String libUri) throws DatabaseException { - return Simantics.getSession().syncRequest(new Read>() { + public static List getInlines(RequestProcessor session, String libUri) throws DatabaseException { + return session.syncRequest(new Read>() { @Override public List perform(ReadGraph graph) throws DatabaseException { Plant3D p3d = Plant3D.getInstance(graph); -- 2.47.1