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

jadex.bridge.nonfunctional.NFMethodPropertyProvider 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;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IMonitoringComponentFeature;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishEventLevel;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishTarget;
import jadex.bridge.service.types.monitoring.MonitoringEvent;
import jadex.commons.MethodInfo;
import jadex.commons.future.CounterResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;

/**
 *  Default implementation for a method property provider.
 */
public class NFMethodPropertyProvider extends NFPropertyProvider implements INFMixedPropertyProvider
{
	/** Non-functional properties of methods. */
	protected Map>> methodnfproperties;
	
//	/**
//	 *  Create a new provider.
//	 */
//	public NFMethodPropertyProvider(INFPropertyProvider parent)
//	{
//		super(parent);
//	}
	
	/**
	 *  Create a new provider.
	 */
	public NFMethodPropertyProvider(IComponentIdentifier parent, IInternalAccess component)
	{
		super(parent, component);
	}
	
	/**
	 *  Returns meta information about a non-functional properties of all methods.
	 *  @return The meta information about a non-functional properties.
	 */
	public IFuture>>  getMethodNFPropertyMetaInfos()
	{
		Map> ret = new HashMap>();
		if(methodnfproperties!=null)
		{
			for(MethodInfo mi: methodnfproperties.keySet())
			{
				Map res = new HashMap();
				ret.put(mi, res);
				Map> tmp = methodnfproperties.get(mi);
				for(String name: tmp.keySet())
				{
					INFProperty prop = tmp.get(name);
					res.put(name, prop.getMetaInfo());
				}
			}
		}
		return new Future>>(ret);
	}
	
	/**
	 *  Returns meta information about a non-functional properties of a method.
	 *  @return The meta information about a non-functional properties.
	 */
	public IFuture> getMethodNFPropertyMetaInfos(MethodInfo method)
	{
		Map ret = new HashMap();
		
		if(methodnfproperties!=null)
		{
			Map> tmp = methodnfproperties.get(method);
			for(String name: tmp.keySet())
			{
				INFProperty prop = tmp.get(name);
				ret.put(name, prop.getMetaInfo());
			}
		}
		
		return new Future>(ret);
	}

	
	/**
	 *  Returns the names of all non-functional properties of the specified method.
	 *  @param method The method targeted by this operation.
	 *  @return The names of the non-functional properties of the specified method.
	 */
	public IFuture getMethodNFPropertyNames(MethodInfo method)
	{
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		return new Future(nfmap != null? nfmap.keySet().toArray(new String[nfproperties.size()]) : new String[0]);
	}
	
	/**
	 *  Returns the names of all non-functional properties of this method.
	 *  @return The names of the non-functional properties of this method.
	 */
	// todo: does this method makes sense because on parent it invokes getNFAllPropertyNames?
	public IFuture getMethodNFAllPropertyNames(final MethodInfo method)
	{
		final Future ret = new Future();
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		final String[] myprops = nfmap != null? nfmap.keySet().toArray(new String[nfproperties.size()]) : new String[0];
		
		if(getParentId()!=null)
		{
//			IComponentManagementService cms = getInternalAccess().getFeature(IRequiredServicesFeature.class).getLocalService(new ServiceQuery<>(IComponentManagementService.class));
			getInternalAccess().getExternalAccessAsync(getParentId()).addResultListener(new ExceptionDelegationResultListener(ret)
			{
				public void customResultAvailable(IExternalAccess component) 
				{
					component.getNFAllPropertyNames().addResultListener(new DelegationResultListener(ret)
					{
						public void customResultAvailable(String[] result)
						{
							Set tmp = new LinkedHashSet();
							for(String p: result)
							{
								tmp.add(p);
							}
							for(String p: myprops)
							{
								tmp.add(p);
							}
							ret.setResult((String[])tmp.toArray(new String[tmp.size()]));
						}
					});
				}
			});
		}
		else
		{
			ret.setResult(myprops);		
		}
		
//		getParent().addResultListener(new ExceptionDelegationResultListener(ret)
//		{
//			public void customResultAvailable(INFPropertyProvider parent)
//			{
//				if(parent!=null)
//				{
//					parent.getNFAllPropertyNames().addResultListener(new DelegationResultListener(ret)
//					{
//						public void customResultAvailable(String[] result)
//						{
//							Set tmp = new LinkedHashSet();
//							for(String p: result)
//							{
//								tmp.add(p);
//							}
//							for(String p: myprops)
//							{
//								tmp.add(p);
//							}
//							ret.setResult((String[])tmp.toArray(new String[tmp.size()]));
//						}
//					});
//				}
//				else
//				{
//					ret.setResult(myprops);
//				}
//			}
//		});
			
		return ret;
	}
	
	/**
	 *  Returns the meta information about a non-functional property of the specified method.
	 *  @param method The method targeted by this operation.
	 *  @param name Name of the property.
	 *  @return The meta information about a non-functional property of the specified method.
	 */
	public IFuture getMethodNFPropertyMetaInfo(MethodInfo method, String name)
	{
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		INFProperty prop = nfmap != null? nfmap.get(name) : null;
		INFPropertyMetaInfo mi = prop != null? prop.getMetaInfo() : null;
		return mi != null? new Future(mi) : getNFPropertyMetaInfo(name);
	}
	
	/**
	 *  Returns the current value of a non-functional property of the specified method.
	 *  @param method The method targeted by this operation.
	 *  @param name Name of the property.
	 *  @param type Type of the property value.
	 *  @return The current value of a non-functional property of the specified method.
	 */
	public  IFuture getMethodNFPropertyValue(MethodInfo method, String name)
	{
		Future ret = new Future();
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		INFProperty prop = (INFProperty) (nfmap != null? nfmap.get(name) : null);
		if(prop != null)
		{
			try
			{
				prop.getValue().addResultListener(new DelegationResultListener(ret));
			}
			catch (Exception e)
			{
				ret.setException(e);
			}
		}
		else
		{
			ret = (Future)getNFPropertyValue(name);
		}
		return ret;
	}
	
	/**
	 *  Returns the current value of a non-functional property of the specified method, performs unit conversion.
	 *  @param method The method targeted by this operation.
	 *  @param name Name of the property.
	 *  @param type Type of the property value.
	 *  @param unit Unit of the property value.
	 *  @return The current value of a non-functional property of the specified method.
	 */
//	public  IFuture getNFPropertyValue(Method method, String name, Class unit)
	public  IFuture getMethodNFPropertyValue(MethodInfo method, String name, U unit)
	{
		Future ret = new Future();
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		INFProperty prop = (INFProperty) (nfmap != null? nfmap.get(name) : null);
		if (prop != null)
		{
			try
			{
				prop.getValue(unit).addResultListener(new DelegationResultListener(ret));
			}
			catch (Exception e)
			{
				ret.setException(e);
			}
		}
		else
		{
			ret = (Future)getNFPropertyValue(name, unit);
		}
		return ret;
	}
	
	/**
	 *  Returns the current value of a non-functional property of this service method.
	 *  @param name Name of the property.
	 *  @param type Type of the property value.
	 *  @return The current value of a non-functional property of this service method.
	 */
	public IFuture getMethodNFPropertyPrettyPrintValue(MethodInfo method, String name) 
	{
		Future ret = new Future();
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		INFProperty prop = (INFProperty) (nfmap != null? nfmap.get(name) : null);
		if(prop != null)
		{
			try
			{
				prop.getPrettyPrintValue().addResultListener(new DelegationResultListener(ret));
			}
			catch (Exception e)
			{
				ret.setException(e);
			}
		}
		else
		{
			ret = (Future)getNFPropertyPrettyPrintValue(name);
		}
		return ret;
	}
	
	/**
	 *  Add a non-functional property.
	 *  @param method The method targeted by this operation.
	 *  @param nfprop The property.
	 */
	public IFuture addMethodNFProperty(MethodInfo method, INFProperty nfprop)
	{
		final Future ret = new Future();
		
		if(methodnfproperties==null)
			methodnfproperties = new HashMap>>();
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		if (nfmap == null)
		{
			nfmap = new HashMap>();
			methodnfproperties.put(method, nfmap);
		}
		nfmap.put(nfprop.getName(), nfprop);
		
		if(getInternalAccess().getFeature(IMonitoringComponentFeature.class).hasEventTargets(PublishTarget.TOALL, PublishEventLevel.COARSE))
		{
			MonitoringEvent me = new MonitoringEvent(getInternalAccess().getId(), getInternalAccess().getDescription().getCreationTime(), 
				MonitoringEvent.TYPE_PROPERTY_REMOVED, System.currentTimeMillis(), PublishEventLevel.COARSE);
			me.setProperty("propname", nfprop.getName());
			getInternalAccess().getFeature(IMonitoringComponentFeature.class).publishEvent(me, PublishTarget.TOALL).addResultListener(new DelegationResultListener(ret));
		}
		else
		{
			ret.setResult(null);
		}
		
		return ret;
	}
	
	/**
	 *  Remove a non-functional property.
	 *  @param method The method targeted by this operation.
	 *  @param The name.
	 */
	public IFuture removeMethodNFProperty(MethodInfo method, final String name)
	{
		final Future ret = new Future();
		Map> nfmap = methodnfproperties != null? methodnfproperties.get(method) : null;
		if(nfmap != null)
		{
			INFProperty prop = nfmap.remove(name);
			if(prop!=null)
			{
				prop.dispose().addResultListener(new DelegationResultListener(ret)
				{
					public void customResultAvailable(Void result)
					{
						if(getInternalAccess().getFeature(IMonitoringComponentFeature.class).hasEventTargets(PublishTarget.TOALL, PublishEventLevel.COARSE))
						{
							MonitoringEvent me = new MonitoringEvent(getInternalAccess().getId(), getInternalAccess().getDescription().getCreationTime(), 
								MonitoringEvent.TYPE_PROPERTY_REMOVED, System.currentTimeMillis(), PublishEventLevel.COARSE);
							me.setProperty("propname", name);
							getInternalAccess().getFeature(IMonitoringComponentFeature.class).publishEvent(me, PublishTarget.TOALL).addResultListener(new DelegationResultListener(ret));
						}
						else
						{
							ret.setResult(null);
						}
					}
				});
			}
			else
			{
				ret.setResult(null);
			}
		}
		else
		{
			ret.setResult(null);
		}
		return ret;
	}
	
	/**
	 *  Shutdown the provider.
	 */
	public IFuture shutdownNFPropertyProvider()
	{
		final Future ret = new Future();
		
		super.shutdownNFPropertyProvider().addResultListener(new DelegationResultListener(ret)
		{
			public void customResultAvailable(Void result)
			{
				if(methodnfproperties!=null)
				{
					int cnt = 0;
					for(Map> maps: methodnfproperties.values())
					{
						for(INFProperty prop: maps.values())
						{
							cnt++;
						}
					}
					
					CounterResultListener lis = new CounterResultListener(cnt, true, new DelegationResultListener(ret));
					for(Map> maps: methodnfproperties.values())
					{
						for(INFProperty prop: maps.values())
						{
							prop.dispose().addResultListener(lis);
						}
					}
				}
				else
				{
					ret.setResult(null);
				}
			}
		});
		
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy