Monday, August 11, 2008

A virtual parameter with a thread in its soul, to simulate a new range of virtual sensors, based on the changing of a value, like fuel

It is intersting actually, two observer/observable patterns, one against the other, to monitor the ongoing value of a ... value ! That simply thanks to a thread and to the common pattern of the earth beat, to decrease or increase with the time, in its own process, with different color levels, a named floating value. This could be fule, for example, or engine oil. It is self sufficient, not needing nothjing more than jdk runtime, and it is so easy that the few classes involved in this game are posted here to let playing everyone who likes:



Parameters threshold for colors:
it.free.i.virtual.machine.hal.devicealias.laboratory.simulator.dynamicparameter.Levels



DynamicParameter factory (actually only the DEcreasing impl is available):
it.free.i.virtual.machine.hal.devicealias.laboratory.simulator.dynamicparameter.
DynamicParameterFactory




Paradigm interface for this entity:
it.free.i.virtual.machine.hal.devicealias.laboratory.simulator.dynamicparameter.
DynamicParameter




The decreasing (and the only written up to now) impl:
it.free.i.virtual.machine.hal.devicealias.laboratory.simulator.dynamicparameter.
DynamicParameterDecreasingImpl






To try just use these lines of test class:

import java.util.Observable;
import java.util.Observer;
import it.free.i.virtual.machine.hal.devicealias.laboratory.simulator.dynamicparameter.*;

public class Prova extends Observable implements Observer{
public Prova(){
init();
}
private void init(){
long hearBeatYeldPause = 1000;
DynamicParameter dynamicParameterDecresingImpl = new DynamicParameterDecreasingImpl(this);
dynamicParameterDecresingImpl.setHeartBeat(100, hearBeatYeldPause);
dynamicParameterDecresingImpl.setParameterName("Fuel");
super.addObserver(dynamicParameterDecresingImpl);
Thread daemonThread = new Thread(dynamicParameterDecresingImpl);
daemonThread.start();
try {
Thread.sleep(110000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i =0; i < 102; i++){
try{
super.setChanged();
super.notifyObservers();
Thread.sleep(hearBeatYeldPause/2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
Prova prova = new Prova();
}
@Override
public void update(Observable observable, Object arg1) {
System.out.println("UPDATE RECEIVED by " + observable.toString() + "");
}
}



No comments: