1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.template2d.ui.function;
14 import java.util.Collections;
16 import java.util.TreeMap;
18 import org.simantics.browsing.ui.model.modifiers.NoModifierRule;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
23 import org.simantics.db.common.request.UnaryRead;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.SelectionHints;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.diagram.content.ElementContext;
29 import org.simantics.diagram.function.PredefinedVariables;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
32 import org.simantics.modeling.ModelingUtils;
33 import org.simantics.modeling.template2d.ontology.Template2dResource;
34 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
35 import org.simantics.scenegraph.profile.request.RuntimeProfileActiveEntryResources;
36 import org.simantics.scl.reflection.annotations.SCLValue;
37 import org.simantics.utils.ui.ISelectionUtils;
42 @SCLValue(type = "a -> b -> c")
43 public static Object variableTransformation(Object _graph, Object input) throws DatabaseException {
45 ReadGraph graph = (ReadGraph) _graph;
47 Resource object = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_MAIN, Resource.class);
51 Resource runtime = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_VARIABLE_RESOURCE,
56 Variable inputVariable = graph.syncRequest(new UnaryRead<Resource, Variable>(runtime) {
59 public Variable perform(ReadGraph graph) throws DatabaseException {
61 DiagramResource dr = DiagramResource.getInstance(graph);
62 String diagramVariable = graph.getPossibleRelatedValue(parameter, dr.RuntimeDiagram_HasVariable);
63 return Variables.getVariable(graph, diagramVariable);
68 if (inputVariable == null)
71 Resource component = ModelingUtils.getPossibleElementCorrespondendence(graph, object);
72 if (component == null)
74 return inputVariable.browsePossible(graph, component);
78 @SCLValue(type = "a -> b -> c")
79 public static Object resourceVariableTransformation(Object _graph, Object input) throws DatabaseException {
81 ReadGraph graph = (ReadGraph) _graph;
83 Resource object = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_MAIN, Resource.class);
87 return Variables.getVariable(graph, object);
89 // Resource runtime = ISelectionUtils.getSinglePossibleKey(input, SelectionHints.KEY_VARIABLE_RESOURCE,
91 // if (runtime == null)
94 // Variable inputVariable = graph.syncRequest(new UnaryRead<Resource, Variable>(runtime) {
97 // public Variable perform(ReadGraph graph) throws DatabaseException {
99 // DiagramResource dr = DiagramResource.getInstance(graph);
100 // String diagramVariable = graph.getPossibleRelatedValue(parameter, dr.RuntimeDiagram_HasVariable);
101 // return Variables.getVariable(graph, diagramVariable);
106 // if (inputVariable == null)
109 // Resource component = ModelingUtils.getPossibleElementCorrespondendence(graph, object);
110 // if (component == null)
112 // return inputVariable.browsePossible(graph, component);
116 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
117 public static String variableReference(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
119 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
121 // System.out.println("CONTEXT: " + context.getURI(graph));
122 // Resource runtime = ScenegraphLoaderUtils.getRuntime(graph, context);
123 // System.out.println("runtime: " + NameUtils.getSafeName(graph, runtime));
125 String path = graph.getRelatedValue(converter, TEMPLATE2D.Profiles_VariableReference_path, Bindings.STRING);
129 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
130 PredefinedVariables vars = PredefinedVariables.getInstance();
131 Variable property = vars.getVariable(graph, path, converter, selection);
132 if (property != null)
133 return property.getValue(graph).toString();
138 public static Resource getTemplate(ReadGraph graph, Resource runtimeDiagram) throws DatabaseException {
139 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
140 DiagramResource DIA = DiagramResource.getInstance(graph);
141 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
142 if (diagram != null) {
143 Resource template = graph.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);
144 if (template != null)
150 @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")
151 public static Object flagTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
152 //Layer0 L0 = Layer0.getInstance(graph);
153 DiagramResource DIA = DiagramResource.getInstance(graph);
154 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
156 Resource flag = context.element;
157 Resource runtimeDiagram = context.runtime;
159 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
161 return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
163 Resource template = getTemplate(graph, runtimeDiagram);
164 if (template == null)
165 return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
167 double gridSize = DiagramGraphUtil.getGridSize(graph, diagram, 0.0);
169 String tableName = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
170 Integer rowIndex = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);
171 FlagInfo flagInfo = new FlagInfo();
172 flagInfo.flag = flag;
173 if (tableName != null && tableName.length() > 0)
174 flagInfo.flagTableName = tableName;
175 flagInfo.flagTableRowIndex = rowIndex;
177 Map<String, FlagTableInfo> name2table = graph.syncRequest(new DrawingTemplateInfo(template), TransientCacheListener.<TreeMap<String, FlagTableInfo>> instance());
178 if (name2table == null)
179 name2table = Collections.emptyMap();
181 if (!name2table.containsKey(flagInfo.flagTableName))
182 return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
184 double[] mat = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
185 TranslateFlag com = new TranslateFlag(flagInfo, name2table, mat, gridSize);
187 boolean isIOTablesActive = false;
188 for (Resource entry : graph.syncRequest(new RuntimeProfileActiveEntryResources(runtimeDiagram))) {
189 //ProfileEntry profile = graph.adapt(entry, ProfileEntry.class);
190 if (TEMPLATE2D.Profiles_DrawingFlagTables.equals(entry)){
191 isIOTablesActive = true;
196 if (isIOTablesActive)
197 return com.perform();
198 return org.simantics.diagram.function.All.flagTransform(graph, converter, context);
201 @SCLValue(type = "ReadGraph -> Resource -> a -> b")
202 public static Object variableReferencePath(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
203 if (context instanceof Variable) {
204 // Input variable is (:VariableReference)#HasDisplayValue. Need to
205 // go to parent to find the variable to be edited.
206 //System.out.println("R: " + NameUtils.getSafeName(graph, resource));
207 //System.out.println("V: " + ((Variable) context).getURI(graph));
208 Variable document = ((Variable)context).browse(graph, ".");
209 //System.out.println("D: " + document.getURI(graph));
210 String path = document.getPossiblePropertyValue(graph, "path", Bindings.STRING);
216 @SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
217 public static Object variableReferenceModifier(ReadGraph graph, Resource resource, final Variable context) throws DatabaseException {
218 return NoModifierRule.NO_MODIFIER;