1 package org.simantics.diagram.function;
3 import java.util.Collection;
4 import java.util.HashSet;
7 import org.simantics.Simantics;
8 import org.simantics.annotation.ontology.AnnotationResource;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.diagram.content.ConnectionUtil;
15 import org.simantics.diagram.flag.FlagUtil;
16 import org.simantics.diagram.stubs.DiagramResource;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.modeling.ModelingResources;
19 import org.simantics.modeling.template2d.ontology.Template2dResource;
20 import org.simantics.structural.stubs.StructuralResource2;
22 public class PredefinedVariables {
23 public static final String root = "root";
24 public static final String project = "project";
25 public static final String model = "model";
26 public static final String template = "template";
27 public static final String current = ".";
28 public static final String diagramComposite = "diagramComposite";
29 public static final String diagram = "diagram";
30 public static final String flagAnnotation = "annotation";
33 * flagSourceObject refers to a structural component which has an element on the flag's diagram
35 public static final String flagSourceObject = "flagSourceObject";
38 * flagTargetObject refers to a structural component which has an element on the counterpart flag's diagram
40 public static final String flagTargetObject = "flagTargetObject";
43 * flagSourceComposite refers to a structural composite which corresponds the flag's diagram
45 public static final String flagSourceComposite = "flagSourceComposite";
48 * flagTargetComposite refers to a structural composite which corresponds the counterpart flag's diagram
50 public static final String flagTargetComposite = "flagTargetComposite";
53 * flagRouterComponent refers to a structural component which corresponds a graphical connection connected with the flag
55 public static final String flagRouterComponent = "flagRouterComponent";
57 private static PredefinedVariables factory = new PredefinedVariables();
59 public static PredefinedVariables getInstance(){
63 public static void setFactory(PredefinedVariables factory){
64 PredefinedVariables.factory = factory;
67 Resource connectedComponent = null;
69 private Resource getConnectedComponent(ReadGraph graph, Resource flag) throws DatabaseException {
70 StructuralResource2 STR = StructuralResource2.getInstance(graph);
71 ModelingResources MOD = ModelingResources.getInstance(graph);
72 ConnectionUtil cu = new ConnectionUtil(graph);
73 Resource connectedComponent = null;
74 Set<Resource> connectors = new HashSet<Resource>();
75 for (Resource connector : graph.getObjects(flag, STR.IsConnectedTo)) {
76 Resource diagramConnection = ConnectionUtil.tryGetConnection(graph, connector);
77 if (diagramConnection != null) {
79 cu.getConnectors(diagramConnection, connectors);
80 connectors.remove(connector);
81 if (connectors.isEmpty()) {
84 //connectedComponent = graph.getPossibleObject(diagramConnection, MOD.ElementToComponent);
85 if (connectedComponent == null) {
86 for (Resource conn : connectors) {
87 Resource element = cu.getConnectedComponent(diagramConnection, conn);
90 connectedComponent = graph.getPossibleObject(element, MOD.ElementToComponent);
91 if (connectedComponent != null)
98 return connectedComponent;
101 public Resource getFlagRouterComponent(ReadGraph graph, Variable variable) throws DatabaseException {
102 Resource flag = variable.getPossibleRepresents(graph);
103 return flag == null ? null : getFlagRouterComponent(graph, flag);
106 public Resource getFlagRouterComponent(ReadGraph graph, Resource flag) throws DatabaseException {
108 Layer0 L0 = Layer0.getInstance(graph);
109 ModelingResources MOD = ModelingResources.getInstance(graph);
111 Resource container = graph.getSingleObject(flag, L0.PartOf);
112 Resource composite = graph.getSingleObject(container, MOD.DiagramToComposite);
113 Variable var = Variables.getVariable(graph, composite);
115 // System.err.println("var: " + var.getURI(graph));
117 Variable result = FlagUtil.getPossibleFlagSignal(graph, var, flag, L0.Entity);
118 if(result != null) return result.getRepresents(graph);
123 private Resource getFlagTargetObject(ReadGraph graph, Variable variable){
125 Resource flag = variable.getRepresents(graph);
126 Set<Resource> targetFlags = FlagUtil.getCounterparts(graph, flag);
128 // if there are multiple targets
129 if (targetFlags.size() != 1)
132 Resource targetFlag = targetFlags.toArray(new Resource[0])[0];
133 if (targetFlag == null)
135 Resource connectedComponent = getConnectedComponent(graph, targetFlag);
136 if (connectedComponent == null)
138 return connectedComponent;
139 } catch (DatabaseException e) {
144 private Resource getFlagSourceObject(ReadGraph graph, Variable variable){
146 Resource flag = variable.getRepresents(graph);
147 Resource connectedComponent = getConnectedComponent(graph, flag);
148 if (connectedComponent == null)
150 return connectedComponent;
151 } catch (DatabaseException e) {
156 private Resource getFlagSourceComposite(ReadGraph graph, Variable variable){
158 Layer0 L0 = Layer0.getInstance(graph);
159 ModelingResources MOD = ModelingResources.getInstance(graph);
161 Resource flag = variable.getRepresents(graph);
162 Resource diagram = graph.getPossibleObject(flag, L0.PartOf);
165 Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
166 if (composite == null)
169 } catch (DatabaseException e) {
174 private Resource getFlagTargetComposite(ReadGraph graph, Variable variable){
176 Layer0 L0 = Layer0.getInstance(graph);
177 ModelingResources MOD = ModelingResources.getInstance(graph);
179 Resource flag = variable.getRepresents(graph);
180 Set<Resource> targetFlags = FlagUtil.getCounterparts(graph, flag);
182 // if there are multiple targets
183 if (targetFlags.size() != 1)
186 Resource targetFlag = targetFlags.toArray(new Resource[0])[0];
187 if (targetFlag == null)
189 Resource diagram = graph.getPossibleObject(targetFlag, L0.PartOf);
192 Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
193 if (composite == null)
196 } catch (DatabaseException e) {
201 public static Resource getAnnotation(ReadGraph graph, Resource res) throws DatabaseException {
202 Layer0 L0 = Layer0.getInstance(graph);
203 Collection<Resource> children = graph.getObjects(res, L0.ConsistsOf);
204 Resource anno = null;
205 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
206 for (Resource child:children){
207 if (graph.isInstanceOf(child, ANNO.Annotation)){
215 private Resource getDiagramComposite(ReadGraph graph, Variable variable){
217 Resource res = variable.getRepresents(graph);
220 DiagramResource DIA = DiagramResource.getInstance(graph);
221 if (graph.isInstanceOf(res, DIA.Flag)){
222 ModelingResources MOD = ModelingResources.getInstance(graph);
223 Layer0 L0 = Layer0.getInstance(graph);
224 Resource parent = graph.getPossibleObject(res, L0.PartOf);
227 if (!graph.isInstanceOf(parent, DIA.Diagram))
229 return graph.getPossibleObject(parent, MOD.DiagramToComposite);
232 } catch (DatabaseException e) {
237 public Resource getPredefinedResource(ReadGraph graph, Variable selection, String id) throws DatabaseException {
238 Resource predefined = null;
240 predefined = graph.getRootLibrary();
241 else if (id.equals(project))
242 predefined = Simantics.getProject().get();
243 else if (id.equals(model))
244 predefined = Variables.getPossibleModel(graph, selection);
245 else if (id.equals(template)){
246 Resource composite = selection.getRepresents(graph);
247 if (composite == null)
249 ModelingResources MOD = ModelingResources.getInstance(graph);
250 Template2dResource TEPLATE2D = Template2dResource.getInstance(graph);
251 Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);
254 predefined = graph.getPossibleObject(diagram, TEPLATE2D.HasDrawingTemplate);
256 else if (id.equals(current))
257 predefined = selection.getPossibleRepresents(graph);
258 else if (id.equals(flagAnnotation)){
259 Resource flag = selection.getPossibleRepresents(graph);
261 predefined = getAnnotation(graph, flag);
263 else if (id.equals(diagram)){
264 Resource composite = selection.getPossibleRepresents(graph);
265 if (composite == null)
267 ModelingResources MOD = ModelingResources.getInstance(graph);
268 predefined = graph.getPossibleObject(composite, MOD.CompositeToDiagram);
270 else if (id.equals(diagramComposite))
271 predefined = getDiagramComposite(graph, selection);
272 else if (id.equals(flagSourceObject))
273 predefined = getFlagSourceObject(graph, selection);
274 else if (id.equals(flagTargetObject))
275 predefined = getFlagTargetObject(graph, selection);
276 else if (id.equals(flagSourceComposite))
277 predefined = getFlagSourceComposite(graph, selection);
278 else if (id.equals(flagTargetComposite))
279 predefined = getFlagTargetComposite(graph, selection);
280 else if (id.equals(flagRouterComponent))
281 predefined = getFlagRouterComponent(graph, selection);
285 public Variable getPredefinedVariable(ReadGraph graph, Variable selection, String id) throws DatabaseException {
286 Resource predefined = this.getPredefinedResource(graph, selection, id);
287 if (predefined == null)
290 Variable v = selection;
291 if (predefined != null)
292 v = Variables.getPossibleVariable(graph, predefined);
296 public Variable getVariable(ReadGraph graph, String path, Resource converter, Variable selection) throws DatabaseException {
299 int colonInx = path.indexOf(':');
300 int firstSlash = path.indexOf('/');
301 int firstHash = path.indexOf('#');
302 int firstFlashInx = firstSlash;
303 if(firstFlashInx == -1) firstFlashInx = firstHash;
305 if(firstHash > -1 && firstHash < firstFlashInx) firstFlashInx = firstHash;
308 String predefined = null;
309 String relativePath = null;
311 // if scheme is followed by absolute uri
313 // pre:/model/Library/logo.svg#data -- get under model
314 // pre:/./Library/logo.svg#data-- get under composite which corresponds the diagram
315 // pre:/project/Library/FORTUM_LOGO_CMYK.svg#data -- get under project
316 // pre:/root/Library/FORTUM_LOGO_CMYK.svg#data -- get under root
318 if (colonInx != -1 && firstFlashInx != -1 && colonInx+1 == firstFlashInx && path.length() > firstFlashInx+1){
319 String scheme = path.substring(0, colonInx);
320 String absPath = path.substring(firstFlashInx+1);
321 if (scheme.equals("pre")){
322 int endOfPredefined1 = absPath.indexOf('/');
323 int endOfPredefined2 = absPath.indexOf('#');
324 if (endOfPredefined1 == -1 && endOfPredefined2 == -1)
325 predefined = absPath;
326 if (endOfPredefined1 == -1 && endOfPredefined2 != -1)
327 predefined = absPath.substring(0, endOfPredefined2);
328 if (endOfPredefined1 != -1 && endOfPredefined2 == -1)
329 predefined = absPath.substring(0, endOfPredefined1);
330 if (endOfPredefined1 != -1 && endOfPredefined2 != -1){
331 if (endOfPredefined2 < endOfPredefined1)
332 endOfPredefined1 = endOfPredefined2;
333 predefined = absPath.substring(0, endOfPredefined1);
335 relativePath = absPath.substring(predefined.length());
339 //Variable activeVariable = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
340 if (selection == null)
343 Variable v = selection;
344 if (predefined != null)
345 v = getPredefinedVariable(graph, selection, predefined);
350 Variable property = null;
351 if (relativePath != null){
352 if (relativePath.startsWith("/."))
353 relativePath = relativePath.substring(1);
354 property = v.browsePossible(graph, relativePath);
357 property = v.browsePossible(graph, path);