[jboss-svn-commits] JBL Code SVN: r9813 - in labs/jbossrules/trunk: drools-compiler/src/test/resources/org/drools/integrationtests and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 27 12:50:55 EST 2007


Author: tirelli
Date: 2007-02-27 12:50:54 -0500 (Tue, 27 Feb 2007)
New Revision: 9813

Added:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
Modified:
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java
   labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/globals_rule_test.drl
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java
Log:
JBRULES-695: globals can now be referenced as regular declarations

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java	2007-02-27 17:37:52 UTC (rev 9812)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/IntegrationCases.java	2007-02-27 17:50:54 UTC (rev 9813)
@@ -49,9 +49,7 @@
 import org.drools.FactB;
 import org.drools.FactHandle;
 import org.drools.FromTestClass;
-import org.drools.GrandParent;
 import org.drools.IndexedNumber;
-import org.drools.Parent;
 import org.drools.Person;
 import org.drools.PersonInterface;
 import org.drools.Precondition;

Modified: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/globals_rule_test.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/globals_rule_test.drl	2007-02-27 17:37:52 UTC (rev 9812)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/globals_rule_test.drl	2007-02-27 17:50:54 UTC (rev 9813)
@@ -8,7 +8,7 @@
 
 rule "global rule test"
     when
-        Cheese( $type : type ->  ( $type.equals( string ) ), type == ( string.toString() ) )
+        Cheese( $type : type ->  ( $type.equals( string ) ), type == ( string ), type == string )
         eval( string.equals( "stilton" ) )
         eval( $type.equals( string ) )
     then

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java	2007-02-27 17:37:52 UTC (rev 9812)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/DeclarationScopeResolver.java	2007-02-27 17:50:54 UTC (rev 9813)
@@ -4,6 +4,7 @@
 import java.util.Map;
 import java.util.Stack;
 
+import org.drools.rule.Column;
 import org.drools.rule.Declaration;
 import org.drools.rule.GroupElement;
 import org.drools.rule.RuleConditionElement;
@@ -32,6 +33,12 @@
     }
 
     public Class getType(final String name) {
+        for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
+            Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
+            if( declaration != null ) {
+                return declaration.getExtractor().getExtractToClass();
+            }
+        }
         for ( int i = 0, length = this.maps.length; i < length; i++ ) {
             final Object object = this.maps[i].get( name );
             if ( object != null ) {
@@ -42,37 +49,41 @@
                 }
             }
         }
-        for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
-            Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
-            if( declaration != null ) {
-                return declaration.getExtractor().getExtractToClass();
-            }
-        }
         return null;
     }
     
     public Declaration getDeclaration( final String name ) {
+        // it may be a local bound variable
         for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
             Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
             if( declaration != null ) {
                 return declaration;
             }
         }
+        // it may be a global or something
+        for ( int i = 0, length = this.maps.length; i < length; i++ ) {
+            if ( this.maps[i].containsKey( (name) ) ) {
+                GlobalExtractor global = new GlobalExtractor( this.maps[i].get( name ));
+                Column dummy = new Column(0, global.getObjectType());
+                Declaration declaration  = new Declaration(name, global, dummy);
+                return declaration;
+            }
+        }
         return null;
     }
 
     public boolean available(final String name) {
-        for ( int i = 0, length = this.maps.length; i < length; i++ ) {
-            if ( this.maps[i].containsKey( (name) ) ) {
-                return true;
-            }
-        }
         for( int i = this.buildStack.size()-1; i >= 0; i-- ) {
             Declaration declaration = ( Declaration ) (( RuleConditionElement ) this.buildStack.get( i )).getInnerDeclarations().get( name );
             if( declaration != null ) {
                 return true;
             }
         }
+        for ( int i = 0, length = this.maps.length; i < length; i++ ) {
+            if ( this.maps[i].containsKey( (name) ) ) {
+                return true;
+            }
+        }
         return false;
     }
     

Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java	2007-02-27 17:50:54 UTC (rev 9813)
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2006 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.
+ */
+
+package org.drools.spi;
+
+import java.lang.reflect.Method;
+
+import org.drools.RuntimeDroolsException;
+import org.drools.base.ClassObjectType;
+import org.drools.base.ValueType;
+
+/**
+ * A special extractor for globals
+ * 
+ * @author etirelli
+ */
+public class GlobalExtractor
+    implements
+    Extractor {
+
+    private static final long serialVersionUID = -756967384190918798L;
+    private Object            value;
+    private ObjectType        objectType;
+
+    public GlobalExtractor(final Object object) {
+        this.value = object;
+        this.objectType = new ClassObjectType( object.getClass() );
+    }
+
+    public Object getValue(final Object object) {
+        return value;
+    }
+    
+    public ObjectType getObjectType() {
+        return this.objectType;
+    }
+
+    public Class getExtractToClass() {
+        return this.value.getClass();
+    }
+
+    public ValueType getValueType() {
+        return this.objectType.getValueType();
+    }
+
+    public boolean getBooleanValue(final Object object) {
+        if ( this.objectType.getValueType().isBoolean() ) {
+            return ((Boolean) value).booleanValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to boolean not supported for type: " + value.getClass() );
+    }
+
+    public byte getByteValue(final Object object) {
+        if ( this.objectType.getValueType().isNumber() ) {
+            return ((Number) value).byteValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to byte not supported for type: " + value.getClass() );
+    }
+
+    public char getCharValue(final Object object) {
+        if ( this.objectType.getValueType().isChar() ) {
+            return ((Character) value).charValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to char not supported for type: " + value.getClass() );
+    }
+
+    public double getDoubleValue(final Object object) {
+        if ( this.objectType.getValueType().isNumber() ) {
+            return ((Number) value).doubleValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to double not supported for type: " + value.getClass() );
+    }
+
+    public float getFloatValue(final Object object) {
+        if ( this.objectType.getValueType().isNumber() ) {
+            return ((Number) value).floatValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to float not supported for type: " + value.getClass() );
+    }
+
+    public int getIntValue(final Object object) {
+        if ( this.objectType.getValueType().isNumber() ) {
+            return ((Number) value).intValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to int not supported for type: " + value.getClass() );
+    }
+
+    public long getLongValue(final Object object) {
+        if ( this.objectType.getValueType().isNumber() ) {
+            return ((Number) value).longValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to long not supported for type: " + value.getClass() );
+    }
+
+    public short getShortValue(final Object object) {
+        if ( this.objectType.getValueType().isNumber() ) {
+            return ((Number) value).shortValue();
+        }
+        throw new RuntimeDroolsException( "Conversion to short not supported for type: " + value.getClass() );
+    }
+
+    public Method getNativeReadMethod() {
+        try {
+            return this.getClass().getDeclaredMethod( "getValue", new Class[] { Object.class } );
+        } catch ( Exception e ) {
+            throw new RuntimeDroolsException("This is a bug. Please report to development team: "+e.getMessage(), e);
+        }
+    }
+
+    public int getHashCode(Object object) {
+        return value.hashCode();
+    }
+    
+    public int hashCode() {
+        return this.objectType.hashCode();
+    }
+    
+    public boolean equals(Object obj) {
+        if( this == obj ) {
+            return true;
+        }
+        if( ! ( obj instanceof GlobalExtractor ) ) {
+            return false;
+        }
+        GlobalExtractor other = (GlobalExtractor) obj;
+        return this.value.equals( other.value );
+    }
+}


Property changes on: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/GlobalExtractor.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list