]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/VariableReferences.java
Find SCL references in SCLModuleEditor with Ctrl+Shift+G
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / utils / VariableReferences.java
1 package org.simantics.modeling.utils;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.databoard.type.Datatype;
7 import org.simantics.databoard.util.ObjectUtils;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.RVI;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.VariableReference;
14 import org.simantics.db.layer0.variable.Variables;
15 import org.simantics.db.request.Read;
16 import org.simantics.modeling.PropertyVariables;
17
18 /**
19  * A intermediate hack solution for converting variables to variable references.
20  * {@link NodeReference}s and {@link Datatype}s for subscription and charting
21  * purposes.
22  * 
23  * @author Tuukka Lehtonen
24  */
25 public final class VariableReferences {
26
27     public static Read<List<VariableReference>> toReferences(final List<PropertyVariables> vars) {
28         return toReferences(null, vars);
29     }
30
31     public static Read<List<VariableReference>> toReferences(final Resource expectedModel, final List<PropertyVariables> vars) {
32         return new Read<List<VariableReference>>() {
33             @Override
34             public List<VariableReference> perform(ReadGraph graph) throws DatabaseException {
35                 List<VariableReference> result = new ArrayList<VariableReference>();
36                 for (PropertyVariables v : vars) {
37                     if (expectedModel != null) {
38                         // Check that the model of the variable matches the target model.
39                         Resource model = Variables.getModel(graph, v.getContainer());
40                         if (!ObjectUtils.objectEquals(expectedModel, model))
41                             continue;
42                     }
43
44                     Datatype datatype = v.getVisualVariable().getPossibleDatatype(graph);
45
46 //                    System.out.println("URI1: " + v.getContainer().getURI(graph));
47 //                    System.out.println("URI2: " + v.getExperiment().getURI(graph));
48 //                    System.out.println("URI3: " + v.getProperty().getURI(graph));
49 //                    System.out.println("RVI: " + Variables.getRVI(graph, v.getProperty()));
50                     RVI rvi = Variables.getRVI2(graph, v.getVisualVariable());
51 //                    String rvi = Variables.getRVI(graph, v.getVisualVariable());
52 //                    String nodeReference = rvi.replaceFirst("/", "");
53 //                    System.out.println("NODEREF: " + nodeReference);
54 //                    System.out.println("DATATYPE: " + datatype);
55                     result.add( VariableReference.of( rvi, datatype, null ) );
56                 }
57                 return result;
58             }
59         };
60     }
61
62     public static Read<List<VariableReference>> variablesToReferences(final List<Variable> vars) {
63         return variablesToReferences((Resource) null, vars);
64     }
65
66     public static Read<List<VariableReference>> variablesToReferences(final Resource expectedModel, final List<Variable> vars) {
67         return new Read<List<VariableReference>>() {
68             @Override
69             public List<VariableReference> perform(ReadGraph graph) throws DatabaseException {
70                 List<VariableReference> result = new ArrayList<VariableReference>();
71                 for (Variable v : vars) {
72                     if (expectedModel != null) {
73                         // Check that the model of the variable matches the target model.
74                         Resource model = Variables.getModel(graph, v);
75                         if (!ObjectUtils.objectEquals(expectedModel, model))
76                             continue;
77                     }
78                     Datatype datatype = v.getPossibleDatatype(graph);
79                     RVI rvi = Variables.getRVI2(graph, v);
80 //                    String rvi = Variables.getRVI(graph, v);
81 //                    String nodeReference = rvi.replaceFirst("/", "");
82                     result.add( VariableReference.of( rvi, datatype, null ) );
83                 }
84                 return result;
85             }
86         };
87     }
88
89 }