]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Add support for dependency changes in SCL 87/1087/3
authorAntti Villberg <antti.villberg@semantum.fi>
Mon, 9 Oct 2017 17:28:00 +0000 (20:28 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 9 Oct 2017 20:30:43 +0000 (23:30 +0300)
refs #7534

Change-Id: Ia5a28f64d5b9f3cec12642fcacd364db8e14db3a

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java
bundles/org.simantics.modeling/scl/Simantics/Misc.scl
bundles/org.simantics.modeling/src/org/simantics/modeling/scl/SCLDependencyChangeListener.java [new file with mode: 0644]
bundles/org.simantics.scl.db/scl/Simantics/DB.scl
bundles/org.simantics/src/org/simantics/Simantics.java

index dc253137f7a74fc3a5cda7783c46706c8bec265c..6b9b2fc6502845763871aa1383d8d8a1835ef33b 100644 (file)
@@ -74,6 +74,7 @@ import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.PossibleChild;
 import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.event.ChangeListener;
 import org.simantics.db.exception.CancelTransactionException;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ServiceException;
@@ -99,6 +100,7 @@ import org.simantics.db.service.ClusterControl;
 import org.simantics.db.service.ClusteringSupport;
 import org.simantics.db.service.CollectionSupport;
 import org.simantics.db.service.DebugSupport;
+import org.simantics.db.service.GraphChangeListenerSupport;
 import org.simantics.db.service.ManagementSupport;
 import org.simantics.db.service.UndoRedoSupport;
 import org.simantics.db.service.XSupport;
@@ -1435,4 +1437,13 @@ public class Layer0Utils {
         }
         return false;
     }
+
+    public static void addMetadataListener(ChangeListener listener) {
+        SimanticsInternal.getSession().getService(GraphChangeListenerSupport.class).addMetadataListener(listener);
+    }
+
+    public static void removeMetadataListener(ChangeListener listener) {
+        SimanticsInternal.getSession().getService(GraphChangeListenerSupport.class).removeMetadataListener(listener);
+    }
+
 }
index 1ab714b789b9e635eb8ec8ab874daddfff8b1449..945e913a69fd71b925581eede8f4a76ad3999387 100644 (file)
@@ -150,5 +150,8 @@ importJava "org.simantics.modeling.ModelingUtils" where
     untrackDependencies :: <Proc> ()
     trackOntologicalRequirements :: <Proc> ()
     untrackOntologicalRequirements :: <Proc> ()
-    
+
+importJava "org.simantics.modeling.scl.SCLDependencyChangeListener" where
+    @JavaName "create"
+    createDependencyChangeListener :: (<Proc> Boolean) -> (MetadataI -> DependencyChanges -> <ReadGraph,Proc> ()) -> ChangeListener
     
\ No newline at end of file
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/SCLDependencyChangeListener.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/SCLDependencyChangeListener.java
new file mode 100644 (file)
index 0000000..5e043d4
--- /dev/null
@@ -0,0 +1,40 @@
+package org.simantics.modeling.scl;
+
+import org.simantics.Simantics;
+import org.simantics.db.MetadataI;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.changeset.GenericChangeListener;
+import org.simantics.db.event.ChangeListener;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest;
+import org.simantics.db.layer0.genericrelation.DependencyChanges;
+import org.simantics.scl.runtime.function.Function;
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple0;
+
+public class SCLDependencyChangeListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
+
+    private Function1<Tuple0, Boolean> preEventRequest;
+    private Function onEvent;
+
+    public SCLDependencyChangeListener(Function1<Tuple0, Boolean> preEventRequest, Function onEvent) {
+        this.preEventRequest = preEventRequest;
+        this.onEvent = onEvent;
+    }
+
+    @Override
+    public boolean preEventRequest() {
+        return preEventRequest.apply(Tuple0.INSTANCE);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
+        Simantics.applySCLRead(graph, onEvent, metadata, event);
+    }
+
+    public static ChangeListener create(Function1<Tuple0, Boolean> preEventRequest, Function onEvent) {
+        return new SCLDependencyChangeListener(preEventRequest, onEvent);
+    }
+
+}
\ No newline at end of file
index a97d573a3c97950c42578d79bd5813ca3f277ec3..cd0242be78426791570a52f794b1065486e5ffef 100644 (file)
@@ -368,6 +368,9 @@ importJava "org.simantics.db.layer0.util.Layer0Utils" where
     possiblePredicateByName :: Resource -> String -> <ReadGraph> Maybe Resource
     @JavaName getPossiblePredicateByNameFromType
     possiblePredicateByNameFromType :: Resource -> String -> <ReadGraph> Maybe Resource
+    
+    addMetadataListener :: ChangeListener -> <Proc> ()
+    removeMetadataListener :: ChangeListener -> <Proc> ()
 
 copyTo :: Resource -> Resource -> <WriteGraph> Resource
 copyTo targetContainer source = do
@@ -496,4 +499,13 @@ possibleChildWithPath parent path =
               Just c -> possibleChild c name
               Nothing -> Nothing 
           ) 
-          (Just parent) path
\ No newline at end of file
+          (Just parent) path
+
+importJava "org.simantics.db.MetadataI" where
+    data MetadataI
+
+importJava "org.simantics.db.event.ChangeListener" where
+    data ChangeListener
+
+importJava "org.simantics.db.layer0.genericrelation.DependencyChanges" where
+    data DependencyChanges
index 9a1a354f17131b35e1fbfd7bfcc0565645b95616..b382fba9151798aa6f414b6b1f65071ebcc21212 100644 (file)
@@ -460,20 +460,30 @@ public class Simantics {
 
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
-       public static <T> T applySCL(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
-        SCLContext sclContext = SCLContext.getCurrent();
-           Object oldGraph = sclContext.put("graph", graph);
-               try {
-                       T t = (T)((Function)SCLOsgi.MODULE_REPOSITORY.getValue(module, function)).applyArray(args);
-                       return t;
-               } catch (ValueNotFound e) {
-                       throw new DatabaseException("SCL Value not found: " + e.name);
-               } catch (Throwable t) {
-                       throw new DatabaseException(t);
-               } finally {
-                       sclContext.put("graph", oldGraph);
-               }
+    public static <T> T applySCL(String module, String function, Object ... args) throws DatabaseException {
+        try {
+            T t = (T)((Function)SCLOsgi.MODULE_REPOSITORY.getValue(module, function)).applyArray(args);
+            return t;
+        } catch (ValueNotFound e) {
+            throw new DatabaseException("SCL Value not found: " + e.name);
+        } catch (Throwable t) {
+            throw new DatabaseException(t);
+        }
+    }
 
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public static <T> T applySCL(String module, String function, ReadGraph graph, Object ... args) throws DatabaseException {
+        SCLContext sclContext = SCLContext.getCurrent();
+        Object oldGraph = sclContext.put("graph", graph);
+        try {
+            return applySCL(module, function, args);
+        } catch (DatabaseException dbe) {
+            throw dbe;
+        } catch (Throwable t) {
+            throw new DatabaseException(t);
+        } finally {
+            sclContext.put("graph", oldGraph);
+        }
     }
 
     @SuppressWarnings("unchecked")