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.db.layer0.variable;
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;
40 * @deprecated exactly the same as {@link VariableWrite}, should be removed. Not
41 * used in the platform at least.
43 public class VariableWrite2 extends WriteRequest {
45 final private Variable variable;
46 final private String label;
47 final private String targetUnit;
49 public VariableWrite2(Variable variable, String label) {
50 this(variable, label, null, null);
53 public VariableWrite2(Variable variable, String label, VirtualGraph provider, String targetUnit) {
55 this.variable = variable;
57 this.targetUnit = targetUnit;
60 public static IUnitConverter converter(final String unit, final String targetUnit) {
62 return new IUnitConverter() {
65 public double convert(double value) {
67 if(targetUnit == null) return value;
69 if("K".equals(unit)) {
70 if("C".equals(targetUnit)) value = value - 273.15;
71 else if("F".equals(targetUnit)) value = (value - 273.15) * 9.0/5.0 + 32.0;
72 } else if ("C".equals(unit)) {
73 if("K".equals(targetUnit)) value = value + 273.15;
74 } else if ("F".equals(unit)) {
75 if("K".equals(targetUnit)) value = (value - 32.0) * 5.0/9.0 + 273.15;
78 value = org.simantics.databoard.Units.convert(value, unit, targetUnit);
79 } catch (UnitParseException e) {
94 public void perform(WriteGraph graph) throws DatabaseException {
95 graph.markUndoPoint();
97 //System.err.println("VariableWrite " + variable.getURI(graph) + " => " + label);
98 Datatype type = variable.getPossibleDatatype(graph);
102 uri = variable.getURI(graph);
103 } catch (DatabaseException e) {
105 throw new org.simantics.db.exception.BindingException("no datatype for variable " + variable + " (URI=" + uri + ")", null);
108 Binding binding = Bindings.getBinding(type);
110 String parsedLabel = label;
113 if (binding instanceof StringBinding) {
114 if (binding instanceof MutableStringBinding)
115 value = new MutableString(label);
118 } else if (binding instanceof BooleanBinding) {
119 // This parses a bit more than just "true"/"false".
121 value = PrimitiveValueParser.parseBoolean(label);
122 } catch (IllegalArgumentException e) {
123 throw new DatabaseException(e);
126 if (binding instanceof NumberBinding) {
127 parsedLabel = label.replace(",", ".");
130 value = binding.parseValue(parsedLabel, new DataValueRepository());
132 // FIXME: this is Balas-specific
133 if (targetUnit != null) {
134 if (type instanceof NumberType) {
135 String unit = Variables.getPossibleUnit(graph, variable);
137 IUnitConverter converter = converter(targetUnit, unit);
138 if(type instanceof FloatType) {
139 float converted = (float)converter.convert(((Number)value).floatValue());
140 value = binding.parseValue(Float.toString(converted), new DataValueRepository());
141 } else if(type instanceof DoubleType) {
142 double converted = converter.convert(((Number)value).doubleValue());
143 value = binding.parseValue(Double.toString(converted), new DataValueRepository());
144 } else if(type instanceof IntegerType) {
145 value = binding.parseValue(Integer.toString(((Number)value).intValue()), new DataValueRepository());
152 //System.out.println("VariableWrite " + ObjectUtils.toString(value));
153 variable.setValue(graph, value, binding);
155 String[] varParts = variable.getParent(graph).getURI(graph).split("/");
156 String varName = varParts[varParts.length - 1];
158 // Add a comment to metadata.
159 Layer0Utils.addCommentMetadata(graph, "Modified variable " + varName + " to " + ObjectUtils.toString(value));
160 } catch (DataTypeSyntaxError e) {
161 throw new DatabaseException(e);
162 } catch (BindingException e) {
163 throw new DatabaseException(e);