1 package org.simantics.document.linking.report.evaluator;
3 import java.util.Collection;
5 import org.simantics.annotation.ontology.AnnotationResource;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.db.layer0.variable.Variables;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.ui.SimanticsUI;
15 * Copy-paste from org.simantics.diagram.function.PredefinedVariables (removed diagram specific stuff)
18 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
21 public class PredefinedVariables {
22 public static final String root = "root";
23 public static final String project = "project";
24 public static final String model = "model";
25 public static final String template = "template";
26 public static final String current = ".";
29 private static PredefinedVariables factory = new PredefinedVariables();
31 public static PredefinedVariables getInstance(){
35 public static void setFactory(PredefinedVariables factory){
36 PredefinedVariables.factory = factory;
39 Resource connectedComponent = null;
45 public static Resource getAnnotation(ReadGraph graph, Resource res) throws DatabaseException {
46 Layer0 L0 = Layer0.getInstance(graph);
47 Collection<Resource> children = graph.getObjects(res, L0.ConsistsOf);
49 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
50 for (Resource child:children){
51 if (graph.isInstanceOf(child, ANNO.Annotation)){
61 public Resource getPredefinedResource(ReadGraph graph, Variable selection, String id) throws DatabaseException {
62 Resource predefined = null;
64 predefined = graph.getRootLibrary();
65 else if (id.equals(project))
66 predefined = SimanticsUI.getProject().get();
67 else if (id.equals(model))
68 predefined = Variables.getModel(graph, selection);
69 else if (id.equals(template)){
70 // Resource composite = selection.getRepresents(graph);
71 // if (composite == null)
73 // ModelingResources MOD = ModelingResources.getInstance(graph);
74 // Template2dResource TEPLATE2D = Template2dResource.getInstance(graph);
75 // Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);
76 // if (diagram == null)
78 // predefined = graph.getPossibleObject(diagram, TEPLATE2D.HasDrawingTemplate);
80 else if (id.equals(current))
81 predefined = selection.getRepresents(graph);
85 public Variable getPredefinedVariable(ReadGraph graph, Variable selection, String id) throws DatabaseException {
86 Resource predefined = this.getPredefinedResource(graph, selection, id);
87 if (predefined == null)
90 Variable v = selection;
91 //Variable test = Variables.getPossibleVariable(graph, "http://Projects/Development%20Project/Model/Configuration/NewAutomationDiagram2/__CONTAINER__/__DIAGRAM__/4");
92 if (predefined != null)
93 v = graph.adapt(predefined, Variable.class);
97 public Variable getVariable(ReadGraph graph, String path, Resource converter, Variable selection) throws DatabaseException {
100 int colonInx = path.indexOf(':');
101 int firstSlash = path.indexOf('/');
102 int firstHash = path.indexOf('#');
103 int firstFlashInx = firstSlash;
104 if(firstFlashInx == -1) firstFlashInx = firstHash;
106 if(firstHash > -1 && firstHash < firstFlashInx) firstFlashInx = firstHash;
109 String predefined = null;
110 String relativePath = null;
112 // if scheme is followed by absolute uri
114 // pre:/model/Library/logo.svg#data -- get under model
115 // pre:/./Library/logo.svg#data-- get under composite which corresponds the diagram
116 // pre:/project/Library/FORTUM_LOGO_CMYK.svg#data -- get under project
117 // pre:/root/Library/FORTUM_LOGO_CMYK.svg#data -- get under root
119 if (colonInx != -1 && firstFlashInx != -1 && colonInx+1 == firstFlashInx && path.length() > firstFlashInx+1){
120 String scheme = path.substring(0, colonInx);
121 String absPath = path.substring(firstFlashInx+1);
122 if (scheme.equals("pre")){
123 int endOfPredefined1 = absPath.indexOf('/');
124 int endOfPredefined2 = absPath.indexOf('#');
125 if (endOfPredefined1 == -1 && endOfPredefined2 == -1)
126 predefined = absPath;
127 if (endOfPredefined1 == -1 && endOfPredefined2 != -1)
128 predefined = absPath.substring(0, endOfPredefined2);
129 if (endOfPredefined1 != -1 && endOfPredefined2 == -1)
130 predefined = absPath.substring(0, endOfPredefined1);
131 if (endOfPredefined1 != -1 && endOfPredefined2 != -1){
132 if (endOfPredefined2 < endOfPredefined1)
133 endOfPredefined1 = endOfPredefined2;
134 predefined = absPath.substring(0, endOfPredefined1);
136 relativePath = absPath.substring(predefined.length());
140 //Variable activeVariable = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
141 if (selection == null)
144 Variable v = selection;
145 if (predefined != null)
146 v = getPredefinedVariable(graph, selection, predefined);
151 Variable property = null;
152 if (relativePath != null){
153 if (relativePath.startsWith("/."))
154 relativePath = relativePath.substring(1);
155 property = v.browsePossible(graph, relativePath);
158 property = v.browsePossible(graph, path);