]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/Utils.java
Added missing org.simantics.modeling.tests plug-ins.
[simantics/platform.git] / bundles / org.simantics.modeling.tests / src / org / simantics / modeling / tests / commands / Utils.java
1 package org.simantics.modeling.tests.commands;
2
3 import java.util.concurrent.TimeUnit;
4
5 import org.simantics.Simantics;
6 import org.simantics.databoard.binding.Binding;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.common.request.ReadRequest;
11 import org.simantics.db.common.request.UniqueRead;
12 import org.simantics.db.common.request.WriteRequest;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.util.EvaluatingListener;
15 import org.simantics.db.layer0.util.EvaluatingListener.Criterion;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.layer0.variable.Variables;
18 import org.simantics.modeling.tests.traits.SingleResourceTrait;
19
20
21
22 public class Utils {
23
24
25         public static void waitMapping() throws DatabaseException {
26                 
27                 sync();
28                 
29         }
30
31         public static void sync() throws DatabaseException {
32
33         // Multiple bugs here:
34         // -Model performs activation in separate write transactions because API does not support changing the virtual graph
35         //  => activation & activation listener is delayed beyond this point
36         // -This should be fixed by the following code 
37         //     TransactionSupport ts = session.getService(TransactionSupport.class);
38         //     ts.waitCompletion();
39         //  but unfortunately this does not work either...
40         // so we synchronize by a familiar write transaction
41
42                 
43         // And then wait still some more
44                 for(int i=0;i<3;i++) {
45                Simantics.getSession().syncRequest(new WriteRequest() {
46                     
47                     @Override
48                     public void perform(WriteGraph graph) throws DatabaseException {
49                         
50                     }
51                         
52                         @Override
53                         public String toString() {
54                                 return "Utils sync";
55                         }
56                     
57                });
58                
59                // And then wait still some more
60                Simantics.getSession().syncRequest(new ReadRequest() {
61                    
62                    @Override
63                    public void run(ReadGraph graph) throws DatabaseException {
64                        
65                    }
66                    
67               });
68                 }
69        
70     }
71         
72         public static void writeConfiguration(final Resource model, final String rvi, final Object value, final Binding binding) throws DatabaseException {
73             
74             class WriteConfiguration extends WriteRequest {
75
76                         @Override
77                         public void perform(WriteGraph graph) throws DatabaseException {
78                     Variable state = Variables.getVariable(graph, graph.getURI(model) + rvi);
79                     state.setValue(graph, value, binding);
80                         }
81                 
82             }
83             
84             Simantics.getSession().syncRequest(new WriteConfiguration());
85             
86         }
87         
88         public static <T>T readValue(final SingleResourceTrait run, final String rvi, final Criterion<T> criterion) throws DatabaseException, InterruptedException {
89
90                 return EvaluatingListener.<T>trySyncRequest(Simantics.getSession(), 
91                                 new UniqueRead<T>(){
92                                         @SuppressWarnings("unchecked")
93                                         @Override
94                                         public T perform(ReadGraph graph) throws DatabaseException{
95                                                 Variable state = Variables.getPossibleVariable(graph, graph.getURI(run.getResource(graph)) + rvi);
96                                                 return (T) (state != null ? state.getValue(graph) : null);
97                                         }
98                                         
99                                 }, 
100                                 criterion,
101                                 15, TimeUnit.SECONDS);
102         }
103
104 }
105