]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Enable model loading using an existing transaction. 03/3903/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 24 Feb 2020 14:56:00 +0000 (16:56 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Mon, 24 Feb 2020 14:56:22 +0000 (16:56 +0200)
gitlab #88

Change-Id: I1c594b284d716a9e428c04f0f3076afcd0001806

org.simantics.g3d/src/org/simantics/g3d/scl/ScriptNodeMap.java
org.simantics.plant3d/META-INF/MANIFEST.MF
org.simantics.plant3d/src/org/simantics/plant3d/dialog/ComponentSelectionDialog.java
org.simantics.plant3d/src/org/simantics/plant3d/dialog/EquipmentSelectionDialog.java
org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java
org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/ControlPointFactory.java
org.simantics.plant3d/src/org/simantics/plant3d/scl/P3DScriptNodeMap.java
org.simantics.plant3d/src/org/simantics/plant3d/scl/SCLUtil.java
org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java
org.simantics.plant3d/src/org/simantics/plant3d/utils/P3DUtil.java

index 3c1cd9f2cd235fed45870fed438ab23bfe97aed7..b7a7de03e8b52f244e42aa49f45b73e308f8c389 100644 (file)
@@ -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<DBObject,E extends INode> implements NodeMap
 
        private static final boolean DEBUG = false;
        
-       protected Session session;
+       protected RequestProcessor session;
        protected IMapping<DBObject,INode> mapping;
        
        protected ParentNode<E> rootNode;
@@ -55,7 +55,7 @@ public abstract class ScriptNodeMap<DBObject,E extends INode> implements NodeMap
        
        private boolean dirty = false;
        
-       public ScriptNodeMap(Session session, IMapping<DBObject,INode> mapping, ParentNode<E> rootNode) {
+       public ScriptNodeMap(RequestProcessor session, IMapping<DBObject,INode> mapping, ParentNode<E> rootNode) {
                this.session = session;
                this.mapping = mapping;
                this.rootNode = rootNode;
index 7b8a3b0de349f2987084862cdda1de731cdcc58d..aa6bfd98bae944347ed7c19f75c16b1a22ea8041 100644 (file)
@@ -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
index 35e761ccfa6eb436071352e6721b192e67255f76..030cce9f12d47c89c8d5d12dcea1a716372c60fb 100644 (file)
@@ -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<Item> 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;
                }
index 99ef9e7c1fa8f19516ae1764e30199ce75d72bbe..9b1bdee96e400b69479584bee6de3349d730777b 100644 (file)
@@ -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<Item> getItems(Class c, String libUri) throws DatabaseException{
+    protected List<Item> 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);
index 2c626c95a00dd91b46eaff7f98b808bbf694ec34..a7559f8009b35a599469a6d7d45cc7845b698a43 100644 (file)
@@ -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);
index ff49b93a74404adf1b5164e7bcbfb2a54ba00518..441d1276477c08c1c1b710ea456a210b27eefed4 100644 (file)
@@ -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<String,Instruction> cache = new HashMap<String, Instruction>();
        
        
-       public static void preloadCache(String libUri) throws Exception {
-               List<Item> 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<Item> 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<Instruction>() {
+       private static Instruction createInstruction(RequestProcessor session, final String type) throws Exception {
+               return session.syncRequest(new Read<Instruction>() {
                        @Override
                        public Instruction perform(ReadGraph graph) throws DatabaseException {
                                Resource res = graph.getResource(type);
index 21a77a6edf07a17a23e0f04c7a4c3752d2644b24..d38af1718b14068331192d82c5bf5aa203220c9e 100644 (file)
@@ -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<Resource,INode> {
        
        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);
        }
index e892106d08bd891ba88fdd1e5146dc7f3abf880e..6b1947d43e802e2b1a5609987e4b727cfd9ab454 100644 (file)
@@ -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 extends P3DScriptNodeMap> NodeMap load(final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<NodeMap>() {
+               return load(getRequestProcessor(), root, schemaProvider, mapProvider);
+       }
+       
+       public static <NodeMap extends P3DScriptNodeMap> NodeMap load(RequestProcessor session, final Resource root, SchemaProvider schemaProvider, NodeMapProvider mapProvider) throws DatabaseException {
+               return session.syncRequest(new Read<NodeMap>() {
                        @Override
                        public NodeMap perform(ReadGraph graph) throws DatabaseException {
                                PipingRules.setEnabled(false);
@@ -75,9 +89,9 @@ public class SCLUtil {
                });
        }
 
-       public static <Root extends P3DRootNode> Root loadReadOnly(final Resource root, SchemaProvider schemaProvider)
+       public static <Root extends P3DRootNode> Root loadReadOnly(RequestProcessor session, final Resource root, SchemaProvider schemaProvider)
                        throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<Root>() {
+               return session.syncRequest(new Read<Root>() {
                        @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 &lt;Proc&gt; or a &lt;ReadGraph&gt; function.
+        */
+       public static <Root extends P3DRootNode> 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();
+       }
+
 }
index c20cf92102a2262a7429c8b41fc7fffda3484af2..2cef7060c14ede3b748ddb821146d26d8bc6ecdb 100644 (file)
@@ -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<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
        private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
        private static Map<String,String> names = new HashMap<String,String>();
        
-       public static void preloadCache() {
-               Simantics.getSession().asyncRequest(new ReadRequest() {
-                       
-                       @Override
-                       public void run(ReadGraph graph) throws DatabaseException {
-                               List<String> types = new ArrayList<String>();
-                               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<String> types = new ArrayList<String>();
+                                       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 {
index c125f105c84d0dfa5eb3686303609456bf283ab5..2f5972a8d86a160dd4fd022fd93c605cc2706194 100644 (file)
@@ -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<Item> getEquipments() throws DatabaseException {
-               return getEquipments(Plant3D.URIs.Builtin);
+       public static List<Item> getEquipments(RequestProcessor session) throws DatabaseException {
+               return getEquipments(session, Plant3D.URIs.Builtin);
        }
        
-       public static List<Item> getEquipments(final String libUri) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getEquipments(RequestProcessor session, final String libUri) throws DatabaseException {
+               return session.syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
@@ -48,8 +49,8 @@ public class P3DUtil {
                });
        }
        
-       public static List<Item> getNozzles(String libUri) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getNozzles(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> 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<Item> getEnds(String libUri) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+
+       public static List<Item> getEnds(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
@@ -104,8 +105,8 @@ public class P3DUtil {
                });
        }
        
-       public static List<Item> getTurns(String libUri) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getTurns(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);
@@ -115,8 +116,8 @@ public class P3DUtil {
                });
        }
        
-       public static List<Item> getInlines(String libUri) throws DatabaseException {
-               return Simantics.getSession().syncRequest(new Read<List<Item>>() {
+       public static List<Item> getInlines(RequestProcessor session, String libUri) throws DatabaseException {
+               return session.syncRequest(new Read<List<Item>>() {
                        @Override
                        public List<Item> perform(ReadGraph graph) throws DatabaseException {
                                Plant3D p3d = Plant3D.getInstance(graph);