]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableWrite.java
Always write model activation information in activations VG
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / VariableWrite.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.variable;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.binding.Binding;
16 import org.simantics.databoard.binding.BooleanBinding;
17 import org.simantics.databoard.binding.NumberBinding;
18 import org.simantics.databoard.binding.StringBinding;
19 import org.simantics.databoard.binding.error.BindingException;
20 import org.simantics.databoard.binding.mutable.MutableStringBinding;
21 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
22 import org.simantics.databoard.parser.repository.DataValueRepository;
23 import org.simantics.databoard.primitives.MutableString;
24 import org.simantics.databoard.type.Datatype;
25 import org.simantics.databoard.type.DoubleType;
26 import org.simantics.databoard.type.FloatType;
27 import org.simantics.databoard.type.IntegerType;
28 import org.simantics.databoard.type.NumberType;
29 import org.simantics.databoard.units.IUnitConverter;
30 import org.simantics.databoard.units.internal.UnitParseException;
31 import org.simantics.databoard.util.ObjectUtils;
32 import org.simantics.db.VirtualGraph;
33 import org.simantics.db.WriteGraph;
34 import org.simantics.db.common.request.WriteRequest;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.layer0.util.Layer0Utils;
37 import org.simantics.db.layer0.util.PrimitiveValueParser;
38
39 public class VariableWrite extends WriteRequest {
40
41     final private Variable variable;
42     final private String label;
43     final private String targetUnit;
44
45     public VariableWrite(Variable variable, String label) {
46         this(variable, label, null, null);
47     }
48
49     public VariableWrite(Variable variable, String label, VirtualGraph provider, String targetUnit) {
50         super(provider);
51         this.variable = variable;
52         this.label = label;
53         this.targetUnit = targetUnit;
54     }
55
56     public static IUnitConverter converter(final String unit, final String targetUnit) {
57
58         return new IUnitConverter() {
59
60             @Override
61             public double convert(double value) {
62
63                 if(targetUnit == null) return value;
64
65                 if("K".equals(unit)) {
66                     if("C".equals(targetUnit)) value = value - 273.15;
67                     else if("F".equals(targetUnit)) value = (value - 273.15) * 9.0/5.0 + 32.0;
68                 } else if ("C".equals(unit)) {
69                     if("K".equals(targetUnit)) value = value + 273.15;
70                 } else if ("F".equals(unit)) {
71                     if("K".equals(targetUnit)) value = (value - 32.0) * 5.0/9.0 + 273.15;
72                 } else {
73                     try {
74                         value = org.simantics.databoard.Units.convert(value, unit, targetUnit);
75                     } catch (UnitParseException e) {
76                         e.printStackTrace();
77                     }
78                 }
79
80                 return value;
81
82             }
83
84         };
85
86
87     }
88
89     @Override
90     public void perform(WriteGraph graph) throws DatabaseException {
91         graph.markUndoPoint();
92         try {
93             //System.err.println("VariableWrite " + variable.getURI(graph) + " => " + label);
94             Datatype type = variable.getPossibleDatatype(graph);
95             if (type == null) {
96                 String uri = null;
97                 try {
98                     uri = variable.getURI(graph);
99                 } catch (DatabaseException e) {
100                 }
101                 throw new org.simantics.db.exception.BindingException("no datatype for variable " + variable + " (URI=" + uri + ")", null);
102             }
103
104             Binding binding = Bindings.getBinding(type);
105
106             String parsedLabel = label;
107             Object value = null;
108
109             if (binding instanceof StringBinding) {
110                 if (binding instanceof MutableStringBinding)
111                     value = new MutableString(label);
112                 else
113                     value = label;
114             } else if (binding instanceof BooleanBinding) {
115                 // This parses a bit more than just "true"/"false".
116                 try {
117                     value = PrimitiveValueParser.parseBoolean(label);
118                 } catch (IllegalArgumentException e) {
119                     throw new DatabaseException(e);
120                 }
121             } else {
122                 if (binding instanceof NumberBinding) {
123                     parsedLabel = label.replace(",", ".");
124                 }
125
126                 value = binding.parseValue(parsedLabel, new DataValueRepository());
127                 
128                 // FIXME: this is Balas-specific
129                 if (targetUnit != null) {
130                     if (type instanceof NumberType) {
131                         String unit = Variables.getPossibleUnit(graph, variable);
132                         if (unit != null) {
133                             IUnitConverter converter = converter(targetUnit, unit);
134                             if(type instanceof FloatType) {
135                                 float converted = (float)converter.convert(((Number)value).floatValue());
136                                 value = binding.parseValue(Float.toString(converted), new DataValueRepository());
137                             } else if(type instanceof DoubleType) {
138                                 double converted = converter.convert(((Number)value).doubleValue());
139                                 value = binding.parseValue(Double.toString(converted), new DataValueRepository());
140                             } else if(type instanceof IntegerType) {
141                                 value = binding.parseValue(Integer.toString(((Number)value).intValue()), new DataValueRepository());
142                             }
143                         }
144                     }
145                 }
146             }
147             variable.setValue(graph, value, binding);
148
149             String[] varParts = variable.getParent(graph).getURI(graph).split("/");
150             String varName = varParts[varParts.length - 1];
151             
152             // Add a comment to metadata.
153             Layer0Utils.addCommentMetadata(graph, "Modified variable " + varName + " to " + ObjectUtils.toString(value));
154             
155         } catch (DataTypeSyntaxError e) {
156             throw new DatabaseException(e);
157         } catch (BindingException e) {
158             throw new DatabaseException(e);
159         }
160
161     }
162
163 }