1 package org.simantics.db.testing.common;
5 import org.simantics.Simantics;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.BinaryRead;
10 import org.simantics.db.common.request.WriteRequest;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.impl.DefaultPasteImportAdvisor;
13 import org.simantics.db.layer0.request.ActivateModel;
14 import org.simantics.db.layer0.variable.Variable;
15 import org.simantics.db.layer0.variable.Variables;
16 import org.simantics.db.request.Read;
17 import org.simantics.graph.db.TransferableGraphs;
18 import org.simantics.graph.representation.Root;
19 import org.simantics.graph.representation.TransferableGraph1;
20 import org.simantics.graph.representation.TransferableGraphFileReader;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.simulation.ontology.SimulationResource;
24 public class TestingUtils {
26 public static Resource importModel(final Resource toLocation, File fromFile, String withName) throws Exception {
28 TransferableGraphFileReader importer = new TransferableGraphFileReader(fromFile);
29 TransferableGraph1 tg = importer.readTG();
32 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(toLocation) {
34 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
35 Resource typeResource = graph.getResource(root.type);
36 if (graph.isInheritedFrom(typeResource, SimulationResource.getInstance(graph).Model)) {
42 TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor, null);
44 Simantics.getSession().syncRequest(new WriteRequest() {
46 public void perform(WriteGraph graph) throws DatabaseException {
47 Layer0 L0 = Layer0.getInstance(graph);
48 String name = graph.getPossibleRelatedValue(advisor.getRoot(), L0.HasName);
50 graph.claimLiteral(advisor.getRoot(), L0.HasLabel, name);
51 graph.syncRequest(new ActivateModel(toLocation, advisor.getRoot()));
56 return advisor.getRoot();
60 public static Resource getResource(final Resource base, final String suffix) throws DatabaseException {
62 return Simantics.getSession().syncRequest(new Read<Resource>() {
65 public Resource perform(ReadGraph graph) throws DatabaseException {
66 String baseURI = graph.getURI(base);
67 return graph.getResource(baseURI + suffix);
74 public static double readConfiguration(Resource model, String rvi) throws DatabaseException {
76 class ReadConfiguration extends BinaryRead<Resource, String, Double> {
78 public ReadConfiguration(Resource model, String rvi) {
83 public Double perform(ReadGraph graph) throws DatabaseException {
84 Variable state = Variables.getVariable(graph, graph.getURI(parameter) + parameter2);
85 float result = state.getValue(graph);
86 return (double)result;
91 return Simantics.getSession().syncRequest(new ReadConfiguration(model, rvi));
95 public static double readState(Resource run, String rvi) throws DatabaseException {
97 class ReadState extends BinaryRead<Resource, String, Double> {
99 public ReadState(Resource run, String rvi) {
104 public Double perform(ReadGraph graph) throws DatabaseException {
105 Variable state = Variables.getVariable(graph, graph.getURI(parameter) + parameter2);
106 float result = state.getValue(graph);
107 return (double)result;
112 return Simantics.getSession().syncRequest(new ReadState(run, rvi));
116 public static boolean lineEquals(String s1, String s2) {
118 if(s2 == null) return true;
121 if(s2 == null) return false;
122 else return s1.equals(s2);
126 public static String compare(String s1, String s2) {
127 if(s1.equals(s2)) return null;
128 String[] lines1 = s1.split("\n");
129 String[] lines2 = s2.split("\n");
130 for(int i=0;i<Math.max(lines1.length, lines2.length); i++) {
131 String t1 = (i < lines1.length) ? lines1[i] : null;
132 String t2 = (i < lines2.length) ? lines2[i] : null;
133 if(!lineEquals(t1, t2)) return "Difference at line " + (i+1) + ": '" + t1 + "' vs. '" + t2 + "'";