jadex.bridge.nonfunctional.hardconstraints.RHardConstraints Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform-bridge Show documentation
Show all versions of jadex-platform-bridge Show documentation
Jadex bridge is a base package for kernels and platforms, i.e., it is used by both and provides commonly used interfaces and classes for active components and their management.
package jadex.bridge.nonfunctional.hardconstraints;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.IService;
import jadex.bridge.service.ServiceScope;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.ServiceQuery;
import jadex.commons.ComposedRemoteFilter;
import jadex.commons.IAsyncFilter;
import jadex.commons.MethodInfo;
import jadex.commons.future.CollectionResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.commons.future.ITerminableIntermediateFuture;
import jadex.commons.future.TerminableIntermediateFuture;
/**
* Class defining runtime hard constraints.
*
*/
public class RHardConstraints
{
/** The component. */
protected IExternalAccess component;
/** Hard constraint model */
protected Collection constraintmodel;
/** The basic hard constraints filter */
protected List> filters = new ArrayList>();
/** Unbound constant value filters */
protected List unboundconstantfilters = new ArrayList();
/**
* Creates the runtime hard constraints.
*
* @param mhc The declared model hard constraints.
*/
public RHardConstraints(IExternalAccess component, Collection constraintmodel)
{
this.component = component;
this.constraintmodel = constraintmodel;
for(MHardConstraint hc : constraintmodel)
{
if (MHardConstraint.CONSTANT.equals(hc.getOperator()))
{
addFilter(new ConstantValueFilter(hc.getPropertyName(), hc.getValue()));
}
else if (MHardConstraint.GREATER.equals(hc.getOperator()))
{
addFilter(new StrictInequalityFilter(false));
}
else if (MHardConstraint.LESS.equals(hc.getOperator()))
{
addFilter(new StrictInequalityFilter(true));
}
else if (MHardConstraint.GREATER_OR_EQUAL.equals(hc.getOperator()))
{
addFilter(new InequalityFilter(false));
}
else if (MHardConstraint.LESS_OR_EQUAL.equals(hc.getOperator()))
{
addFilter(new StrictInequalityFilter(true));
}
else
{
throw new RuntimeException("Unknown hard constraint type: " + hc.getOperator());
}
}
}
/**
* Adds a filter.
*
* @param filter The filter.
*/
protected void addFilter(IAsyncFilter filter)
{
if (filter instanceof ConstantValueFilter &&
((ConstantValueFilter) filter).getValue() == null)
{
unboundconstantfilters.add((ConstantValueFilter) filter);
}
else
{
filters.add(filter);
}
}
/**
* Gets the filter that is remotable.
*
* @return Remotable filter.
*/
public IAsyncFilter> getRemotableFilter()
{
IAsyncFilter> ret = null;
if (filters.isEmpty())
{
ret = IAsyncFilter.ALWAYS;
}
else
{
ret = new ComposedRemoteFilter(filters.toArray(new IAsyncFilter[filters.size()]));
}
return (IAsyncFilter>) ret;
}
/**
* Gets the filter for local filtering.
*
* @return Filter for local filtering.
*/
public IAsyncFilter> getLocalFilter()
{
return getLocalFilter(null);
}
/**
* Gets the filter for local filtering.
*
* @return Filter for local filtering.
*/
public IAsyncFilter getLocalFilter(final MethodInfo method)
{
IAsyncFilter ret = null;
if(unboundconstantfilters.isEmpty())
{
ret = (IAsyncFilter)IAsyncFilter.ALWAYS;
}
else
{
ret = new IAsyncFilter()
{
public IFuture filter(final IService service)
{
final Future filterret = new Future();
final List boundconstantfilters = new ArrayList();
final CollectionResultListener constantrl = new CollectionResultListener(unboundconstantfilters.size(), false, new IResultListener>()
{
public void resultAvailable(Collection result)
{
boolean filterresult = false;
if(result!=null)
{
Boolean[] results = result.toArray(new Boolean[result.size()]);
filterresult = true;
for (int i = 0; i < results.length && filterresult; ++i)
{
filterresult &= results[i];
}
}
if(!filterresult)
{
for (ConstantValueFilter bfil : boundconstantfilters)
{
bfil.unbind();
}
}
filterret.setResult(filterresult);
};
public void exceptionOccurred(Exception exception)
{
resultAvailable(null);
}
});
for (int i = 0; i < unboundconstantfilters.size(); ++i)
{
final ConstantValueFilter filter = unboundconstantfilters.get(i);
component.getMethodNFPropertyValue(((IService)service).getServiceId(), method, filter.getValueName())
.addResultListener(new IResultListener