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