X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.tests%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftests%2Fcommands%2FUtils.java;fp=bundles%2Forg.simantics.modeling.tests%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftests%2Fcommands%2FUtils.java;h=29046cfe74c9bd17edadc9676341adb9ed6bc519;hb=ca6fcd858a0b82eb1b5a8f12c4d5df30e84393e0;hp=0000000000000000000000000000000000000000;hpb=67fd62f9c742337ec80eef658192db198a0efaac;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/Utils.java b/bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/Utils.java new file mode 100644 index 000000000..29046cfe7 --- /dev/null +++ b/bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/Utils.java @@ -0,0 +1,105 @@ +package org.simantics.modeling.tests.commands; + +import java.util.concurrent.TimeUnit; + +import org.simantics.Simantics; +import org.simantics.databoard.binding.Binding; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.EvaluatingListener; +import org.simantics.db.layer0.util.EvaluatingListener.Criterion; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.modeling.tests.traits.SingleResourceTrait; + + + +public class Utils { + + + public static void waitMapping() throws DatabaseException { + + sync(); + + } + + public static void sync() throws DatabaseException { + + // Multiple bugs here: + // -Model performs activation in separate write transactions because API does not support changing the virtual graph + // => activation & activation listener is delayed beyond this point + // -This should be fixed by the following code + // TransactionSupport ts = session.getService(TransactionSupport.class); + // ts.waitCompletion(); + // but unfortunately this does not work either... + // so we synchronize by a familiar write transaction + + + // And then wait still some more + for(int i=0;i<3;i++) { + Simantics.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + + } + + @Override + public String toString() { + return "Utils sync"; + } + + }); + + // And then wait still some more + Simantics.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + + } + + }); + } + + } + + public static void writeConfiguration(final Resource model, final String rvi, final Object value, final Binding binding) throws DatabaseException { + + class WriteConfiguration extends WriteRequest { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Variable state = Variables.getVariable(graph, graph.getURI(model) + rvi); + state.setValue(graph, value, binding); + } + + } + + Simantics.getSession().syncRequest(new WriteConfiguration()); + + } + + public static T readValue(final SingleResourceTrait run, final String rvi, final Criterion criterion) throws DatabaseException, InterruptedException { + + return EvaluatingListener.trySyncRequest(Simantics.getSession(), + new UniqueRead(){ + @SuppressWarnings("unchecked") + @Override + public T perform(ReadGraph graph) throws DatabaseException{ + Variable state = Variables.getPossibleVariable(graph, graph.getURI(run.getResource(graph)) + rvi); + return (T) (state != null ? state.getValue(graph) : null); + } + + }, + criterion, + 15, TimeUnit.SECONDS); + } + +} +