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;
39 public class VariableWrite extends WriteRequest {
41 final private Variable variable;
42 final private String label;
43 final private String targetUnit;
45 public VariableWrite(Variable variable, String label) {
46 this(variable, label, null, null);
49 public VariableWrite(Variable variable, String label, VirtualGraph provider, String targetUnit) {
51 this.variable = variable;
53 this.targetUnit = targetUnit;
56 public static IUnitConverter converter(final String unit, final String targetUnit) {
58 return new IUnitConverter() {
61 public double convert(double value) {
63 if(targetUnit == null) return value;
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;
74 value = org.simantics.databoard.Units.convert(value, unit, targetUnit);
75 } catch (UnitParseException e) {
90 public void perform(WriteGraph graph) throws DatabaseException {
91 graph.markUndoPoint();
93 //System.err.println("VariableWrite " + variable.getURI(graph) + " => " + label);
94 Datatype type = variable.getPossibleDatatype(graph);
98 uri = variable.getURI(graph);
99 } catch (DatabaseException e) {
101 throw new org.simantics.db.exception.BindingException("no datatype for variable " + variable + " (URI=" + uri + ")", null);
104 Binding binding = Bindings.getBinding(type);
106 String parsedLabel = label;
109 if (binding instanceof StringBinding) {
110 if (binding instanceof MutableStringBinding)
111 value = new MutableString(label);
114 } else if (binding instanceof BooleanBinding) {
115 // This parses a bit more than just "true"/"false".
117 value = PrimitiveValueParser.parseBoolean(label);
118 } catch (IllegalArgumentException e) {
119 throw new DatabaseException(e);
122 if (binding instanceof NumberBinding) {
123 parsedLabel = label.replace(",", ".");
126 value = binding.parseValue(parsedLabel, new DataValueRepository());
128 // FIXME: this is Balas-specific
129 if (targetUnit != null) {
130 if (type instanceof NumberType) {
131 String unit = Variables.getPossibleUnit(graph, variable);
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());
147 variable.setValue(graph, value, binding);
149 String[] varParts = variable.getParent(graph).getURI(graph).split("/");
150 String varName = varParts[varParts.length - 1];
152 // Add a comment to metadata.
153 Layer0Utils.addCommentMetadata(graph, "Modified variable " + varName + " to " + ObjectUtils.toString(value));
155 } catch (DataTypeSyntaxError e) {
156 throw new DatabaseException(e);
157 } catch (BindingException e) {
158 throw new DatabaseException(e);