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

jadex.rules.parser.conditions.javagrammar.SJavaParser 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.parser.conditions.javagrammar;

import jadex.commons.SReflect;
import jadex.rules.state.OAVObjectType;
import jadex.rules.state.OAVTypeModel;

import org.antlr.runtime.TokenStream;

/**
 *  Static helper methods for Java condition parser.
 */
// Currently not used (todo: replace syntactic predicates in primaryPrefix() with semantic ones)
public class SJavaParser
{
	/**
     *  Lookahead for a type.
     *  @param input	The token stream.
     *  @param tmodel	The OAV type model.
     *  @return	The token index of the last token (identifier) or -1 if no type could be matched.
     */
    protected static int	lookaheadType(TokenStream input, OAVTypeModel tmodel, String[] imports)
    {
    	return lookaheadType(1, input, tmodel, imports);
    }

    /**
     *  Lookahead for a type.
     *  @param index	The start index (starts with 1).
     *  @param input	The token stream.
     *  @param tmodel	The OAV type model.
     *  @return	The token index of the last token (identifier) or -1 if no type could be matched.
     */
    protected static int	lookaheadType(int index, TokenStream input, OAVTypeModel tmodel, String[] imports)
    {
    	if(input.LA(index)==JavaJadexLexer.IDENTIFIER)
    	{
	    	String	typename	= input.LT(index).getText();
	    	OAVObjectType	type	= null;
	    	while(type==null && index!=-1)
	    	{
		    	try
		    	{
		        	type	= tmodel.getObjectType(typename);	        	
		    	}
		    	catch(Throwable e)
		    	{
		    		Class	clazz	= SReflect.findClass0(typename, imports, tmodel.getClassLoader());
		    		if(clazz!=null)
		    		{
		    			type	= tmodel.getJavaType(clazz);
		    		}
		    		else if(".".equals(input.LT(index+1).getText()))
		    		{
		    			index	+= 2;
		    			typename	+= "." + input.LT(index).getText();
		    		}
		    		else
		    		{
		    			index	= -1;
		    		}
		    	}
	    	}
    	}
    	else
    	{
    		index	= -1;
    	}
    	
    	return index;
    }

    /**
     *  Lookahead for an existential declaration (type var).
     *  @param input	The token stream.
     *  @param tmodel	The OAV type model.
     *  @return	True for an existential declaration.
     */
    protected static boolean	lookaheadExistential(TokenStream input, OAVTypeModel tmodel, String[] imports)
    {
    	int index	= lookaheadType(input, tmodel, imports);
    	return index!=-1 && input.LA(index+1)==JavaJadexLexer.IDENTIFIER;
    }

    /**
     *  Lookahead for a static field.
     *  @param input	The token stream.
     *  @param tmodel	The OAV type model.
     *  @return	True for a static field.
     */
    protected static boolean	lookaheadStaticField(TokenStream input, OAVTypeModel tmodel, String[] imports)
    {
    	int index	= lookaheadType(input, tmodel, imports);
    	return index!=-1 &&	".".equals(input.LT(index+1).getText()) && input.LA(index+2)==JavaJadexLexer.IDENTIFIER;
    }
    
    /**
     *  Lookahead for a static method.
     *  @param input	The token stream.
     *  @param tmodel	The OAV type model.
     *  @return	True for a static field.
     */
    protected static boolean	lookaheadStaticMethod(TokenStream input, OAVTypeModel tmodel, String[] imports)
    {
    	int index	= lookaheadType(input, tmodel, imports);
    	return index!=-1 &&	".".equals(input.LT(index+1).getText()) && input.LA(index+2)==JavaJadexLexer.IDENTIFIER  &&	"(".equals(input.LT(index+3).getText());
    }

    
    /**
     *  Lookahead for a type cast.
     *  @param input	The token stream.
     *  @param tmodel	The OAV type model.
     *  @return	True for a type cast.
     */
    protected static boolean	lookaheadCast(TokenStream input, OAVTypeModel tmodel, String[] imports)
    {
    	boolean	ret	= "(".equals(input.LT(1).getText());
    	if(ret)
    	{
    		int index	= lookaheadType(2, input, tmodel, imports);
    		ret	= index!=-1 && ")".equals(input.LT(index+1).getText());
    	}
    	return ret;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy