1 package org.simantics.modeling.utils;
3 import java.util.ArrayList;
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;
19 * A intermediate hack solution for converting variables to variable references.
20 * {@link NodeReference}s and {@link Datatype}s for subscription and charting
23 * @author Tuukka Lehtonen
25 public final class VariableReferences {
27 public static Read<List<VariableReference>> toReferences(final List<PropertyVariables> vars) {
28 return toReferences(null, vars);
31 public static Read<List<VariableReference>> toReferences(final Resource expectedModel, final List<PropertyVariables> vars) {
32 return new Read<List<VariableReference>>() {
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))
44 Datatype datatype = v.getVisualVariable().getPossibleDatatype(graph);
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 ) );
62 public static Read<List<VariableReference>> variablesToReferences(final List<Variable> vars) {
63 return variablesToReferences((Resource) null, vars);
66 public static Read<List<VariableReference>> variablesToReferences(final Resource expectedModel, final List<Variable> vars) {
67 return new Read<List<VariableReference>>() {
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))
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 ) );