]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.testing/src/org/simantics/db/testing/common/TestingUtils.java
Minor fix and optimization for exporting TG and Pgraph with sharedlib
[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         TransferableGraphFileReader importer = new TransferableGraphFileReader(fromFile);
29         TransferableGraph1 tg = importer.readTG();
30         importer.close();
31
32         final DefaultPasteImportAdvisor advisor = new DefaultPasteImportAdvisor(toLocation) {
33             @Override
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)) {
37                     library = toLocation;
38                 }
39             }
40         };
41         
42         TransferableGraphs.importGraph1(Simantics.getSession(), tg, advisor, null);
43         
44         Simantics.getSession().syncRequest(new WriteRequest() {
45             @Override
46             public void perform(WriteGraph graph) throws DatabaseException {
47                 Layer0 L0 = Layer0.getInstance(graph);
48                 String name = graph.getPossibleRelatedValue(advisor.getRoot(), L0.HasName);
49                 if (name != null)
50                     graph.claimLiteral(advisor.getRoot(), L0.HasLabel, name);
51                 graph.syncRequest(new ActivateModel(toLocation, advisor.getRoot()));
52                 
53             }
54         });
55         
56         return advisor.getRoot();
57                 
58         }
59         
60         public static Resource getResource(final Resource base, final String suffix) throws DatabaseException {
61             
62             return Simantics.getSession().syncRequest(new Read<Resource>() {
63
64             @Override
65             public Resource perform(ReadGraph graph) throws DatabaseException {
66                 String baseURI = graph.getURI(base);
67                 return graph.getResource(baseURI + suffix);
68             }
69                 
70             });
71             
72         }
73         
74         public static double readConfiguration(Resource model, String rvi) throws DatabaseException {
75             
76             class ReadConfiguration extends BinaryRead<Resource, String, Double> {
77                 
78                 public ReadConfiguration(Resource model, String rvi) {
79                     super(model, rvi);
80                 }
81
82                 @Override
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;
87                 }
88                 
89             }
90             
91             return Simantics.getSession().syncRequest(new ReadConfiguration(model, rvi));
92             
93         }
94         
95     public static double readState(Resource run, String rvi) throws DatabaseException {
96         
97         class ReadState extends BinaryRead<Resource, String, Double> {
98             
99             public ReadState(Resource run, String rvi) {
100                 super(run, rvi);
101             }
102
103             @Override
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;
108             }
109             
110         }
111         
112         return Simantics.getSession().syncRequest(new ReadState(run, rvi));
113         
114     }
115
116         public static boolean lineEquals(String s1, String s2) {
117                 if(s1 == null) {
118                         if(s2 == null) return true;
119                         else return false;
120                 } else {
121                         if(s2 == null) return false;
122                         else return s1.equals(s2);
123                 }
124         }
125         
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 + "'";
134                 }
135                 return null;
136         }
137         
138 }