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

jadex.rules.rulesystem.rete.nodes.MixedIdentityHashSet Maven / Gradle / Ivy

Go to download

Jadex Rules is a small lightweight rule engine, which currently employs the well-known Rete algorithm for highly efficient rule matching. Jadex rules is therefore similar to other rule engines like JESS and Drools. Despite the similarities there are also important differences between these systems: * Jadex Rules is very small and intended to be used as component of other software. Even though rules can be specified in a Java dialect as well as (a small variation of) the CLIPS language its primary usage is on the API level. Jadex Rules is currently the core component of the Jadex BDI reasoning engine. * Jadex Rules cleanly separates between state and rule representation. This allows the state implementation as well as the matcher to be flexibly exchanged. Some experiments have e.g. been conducted with a Jena representation. Regarding the matcher, it is planned to support also the Treat algorithm, which has a lower memory footprint than Rete. * Jadex Rules pays close attention to rule debugging. The state as well as the rete engine can be observed at runtime. The rule debugger provides functionalities to execute a rule program stepwise and also use rule breakpoints to stop the execution at those points.

There is a newer version: 2.4
Show newest version
package jadex.rules.rulesystem.rete.nodes;

import jadex.commons.SUtil;
import jadex.commons.collection.IdentityHashSet;
import jadex.rules.rulesystem.rete.Tuple;
import jadex.rules.state.IOAVState;
import jadex.rules.state.OAVJavaType;
import jadex.rules.state.OAVObjectType;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 *  A mixed identity hash set allows to store java objects
 *  using identity and java values (numbers, strings, etc.)
 *  using equality. Rete tuples are always stored by equality. 
 */
public class MixedIdentityHashSet	implements Set
{
	//-------- attributes --------
	
	/** The state. */
	protected IOAVState	state;
	
	/** The equality map. */
	protected Set	equality;
	
	/** The identity map. */
	protected Set	identity;
	
	//-------- constructors --------
	
	/**
	 *  Create a new mixed identity map.
	 */
	public MixedIdentityHashSet(IOAVState state)
	{
		this.state	= state;
	}
	
	//-------- Set interface --------

	public void clear()
	{
		if(equality!=null) equality.clear();
		if(identity!=null) identity.clear();
	}
	
	public boolean contains(Object value)
	{
		return equality!=null && equality.contains(value) || identity!=null && identity.contains(value);
	}
	
	public boolean isEmpty()
	{
		return (equality==null || equality.isEmpty()) && (identity==null ||	identity.isEmpty());
	}
	
	public boolean add(Object value)
	{
		OAVObjectType	type	= value!=null && !(value instanceof Tuple) ? state.getType(value) : null;
		boolean	ret;
		if(type instanceof OAVJavaType && !OAVJavaType.KIND_VALUE.equals(((OAVJavaType)type).getKind()))
		{
			if(identity==null)
				identity	= new IdentityHashSet();
			ret	= identity.add(value);
		}
		else
		{
			if(equality==null)
				equality	= new LinkedHashSet();
			ret	= equality.add(value);
		}
		return ret;
	}
	
	public boolean	addAll(Collection coll)
	{
		boolean	ret	= false;
		for(Iterator it=coll.iterator(); it.hasNext();)
		{
			ret	= add(it.next()) || ret;
		}
		return ret;
	}
	
	public boolean remove(Object value)
	{
		return equality!=null && equality.contains(value) ? equality.remove(value) : identity!=null ? identity.remove(value) : false; 
	}
	
	public int size()
	{
		return (equality!=null ? equality.size() : 0) + (identity!=null ? identity.size() : 0);
	}
	
	public boolean containsAll(Collection coll)
	{
		boolean	ret	= true;
		for(Iterator it=coll.iterator(); ret && it.hasNext();)
		{
			ret	= contains(it.next());
		}
		return ret;
	}
	
	public Iterator iterator()
	{
		return new Iterator()
		{
			Iterator it1 = equality!=null ? equality.iterator() : null;
			Iterator it2 = identity!=null ? identity.iterator() : null;
			
			public boolean hasNext()
			{
				return it1!=null && it1.hasNext() || it2!=null && it2.hasNext();
			}
			
			public Object next()
			{
				Object ret = null;
				if(it1!=null && it1.hasNext())
				{
					ret = it1.next();
				}
				else if(it2!=null && it2.hasNext())
				{
					ret = it2.next();
				}
				else
				{
					throw new RuntimeException("No next element.");
				}
				return ret;
			}
			
			public void remove()
			{
				throw new UnsupportedOperationException();
			}
		};
	}
	
	public boolean removeAll(Collection coll)
	{
		boolean	ret	= false;
		for(Iterator it=coll.iterator(); it.hasNext();)
		{
			ret	= remove(it.next()) || ret;
		}
		return ret;
	}
	
	public boolean retainAll(Collection coll)
	{
		boolean	ret	= false;
		Object[]	values	= toArray();
		for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy