]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableWriteImplied.java
Merge "Fixed ProfileObserver.update race with multiple query threads"
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / VariableWriteImplied.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.CommentMetadata;
35 import org.simantics.db.common.request.WriteRequest;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.layer0.util.PrimitiveValueParser;
38
39 public class VariableWriteImplied extends WriteRequest {
40
41     final private Variable variable;
42     final private String label;
43     final private String targetUnit;
44
45     public VariableWriteImplied(Variable variable, String label) {
46         this(variable, label, null, null);
47     }
48
49     public VariableWriteImplied(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
92         try {
93             //System.err.println("VariableWriteImplied " + 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                 //System.out.println("VariableWrite " + parsedLabel);
127                 value = binding.parseValue(parsedLabel, new DataValueRepository());
128                 //System.out.println("VariableWrite value " + value);
129
130                 // FIXME: this is Balas-specific
131                 if (targetUnit != null) {
132                     if (type instanceof NumberType) {
133                         String unit = Variables.getPossibleUnit(graph, variable);
134                         if (unit != null) {
135                             IUnitConverter converter = converter(targetUnit, unit);
136                             if(type instanceof FloatType) {
137                                 float converted = (float)converter.convert(((Number)value).floatValue());
138                                 value = binding.parseValue(Float.toString(converted), new DataValueRepository());
139                             } else if(type instanceof DoubleType) {
140                                 double converted = converter.convert(((Number)value).doubleValue());
141                                 value = binding.parseValue(Double.toString(converted), new DataValueRepository());
142                             } else if(type instanceof IntegerType) {
143                                 value = binding.parseValue(Integer.toString(((Number)value).intValue()), new DataValueRepository());
144                             }
145                         }
146                     }
147                 }
148             }
149
150             //System.out.println("VariableWriteImplied " + ObjectUtils.toString(value));
151             variable.setValue(graph, value, binding);
152
153             // Add a comment to metadata.
154             CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
155             graph.addMetadata(cm.add("Set value " + ObjectUtils.toString(value)));
156
157         } catch (DataTypeSyntaxError e) {
158             throw new DatabaseException(e);
159         } catch (BindingException e) {
160             throw new DatabaseException(e);
161         }
162
163     }
164
165 }