1 package org.simantics.diagram.function;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.util.Arrays;
6 import java.util.Collection;
9 import org.simantics.databoard.Bindings;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.common.request.PossibleIndexRoot;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.exception.MissingVariableException;
15 import org.simantics.db.layer0.request.PossibleConfiguration;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.layer0.variable.Variables;
18 import org.simantics.diagram.content.ElementContext;
19 import org.simantics.diagram.stubs.DiagramResource;
20 import org.simantics.diagram.stubs.G2DResource;
21 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
22 import org.simantics.issues.common.IssueUtils;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.modeling.ModelingResources;
25 import org.simantics.modeling.template2d.ontology.Template2dResource;
26 import org.simantics.scenegraph.loader.SceneGraphContext;
27 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
28 import org.simantics.scl.reflection.annotations.SCLValue;
33 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
34 public static Variable defaultRuntimeVariable(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
36 SceneGraphContext vc = ScenegraphLoaderUtils.getContext(graph, context);
37 if(vc == null) return null;
39 DiagramResource DIA = DiagramResource.getInstance(graph);
40 String uri = graph.getRelatedValue(vc.getRuntime(), DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
41 return Variables.getVariable(graph, uri);
45 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
46 public static String referenceText(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
48 //DiagramResource DIA = DiagramResource.getInstance(graph);
50 //Resource runtime = ScenegraphLoaderUtils.getRuntime(graph, context);
51 String path = context.getParent(graph).getPropertyValue(graph, "reference", Bindings.STRING);
53 // diagram uri and diagram variable
54 // String diagramURI = graph.getRelatedValue(runtime, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
55 // Variable diagramVariable = org.simantics.db.layer0.variable.Variables.getVariable(graph, diagramURI);
57 // Variable property = diagramVariable.browse(graph, path);
60 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
61 PredefinedVariables vars = PredefinedVariables.getInstance();
62 Variable property = vars.getVariable(graph, path, converter, selection);
64 return property.getValue(graph).toString();
65 } catch (MissingVariableException e){
66 return "Invalid path.";
71 @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")
72 public static Object modelledTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
73 return graph.getRelatedValue(context.element, G2DResource.getInstance(graph).HasTransform, Bindings.DOUBLE_ARRAY);
76 @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")
77 public static Object flagTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
78 Layer0 L0 = Layer0.getInstance(graph);
79 DiagramResource DIA = DiagramResource.getInstance(graph);
80 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
82 Resource flag = context.element;
83 Resource runtimeDiagram = context.runtime;
85 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
87 return flagTransformImpl(graph, converter, context);
89 Resource template = getTemplateFromDiagram(graph, diagram);
91 return flagTransformImpl(graph, converter, context);
93 double gridSize = DiagramGraphUtil.getGridSize(graph, diagram, 0.0);
95 String tableName = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
96 Integer rowIndex = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);
97 // double[] mat = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
100 Collection<Resource> slotTables = graph.getObjects(template, L0.ConsistsOf);
101 for (Resource slotTable:slotTables){
102 if (!graph.isInstanceOf(slotTable, TEMPLATE2D.FlagTable))
104 String name = graph.getPossibleRelatedValue(slotTable, L0.HasName, Bindings.STRING);
105 if (!name.equals(tableName))
108 double[] transform = graph.getPossibleRelatedValue2(slotTable, DIA.Scenegraph_Composite_transform, Bindings.getBindingUnchecked(double[].class));
109 Resource align = graph.getPossibleObject(slotTable, TEMPLATE2D.FlagTable_HasAlignment);
110 boolean isRightAlignment = false;
111 if (align.equals(TEMPLATE2D.FlagTable_Alignment_Right))
112 isRightAlignment = true;
113 Float width = graph.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasWidth, Bindings.FLOAT);
114 Float rowHeight = graph.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasRowHeigth, Bindings.FLOAT);
115 //Integer rowCount = g.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasRowCount, Bindings.INTEGER);
117 // Rectangle2D rowBounds = new Rectangle2D.Double(0, rowHeight*rowIndex, width, rowHeight);
118 // rowBounds = GeometryUtils.transformShape(rowBounds, new AffineTransform(transform)).getBounds2D();
119 // e.setHint(FlagClass.KEY_FLAG_BOUNDS, rowBounds);
121 double[] flagTransform = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
122 return calcTransform(transform, flagTransform, rowHeight, width, isRightAlignment, rowIndex, gridSize);
125 return flagTransformImpl(graph, converter, context);
128 private static Object flagTransformImpl(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
129 double[] mat = graph.getRelatedValue(context.element, G2DResource.getInstance(graph).HasTransform, Bindings.DOUBLE_ARRAY);
133 private static double[] calcTransform(double[] tableTransform, double[] flagTransform, Float rowHeight, Float tableWidth, boolean isRightAlignment, Integer flagRowIndex, double gridSize) {
134 double scale = tableTransform[3];
135 double scaledRowHeigth = scale*rowHeight;
137 // move to coordinate system of parent of iotable
138 double rowMiddleY = tableTransform[5]+(flagRowIndex+0.5F)*scaledRowHeigth;
139 if (gridSize != 0.0) {
140 double modulo = rowMiddleY%gridSize;
141 int count =(int) (rowMiddleY/gridSize);
142 double gridOnRow = (modulo > (gridSize/2))?gridSize*(1+count):gridSize*count;
143 double diff = (gridOnRow > rowMiddleY)? gridOnRow - rowMiddleY:rowMiddleY-gridOnRow;
144 if (diff < (scaledRowHeigth/2))
145 rowMiddleY = gridOnRow;
147 // move back to iotable coordinate system
148 rowMiddleY = (rowMiddleY-tableTransform[5])/scale;
150 double xx = isRightAlignment ? tableWidth : 0.0;
152 AffineTransform trans = new AffineTransform(new double[]{tableTransform[0],tableTransform[1],tableTransform[2],tableTransform[3],0.0,0.0});
153 Point2D point = new Point2D.Double(xx, rowMiddleY);
154 trans.transform(point, point);
156 double[] copy = Arrays.copyOf(flagTransform, flagTransform.length);
157 copy[4] = tableTransform[4] + point.getX();
158 copy[5] = tableTransform[5] + point.getY();
162 public static Resource getTemplate(ReadGraph graph, Resource runtimeDiagram) throws DatabaseException {
163 DiagramResource DIA = DiagramResource.getInstance(graph);
164 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
167 return getTemplateFromDiagram(graph, diagram);
170 public static Resource getTemplateFromDiagram(ReadGraph graph, Resource diagram) throws DatabaseException {
171 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
172 Resource template = graph.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);
176 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
177 public static String diagramElementIssuePath(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
178 Layer0 L0 = Layer0.getInstance(graph);
179 ModelingResources MOD = ModelingResources.getInstance(graph);
180 DiagramResource DIA = DiagramResource.getInstance(graph);
182 List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
183 if(contexts.isEmpty()) return "";
185 Resource context = contexts.get(0);
187 Resource mapped = graph.getPossibleObject(context, MOD.ComponentToElement);
188 if(mapped != null) context = mapped;
189 mapped = graph.getPossibleObject(context, MOD.ConnectionToDiagramConnection);
190 if(mapped != null) context = mapped;
192 if(!graph.isInstanceOf(context, DIA.Element)) return "";
194 Resource configuration = graph.sync(new PossibleConfiguration(context));
195 if(configuration == null) return "";
197 Resource diagram = graph.getPossibleObject(context, L0.PartOf);
198 if(diagram == null) return "";
200 Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
201 if(composite == null) return "";
203 Resource issueRoot = Variables.getPossibleIndexRoot(graph, property);
204 if(issueRoot == null) return "";
205 Resource contextRoot = graph.sync(new PossibleIndexRoot(composite));
206 if(contextRoot == null) return "";
207 if(issueRoot.equals(contextRoot)) {
208 String uri = graph.getURI(composite);
209 String modelURI = graph.getURI(contextRoot);
210 return IssueUtils.pathString(uri, modelURI.length()+1);
212 String uri = graph.getURI(composite);
213 String modelURI = graph.getURI(graph.getRootLibrary());
214 return IssueUtils.pathString(uri, modelURI.length()+1);