1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.diagram.adapter;
14 import java.util.Collections;
15 import java.util.List;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.utils.OrderedSetUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.diagram.profile.StyleBase;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.modeling.ModelingResources;
28 import org.simantics.operation.Layer0X;
29 import org.simantics.scenegraph.INode;
30 import org.simantics.scenegraph.profile.EvaluationContext;
31 import org.simantics.scenegraph.profile.common.ProfileVariables;
32 import org.simantics.structural.stubs.StructuralResource2;
35 * @author Antti Villberg
37 public class ExpressionStyle extends StyleBase<Map<String, Object>> {
41 public ExpressionStyle(Resource style) {
46 public Map<String, Object> calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable activeVariable) throws DatabaseException {
48 //System.err.println("calculateExpressionStyle " + activeVariable.getURI(graph));
50 Layer0 L0 = Layer0.getInstance(graph);
51 Layer0X L0X = Layer0X.getInstance(graph);
52 DiagramResource DIA = DiagramResource.getInstance(graph);
53 ModelingResources MOD = ModelingResources.getInstance(graph);
54 StructuralResource2 SR = StructuralResource2.getInstance(graph);
56 String key = graph.getPossibleRelatedValue(style, DIA.HasVariableName, Bindings.STRING);
57 String expression = graph.getPossibleRelatedValue(style, L0X.HasExpression, Bindings.STRING);
58 //System.out.println(key + ": " + expression);
60 Resource module = graph.getPossibleObject(element, MOD.ElementToComponent);
65 Resource elementType = graph.getPossibleType(element, L0.Entity);
66 if(elementType == null) return null;
67 Resource definition = graph.getPossibleObject(elementType, SR.IsDefinedBy);
68 if(definition == null) return null;
69 List<Resource> items = OrderedSetUtils.toList(graph, definition);
70 if(items == null) return null;
71 for(Resource item : items) {
72 if(graph.isInstanceOf(item, DIA.AnimatedSVGElement)) {
77 if(svg == null) return null;
79 String moduleName = graph.getPossibleRelatedValue(module, L0.HasName, Bindings.STRING);
80 if (moduleName == null)
83 Variable moduleVariable = activeVariable.getChild(graph, moduleName);
85 //System.err.println("mv " + moduleVariable.getURI(graph));
87 Object value = moduleVariable.getPossiblePropertyValue(graph, expression);
88 Map<String, Object> values = Collections.singletonMap(key, value);
90 // for(Resource desc : graph.getObjects(svg, DIA.HasConfigurationVariable)) {
91 // String name = graph.getRelatedValue(desc, L0.HasName);
92 // System.err.println("name=" + name);
93 // String formula = graph.getRelatedValue(desc, L0X.HasExpression);
94 // Object value = moduleVariable.getPossiblePropertyValue(graph, formula);
95 // System.err.println(name + " => " + formula + " => " + value);
96 // values.put(name, value);
104 public void applyStyleForNode(EvaluationContext evaluationContext, INode node, Map<String, Object> values) {
105 //System.out.println(this + ": apply values " + values);
109 ProfileVariables.claimNodeProperty(node, "valuezz", values, evaluationContext);
111 // Variables.set(element, "valuezz", values, observer);
112 //// for(Map.Entry<String, Object> entry : values.entrySet()) {
113 //// Variables.set(element, entry.getKey(), entry.getValue(), observer);
116 // PropertySetter setter = element.getElementClass().getAtMostOneItemOfClass(PropertySetter.class);
117 // if (setter != null)
118 // setter.syncPropertiesToNode(element);
122 protected void cleanupStyleForNode(EvaluationContext observer, INode node) {
123 ProfileVariables.init(node, observer);
127 public String toString() {