]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation/src/org/simantics/simulation/data/PseudoSolver.java
9a5be411b09df0b4cd90981cb80c6b6fc57953f3
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / data / PseudoSolver.java
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.simulation.data;
12
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;
18
19 import org.simantics.databoard.Datatypes;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.NumberBinding;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.type.Datatype;
24 import org.simantics.databoard.util.Bean;
25
26 /**
27  * Pseudo solver has three variables: Sine, Ramp and Random
28  * 
29  * Values are sampled in real time until the solver is disposed.
30  *
31  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
32  */
33 public class PseudoSolver extends AbstractDatasource {
34         
35         public static String SINE = "Sine";
36         public static String RAMP = "Ramp";
37         public static String RANDOM = "Random";
38         public static List<String> KEYS;
39         
40         Thread thread;
41         long startTime;
42         double time;
43         Random random;
44         int cycle = 0;
45                 
46         public PseudoSolver() {
47                 super();                
48                 random = new Random();
49         }
50         
51         @Override
52         public Collection<String> getVariables() {
53                 return KEYS;
54         }
55         
56         public void start() {
57                 thread = new Thread() {
58                         @Override
59                         public void run() {
60                                 startTime = System.nanoTime();
61                                 notifyStep();
62                                 while (Thread.currentThread() == thread) {
63                                         writeLock.lock();
64                                         try {
65                                                 if (Thread.currentThread() != thread) return;
66                                                 for (int i=0; i<10; i++) {
67                                                         cycle++;
68                                                         time = cycle * 0.01;
69                                                         notifyStep();
70                                                 }
71                                         } finally {
72                                                 writeLock.unlock();
73                                         }
74                                         try {Thread.sleep(15); } catch (InterruptedException e) {}
75                                 }
76                         }
77                 };
78                 thread.start();
79         }
80         
81         public void stop() {
82                 writeLock.lock();
83                 try {
84                         thread = null;
85                 } finally {
86                         writeLock.unlock();
87                 }
88         }
89         
90         @Override
91         public Datatype getType(String key) {
92                 //if (key.equals("Ramp")) return Datatypes.BOOLEAN;
93                 return Datatypes.DOUBLE;
94         }
95         
96         @Override
97         public Object getTime(NumberBinding binding) {
98                 try {
99                         return binding.create(time);
100                 } catch (BindingException e) {
101                         throw new RuntimeException(e);
102                 }
103         }
104
105         @Override
106         public VariableHandle openHandle(Bean item, String key, Binding valueBinding) {
107                 getValue(key, valueBinding);
108                 return new MyVariableHandle(key, valueBinding);
109         }
110         
111         public Object getValue(String key, Binding binding) {
112                 if (binding instanceof NumberBinding == false) throw new IllegalArgumentException("binding must be NumberBinding");
113                 NumberBinding nb = (NumberBinding) binding;
114                 try {
115                         if (key.equals(SINE)) {
116                                 double value = Math.sin(time);
117                                 return nb.create(value);
118                         } else if (key.equals(RAMP)) {
119                                 boolean up = time - Math.floor(time) >= 0.5;
120                                 double value = up ? 1.0 : 0.0; 
121                                 return value;                           
122                         } else if (key.equals(RANDOM)) {
123                                 return random.nextDouble()*10;
124                         }
125                 } catch (BindingException e) {
126                         throw new RuntimeException(e);
127                 }
128                 throw new RuntimeException("Unknown variable "+key);
129         }       
130         
131         static {
132                 List<String> keys = new ArrayList<String>();
133                 keys.add(SINE);
134                 keys.add(RAMP);
135                 keys.add(RANDOM);
136                 KEYS = Collections.unmodifiableList( keys );
137         }
138         
139         class MyVariableHandle implements VariableHandle {
140
141                 String key;             
142                 Binding b;
143                 
144                 public MyVariableHandle(String key, Binding b) {
145                         this.b = b;
146                         this.key = key;
147                 }
148
149                 @Override
150                 public String key() {
151                         return key;
152                 }
153                 
154                 @Override
155                 public Binding binding() {
156                         return b;
157                 }
158
159                 @Override
160                 public Object getValue() {
161                         return PseudoSolver.this.getValue(key, b);
162                 }
163
164                 @Override
165                 public void dispose() {
166                 }
167                 
168         }
169 }
170