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 TransferableGraph1 tg = TransferableGraphFileReader.read(fromFile);
30 final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(toLocation) {
32 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
33 Resource typeResource = graph.getResource(root.type);
34 if (graph.isInheritedFrom(typeResource, SimulationResource.getInstance(graph).Model)) {
40 TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor, null);
42 Simantics.getSession().syncRequest(new WriteRequest() {
44 public void perform(WriteGraph graph) throws DatabaseException {
45 Layer0 L0 = Layer0.getInstance(graph);
46 String name = graph.getPossibleRelatedValue(advisor.getRoot(), L0.HasName);
48 graph.claimLiteral(advisor.getRoot(), L0.HasLabel, name);
49 graph.syncRequest(new ActivateModel(toLocation, advisor.getRoot()));
54 return advisor.getRoot();
58 public static Resource getResource(final Resource base, final String suffix) throws DatabaseException {
60 return Simantics.getSession().syncRequest(new Read<Resource>() {
63 public Resource perform(ReadGraph graph) throws DatabaseException {
64 String baseURI = graph.getURI(base);
65 return graph.getResource(baseURI + suffix);
72 public static double readConfiguration(Resource model, String rvi) throws DatabaseException {
74 class ReadConfiguration extends BinaryRead<Resource, String, Double> {
76 public ReadConfiguration(Resource model, String rvi) {
81 public Double perform(ReadGraph graph) throws DatabaseException {
82 Variable state = Variables.getVariable(graph, graph.getURI(parameter) + parameter2);
83 float result = state.getValue(graph);
84 return (double)result;
89 return Simantics.getSession().syncRequest(new ReadConfiguration(model, rvi));
93 public static double readState(Resource run, String rvi) throws DatabaseException {
95 class ReadState extends BinaryRead<Resource, String, Double> {
97 public ReadState(Resource run, String rvi) {
102 public Double perform(ReadGraph graph) throws DatabaseException {
103 Variable state = Variables.getVariable(graph, graph.getURI(parameter) + parameter2);
104 float result = state.getValue(graph);
105 return (double)result;
110 return Simantics.getSession().syncRequest(new ReadState(run, rvi));
114 public static boolean lineEquals(String s1, String s2) {
116 if(s2 == null) return true;
119 if(s2 == null) return false;
120 else return s1.equals(s2);
124 public static String compare(String s1, String s2) {
125 if(s1.equals(s2)) return null;
126 String[] lines1 = s1.split("\n");
127 String[] lines2 = s2.split("\n");
128 for(int i=0;i<Math.max(lines1.length, lines2.length); i++) {
129 String t1 = (i < lines1.length) ? lines1[i] : null;
130 String t2 = (i < lines2.length) ? lines2[i] : null;
131 if(!lineEquals(t1, t2)) return "Difference at line " + (i+1) + ": '" + t1 + "' vs. '" + t2 + "'";