X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.testing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftesting%2Fcommon%2FTestingUtils.java;fp=bundles%2Forg.simantics.db.testing%2Fsrc%2Forg%2Fsimantics%2Fdb%2Ftesting%2Fcommon%2FTestingUtils.java;h=819be78076f965bc01575b8b871ad836d1bfdf2d;hb=67fd62f9c742337ec80eef658192db198a0efaac;hp=0000000000000000000000000000000000000000;hpb=cde82ba81327d5515fdca362f7f4c70f5103ae80;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/TestingUtils.java b/bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/TestingUtils.java new file mode 100644 index 000000000..819be7807 --- /dev/null +++ b/bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/TestingUtils.java @@ -0,0 +1,138 @@ +package org.simantics.db.testing.common; + +import java.io.File; + +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.BinaryRead; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor; +import org.simantics.db.layer0.request.ActivateModel; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.db.request.Read; +import org.simantics.graph.db.TransferableGraphs; +import org.simantics.graph.representation.Root; +import org.simantics.graph.representation.TransferableGraph1; +import org.simantics.graph.representation.TransferableGraphFileReader; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; + +public class TestingUtils { + + public static Resource importModel(final Resource toLocation, File fromFile, String withName) throws Exception { + + TransferableGraphFileReader importer = new TransferableGraphFileReader(fromFile); + TransferableGraph1 tg = importer.readTG(); + importer.close(); + + final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(toLocation) { + @Override + public void analyzeType(ReadGraph graph, Root root) throws DatabaseException { + Resource typeResource = graph.getResource(root.type); + if (graph.isInheritedFrom(typeResource, SimulationResource.getInstance(graph).Model)) { + library = toLocation; + } + } + }; + + TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor, null); + + Simantics.getSession().syncRequest(new WriteRequest() { + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + String name = graph.getPossibleRelatedValue(advisor.getRoot(), L0.HasName); + if (name != null) + graph.claimLiteral(advisor.getRoot(), L0.HasLabel, name); + graph.syncRequest(new ActivateModel(toLocation, advisor.getRoot())); + + } + }); + + return advisor.getRoot(); + + } + + public static Resource getResource(final Resource base, final String suffix) throws DatabaseException { + + return Simantics.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + String baseURI = graph.getURI(base); + return graph.getResource(baseURI + suffix); + } + + }); + + } + + public static double readConfiguration(Resource model, String rvi) throws DatabaseException { + + class ReadConfiguration extends BinaryRead { + + public ReadConfiguration(Resource model, String rvi) { + super(model, rvi); + } + + @Override + public Double perform(ReadGraph graph) throws DatabaseException { + Variable state = Variables.getVariable(graph, graph.getURI(parameter) + parameter2); + float result = state.getValue(graph); + return (double)result; + } + + } + + return Simantics.getSession().syncRequest(new ReadConfiguration(model, rvi)); + + } + + public static double readState(Resource run, String rvi) throws DatabaseException { + + class ReadState extends BinaryRead { + + public ReadState(Resource run, String rvi) { + super(run, rvi); + } + + @Override + public Double perform(ReadGraph graph) throws DatabaseException { + Variable state = Variables.getVariable(graph, graph.getURI(parameter) + parameter2); + float result = state.getValue(graph); + return (double)result; + } + + } + + return Simantics.getSession().syncRequest(new ReadState(run, rvi)); + + } + + public static boolean lineEquals(String s1, String s2) { + if(s1 == null) { + if(s2 == null) return true; + else return false; + } else { + if(s2 == null) return false; + else return s1.equals(s2); + } + } + + public static String compare(String s1, String s2) { + if(s1.equals(s2)) return null; + String[] lines1 = s1.split("\n"); + String[] lines2 = s2.split("\n"); + for(int i=0;i