From: Antti Villberg Date: Mon, 9 Oct 2017 17:28:00 +0000 (+0300) Subject: Add support for dependency changes in SCL X-Git-Tag: v1.31.0~134 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c7adae2a1ad46c9f316093213df0320b44461606;p=simantics%2Fplatform.git Add support for dependency changes in SCL refs #7534 Change-Id: Ia5a28f64d5b9f3cec12642fcacd364db8e14db3a --- diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java index dc253137f..6b9b2fc65 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java @@ -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); + } + } diff --git a/bundles/org.simantics.modeling/scl/Simantics/Misc.scl b/bundles/org.simantics.modeling/scl/Simantics/Misc.scl index 1ab714b78..945e913a6 100644 --- a/bundles/org.simantics.modeling/scl/Simantics/Misc.scl +++ b/bundles/org.simantics.modeling/scl/Simantics/Misc.scl @@ -150,5 +150,8 @@ importJava "org.simantics.modeling.ModelingUtils" where untrackDependencies :: () trackOntologicalRequirements :: () untrackOntologicalRequirements :: () - + +importJava "org.simantics.modeling.scl.SCLDependencyChangeListener" where + @JavaName "create" + createDependencyChangeListener :: ( Boolean) -> (MetadataI -> DependencyChanges -> ()) -> 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 index 000000000..5e043d4a1 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/scl/SCLDependencyChangeListener.java @@ -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 { + + private Function1 preEventRequest; + private Function onEvent; + + public SCLDependencyChangeListener(Function1 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 preEventRequest, Function onEvent) { + return new SCLDependencyChangeListener(preEventRequest, onEvent); + } + +} \ No newline at end of file diff --git a/bundles/org.simantics.scl.db/scl/Simantics/DB.scl b/bundles/org.simantics.scl.db/scl/Simantics/DB.scl index a97d573a3..cd0242be7 100644 --- a/bundles/org.simantics.scl.db/scl/Simantics/DB.scl +++ b/bundles/org.simantics.scl.db/scl/Simantics/DB.scl @@ -368,6 +368,9 @@ importJava "org.simantics.db.layer0.util.Layer0Utils" where possiblePredicateByName :: Resource -> String -> Maybe Resource @JavaName getPossiblePredicateByNameFromType possiblePredicateByNameFromType :: Resource -> String -> Maybe Resource + + addMetadataListener :: ChangeListener -> () + removeMetadataListener :: ChangeListener -> () copyTo :: Resource -> Resource -> 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 diff --git a/bundles/org.simantics/src/org/simantics/Simantics.java b/bundles/org.simantics/src/org/simantics/Simantics.java index 9a1a354f1..b382fba91 100644 --- a/bundles/org.simantics/src/org/simantics/Simantics.java +++ b/bundles/org.simantics/src/org/simantics/Simantics.java @@ -460,20 +460,30 @@ public class Simantics { @SuppressWarnings({ "unchecked", "rawtypes" }) - public static 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 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 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")