All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.bridge.nonfunctional.hardconstraints.RHardConstraints Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 4.0.267
Show newest version
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()
						{
							public void resultAvailable(Object result)
							{
								if(filter.getValue() == null)
								{
									filter.bind(result);
									boundconstantfilters.add(filter);
								}
								filter.filter(service).addResultListener(constantrl);
							}
							
							public void exceptionOccurred(Exception exception)
							{
								constantrl.exceptionOccurred(exception);
							}
						});
						
//						INFMixedPropertyProvider prov = ((INFMixedPropertyProvider)((IService)service).getExternalComponentFeature(INFPropertyComponentFeature.class));
//						((IService)service).getNFPropertyValue(propname).addResultListener(new IResultListener()
//						prov.getMethodNFPropertyValue(method, filter.getValueName()).addResultListener(new IResultListener()
////						service.getMethodNFPropertyValue(method, filter.getValueName()).addResultListener(new IResultListener()
//						{
//							public void resultAvailable(Object result)
//							{
//								if (filter.getValue() == null)
//								{
//									filter.bind(result);
//									boundconstantfilters.add(filter);
//								}
//								filter.filter(service).addResultListener(constantrl);
//							}
//							
//							public void exceptionOccurred(Exception exception)
//							{
//								constantrl.exceptionOccurred(exception);
//							}
//						});
					}
					
					return filterret;
				}
			};
		}
		
		return ret;
	}
	
	/**
	 *  Used after searches to make bound filters remotable.
	 */
	public void optimizeFilters()
	{
		List newunboundconstantfilters = new ArrayList();
		for (ConstantValueFilter fil : unboundconstantfilters)
		{
			if (fil.getValue() != null)
			{
				filters.add(fil);
			}
			else
			{
				newunboundconstantfilters.add(fil);
			}
		}
		unboundconstantfilters = newunboundconstantfilters;
	}
	
	public static  ITerminableIntermediateFuture getServices(final IInternalAccess ia, final Class type, final ServiceScope scope, final MethodInfo method, final RHardConstraints hardconstraints)
	{
		if(hardconstraints == null)
		{
			return ia.getFeature(IRequiredServicesFeature.class).searchServices(new ServiceQuery<>(type, scope));
		}
		else
		{
			final TerminableIntermediateFuture ret = new TerminableIntermediateFuture();
			
			// dropped for v4???
//			SServiceProvider.getServices(ea, type, scope, (IAsyncFilter) hardconstraints.getRemotableFilter()).addResultListener(new IResultListener>()
//			{
//				public void resultAvailable(Collection results)
//				{
////					List filteredresults = new ArrayList();
//					IAsyncFilter filter = (IAsyncFilter) hardconstraints.getLocalFilter();
//					
////					CollectionResultListener crl = new CollectionResultListener(results.size(), true, new DelegationResultListener(new IResultListener()
////					{
////						
////					}));
//					
//					for (T result : results)
//					{
//					}
//				}
//				
//				public void exceptionOccurred(Exception exception)
//				{
//					ret.setException(exception);
//				}
//			});
		}
		return null;
	}
}