
jadex.nfproperty.impl.SimpleValueNFProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nfproperty Show documentation
Show all versions of nfproperty Show documentation
Non functional properties feature
package jadex.nfproperty.impl;
import jadex.common.ClassInfo;
import jadex.core.ComponentTerminatedException;
import jadex.core.IComponent;
import jadex.execution.IExecutionFeature;
import jadex.future.Future;
import jadex.future.IFuture;
import jadex.future.IResultListener;
import jadex.nfproperty.sensor.unit.IConvertableUnit;
import jadex.nfproperty.sensor.unit.IPrettyPrintUnit;
/**
*
*/
public abstract class SimpleValueNFProperty extends AbstractNFProperty
{
/** The current value. */
protected T value;
/** The component. */
protected IComponent comp;
/**
* Create a new property.
*/
public SimpleValueNFProperty(final IComponent comp, final NFPropertyMetaInfo mi)
{
super(mi);
this.comp = comp;
if(mi.isDynamic() && mi.getUpdateRate()>0)
{
setValue(measureValue());
IResultListener res = new IResultListener()
{
public void resultAvailable(Void result)
{
cont();
}
public void exceptionOccurred(Exception exception)
{
if(!(exception instanceof ComponentTerminatedException)
|| !comp.getId().equals(((ComponentTerminatedException) exception).getComponentIdentifier()))
{
//System.out.println("Exception in nfproperty: "+mi.getName()+" "+exception);
//comp.getLogger().warning("Exception in nfproperty: "+mi.getName()+" "+exception);
System.out.println("Exception in nfproperty: "+mi.getName()+" "+exception);
}
}
protected void cont()
{
setValue(measureValue());
comp.getFeature(IExecutionFeature.class).waitForDelay(mi.getUpdateRate()).addResultListener(this);
}
};
comp.getFeature(IExecutionFeature.class).waitForDelay(mi.getUpdateRate()).addResultListener(res);
}
else
{
setValue(measureValue());
}
}
/**
* Get the value converted by a unit.
*/
public IFuture getValue(U unit)
{
if(getMetaInfo().isDynamic() && getMetaInfo().getUpdateRate()==0)
setValue(measureValue());
T ret = value;
if(unit instanceof IConvertableUnit)
ret = ((IConvertableUnit)unit).convert(ret);
return new Future(ret);
}
/**
* Returns the current value of the property in a human readable form.
* @return The current value of the property.
*/
public IFuture getPrettyPrintValue()
{
Future ret = new Future<>();
getValue().then(v ->
{
NFPropertyMetaInfo mi = getMetaInfo();
ClassInfo ci = mi.getUnit();
if(ci!=null)
{
Class> cl = ci.getType(comp.getClass().getClassLoader());
if(cl.isEnum())
{
Object[] enums = cl.getEnumConstants();
if(enums!=null)
{
Object e = enums[0];
if(e instanceof IPrettyPrintUnit)
{
ret.setResult(((IPrettyPrintUnit
© 2015 - 2025 Weber Informatics LLC | Privacy Policy