]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/TestingUtils.java
Finalizing improve startup time for fresh or rollback'd session
[simantics/platform.git] / bundles / org.simantics.db.testing / src / org / simantics / db / testing / common / TestingUtils.java
1 package org.simantics.db.testing.common;
2
3 import java.io.File;
4
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;
23
24 public class TestingUtils {
25
26         public static Resource importModel(final Resource toLocation, File fromFile, String withName) throws Exception {
27                 
28         TransferableGraph1 tg = TransferableGraphFileReader.read(fromFile);
29
30         final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(toLocation) {
31             @Override
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)) {
35                     library = toLocation;
36                 }
37             }
38         };
39         
40         TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor, null);
41         
42         Simantics.getSession().syncRequest(new WriteRequest() {
43             @Override
44             public void perform(WriteGraph graph) throws DatabaseException {
45                 Layer0 L0 = Layer0.getInstance(graph);
46                 String name = graph.getPossibleRelatedValue(advisor.getRoot(), L0.HasName);
47                 if (name != null)
48                     graph.claimLiteral(advisor.getRoot(), L0.HasLabel, name);
49                 graph.syncRequest(new ActivateModel(toLocation, advisor.getRoot()));
50                 
51             }
52         });
53         
54         return advisor.getRoot();
55                 
56         }
57         
58         public static Resource getResource(final Resource base, final String suffix) throws DatabaseException {
59             
60             return Simantics.getSession().syncRequest(new Read<Resource>() {
61
62             @Override
63             public Resource perform(ReadGraph graph) throws DatabaseException {
64                 String baseURI = graph.getURI(base);
65                 return graph.getResource(baseURI + suffix);
66             }
67                 
68             });
69             
70         }
71         
72         public static double readConfiguration(Resource model, String rvi) throws DatabaseException {
73             
74             class ReadConfiguration extends BinaryRead<Resource, String, Double> {
75                 
76                 public ReadConfiguration(Resource model, String rvi) {
77                     super(model, rvi);
78                 }
79
80                 @Override
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;
85                 }
86                 
87             }
88             
89             return Simantics.getSession().syncRequest(new ReadConfiguration(model, rvi));
90             
91         }
92         
93     public static double readState(Resource run, String rvi) throws DatabaseException {
94         
95         class ReadState extends BinaryRead<Resource, String, Double> {
96             
97             public ReadState(Resource run, String rvi) {
98                 super(run, rvi);
99             }
100
101             @Override
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;
106             }
107             
108         }
109         
110         return Simantics.getSession().syncRequest(new ReadState(run, rvi));
111         
112     }
113
114         public static boolean lineEquals(String s1, String s2) {
115                 if(s1 == null) {
116                         if(s2 == null) return true;
117                         else return false;
118                 } else {
119                         if(s2 == null) return false;
120                         else return s1.equals(s2);
121                 }
122         }
123         
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 + "'";
132                 }
133                 return null;
134         }
135         
136 }