[jboss-svn-commits] JBL Code SVN: r9753 - in labs/jbossrules/trunk/drools-compiler/src: main/java/org/drools/semantics/java and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sun Feb 25 19:17:38 EST 2007
Author: mark.proctor at jboss.com
Date: 2007-02-25 19:17:38 -0500 (Sun, 25 Feb 2007)
New Revision: 9753
Removed:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/MethodResolver.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/StaticMethodFunctionResolver.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/StaticMethodFunctionResolverTest.java
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/AccumulateBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/BuildUtils.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ColumnBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ConsequenceBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/EvalBuilder.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/RuleClassBuilder.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/KnowledgeHelperFixerTest.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/RuleBuilderTest.java
Log:
JBRULES-707 Move function implementation to use static imports
-We now have full support for static imports.
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -56,10 +56,8 @@
import org.drools.rule.Package;
import org.drools.rule.Rule;
import org.drools.semantics.java.FunctionBuilder;
-import org.drools.semantics.java.FunctionFixer;
import org.drools.semantics.java.PackageStore;
import org.drools.semantics.java.RuleBuilder;
-import org.drools.semantics.java.StaticMethodFunctionResolver;
import org.drools.spi.FunctionResolver;
import org.drools.xml.XmlPackageReader;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
@@ -82,7 +80,6 @@
private Map errorHandlers;
private List generatedClassList;
private TypeResolver typeResolver;
- private FunctionFixer functionFixer;
private FunctionResolver functionResolver;
private ClassFieldExtractorCache classFieldExtractorCache;
private Map lineMappings;
@@ -206,7 +203,6 @@
}
builder = new RuleBuilder( getTypeResolver(),
- getFunctionFixer(),
this.classFieldExtractorCache );
//only try to compile if there are no parse errors
@@ -274,7 +270,7 @@
}
for ( final Iterator it = packageDescr.getFunctionImports().iterator(); it.hasNext(); ) {
- pkg.addFunctionImport( ((FunctionImportDescr) it.next()).getTarget() );
+ pkg.addStaticImport( ((FunctionImportDescr) it.next()).getTarget() );
}
final TypeResolver typeResolver = new ClassTypeResolver( pkg.getImports(),
@@ -324,6 +320,7 @@
private void addFunction(final FunctionDescr functionDescr) {
String functionClassName = this.pkg.getName() + "." + ucFirst( functionDescr.getName() );
+ this.pkg.addStaticImport( functionClassName + "." + functionDescr.getName() );
functionDescr.setClassName( functionClassName );
final FunctionBuilder builder = new FunctionBuilder();
@@ -333,7 +330,6 @@
functionDescr,
builder.build( this.pkg,
functionDescr,
- getFunctionFixer(),
getTypeResolver(),
lineMappings ),
this.src,
@@ -417,23 +413,6 @@
return this.typeResolver;
}
- private FunctionResolver getFunctionResolver() {
- if ( this.functionResolver == null ) {
- this.functionResolver = new StaticMethodFunctionResolver( this.pkg.getFunctionImports(),
- getTypeResolver() );
- }
-
- return this.functionResolver;
- }
-
- private FunctionFixer getFunctionFixer() {
- if ( this.functionFixer == null ) {
- this.functionFixer = new FunctionFixer( this.pkg,
- getFunctionResolver() );
- }
- return this.functionFixer;
- }
-
/**
* This will setup the semantic components of the rule for compiling later on.
* It will not actually call the compiler
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -44,7 +44,6 @@
public String build(final Package pkg,
final FunctionDescr functionDescr,
- final FunctionFixer fixer,
final TypeResolver typeResolver,
final Map lineMappings) {
final StringTemplate st = FunctionBuilder.functionGroup.getInstanceOf( "function" );
@@ -82,7 +81,7 @@
}
st.setAttribute( "text",
- fixer.fix( functionDescr.getText(), new DeclarationScopeResolver( new Map[] { params } ) ) );
+ functionDescr.getText() );
String text = st.toString();
Deleted: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/FunctionFixer.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -1,286 +0,0 @@
-package org.drools.semantics.java;
-
-/*
- * Copyright 2005 JBoss Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.drools.spi.DeclarationScopeResolver;
-import org.drools.spi.FunctionResolver;
-import org.drools.rule.Package;
-
-/**
- * This horrific utility adds in the function class name (which is the same as the functions method name)
- * into the RHS guts of a rule. It has to tip toe around method calls, new declarations and other
- * stuff like that.
- * A better future solution is to use a static import as found in Java 5, then ALL THIS can
- * disappear. Oh Happy day.
- *
- * @author Michael Neale (sadly..)
- * @author Ricardo Barone
- * (Ricardo actually made this all work !).
- *
- */
-public class FunctionFixer {
-
- static Pattern FUNCTION = Pattern.compile( "(\\S*\\s*|\\.\\s*)\\b([\\S&&[^\\.\\(\\)]]+)\\s*\\(([^)]*)\\)",
- Pattern.DOTALL );
- static final Set KEYWORDS = getJavaKeywords();
-
- private final FunctionResolver resolver;
-
- private final Package pkg;
-
- public FunctionFixer(Package pkg,
- FunctionResolver resolver) {
- this.resolver = resolver;
- this.pkg = pkg;
- }
-
- public String fix(final String raw) {
- //return raw;
- return fix( raw,
- FunctionFixer.FUNCTION,
- null );
- }
-
- public String fix(final String raw, final DeclarationScopeResolver variables) {
- //return raw;
- return fix( raw,
- FunctionFixer.FUNCTION,
- variables);
- }
-
- public String fix(final String raw,
- final Pattern pattern,
- final DeclarationScopeResolver variables ) {
- if ( raw == null ) {
- return null;
- }
- final StringBuffer buf = new StringBuffer();
- int lastIndex = 0, startIndex = 0;
-
- final Matcher matcher = pattern.matcher( raw );
- while ( matcher.find( startIndex ) ) {
-
- // instead of just using matcher.end(), grow the endIndex
- // as necessary to close all '(' in the matched String
- final int endIndex = findEndParenthesis( raw,
- matcher );
- if ( endIndex < 0 ) {
- // this means that the first '(' is inside quotes - jump it and try again
- startIndex = matcher.start( 3 );
- continue;
- } else {
- // next iteration will start from here
- startIndex = endIndex;
- }
-
- String params = matcher.group( 3 ).trim();
- if ( endIndex > matcher.end() ) {
- // params have to grow since endIndex changed
- params += raw.substring( matcher.end() - 1,
- endIndex - 1 );
- }
- // Recursively process parameters
- params = fix( params,
- pattern,
- variables );
-
- String function = null;
-
- final String pre = matcher.group( 1 );
- if ( matcher.group( 1 ) != null ) {
- final String trimmedPre = pre.trim();
- if ( trimmedPre.endsWith( "." ) || trimmedPre.endsWith( "new" ) ) {
- // leave alone
- function = raw.substring( matcher.start( 2 ),
- matcher.start( 3 ) - 1 );
- }
- }
-
- if ( function == null ) {
- function = matcher.group( 2 ).trim();
- // if we have a reserved work, DO NOT TOUCH !
- // if we have no function name, DO NOT TOUCH !
- if ( function == null || function.length() == 0 || FunctionFixer.KEYWORDS.contains( function ) ) {
- function = raw.substring( matcher.start( 2 ),
- matcher.start( 3 ) - 1 );
- } else {
- if ( this.pkg.getFunctions().contains( function ) ) {
- function = ucFirst( function ) + "." + function;
- } else {
- function = resolver.resolveFunction( function,
- params,
- variables) + "." + function;
- }
- }
- }
-
- // Every scenario must reach this so that "params"
- // are correctly processed
- final String target = function + "(" + params + ")";
-
- buf.append( raw.substring( lastIndex,
- matcher.start( 2 ) ) );
- buf.append( target );
-
- lastIndex = endIndex;
- }
-
- buf.append( raw.substring( lastIndex ) );
- return buf.toString();
- }
-
- private String ucFirst(final String name) {
- return name.toUpperCase().charAt( 0 ) + name.substring( 1 );
- }
-
- /**
- * Search a raw string for all '(' after matcher.start(3), until
- * the match is over and return the end index the contains the last
- * ')' needed to close all opened parenthesis.
- *
- * @param raw
- * The raw String containg the match and everything else.
- * @param matcher
- * The matched stuff.
- * @return the index of the last ')' needed to close all '(' in the match,
- * or -1 if the first '(' is inside quotes (and thus being invalid).
- */
- private int findEndParenthesis(final String raw,
- final Matcher matcher) {
- // start is the first '('; end is the end of the match
- final int start = matcher.start( 3 ) - 1;
- int end = matcher.end();
-
- // Count the number of '(' and ')' in raw String
- int oCount = 0, cCount = 0;
-
- // Handles text inside quotes (""):
- // * -1 means no quote opened
- // * positive int represent the index of the opened quote
- int lastQuoteIndex = -1;
-
- for ( int i = 0; i < raw.length(); i++ ) {
-
- // if we reached the end of raw and opened/close parenthesis are OK
- if ( i > end && oCount == cCount ) {
- // Check if there was an opened quote that was never closed
- // (before the first '(')
- if ( lastQuoteIndex >= 0 && lastQuoteIndex <= start ) {
- return -1;
- }
- // Everything OK - we are done!
- break;
- }
-
- switch ( raw.charAt( i ) ) {
- case '"' :
- if ( lastQuoteIndex >= 0 ) {
- // Check if the quotes contains the first '('
- if ( lastQuoteIndex <= start && start <= i ) {
- return -1;
- }
- lastQuoteIndex = -1;
- } else {
- lastQuoteIndex = i;
- }
- break;
- case '(' :
- if ( lastQuoteIndex < 0 && i >= start ) {
- ++oCount;
- }
- break;
- case ')' :
- if ( lastQuoteIndex < 0 && i >= start ) {
- ++cCount;
- if ( i >= end ) {
- // found a ')' that needs to be included
- end = i + 1;
- }
- }
- }
-
- } // for
-
- return end;
- }
-
- /**
- * This list was obtained from
- * http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
- */
- private static Set getJavaKeywords() {
- final Set keys = new HashSet();
- keys.add( "abstract" );
- keys.add( "continue" );
- keys.add( "for" );
- keys.add( "new" );
- keys.add( "switch" );
- keys.add( "assert" );
- keys.add( "default" );
- keys.add( "goto" );
- keys.add( "package" );
- keys.add( "synchronized" );
- keys.add( "boolean" );
- keys.add( "do" );
- keys.add( "if" );
- keys.add( "private" );
- keys.add( "this" );
- keys.add( "break" );
- keys.add( "double" );
- keys.add( "implements" );
- keys.add( "protected" );
- keys.add( "throw" );
- keys.add( "byte" );
- keys.add( "else" );
- keys.add( "import" );
- keys.add( "public" );
- keys.add( "throws" );
- keys.add( "case" );
- keys.add( "enum" );
- keys.add( "instanceof" );
- keys.add( "return" );
- keys.add( "transient" );
- keys.add( "catch" );
- keys.add( "extends" );
- keys.add( "int" );
- keys.add( "short" );
- keys.add( "try" );
- keys.add( "char" );
- keys.add( "final" );
- keys.add( "interface" );
- keys.add( "static" );
- keys.add( "void" );
- keys.add( "class" );
- keys.add( "finally" );
- keys.add( "long" );
- keys.add( "strictfp" );
- keys.add( "volatile" );
- keys.add( "const" );
- keys.add( "float" );
- keys.add( "native" );
- keys.add( "super" );
- keys.add( "while" );
- return keys;
- }
-
-}
\ No newline at end of file
Deleted: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/MethodResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/MethodResolver.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/MethodResolver.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -1,155 +0,0 @@
-package org.drools.semantics.java;
-
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.drools.RuntimeDroolsException;
-
-/**
- * Determines the method to call, based on the given values
- *
- */
-public class MethodResolver {
-
- private final Class clazz;
- private final String name;
- private boolean staticMethod;
-
- public MethodResolver(Class clazz,
- String name) {
- super();
- this.clazz = clazz;
- this.name = name;
- }
-
- public Class getClazz() {
- return this.clazz;
- }
-
- public String getName() {
- return this.name;
- }
-
- public boolean isStaticMethod() {
- return this.staticMethod;
- }
-
- public Method resolveMethod(Class[] params) {
- Method method = null;
-
- boolean allDefined = true;
-
- //check for null params, so we know if all the types are defined
- for ( int i = 0, length = params.length; i < length; i++ ) {
- if ( params[i] == null ) {
- allDefined = false;
- }
- }
-
- if ( allDefined ) {
- method = getMethod( this.clazz,
- this.name,
- params );
- } else {
- // For some reason all the types are not specified, so just match the first method with the same
- // number of arguments
- Method[] methods = getMethods( this.clazz, this.name, params.length );
- if ( methods != null && methods.length != 0 ) {
- method = methods[0];
- }
- }
-
-
- if ( method != null && (method.getModifiers() & Modifier.STATIC) == Modifier.STATIC ) {
- this.staticMethod = true;
- }
-
-// @todo We could potentially output a warning here
-// if ( (method.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC ) {
-// throw new RuntimeDroolsException( "Unable to call the private method [" + name + "] on class [" + clazz.getName() + "] for parameters " + arrayToString( params ) );
-// }
- return method;
- }
-
- /**
- * work out what method we will be calling at runtime, based on the name and number of parameters.
- */
- private Method getMethod(Class clazz,
- String methodName,
- Class[] args) {
-
- // Fist get methods with same number of arguments
- Method[] methods = getMethods( clazz, methodName, args.length );
- if ( methods.length == 0 ) {
- return null;
- }
-
- // now iterate to find correct method
- for ( int i = 0, length = methods.length; i < length; i++ ) {
- if ( matchesTypes( methods[i].getParameterTypes(),
- args ) ) {
- return methods[i];
- }
- }
-
- // Casting hasn't worked, so just return the first method with the same number of params
- return methods[0];
- }
-
- private Method[] getMethods(Class clazz,
- String methodName,
- int length) {
- List list = new ArrayList();
- Method[] methods = clazz.getMethods();
- for ( int i = 0; i < methods.length; i++ ) {
- if ( methods[i].getName().equals( methodName ) ) {
- if ( methods[i].getParameterTypes().length == length ) {
- list.add( methods[i] );
- }
- }
- }
- return ( Method[] ) list.toArray( new Method[ list.size() ] );
- }
-
- private boolean matchesTypes(Class[] methodClasses,
- Class[] argumentClasses) {
- for ( int i = 0, length = methodClasses.length; i < length; i++ ) {
- Class methodClass = methodClasses[i];
- Class argumentClass = argumentClasses[i];
- if ( methodClasses[i].isPrimitive() ) {
- // try matching to primitive
- if ( methodClass == int.class && argumentClass == Integer.class ) {
- continue;
- } else if ( methodClass == long.class && argumentClass == Long.class ) {
- continue;
- } else if ( methodClass == float.class && argumentClass == Float.class ) {
- continue;
- } else if ( methodClass == double.class && argumentClass == Double.class ) {
- continue;
- } else {
- return false;
- }
- } else if ( methodClasses[i] != argumentClasses[i] ) {
- return false;
- }
- }
- return true;
- }
-
- private String arrayToString(Object[] values) {
- StringBuffer args = new StringBuffer();
- for ( int i = 0, length = values.length; i < length; i++ ) {
- args.append( "[" );
- args.append( values[i].getClass() );
- args.append( "]" );
- if ( i < length - 1 ) {
- args.append( ", " );
- }
- }
- return args.toString();
- }
-
-}
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/RuleBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -81,7 +81,6 @@
// Constructor
public RuleBuilder(final TypeResolver typeResolver,
- final FunctionFixer functionFixer,
final ClassFieldExtractorCache cache) {
// statically adding all builders to the map
@@ -109,8 +108,7 @@
gebuilder);
- this.utils = new BuildUtils( functionFixer,
- new KnowledgeHelperFixer(),
+ this.utils = new BuildUtils( new KnowledgeHelperFixer(),
new DeclarationTypeFixer(),
new JavaExprAnalyzer(),
typeResolver,
Deleted: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/StaticMethodFunctionResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/StaticMethodFunctionResolver.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/StaticMethodFunctionResolver.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -1,195 +0,0 @@
-package org.drools.semantics.java;
-
-import java.lang.reflect.Method;
-import java.util.Iterator;
-import java.util.List;
-
-import org.codehaus.jfdi.interpreter.TypeResolver;
-import org.drools.RuntimeDroolsException;
-import org.drools.spi.DeclarationScopeResolver;
-import org.drools.spi.FunctionResolver;
-
-public class StaticMethodFunctionResolver
- implements
- FunctionResolver {
-
- private final List functionImports;
-
- private final TypeResolver typeResolver;
-
- public StaticMethodFunctionResolver(final List imports,
- final TypeResolver typeResolver) {
- this.functionImports = imports;
-
- this.typeResolver = typeResolver;
- }
-
- public void addFunctionImport(String functionImport) {
- this.functionImports.add( functionImport );
-
- }
-
- public List getFunctionImports() {
- return this.functionImports;
- }
-
- public String resolveFunction(String functionName,
- String params) {
- return resolveFunction(functionName,
- params,
- null );
- }
-
- public String resolveFunction(String functionName,
- String params,
- DeclarationScopeResolver variables) {
- for ( Iterator it = this.functionImports.iterator(); it.hasNext(); ) {
- String functionImport = (String) it.next();
-
- // find the last '.' so we can separte the method/* from the package and Class
- int lastDot = functionImport.lastIndexOf( '.' );
- String name = functionImport.substring( lastDot );
-
- // if a functionImport imports the function name directly, i.e not using *, then only go further if they match.
- if ( !name.endsWith( "*" ) && !!functionName.equals( name ) ) {
- continue;
- }
-
- Class clazz;
- try {
- clazz = typeResolver.resolveType( functionImport.substring( 0,
- lastDot ) );
- } catch ( ClassNotFoundException e ) {
- // todo : must be a better way so we don't have to try/catch each resolveType call
- throw new RuntimeDroolsException( e );
- }
- MethodResolver methodResolver = new MethodResolver( clazz,
- functionName );
-
-
- Class[] classes = determineParameterTypes( params, variables );
-
- Method method = methodResolver.resolveMethod( classes );
-
- if ( method != null && methodResolver.isStaticMethod() ) {
- return clazz.getName().replace( '$',
- '.' );
- }
- }
- throw new RuntimeDroolsException( "unable to find the function '" + functionName + "'" );
- }
-
- /**
- * pass a string representation of parameters and determine the used class types
- * @param paramString
- * @return
- */
- private Class[] determineParameterTypes(String paramString, DeclarationScopeResolver variables) {
- if ( paramString.trim().equals( "" ) ) {
- return new Class[0];
- }
- String[] params = paramString.split( "," );
- Class[] classes = new Class[params.length];
-
- for ( int i = 0, length = params.length; i < length; i++ ) {
- String param = params[i].trim();
- if ( param.charAt( 0 ) == '"' && param.charAt( param.length() - 1 ) == '"' ) {
- // we have a string literal
- classes[i] = String.class;
- } else if ( Character.getType( param.charAt( 0 ) ) == Character.DECIMAL_DIGIT_NUMBER ) {
- // we have a number
-
- char c = param.charAt( param.length() - 1 );
- if ( param.indexOf( '.' ) == -1 ) {
- /// we have an integral
- if ( Character.getType( c ) != Character.DECIMAL_DIGIT_NUMBER ) {
- switch ( c ) {
- case 'l' :
- case 'L' :
- classes[i] = Long.class;
- break;
- case 'f' :
- case 'F' :
- classes[i] = Float.class;
- break;
- case 'd' :
- case 'D' :
- classes[i] = Double.class;
- break;
- default :
- throw new IllegalArgumentException( "invalid type identifier '" + c + "' used with number [" + param + "]" );
- }
- } else {
- classes[i] = Integer.class;
- }
- } else {
- // we have a decimal
- if ( Character.getType( c ) != Character.DECIMAL_DIGIT_NUMBER ) {
- switch ( c ) {
- case 'l' :
- case 'L' :
- throw new IllegalArgumentException( "invalid type identifier '" + c + "' used with number [" + param + "]" );
- case 'f' :
- case 'F' :
- classes[i] = Float.class;
- break;
- case 'd' :
- case 'D' :
- classes[i] = Double.class;
- break;
- default :
- throw new IllegalArgumentException( "invalid type identifier '" + c + "' used with number [" + param + "]" );
- }
- } else {
- classes[i] = Float.class;
- }
- }
- } else if ( param.startsWith( "new" ) ) {
- //We have a new instance, get its type
- int start = 3;
- int wordLength = param.length();
- // scan to start of first word
- for ( int j = start; j < wordLength; j++ ) {
- if ( param.charAt( j ) != ' ') {
- break;
- }
- start++;
- }
-
- int end = start;
- // now scan to end of the first word
- for ( int j = start; j <= wordLength; j++ ) {
- char c = param.charAt( j );
- if ( c == ' ' || c == '(' ) {
- break;
- }
- end++;
- }
-
- char[] word = new char[end-start];
- // now copy the word
- int k = 0;
- for ( int j = start; j < end; j++ ) {
- word[k++]= param.charAt( j );
- }
-
- Class clazz = null;
- try {
- clazz = this.typeResolver.resolveType( new String( word ) );
- } catch ( Exception e ) {
- throw new IllegalArgumentException( "Unable to resovle type [" + new String( word ) + "]" );
- }
- classes[i] = clazz;
-
-
- } else {
- // we have a varable
- if ( variables != null ) {
- classes[i] = variables.getType( param );
- }
- }
-
- }
- return classes;
- }
-}
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/AccumulateBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/AccumulateBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/AccumulateBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -99,9 +99,9 @@
st.setAttribute( "methodName",
className );
- final String initCode = utils.getFunctionFixer().fix( accumDescr.getInitCode() );
- final String actionCode = utils.getFunctionFixer().fix( accumDescr.getActionCode() );
- final String resultCode = utils.getFunctionFixer().fix( accumDescr.getResultCode() );
+ final String initCode = accumDescr.getInitCode();
+ final String actionCode = accumDescr.getActionCode();
+ final String resultCode = accumDescr.getResultCode();
st.setAttribute( "initCode",
initCode );
st.setAttribute( "actionCode",
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/BuildUtils.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/BuildUtils.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/BuildUtils.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -31,7 +31,6 @@
import org.drools.lang.descr.BaseDescr;
import org.drools.rule.Declaration;
import org.drools.semantics.java.DeclarationTypeFixer;
-import org.drools.semantics.java.FunctionFixer;
import org.drools.semantics.java.JavaExprAnalyzer;
import org.drools.semantics.java.KnowledgeHelperFixer;
import org.drools.semantics.java.RuleBuilder;
@@ -51,9 +50,7 @@
AngleBracketTemplateLexer.class );
private final KnowledgeHelperFixer knowledgeHelperFixer;
-
- private final FunctionFixer functionFixer;
-
+
private final DeclarationTypeFixer typeFixer;
private final JavaExprAnalyzer analyzer;
@@ -64,14 +61,12 @@
private final Map builders;
- public BuildUtils(final FunctionFixer functionFixer,
- final KnowledgeHelperFixer knowledgeHelperFixer,
+ public BuildUtils(final KnowledgeHelperFixer knowledgeHelperFixer,
final DeclarationTypeFixer typeFixer,
final JavaExprAnalyzer analyzer,
final TypeResolver typeResolver,
final ClassFieldExtractorCache classFieldExtractorCache,
final Map builders ) {
- this.functionFixer = functionFixer;
this.knowledgeHelperFixer = knowledgeHelperFixer;
this.typeFixer = typeFixer;
this.analyzer = analyzer;
@@ -184,14 +179,6 @@
}
/**
- * Returns the function fixer instance
- * @return
- */
- public FunctionFixer getFunctionFixer() {
- return functionFixer;
- }
-
- /**
* Returns the current type resolver instance
* @return
*/
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ColumnBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ColumnBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ColumnBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -400,8 +400,8 @@
st.setAttribute( "methodName",
className );
- final String predicateText = utils.getFunctionFixer().fix( predicateDescr.getText(),
- context.getDeclarationResolver() );
+ final String predicateText = predicateDescr.getText();
+
st.setAttribute( "text",
predicateText );
@@ -617,8 +617,7 @@
st.setAttribute( "methodName",
className );
- final String returnValueText = utils.getFunctionFixer().fix( returnValueRestrictionDescr.getText(),
- context.getDeclarationResolver() );
+ final String returnValueText = returnValueRestrictionDescr.getText();
st.setAttribute( "text",
returnValueText );
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ConsequenceBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ConsequenceBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/ConsequenceBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -60,8 +60,7 @@
declarations,
(String[]) usedIdentifiers[1].toArray( new String[usedIdentifiers[1].size()] ) );
st.setAttribute( "text",
- utils.getFunctionFixer().fix( utils.getKnowledgeHelperFixer().fix( ruleDescr.getConsequence() ),
- context.getDeclarationResolver() ) );
+ utils.getKnowledgeHelperFixer().fix( ruleDescr.getConsequence() ) );
context.getMethods().add( st.toString() );
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/EvalBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/EvalBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/EvalBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -75,8 +75,7 @@
st.setAttribute( "methodName",
className );
- final String evalText = utils.getFunctionFixer().fix( evalDescr.getText(),
- context.getDeclarationResolver() );
+ final String evalText = evalDescr.getText();
st.setAttribute( "text",
evalText );
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/RuleClassBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/RuleClassBuilder.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/semantics/java/builder/RuleClassBuilder.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -42,6 +42,10 @@
for ( final Iterator it = context.getPkg().getImports().iterator(); it.hasNext(); ) {
buffer.append( "import " + it.next() + ";" + lineSeparator );
}
+
+ for ( final Iterator it = context.getPkg().getStaticImports().iterator(); it.hasNext(); ) {
+ buffer.append( "import static " + it.next() + ";" + lineSeparator );
+ }
buffer.append( "public class " + utils.ucFirst( ruleDescr.getClassName() ) + " {" + lineSeparator );
buffer.append( " private static final long serialVersionUID = 320L;" + lineSeparator );
Deleted: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/FunctionFixerTest.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -1,130 +0,0 @@
-package org.drools.semantics.java;
-
-/*
- * Copyright 2005 JBoss Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.codehaus.jfdi.interpreter.TypeResolver;
-import org.codehaus.jfdi.interpreter.ClassTypeResolver;
-import org.drools.spi.DeclarationScopeResolver;
-import org.drools.rule.Package;
-
-import junit.framework.TestCase;
-
-public class FunctionFixerTest extends TestCase {
-
- private FunctionFixer fixer;
-
- public void setUp() {
- List list = new ArrayList();
- list.add( "org.drools.*" );
- TypeResolver typeResolver = new ClassTypeResolver( list );
- Package pkg = new Package("testPackage");
- pkg.addFunction( "addFive" );
- pkg.addFunction( "foo" );
- pkg.addFunction( "bar" );
-
- list = new ArrayList();
- list.add( "Func.*" );
- this.fixer = new FunctionFixer( pkg, new StaticMethodFunctionResolver(list, typeResolver) );
- }
-
- public void testSimple() {
- assertEquals( "org.drools.Func.func()",
- fixer.fix( "func( )" ) );
- }
-
- public void testSimpleWithPArams() {
- Map variables = new HashMap();
- variables.put( "yyy", String.class );
- variables.put( "iii", String.class );
- assertEquals( "org.drools.Func.func(yyy, iii)",
- fixer.fix( "func(yyy, iii)", new DeclarationScopeResolver( new Map[] { variables } ) ) );
- }
-
- public void testMoreComplex() {
- Map variables = new HashMap();
- variables.put( "yyy", String.class );
- variables.put( "iii", String.class );
- assertEquals( "xxx org.drools.Func.func(yyy, iii) yyy",
- fixer.fix( "xxx func(yyy, iii) yyy", new DeclarationScopeResolver( new Map[] { variables } ) ) );
- }
-
- public void testLeaveAloneNew() {
- assertEquals( "new Integer (5)",
- fixer.fix( "new Integer (5)" ) );
- }
-
- public void testLeaveAloneDrools() {
- assertEquals( "xxx drools.org(iii) org.drools.Func.func(yyy, iii) yyy",
- fixer.fix( "xxx drools.org(iii) func(yyy, iii) yyy" ) );
- }
-
- public void testWorkWithDotAll() {
- assertEquals( "\n\t\n\tAddFive.addFive(list) ;",
- fixer.fix( "\n\t\n\taddFive ( list ) ;" ) );
- }
-
- public void testWithDollarSigns() {
- assertEquals( "\nFoo.foo($list);",
- fixer.fix( "\nfoo($list);" ) );
- }
-
- public void testReservedWordsInJava() {
- assertEquals( "\nfor(int i=0; i < 2; i++) { /*do noithing*/ }",
- fixer.fix( "\nfor(int i=0; i < 2; i++) { /*do noithing*/ }" ) );
- }
-
- public void testMultipleInABracket() {
- assertEquals( "if (Foo.foo(bar)) { Bar.bar(baz); }",
- fixer.fix( "if (foo(bar)) { bar(baz); }" ) );
- }
-
- public void testInBrackets() {
- assertEquals( "if (Foo.foo(bar))",
- fixer.fix( "if (foo(bar))" ) );
- }
-
- public void testAlreadyAdded() {
- assertEquals( "Foo.foo(bar)",
- fixer.fix( "Foo.foo(bar)" ) );
- }
-
- public void testInString() {
- assertEquals( "\"if (foo(bar))\"",
- fixer.fix( "\"if (foo(bar))\"" ) );
- }
-
- public void testComplexWithBrackets() {
- assertEquals( "System.out.println(\"foo(\" + Foo.foo(bar) + Bar.bar(baz)",
- fixer.fix( "System.out.println(\"foo(\" + foo(bar) + bar(baz)" ) );
- }
-
- public void testXPath() {
- assertEquals( "foo.executeXpath(\"//node1/node2/text()\")",
- fixer.fix("foo.executeXpath(\"//node1/node2/text()\")" ) );
- }
-
- public void testExpressionGrouping() {
- assertEquals( "while((foo = bar.baz()) != null)",
- fixer.fix( "while((foo = bar.baz()) != null)" ) );
- }
-
-}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/KnowledgeHelperFixerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/KnowledgeHelperFixerTest.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/KnowledgeHelperFixerTest.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -93,7 +93,7 @@
}
public void testLeaveLargeAlone() {
- final String original = "yeah yeah yeah this is a long() thing Person (name=='drools') modify a thing";
+ final String original = "yeah yeah yeah massert( xxx ) this is a long() thing Person (name=='drools') modify a thing";
final String result = KnowledgeHelperFixerTest.fixer.fix( original );
assertEquals( original,
result );
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/RuleBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/RuleBuilderTest.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/RuleBuilderTest.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -81,14 +81,7 @@
typeResolver.addImport( pkgDescr.getName() + ".*" );
typeResolver.addImport( "java.lang.*" );
- FunctionResolver functionResolver = new StaticMethodFunctionResolver( pkg.getFunctionImports(),
- typeResolver );
-
- FunctionFixer functionFixer = new FunctionFixer( pkg,
- functionResolver );
-
final RuleBuilder builder = new RuleBuilder( typeResolver,
- functionFixer,
new ClassFieldExtractorCache() );
builder.build( pkg,
@@ -125,7 +118,7 @@
}
public void testBuildAttributes() throws Exception {
- RuleBuilder builder = new RuleBuilder(null, null, null);
+ RuleBuilder builder = new RuleBuilder(null, null);
Rule rule = new Rule("myrule");
List attributes = new ArrayList();
Deleted: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/StaticMethodFunctionResolverTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/StaticMethodFunctionResolverTest.java 2007-02-26 00:15:49 UTC (rev 9752)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/semantics/java/StaticMethodFunctionResolverTest.java 2007-02-26 00:17:38 UTC (rev 9753)
@@ -1,40 +0,0 @@
-package org.drools.semantics.java;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-import org.codehaus.jfdi.interpreter.TypeResolver;
-import org.codehaus.jfdi.interpreter.ClassTypeResolver;
-import org.drools.spi.DeclarationScopeResolver;
-import org.drools.spi.FunctionResolver;
-
-public class StaticMethodFunctionResolverTest extends TestCase {
- public void test1() throws Exception {
- List list = new ArrayList();
- list.add( "org.drools.StaticMethods" );
- TypeResolver typeResolver = new ClassTypeResolver( list );
-
- list = new ArrayList();
- list.add( "StaticMethods.*" );
- FunctionResolver functionResolver = new StaticMethodFunctionResolver( list,
- typeResolver );
-
- assertEquals( "org.drools.StaticMethods",
- functionResolver.resolveFunction( "getString1",
- "a" ) );
-
- Map map = new HashMap();
- map.put( "a",
- String.class );
-
- assertEquals( "org.drools.StaticMethods",
- functionResolver.resolveFunction( "getString1",
- "a",
- new DeclarationScopeResolver( new Map[]{map} ) ) );
-
- }
-}
More information about the jboss-svn-commits
mailing list