[jboss-svn-commits] JBL Code SVN: r12446 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/base and 11 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sun Jun 10 21:51:48 EDT 2007
Author: mark.proctor at jboss.com
Date: 2007-06-10 21:51:48 -0400 (Sun, 10 Jun 2007)
New Revision: 12446
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DroolsClassLoader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MapBackedClassLoader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseFactory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateObjectType.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/MemoryVisitor.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/CompositePackageClassLoader.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PackageCompilationData.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ObjectType.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/event/AgendaEventSupportTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/examples/manners/BaseMannersTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AddRemoveTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/OtherwiseTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockObjectType.java
Log:
JBRULES-914 Refactor shadow facts into core
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseConfiguration.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -324,6 +324,10 @@
throw new IllegalArgumentException( "Illegal enum value '" + this.value + "' for AssertBehaviour" );
}
}
+
+ public String toString() {
+ return "AssertBehaviour : " + ( ( this.value == 0) ? "identity" : "equality" );
+ }
}
public static class LogicalOverride
@@ -360,6 +364,10 @@
throw new IllegalArgumentException( "Illegal enum value '" + this.value + "' for LogicalOverride" );
}
}
+
+ public String toString() {
+ return "LogicalOverride : " + ( ( this.value == 0) ? "preserve" : "discard" );
+ }
}
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseFactory.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/RuleBaseFactory.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -39,6 +39,11 @@
return RuleBaseFactory.newRuleBase( RuleBase.RETEOO,
null );
}
+
+ public static RuleBase newRuleBase(final RuleBaseConfiguration config) {
+ return RuleBaseFactory.newRuleBase( RuleBase.RETEOO,
+ config );
+ }
public static RuleBase newRuleBase(final int type) {
return RuleBaseFactory.newRuleBase( type,
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ClassObjectType.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -35,9 +35,6 @@
implements
ObjectType {
- // Objenesis instance without cache (false)
- private static final Objenesis OBJENESIS = new ObjenesisStd( false );
-
/**
*
*/
@@ -48,14 +45,8 @@
protected ValueType valueType;
- protected boolean shadowEnabled;
- protected Class shadowClass;
- protected transient ObjectInstantiator instantiator;
-
- protected transient Field delegate;
-
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
@@ -67,51 +58,9 @@
* Java object class.
*/
public ClassObjectType(final Class objectTypeClass) {
- this( objectTypeClass,
- null );
- }
-
- /**
- * Creates a new class object type with the given class
- * as its shadow class.
- *
- * @param objectTypeClass
- * Java object class.
- * @param shadowClass
- * The shadow class for the given objectTypeClass
- */
- public ClassObjectType(final Class objectTypeClass,
- final Class shadowClass) {
this.objectTypeClass = objectTypeClass;
this.valueType = ValueType.determineValueType( objectTypeClass );
- if ( shadowClass != null ) {
- this.shadowClass = shadowClass;
- this.shadowEnabled = true;
- setInstantiator();
- setDelegateFieldObject();
- }
}
-
- /**
- *
- */
- private void setInstantiator() {
- this.instantiator = OBJENESIS.getInstantiatorOf( this.shadowClass );
- }
-
- /**
- *
- */
- private void setDelegateFieldObject() {
- try {
- this.delegate = this.shadowClass.getDeclaredField( ShadowProxyFactory.DELEGATE_FIELD_NAME );
- this.delegate.setAccessible( true );
- } catch ( final Exception e ) {
- throw new RuntimeDroolsException( "Error retriving delegate field for shadow proxy class: " + this.shadowClass.getName(),
- e );
- }
- }
-
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
@@ -147,31 +96,6 @@
return this.valueType;
}
- public Object getShadow(final Object fact) throws RuntimeDroolsException {
- ShadowProxy proxy = null;
- if ( isShadowEnabled() ) {
- try {
- if ( this.delegate == null ) {
- this.setDelegateFieldObject();
- }
- if ( this.instantiator == null ) {
- this.setInstantiator();
- }
- proxy = (ShadowProxy) this.instantiator.newInstance();
- this.delegate.set( proxy,
- fact );
- } catch ( final Exception e ) {
- throw new RuntimeDroolsException( "Error creating shadow fact for object: " + fact,
- e );
- }
- }
- return proxy;
- }
-
- public boolean isShadowEnabled() {
- return this.shadowEnabled;
- }
-
public String toString() {
return "[ClassObjectType class=" + getClassType().getName() + "]";
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -41,6 +41,7 @@
import org.drools.StatefulSession;
import org.drools.rule.CompositePackageClassLoader;
import org.drools.rule.InvalidPatternException;
+import org.drools.rule.MapBackedClassLoader;
import org.drools.rule.Package;
import org.drools.rule.PackageCompilationData;
import org.drools.rule.Rule;
@@ -74,6 +75,8 @@
protected Map processes;
protected transient CompositePackageClassLoader packageClassLoader;
+
+ protected transient MapBackedClassLoader classLoader;
/** The fact handle factory. */
protected FactHandleFactory factHandleFactory;
@@ -84,7 +87,7 @@
* WeakHashMap to keep references of WorkingMemories but allow them to be
* garbage collected
*/
- protected transient ObjectHashSet statefulSessions;
+ protected transient ObjectHashSet statefulSessions;
/**
* Default constructor - for Externalizable. This should never be used by a user, as it
@@ -113,6 +116,8 @@
this.factHandleFactory = factHandleFactory;
this.packageClassLoader = new CompositePackageClassLoader( Thread.currentThread().getContextClassLoader() );
+ this.classLoader = new MapBackedClassLoader( Thread.currentThread().getContextClassLoader() );
+ this.packageClassLoader.addClassLoader( this.classLoader );
this.pkgs = new HashMap();
this.processes = new HashMap();
this.globals = new HashMap();
@@ -164,6 +169,9 @@
for ( final Iterator it = this.pkgs.values().iterator(); it.hasNext(); ) {
this.packageClassLoader.addClassLoader( ((Package) it.next()).getPackageCompilationData().getClassLoader() );
}
+
+ this.classLoader = new MapBackedClassLoader( Thread.currentThread().getContextClassLoader() );
+ this.packageClassLoader.addClassLoader( this.classLoader );
// Return the rules stored as a byte[]
final byte[] bytes = (byte[]) stream.readObject();
@@ -479,6 +487,10 @@
protected synchronized void addStatefulSession(final StatefulSession statefulSession) {
this.statefulSessions.add( statefulSession );
}
+
+ public Package getPackage(String name) {
+ return ( Package ) this.pkgs.get( name );
+ }
public StatefulSession[] getStatefulSessions() {
return (StatefulSession[]) this.statefulSessions.toArray( new StatefulSession[ this.statefulSessions.size() ] );
@@ -510,4 +522,16 @@
return (StatefulSession) workingMemory;
}
+
+ public void addClass(String className, byte[] bytes) {
+ this.classLoader.addClass( className, bytes );
+ }
+
+ public CompositePackageClassLoader getCompositePackageClassLoader() {
+ return this.packageClassLoader;
+ }
+
+ public MapBackedClassLoader getMapBackedClassLoader() {
+ return this.classLoader;
+ }
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -24,6 +24,9 @@
import org.drools.RuleBaseConfiguration;
import org.drools.StatefulSession;
import org.drools.reteoo.ReteooWorkingMemory;
+import org.drools.rule.CompositePackageClassLoader;
+import org.drools.rule.MapBackedClassLoader;
+import org.drools.rule.Package;
import org.drools.spi.FactHandleFactory;
import org.drools.spi.PropagationContext;
@@ -41,6 +44,8 @@
public Map getGlobals();
public RuleBaseConfiguration getConfiguration();
+
+ public Package getPackage(String name);
void disposeStatefulSession(StatefulSession statefulSession);
@@ -76,4 +81,10 @@
public void retractObject(FactHandle handle,
PropagationContext context,
ReteooWorkingMemory workingMemory) throws FactException;
+
+ public void addClass(String className, byte[] bytes);
+
+ public CompositePackageClassLoader getCompositePackageClassLoader();
+
+ public MapBackedClassLoader getMapBackedClassLoader();
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateObjectType.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateObjectType.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/facttemplates/FactTemplateObjectType.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -93,17 +93,7 @@
public ValueType getValueType() {
return ValueType.FACTTEMPLATE_TYPE;
}
-
- public Object getShadow(final Object fact) {
- // @TODO: shadow not implemented for templates yet
- return null;
- }
-
- public boolean isShadowEnabled() {
- // @TODO: shadow not implemented for templates yet
- return false;
- }
-
+
public String toString() {
return "[ClassObjectType class=" + this.factTemplate.getName() + "]";
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/MemoryVisitor.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/MemoryVisitor.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/MemoryVisitor.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -42,20 +42,6 @@
for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
visit( entry.getValue() );
}
-
- // this.rootVertex = (ReteNodeVertex) this.visitedNodes.get( dotId( rete ) );
- // if ( this.rootVertex == null ) {
- // this.rootVertex = new ReteNodeVertex( rete );
- // this.visitedNodes.put( dotId( rete ),
- // this.rootVertex );
- // }
- //
- // this.graph.addVertex( this.rootVertex );
- // this.parentVertex = this.rootVertex;
- // for ( final Iterator i = rete.objectTypeNodeIterator(); i.hasNext(); ) {
- // final Object nextNode = i.next();
- // visitNode( nextNode );
- // }
}
public void visitObjectTypeNode(final ObjectTypeNode node) {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ObjectTypeNode.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -145,33 +145,6 @@
final InternalWorkingMemory workingMemory) {
final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( this );
- // checks if shadow is enabled
- if ( this.objectType.isShadowEnabled() ) {
- // need to improve this
- if ( !(handle.getObject() instanceof ShadowProxy) ) {
- // replaces the actual object by its shadow before propagating
- handle.setObject( this.objectType.getShadow( handle.getObject() ) );
- handle.setShadowFact( true );
- } else {
- // we need to check if the shadow proxy matches the current object type
- // because in a class hirarchy it may happen that the shadow proxy does not
- // match a class down the hirarchy. See: JBRULES-696.
- if ( this.objectType.matches( handle.getObject() ) ) {
- ((ShadowProxy) handle.getObject()).updateProxy();
- } else {
- // replaces the old shadow proxy for a more specialized version of it
- final ShadowProxy old = (ShadowProxy) handle.getObject();
- handle.setObject( this.objectType.getShadow( old.getShadowedObject() ) );
- handle.setShadowFact( true );
-
- // we MUST copy state from one proxy to the other as we are in a dynamic rules engine
- // and new rules with different object types may be added at any moment
- ShadowProxyHelper.copyState( old,
- (ShadowProxy) handle.getObject() );
- }
- }
- }
-
if ( context.getType() == PropagationContext.MODIFICATION && this.skipOnModify && context.getDormantActivations() == 0 ) {
// we do this after the shadowproxy update, just so that its up to date for the future
return;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/Rete.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -17,22 +17,32 @@
*/
import java.io.Serializable;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.drools.FactException;
import org.drools.RuleBaseConfiguration;
+import org.drools.RuntimeDroolsException;
import org.drools.base.ShadowProxy;
+import org.drools.base.ShadowProxyFactory;
+import org.drools.base.ShadowProxyHelper;
import org.drools.common.BaseNode;
import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
import org.drools.common.NodeMemory;
import org.drools.facttemplates.Fact;
import org.drools.facttemplates.FactImpl;
+import org.drools.rule.Package;
import org.drools.spi.PropagationContext;
+import org.drools.util.ClassUtils;
import org.drools.util.Iterator;
import org.drools.util.ObjectHashMap;
import org.drools.util.ObjectHashMap.ObjectEntry;
+import org.objenesis.Objenesis;
+import org.objenesis.ObjenesisStd;
+import org.objenesis.instantiator.ObjectInstantiator;
/**
* The Rete-OO network.
@@ -68,15 +78,22 @@
private static final long serialVersionUID = 320L;
/** The <code>Map</code> of <code>ObjectTypeNodes</code>. */
private final ObjectHashMap objectTypeNodes;
+
+ private transient InternalRuleBase ruleBase;
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
- public Rete() {
+ public Rete(InternalRuleBase ruleBase) {
super( 0 );
this.objectTypeNodes = new ObjectHashMap();
+ this.ruleBase = ruleBase;
}
+
+ public void setRuleBase(InternalRuleBase ruleBase) {
+ this.ruleBase = ruleBase;
+ }
// ------------------------------------------------------------
// Instance methods
@@ -101,22 +118,45 @@
final Object object = handle.getObject();
- Object key = null;
-
+ ObjectTypeConf ojectTypeConf;
if ( object instanceof FactImpl ) {
- key = ((Fact) object).getFactTemplate().getName();
+ String key = ((Fact) object).getFactTemplate().getName();
+ ojectTypeConf = (ObjectTypeConf) memory.get( key );
+ if ( ojectTypeConf == null ) {
+ ojectTypeConf = new ObjectTypeConf( null, this);
+ memory.put( key,
+ ojectTypeConf,
+ false );
+ }
} else {
- key = object.getClass();
+ Class cls = object.getClass();
+ if ( object instanceof ShadowProxy ) {
+ cls = cls.getSuperclass();
+ }
+
+ ojectTypeConf = (ObjectTypeConf) memory.get( cls );
+ if ( ojectTypeConf == null ) {
+ ojectTypeConf = new ObjectTypeConf( cls, this);
+ memory.put( cls,
+ ojectTypeConf,
+ false );
+ }
+
+ // checks if shadow is enabled
+ if ( ojectTypeConf.isShadowEnabled() ) {
+ // need to improve this
+ if ( !(handle.getObject() instanceof ShadowProxy) ) {
+ // replaces the actual object by its shadow before propagating
+ handle.setObject( ojectTypeConf.getShadow( handle.getObject() ) );
+ handle.setShadowFact( true );
+ } else {
+ ((ShadowProxy) handle.getObject()).updateProxy();
+ }
+ }
}
+
+ ObjectTypeNode[] cachedNodes = ojectTypeConf.getObjectTypeNodes( object );
- ObjectTypeNode[] cachedNodes = (ObjectTypeNode[]) memory.get( key );
- if ( cachedNodes == null ) {
- cachedNodes = getMatchingNodes( object );
- memory.put( key,
- cachedNodes,
- false );
- }
-
for ( int i = 0, length = cachedNodes.length; i < length; i++ ) {
cachedNodes[i].assertObject( handle,
context,
@@ -140,28 +180,30 @@
final Object object = handle.getObject();
- ObjectTypeNode[] cachedNodes;
+ ObjectTypeConf objectTypeConf;
if ( object instanceof ShadowProxy ) {
- cachedNodes = (ObjectTypeNode[]) memory.get( object.getClass().getSuperclass() );
+ objectTypeConf = (ObjectTypeConf) memory.get( object.getClass().getSuperclass() );
} else {
- cachedNodes = (ObjectTypeNode[]) memory.get( object.getClass() );
+ objectTypeConf = (ObjectTypeConf) memory.get( object.getClass() );
}
+
+ ObjectTypeNode[] cachedNodes = objectTypeConf.getObjectTypeNodes( object );
- // cached might have been cleared, so recalculate matching nodes
- if ( cachedNodes == null ) {
- cachedNodes = getMatchingNodes( object );
- Object key = null;
+// // cached might have been cleared, so recalculate matching nodes
+// if ( cachedNodes == null ) {
+// cachedNodes = getMatchingNodes( object );
+// Object key = null;
+//
+// if ( object instanceof FactImpl ) {
+// key = ((Fact) object).getFactTemplate().getName();
+// } else {
+// key = object.getClass();
+// }
+// memory.put( key,
+// cachedNodes,
+// false );
+// }
- if ( object instanceof FactImpl ) {
- key = ((Fact) object).getFactTemplate().getName();
- } else {
- key = object.getClass();
- }
- memory.put( key,
- cachedNodes,
- false );
- }
-
if ( cachedNodes == null ) {
// it is possible that there are no ObjectTypeNodes for an object being retracted
return;
@@ -174,20 +216,8 @@
}
}
- private ObjectTypeNode[] getMatchingNodes(final Object object) throws FactException {
- final List cache = new ArrayList();
- final Iterator it = this.objectTypeNodes.iterator();
- for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
- final ObjectTypeNode node = (ObjectTypeNode) entry.getValue();
- if ( node.matches( object ) ) {
- cache.add( node );
- }
- }
- return (ObjectTypeNode[]) cache.toArray( new ObjectTypeNode[cache.size()] );
- }
-
/**
* Adds the <code>TupleSink</code> so that it may receive
* <code>Tuples</code> propagated from this <code>TupleSource</code>.
@@ -230,6 +260,10 @@
return new ObjectHashMap();
}
+ public InternalRuleBase getRuleBase() {
+ return this.ruleBase;
+ }
+
public int hashCode() {
return this.objectTypeNodes.hashCode();
}
@@ -250,13 +284,15 @@
public void updateSink(final ObjectSink sink,
final PropagationContext context,
final InternalWorkingMemory workingMemory) {
- // JBRULES-612: the cache MUST be invalidated when a new
- // node type is added to the network
+ // JBRULES-612: the cache MUST be invalidated when a new node type is added to the network, so iterate and reset all caches.
final ObjectHashMap memory = (ObjectHashMap) workingMemory.getNodeMemory( this );
- memory.clear();
+ Iterator it = memory.iterator();
+ for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
+ (( ObjectTypeConf) entry.getValue() ).resetCache();
+ }
final ObjectTypeNode node = (ObjectTypeNode) sink;
- final Iterator it = workingMemory.getFactHandleMap().iterator();
+ it = workingMemory.getFactHandleMap().iterator();
for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
final InternalFactHandle handle = (InternalFactHandle) entry.getValue();
if ( node.matches( handle.getObject() ) ) {
@@ -266,5 +302,133 @@
}
}
}
+
+ public static class ObjectTypeConf implements Serializable {
+ // Objenesis instance without cache (false)
+ private static final Objenesis OBJENESIS = new ObjenesisStd( false );
+
+ private final Class cls;
+ private final Rete rete;
+ private ObjectTypeNode[] objectTypeNodes;
+
+ protected boolean shadowEnabled;
+ protected Class shadowClass;
+ protected transient ObjectInstantiator instantiator;
+ protected transient Field delegate;
+ //private final InternalRuleBase ruleBase;
+
+ public ObjectTypeConf(Class cls, Rete rete) {
+ this.cls = cls;
+ this.rete = rete;
+
+ if ( cls == null ) {
+ return;
+ }
+
+ String pkgName = cls.getPackage().getName();
+ if ( "org.drools.reteoo".equals( pkgName ) || "org.drools.base".equals( pkgName )) {
+ // We don't shadow internal classes
+ this.shadowEnabled = false;
+ return;
+ }
+
+ Class shadowClass = null;
+ final String shadowProxyName = ShadowProxyFactory.getProxyClassNameForClass( this.cls );
+ try {
+ // if already loaded
+ shadowClass = this.rete.getRuleBase().getMapBackedClassLoader().loadClass( shadowProxyName );
+ } catch ( final ClassNotFoundException cnfe ) {
+ // otherwise, create and load
+ final byte[] proxyBytes = ShadowProxyFactory.getProxyBytes( cls );
+ if ( proxyBytes != null ) {
+ this.rete.getRuleBase().getMapBackedClassLoader().addClass( shadowProxyName,
+ proxyBytes );
+ try {
+ shadowClass = this.rete.getRuleBase().getMapBackedClassLoader().loadClass( shadowProxyName );
+ } catch ( ClassNotFoundException e ) {
+ throw new RuntimeException( "Unable to find or generate the ShadowProxy implementation for '" + this.cls.getName() + "'" );
+ }
+ }
+ }
+
+ if ( shadowClass != null ) {
+ this.shadowClass = shadowClass;
+ this.shadowEnabled = true;
+ setInstantiator();
+ setDelegateFieldObject();
+ }
+ }
+
+ /**
+ *
+ */
+ private void setInstantiator() {
+ this.instantiator = OBJENESIS.getInstantiatorOf( this.shadowClass );
+ }
+
+ /**
+ *
+ */
+ private void setDelegateFieldObject() {
+ try {
+ this.delegate = this.shadowClass.getDeclaredField( ShadowProxyFactory.DELEGATE_FIELD_NAME );
+ this.delegate.setAccessible( true );
+ } catch ( final Exception e ) {
+ throw new RuntimeDroolsException( "Error retriving delegate field for shadow proxy class: " + this.shadowClass.getName(),
+ e );
+ }
+ }
+
+ public Object getShadow(final Object fact) throws RuntimeDroolsException {
+ ShadowProxy proxy = null;
+ if ( isShadowEnabled() ) {
+ try {
+ if ( this.delegate == null ) {
+ this.setDelegateFieldObject();
+ }
+ if ( this.instantiator == null ) {
+ this.setInstantiator();
+ }
+ proxy = (ShadowProxy) this.instantiator.newInstance();
+ this.delegate.set( proxy,
+ fact );
+ } catch ( final Exception e ) {
+ throw new RuntimeDroolsException( "Error creating shadow fact for object: " + fact,
+ e );
+ }
+ }
+ return proxy;
+ }
+
+ public boolean isShadowEnabled() {
+ return this.shadowEnabled;
+ }
+
+ public void resetCache() {
+ this.objectTypeNodes = null;
+ }
+
+ public ObjectTypeNode[] getObjectTypeNodes(final Object object) {
+ if ( this.objectTypeNodes == null ) {
+ buildCache( object );
+ }
+ return this.objectTypeNodes;
+ }
+
+ private void buildCache(final Object object) throws FactException {
+ final List cache = new ArrayList();
+
+ final Iterator it = this.rete.getObjectTypeNodes().iterator();
+ for ( ObjectEntry entry = (ObjectEntry) it.next(); entry != null; entry = (ObjectEntry) it.next() ) {
+ final ObjectTypeNode node = (ObjectTypeNode) entry.getValue();
+ if ( node.matches( object ) ) {
+ cache.add( node );
+ }
+ }
+
+ this.objectTypeNodes = ( ObjectTypeNode[] ) cache.toArray( new ObjectTypeNode[cache.size()] );
+ }
+ }
+
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -134,7 +134,7 @@
this.globals = new HashMap();
this.statefulSessions = new ObjectHashSet();
- this.rete = new Rete();
+ this.rete = new Rete(this);
this.reteooBuilder = new ReteooBuilder( this );
}
@@ -162,6 +162,7 @@
objects );
this.rete = (Rete) objects[0];
+ this.rete.setRuleBase( this );
this.reteooBuilder = (ReteooBuilder) objects[1];
this.reteooBuilder.setRuleBase( this );
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/CompositePackageClassLoader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/CompositePackageClassLoader.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/CompositePackageClassLoader.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -29,7 +29,7 @@
private Class compositeFastFindClass(final String name) {
for ( final Iterator it = this.classLoaders.iterator(); it.hasNext(); ) {
- final PackageClassLoader classLoader = (PackageClassLoader) it.next();
+ final DroolsClassLoader classLoader = (DroolsClassLoader) it.next();
final Class clazz = classLoader.fastFindClass( name );
if ( clazz != null ) {
return clazz;
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DroolsClassLoader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DroolsClassLoader.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/DroolsClassLoader.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -0,0 +1,5 @@
+package org.drools.rule;
+
+public interface DroolsClassLoader {
+ public Class fastFindClass(final String name);
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MapBackedClassLoader.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MapBackedClassLoader.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/MapBackedClassLoader.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -0,0 +1,98 @@
+package org.drools.rule;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.common.ObjectInputStreamWithLoader;
+
+public class MapBackedClassLoader extends ClassLoader
+ implements
+ DroolsClassLoader,
+ Serializable {
+ private Map store;
+
+ public MapBackedClassLoader(final ClassLoader parentClassLoader) {
+ super( parentClassLoader );
+ this.store = new HashMap();
+ }
+
+ public void addClass(String className,
+ byte[] bytes) {
+ this.store.put( className,
+ bytes );
+ }
+
+ public Class fastFindClass(final String name) {
+ final Class clazz = findLoadedClass( name );
+
+ if ( clazz == null ) {
+ final byte[] clazzBytes = (byte[]) this.store.get( name );
+ if ( clazzBytes != null ) {
+ return defineClass( name,
+ clazzBytes,
+ 0,
+ clazzBytes.length );
+ }
+ }
+
+ return clazz;
+ }
+
+ /**
+ * Javadocs recommend that this method not be overloaded. We overload this so that we can prioritise the fastFindClass
+ * over method calls to parent.loadClass(name, false); and c = findBootstrapClass0(name); which the default implementation
+ * would first - hence why we call it "fastFindClass" instead of standard findClass, this indicates that we give it a
+ * higher priority than normal.
+ *
+ */
+ protected synchronized Class loadClass(final String name,
+ final boolean resolve) throws ClassNotFoundException {
+ Class clazz = fastFindClass( name );
+
+ if ( clazz == null ) {
+ final ClassLoader parent = getParent();
+ if ( parent != null ) {
+ clazz = parent.loadClass( name );
+ } else {
+ throw new ClassNotFoundException( name );
+ }
+ }
+
+ if ( resolve ) {
+ resolveClass( clazz );
+ }
+
+ return clazz;
+ }
+
+ protected Class findClass(final String name) throws ClassNotFoundException {
+ final Class clazz = fastFindClass( name );
+ if ( clazz == null ) {
+ throw new ClassNotFoundException( name );
+ }
+ return clazz;
+ }
+
+ public InputStream getResourceAsStream(final String name) {
+ final byte[] bytes = (byte[]) this.store.get( name );
+ if ( bytes != null ) {
+ return new ByteArrayInputStream( bytes );
+ } else {
+ InputStream input = this.getParent().getResourceAsStream( name );
+ if ( input == null ) {
+ input = super.getResourceAsStream( name );
+ }
+ return input;
+ }
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PackageCompilationData.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PackageCompilationData.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/PackageCompilationData.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -286,7 +286,7 @@
* @author mproctor
*
*/
- public class PackageClassLoader extends ClassLoader {
+ public class PackageClassLoader extends ClassLoader implements DroolsClassLoader {
public PackageClassLoader(final ClassLoader parentClassLoader) {
super( parentClassLoader );
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ObjectType.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ObjectType.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/ObjectType.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -43,21 +43,4 @@
boolean matches(Object object);
ValueType getValueType();
-
- /**
- * If shadow facts are enabled, returns the shadow fact,
- * otherwise returns null
- *
- * @param fact the fact to be shadowed
- *
- * @return the shadow fact for the given fact or null
- * if shadow facts are disabled
- */
- Object getShadow(Object fact);
-
- /**
- * Returns true in case shadow facts are enabled
- * @return true if shadow facts are enabled, false otherwise
- */
- boolean isShadowEnabled();
}
\ No newline at end of file
Copied: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java (from rev 12325, labs/jbossrules/trunk/drools-compiler/src/main/java/org/apache/commons/jci/utils/ClassUtils.java)
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ClassUtils.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -0,0 +1,62 @@
+package org.drools.util;
+
+import java.io.File;
+
+public final class ClassUtils {
+
+ /**
+ * Please do not use - internal
+ * org/my/Class.xxx -> org.my.Class
+ */
+ public static String convertResourceToClassName(final String pResourceName) {
+ return ClassUtils.stripExtension( pResourceName ).replace( '/',
+ '.' );
+ }
+
+ /**
+ * Please do not use - internal
+ * org.my.Class -> org/my/Class.class
+ */
+ public static String convertClassToResourcePath(final String pName) {
+ return pName.replace( '.',
+ '/' ) + ".class";
+ }
+
+ /**
+ * Please do not use - internal
+ * org/my/Class.xxx -> org/my/Class
+ */
+ public static String stripExtension(final String pResourceName) {
+ final int i = pResourceName.lastIndexOf( '.' );
+ final String withoutExtension = pResourceName.substring( 0,
+ i );
+ return withoutExtension;
+ }
+
+ public static String toJavaCasing(final String pName) {
+ final char[] name = pName.toLowerCase().toCharArray();
+ name[0] = Character.toUpperCase( name[0] );
+ return new String( name );
+ }
+
+ public static String clazzName(final File base,
+ final File file) {
+ final int rootLength = base.getAbsolutePath().length();
+ final String absFileName = file.getAbsolutePath();
+ final int p = absFileName.lastIndexOf( '.' );
+ final String relFileName = absFileName.substring( rootLength + 1,
+ p );
+ final String clazzName = relFileName.replace( File.separatorChar,
+ '.' );
+ return clazzName;
+ }
+
+ public static String relative(final File base,
+ final File file) {
+ final int rootLength = base.getAbsolutePath().length();
+ final String absFileName = file.getAbsolutePath();
+ final String relFileName = absFileName.substring( rootLength + 1 );
+ return relFileName;
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/event/AgendaEventSupportTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/event/AgendaEventSupportTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/event/AgendaEventSupportTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -28,6 +28,7 @@
import org.drools.base.ClassFieldExtractor;
import org.drools.base.ClassObjectType;
import org.drools.base.FieldFactory;
+import org.drools.base.ShadowProxy;
import org.drools.base.ValueType;
import org.drools.base.evaluators.Operator;
import org.drools.rule.Pattern;
@@ -129,7 +130,7 @@
agendaList.size() );
ActivationCreatedEvent createdEvent = (ActivationCreatedEvent) agendaList.get( 0 );
assertSame( cheddar,
- createdEvent.getActivation().getTuple().get( 0 ).getObject() );
+ unwrapShadow( createdEvent.getActivation().getTuple().get( 0 ).getObject() ) );
agendaList.clear();
// modify results in a ActivationCancelledEvent and an ActivationCreatedEvent, note the object is always resolvable
@@ -140,10 +141,10 @@
agendaList.size() );
ActivationCancelledEvent cancelledEvent = (ActivationCancelledEvent) agendaList.get( 0 );
assertSame( cheddar,
- cancelledEvent.getActivation().getTuple().get( 0 ).getObject() );
+ unwrapShadow( cancelledEvent.getActivation().getTuple().get( 0 ).getObject() ) );
createdEvent = (ActivationCreatedEvent) agendaList.get( 1 );
assertSame( cheddar,
- createdEvent.getActivation().getTuple().get( 0 ).getObject() );
+ unwrapShadow( createdEvent.getActivation().getTuple().get( 0 ).getObject() ) );
agendaList.clear();
// retract results in a ActivationCancelledEvent, noe the object is not resolveable now as it no longer exists
@@ -173,12 +174,20 @@
agendaList.size() );
final BeforeActivationFiredEvent beforeEvent = (BeforeActivationFiredEvent) agendaList.get( 0 );
assertSame( cheddar,
- beforeEvent.getActivation().getTuple().get( 0 ).getObject() );
+ unwrapShadow( beforeEvent.getActivation().getTuple().get( 0 ).getObject() ) );
final AfterActivationFiredEvent afterEvent = (AfterActivationFiredEvent) agendaList.get( 1 );
assertSame( cheddar,
- afterEvent.getActivation().getTuple().get( 0 ).getObject() );
+ unwrapShadow( afterEvent.getActivation().getTuple().get( 0 ).getObject() ) );
final AgendaGroupPoppedEvent poppedEvent = (AgendaGroupPoppedEvent) agendaList.get( 2 );
assertEquals( "test group",
poppedEvent.getAgendaGroup().getName() );
}
+
+ private Object unwrapShadow(Object object) {
+ if ( object instanceof ShadowProxy ) {
+ return ((ShadowProxy) object).getShadowedObject();
+ } else {
+ return object;
+ }
+ }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/examples/manners/BaseMannersTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/examples/manners/BaseMannersTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/examples/manners/BaseMannersTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -90,33 +90,26 @@
//private Evaluator booleanNotEqualEvaluator;
protected void setUp() throws Exception {
- Class shadow = ShadowProxyFactory.getProxy( Context.class );
- this.contextType = new ClassObjectType( Context.class,
- shadow );
+ //Class shadow = ShadowProxyFactory.getProxy( Context.class );
+ this.contextType = new ClassObjectType( Context.class );
- shadow = ShadowProxyFactory.getProxy( Guest.class );
- this.guestType = new ClassObjectType( Guest.class,
- shadow );
+ //shadow = ShadowProxyFactory.getProxy( Guest.class );
+ this.guestType = new ClassObjectType( Guest.class );
- shadow = ShadowProxyFactory.getProxy( Seating.class );
- this.seatingType = new ClassObjectType( Seating.class,
- shadow );
+ //shadow = ShadowProxyFactory.getProxy( Seating.class );
+ this.seatingType = new ClassObjectType( Seating.class );
- shadow = ShadowProxyFactory.getProxy( LastSeat.class );
- this.lastSeatType = new ClassObjectType( LastSeat.class,
- shadow );
+ //shadow = ShadowProxyFactory.getProxy( LastSeat.class );
+ this.lastSeatType = new ClassObjectType( LastSeat.class );
- shadow = ShadowProxyFactory.getProxy( Count.class );
- this.countType = new ClassObjectType( Count.class,
- shadow );
+ //shadow = ShadowProxyFactory.getProxy( Count.class );
+ this.countType = new ClassObjectType( Count.class );
- shadow = ShadowProxyFactory.getProxy( Path.class );
- this.pathType = new ClassObjectType( Path.class,
- shadow );
+ //shadow = ShadowProxyFactory.getProxy( Path.class );
+ this.pathType = new ClassObjectType( Path.class );
- shadow = ShadowProxyFactory.getProxy( Chosen.class );
- this.chosenType = new ClassObjectType( Chosen.class,
- shadow );
+ //shadow = ShadowProxyFactory.getProxy( Chosen.class );
+ this.chosenType = new ClassObjectType( Chosen.class );
this.integerEqualEvaluator = ValueType.PINTEGER_TYPE.getEvaluator( Operator.EQUAL );
//this.integerNotEqualEvaluator = ValueType.INTEGER_TYPE.getEvaluator( Operator.NOT_EQUAL );
@@ -125,7 +118,7 @@
this.booleanEqualEvaluator = ValueType.PBOOLEAN_TYPE.getEvaluator( Operator.EQUAL );
//this.booleanNotEqualEvaluator = ValueType.BOOLEAN_TYPE.getEvaluator( Operator.NOT_EQUAL );
- this.pkg = new Package( "Miss Manners" );
+ this.pkg = new Package( "org.drools.examples.manners" );
this.pkg.addRule( getAssignFirstSeatRule() );
this.pkg.addRule( getFindSeating() );
this.pkg.addRule( getPathDone() );
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AddRemoveTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AddRemoveTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/AddRemoveTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -20,6 +20,7 @@
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.base.ClassObjectType;
+import org.drools.common.InternalRuleBase;
public class AddRemoveTest extends DroolsTestCase {
public void testAdd() {
@@ -27,7 +28,8 @@
* create a RuleBase with a single ObjectTypeNode we attach a
* MockObjectSink so we can detect assertions and retractions
*/
- final Rete rete = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete rete = new Rete( (InternalRuleBase) ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( Object.class ),
rete,
@@ -36,7 +38,7 @@
final MockObjectSink sink = new MockObjectSink();
objectTypeNode.addObjectSink( sink );
- final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
// objectTypeNode.
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/LogicalAssertionTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -25,9 +25,12 @@
import org.drools.RuleBaseConfiguration;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
+import org.drools.RuleBaseConfiguration.LogicalOverride;
import org.drools.base.ClassObjectType;
+import org.drools.base.ShadowProxy;
import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalAgenda;
+import org.drools.common.InternalRuleBase;
import org.drools.common.PropagationContextImpl;
import org.drools.rule.Rule;
import org.drools.spi.Consequence;
@@ -140,7 +143,8 @@
// MockObjectSink so w can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
- final Rete rete = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete rete = new Rete( (InternalRuleBase) ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete,
@@ -154,7 +158,7 @@
rule1,
rule1.getLhs() );
- final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Agenda agenda = workingMemory.getAgenda();
@@ -229,7 +233,8 @@
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
- final Rete rete = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete rete = new Rete( (InternalRuleBase) ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete,
@@ -241,7 +246,7 @@
new MockTupleSource( 2 ),
rule1,
rule1.getLhs() );
- final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Agenda agenda = workingMemory.getAgenda();
@@ -363,7 +368,10 @@
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
- final Rete rete = new Rete();
+ RuleBaseConfiguration conf = new RuleBaseConfiguration();
+ conf.setLogicalOverride( LogicalOverride.PRESERVE );
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase( conf );
+ final Rete rete = new Rete( (InternalRuleBase) ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete,
@@ -375,10 +383,6 @@
new MockTupleSource( 2 ),
rule1,
rule1.getLhs() );
- final RuleBaseConfiguration conf = new RuleBaseConfiguration();
- conf.setLogicalOverride( RuleBaseConfiguration.LogicalOverride.PRESERVE );
-
- final RuleBase ruleBase = new ReteooRuleBase( conf );
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Agenda agenda = workingMemory.getAgenda();
@@ -453,7 +457,8 @@
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
- final Rete rete = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete rete = new Rete( (InternalRuleBase)ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete,
@@ -465,7 +470,7 @@
new MockTupleSource( 2 ),
rule1,
rule1.getLhs() );
- final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Consequence consequence = new Consequence() {
@@ -678,7 +683,8 @@
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
- final Rete rete = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete rete = new Rete( (InternalRuleBase)ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete,
@@ -690,7 +696,7 @@
new MockTupleSource( 2 ),
rule1,
rule1.getLhs() );
- final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Agenda agenda = workingMemory.getAgenda();
@@ -777,7 +783,8 @@
// create a RuleBase with a single ObjectTypeNode we attach a
// MockObjectSink so we can detect assertions and retractions
final Rule rule1 = new Rule( "test-rule1" );
- final Rete rete = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete rete = new Rete( (InternalRuleBase)ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 0,
new ClassObjectType( String.class ),
rete,
@@ -789,7 +796,6 @@
new MockTupleSource( 2 ),
rule1,
rule1.getLhs() );
- final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
final ReteooWorkingMemory workingMemory = (ReteooWorkingMemory) ruleBase.newStatefulSession();
final Agenda agenda = workingMemory.getAgenda();
@@ -845,4 +851,11 @@
workingMemory.getTruthMaintenanceSystem().getAssertMap().size() );
}
+ private Object unwrapShadow(Object object) {
+ if ( object instanceof ShadowProxy ) {
+ return ((ShadowProxy) object).getShadowedObject();
+ } else {
+ return object;
+ }
+ }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ObjectTypeNodeTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -23,12 +23,14 @@
import org.drools.DroolsTestCase;
import org.drools.FactException;
import org.drools.Person;
+import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.base.ClassObjectType;
import org.drools.base.ShadowProxy;
import org.drools.base.ShadowProxyFactory;
import org.drools.common.DefaultFactHandle;
import org.drools.common.InternalFactHandle;
+import org.drools.common.InternalRuleBase;
import org.drools.common.PropagationContextImpl;
import org.drools.spi.ObjectType;
import org.drools.spi.PropagationContext;
@@ -38,7 +40,8 @@
public class ObjectTypeNodeTest extends DroolsTestCase {
public void testAttach() throws Exception {
- final Rete source = new Rete();
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete source = new Rete( (InternalRuleBase)ruleBase);
final ObjectType objectType = new ClassObjectType( String.class );
@@ -110,12 +113,14 @@
}
public void testMemory() {
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+
final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
- (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
+ (ReteooRuleBase) ruleBase );
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
new ClassObjectType( String.class ),
- new Rete(),
+ new Rete( (InternalRuleBase) ruleBase),
3 );
final FactHashTable memory = (FactHashTable) workingMemory.getNodeMemory( objectTypeNode );
@@ -124,9 +129,9 @@
}
public void testMatches() {
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
+ final Rete source = new Rete((InternalRuleBase) ruleBase);
- final Rete source = new Rete();
-
ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
new ClassObjectType( String.class ),
source,
@@ -148,15 +153,16 @@
}
public void testRetractObject() throws Exception {
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
final PropagationContext context = new PropagationContextImpl( 0,
PropagationContext.ASSERTION,
null,
null );
final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
- (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
+ (ReteooRuleBase) ruleBase );
- final Rete source = new Rete();
+ final Rete source = new Rete((InternalRuleBase) ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
new ClassObjectType( String.class ),
@@ -204,11 +210,11 @@
PropagationContext.ASSERTION,
null,
null );
-
+ final RuleBase ruleBase = RuleBaseFactory.newRuleBase();
final ReteooWorkingMemory workingMemory = new ReteooWorkingMemory( 1,
- (ReteooRuleBase) RuleBaseFactory.newRuleBase() );
+ (ReteooRuleBase) ruleBase );
- final Rete source = new Rete();
+ final Rete source = new Rete( (InternalRuleBase)ruleBase);
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
new ClassObjectType( String.class ),
@@ -282,8 +288,7 @@
final Class shadowClass = ShadowProxyFactory.getProxy( Cheese.class );
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
- new ClassObjectType( Cheese.class,
- shadowClass ),
+ new ClassObjectType( Cheese.class ),
source,
3 );
@@ -329,8 +334,7 @@
final Class shadowClass = ShadowProxyFactory.getProxy( Person.class );
final ObjectTypeNode objectTypeNode = new ObjectTypeNode( 1,
- new ClassObjectType( Person.class,
- shadowClass ),
+ new ClassObjectType( Person.class ),
source,
3 );
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/OtherwiseTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/OtherwiseTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/OtherwiseTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -117,8 +117,7 @@
private Rule getOtherwise(final String name) {
final Rule rule = new Rule( name );
final Pattern pat = new Pattern( 0,
- new ClassObjectType( Otherwise.class,
- ShadowProxyFactory.getProxy( Otherwise.class ) ) );
+ new ClassObjectType( Otherwise.class) );
rule.addPattern( pat );
rule.setConsequence( new MockConsequence() );
return rule;
@@ -128,8 +127,7 @@
final Rule rule = new Rule( name );
final Pattern pat = new Pattern( 0,
- new ClassObjectType( TestBean.class,
- ShadowProxyFactory.getProxy( TestBean.class ) ) );
+ new ClassObjectType( TestBean.class ) );
rule.addPattern( pat );
rule.setConsequence( new MockConsequence() );
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -26,8 +26,10 @@
import org.drools.FactException;
import org.drools.RuleBaseFactory;
import org.drools.base.ClassObjectType;
+import org.drools.base.ShadowProxy;
import org.drools.common.DefaultFactHandle;
import org.drools.common.PropagationContextImpl;
+import org.drools.reteoo.Rete.ObjectTypeConf;
import org.drools.spi.PropagationContext;
import org.drools.util.ObjectHashMap;
@@ -127,11 +129,13 @@
workingMemory );
final ObjectHashMap map = (ObjectHashMap) workingMemory.getNodeMemory( rete );
+ ObjectTypeConf conf = ( ObjectTypeConf ) map.get( ArrayList.class );
assertLength( 3,
- (ObjectTypeNode[]) map.get( ArrayList.class ) );
+ conf.getObjectTypeNodes( new ArrayList() ) );
+ conf = ( ObjectTypeConf ) map.get( LinkedList.class );
assertLength( 2,
- (ObjectTypeNode[]) map.get( LinkedList.class ) );
+ conf.getObjectTypeNodes( new LinkedList() ) );
}
@@ -186,7 +190,7 @@
final Object[] results = (Object[]) asserted.get( 0 );
assertSame( list,
- ((DefaultFactHandle) results[0]).getObject() );
+ unwrapShadow( ((DefaultFactHandle) results[0]).getObject()) );
}
/**
@@ -245,7 +249,14 @@
final Object[] results = (Object[]) retracted.get( 0 );
assertSame( list,
- ((DefaultFactHandle) results[0]).getObject() );
+ unwrapShadow( ((DefaultFactHandle) results[0]).getObject() ) );
}
+ private Object unwrapShadow(Object object) {
+ if ( object instanceof ShadowProxy ) {
+ return ((ShadowProxy) object).getShadowedObject();
+ } else {
+ return object;
+ }
+ }
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -27,6 +27,7 @@
import org.drools.base.ClassFieldExtractor;
import org.drools.base.ClassObjectType;
import org.drools.base.FieldFactory;
+import org.drools.base.ShadowProxy;
import org.drools.base.ValueType;
import org.drools.base.evaluators.Operator;
import org.drools.common.InternalFactHandle;
@@ -374,14 +375,17 @@
workingMemory ) );
cheddar.setPrice( 5 );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertFalse( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
cheddar.setType( "stilton" );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertFalse( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
cheddar.setPrice( 15 );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertFalse( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
}
@@ -439,14 +443,17 @@
workingMemory ) );
cheddar.setPrice( 5 );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertTrue( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
cheddar.setType( "stilton" );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertFalse( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
cheddar.setPrice( 15 );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertTrue( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
}
@@ -532,14 +539,17 @@
workingMemory ) );
cheddar.setPrice( 5 );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertFalse( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
cheddar.setType( "stilton" );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertTrue( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
cheddar.setPrice( 15 );
+ ((ShadowProxy) cheddarHandle.getObject()).updateProxy();
assertFalse( constraint.isAllowed( cheddarHandle.getObject(),
workingMemory ) );
}
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockObjectType.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockObjectType.java 2007-06-10 22:34:17 UTC (rev 12445)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockObjectType.java 2007-06-11 01:51:48 UTC (rev 12446)
@@ -79,12 +79,4 @@
return ValueType.OBJECT_TYPE;
}
- public Object getShadow(final Object fact) {
- return null;
- }
-
- public boolean isShadowEnabled() {
- return false;
- }
-
}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list