1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.simulation.data;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Random;
19 import org.simantics.databoard.Datatypes;
20 import org.simantics.databoard.accessor.error.AccessorException;
21 import org.simantics.databoard.binding.Binding;
22 import org.simantics.databoard.binding.NumberBinding;
23 import org.simantics.databoard.binding.error.BindingException;
24 import org.simantics.databoard.type.Datatype;
25 import org.simantics.databoard.util.Bean;
28 * Pseudo solver has three variables: Sine, Ramp and Random
30 * Values are sampled in real time until the solver is disposed.
32 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
34 public class PseudoSolver extends AbstractDatasource {
36 public static String SINE = "Sine";
37 public static String RAMP = "Ramp";
38 public static String RANDOM = "Random";
39 public static List<String> KEYS;
47 public PseudoSolver() {
49 random = new Random();
53 public Collection<String> getVariables() {
58 thread = new Thread() {
61 startTime = System.nanoTime();
63 while (Thread.currentThread() == thread) {
66 if (Thread.currentThread() != thread) return;
67 for (int i=0; i<10; i++) {
75 try {Thread.sleep(15); } catch (InterruptedException e) {}
92 public Datatype getType(String key) {
93 //if (key.equals("Ramp")) return Datatypes.BOOLEAN;
94 return Datatypes.DOUBLE;
98 public Object getTime(NumberBinding binding) {
100 return binding.create(time);
101 } catch (BindingException e) {
102 throw new RuntimeException(e);
107 public VariableHandle openHandle(Bean item, String key, Binding valueBinding) {
108 getValue(key, valueBinding);
109 return new MyVariableHandle(key, valueBinding);
112 public Object getValue(String key, Binding binding) {
113 if (binding instanceof NumberBinding == false) throw new IllegalArgumentException("binding must be NumberBinding");
114 NumberBinding nb = (NumberBinding) binding;
116 if (key.equals(SINE)) {
117 double value = Math.sin(time);
118 return nb.create(value);
119 } else if (key.equals(RAMP)) {
120 boolean up = time - Math.floor(time) >= 0.5;
121 double value = up ? 1.0 : 0.0;
123 } else if (key.equals(RANDOM)) {
124 return random.nextDouble()*10;
126 } catch (BindingException e) {
127 throw new RuntimeException(e);
129 throw new RuntimeException("Unknown variable "+key);
133 List<String> keys = new ArrayList<String>();
137 KEYS = Collections.unmodifiableList( keys );
140 class MyVariableHandle implements VariableHandle {
145 public MyVariableHandle(String key, Binding b) {
151 public String key() {
156 public Binding binding() {
161 public Object getValue() {
162 return PseudoSolver.this.getValue(key, b);
166 public Object getValue(Datasource datasource) throws AccessorException {
167 return PseudoSolver.this.getValue(key, b);
171 public void dispose() {