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