[jboss-svn-commits] JBL Code SVN: r19008 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/base and 6 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Mar 15 02:13:59 EDT 2008
Author: mingjin
Date: 2008-03-15 02:13:59 -0400 (Sat, 15 Mar 2008)
New Revision: 19008
Added:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryEntryPoint.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/NamedEntryPoint.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ObjectTypeConfigurationRegistry.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedTemporalWorkingMemoryContext.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedWorkingMemoryContext.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ArrayIterator.java
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.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/AbstractWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryActions.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ClassObjectTypeConf.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EntryPointNode.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java
Log:
compiled after merge, 2 test fail.
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemory.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -36,7 +36,7 @@
* in WorkingMemories from an InputStream.
*
*/
-public interface WorkingMemory extends WorkingMemoryEventManager, Externalizable {
+public interface WorkingMemory extends WorkingMemoryEventManager, WorkingMemoryEntryPoint, Externalizable {
/**
* Returns the Agenda for this WorkingMemory. While the WorkingMemory interface is considered public, the Agenda interface
@@ -206,34 +206,6 @@
/**
- * Assert a fact.
- *
- * @param object
- * The fact object.
- *
- * @return The new fact-handle associated with the object.
- *
- * @throws FactException
- * If a RuntimeException error occurs.
- */
- FactHandle insert(Object object) throws FactException;
-
- /**
- * Assert a fact with inherent duration.
- *
- * @param object
- * The fact object.
- * @param duration
- * The duration of the fact.
- *
- * @return The new fact-handle associated with the object.
- *
- * @throws FactException
- * If a RuntimeException error occurs.
- */
- FactHandle insert(Object object, long duration) throws FactException;
-
- /**
* Retrieve the QueryResults of the specified query.
*
* @param query
@@ -265,88 +237,6 @@
public QueryResults getQueryResults(String query, Object[] arguments);
/**
- * Insert a fact registering JavaBean <code>PropertyChangeListeners</code>
- * on the Object to automatically trigger <code>update</code> calls
- * if <code>dynamic</code> is <code>true</code>.
- *
- * @param object
- * The fact object.
- * @param dynamic
- * true if Drools should add JavaBean
- * <code>PropertyChangeListeners</code> to the object.
- *
- * @return The new fact-handle associated with the object.
- *
- * @throws FactException
- * If a RuntimeException error occurs.
- */
- FactHandle insert(Object object,
- boolean dynamic) throws FactException;
-
- /**
- * Insert a fact with inherent duration registering JavaBean
- * <code>PropertyChangeListeners</code> on the Object to
- * automatically trigger <code>update</code> calls
- * if <code>dynamic</code> is <code>true</code>.
- *
- * @param object
- * The fact object.
- * @param duration
- * The duration of the fact.
- * @param dynamic
- * true if Drools should add JavaBean
- * <code>PropertyChangeListeners</code> to the object.
- *
- * @return The new fact-handle associated with the object.
- *
- * @throws FactException
- * If a RuntimeException error occurs.
- */
- FactHandle insert(Object object,
- long duration,
- boolean dynamic) throws FactException;
-
- /**
- * Retract a fact.
- *
- * @param handle
- * The fact-handle associated with the fact to retract.
- *
- * @throws FactException
- * If a RuntimeException error occurs.
- */
- void retract(FactHandle handle) throws FactException;
-
- /**
- * Inform the WorkingMemory that a Fact has been modified and that it
- * should now update the network.
- *
- * @param handle
- * The fact-handle associated with the fact to modify.
- * @param object
- * The new value of the fact.
- *
- * @throws FactException
- * If a RuntimeException error occurs.
- */
- void update(FactHandle handle,
- Object object) throws FactException;
-
- /**
- *
- * @param factHandle
- */
- public void modifyRetract(final FactHandle factHandle);
-
- /**
- *
- * @param factHandle
- * @param object
- */
- public void modifyInsert(final FactHandle factHandle,
- final Object object);
-
- /**
* Sets the AsyncExceptionHandler to handle exceptions thrown by the Agenda
* Scheduler used for duration rules.
*
@@ -414,6 +304,6 @@
* @param id the id of the entry point, as defined in the rules file
* @return
*/
- public EntryPointInterface getEntryPoint( String id );
+ public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint( String id );
}
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/WorkingMemoryEntryPoint.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2007 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.
+ *
+ * Created on Dec 14, 2007
+ */
+package org.drools;
+
+import java.io.Externalizable;
+
+/**
+ * An interface for instances that allow handling of entry-point-scoped
+ * facts
+ *
+ * @author etirelli
+ */
+public interface WorkingMemoryEntryPoint
+ extends
+ Externalizable {
+ /**
+ * Assert a fact.
+ *
+ * @param object
+ * The fact object.
+ *
+ * @return The new fact-handle associated with the object.
+ *
+ * @throws FactException
+ * If a RuntimeException error occurs.
+ */
+ FactHandle insert(Object object) throws FactException;
+
+ /**
+ * Insert a fact registering JavaBean <code>PropertyChangeListeners</code>
+ * on the Object to automatically trigger <code>update</code> calls
+ * if <code>dynamic</code> is <code>true</code>.
+ *
+ * @param object
+ * The fact object.
+ * @param dynamic
+ * true if Drools should add JavaBean
+ * <code>PropertyChangeListeners</code> to the object.
+ *
+ * @return The new fact-handle associated with the object.
+ *
+ * @throws FactException
+ * If a RuntimeException error occurs.
+ */
+ FactHandle insert(Object object,
+ boolean dynamic) throws FactException;
+
+ /**
+ * Retract a fact.
+ *
+ * @param handle
+ * The fact-handle associated with the fact to retract.
+ *
+ * @throws FactException
+ * If a RuntimeException error occurs.
+ */
+ void retract(FactHandle handle) throws FactException;
+
+ /**
+ * Inform the WorkingMemory that a Fact has been modified and that it
+ * should now update the network.
+ *
+ * @param handle
+ * The fact-handle associated with the fact to modify.
+ * @param object
+ * The new value of the fact.
+ *
+ * @throws FactException
+ * If a RuntimeException error occurs.
+ */
+ void update(FactHandle handle,
+ Object object) throws FactException;
+
+ /**
+ *
+ * @param factHandle
+ */
+ public void modifyRetract(final FactHandle factHandle);
+
+ /**
+ *
+ * @param factHandle
+ * @param object
+ */
+ public void modifyInsert(final FactHandle factHandle,
+ final Object object);
+
+ public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint(String name);
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/DefaultKnowledgeHelper.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -27,14 +27,13 @@
import org.drools.spi.KnowledgeHelper;
import org.drools.spi.Tuple;
-import java.io.Externalizable;
import java.io.IOException;
-import java.io.ObjectOutput;
import java.io.ObjectInput;
+import java.io.ObjectOutput;
public class DefaultKnowledgeHelper
implements
- KnowledgeHelper, Externalizable {
+ KnowledgeHelper {
private static final long serialVersionUID = 400L;
@@ -45,8 +44,8 @@
private InternalWorkingMemoryActions workingMemory;
public DefaultKnowledgeHelper() {
-
}
+
public DefaultKnowledgeHelper(final WorkingMemory workingMemory) {
this.workingMemory = (InternalWorkingMemoryActions) workingMemory;
}
@@ -83,71 +82,32 @@
public void insert(final Object object) throws FactException {
insert( object,
- 0,
false );
}
public void insert(final Object object,
- final long duration) throws FactException {
- insert( object,
- duration,
- false );
- }
-
- public void insert(final Object object,
final boolean dynamic) throws FactException {
this.workingMemory.insert( object,
- 0,
dynamic,
false,
this.rule,
this.activation );
}
- public void insert(final Object object,
- final long duration,
- final boolean dynamic) throws FactException {
- this.workingMemory.insert( object,
- duration,
- dynamic,
- false,
- this.rule,
- this.activation );
- }
-
public void insertLogical(final Object object) throws FactException {
insertLogical( object,
- 0,
false );
}
- public void insertLogical(final Object object, final long duration) throws FactException {
- insertLogical( object,
- duration,
- false );
- }
-
public void insertLogical(final Object object,
final boolean dynamic) throws FactException {
this.workingMemory.insert( object,
- 0,
dynamic,
true,
this.rule,
this.activation );
}
- public void insertLogical(final Object object,
- final long duration,
- final boolean dynamic) throws FactException {
- this.workingMemory.insert( object,
- duration,
- dynamic,
- true,
- this.rule,
- this.activation );
- }
-
public void update(final FactHandle handle,
final Object newObject) throws FactException {
// only update if this fact exists in the wm
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/SequentialKnowledgeHelper.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -16,21 +16,21 @@
* limitations under the License.
*/
-import java.util.List;
-
import org.drools.FactException;
import org.drools.FactHandle;
-import org.drools.QueryResults;
import org.drools.WorkingMemory;
import org.drools.common.InternalWorkingMemoryActions;
import org.drools.rule.Declaration;
import org.drools.rule.GroupElement;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
-import org.drools.spi.AgendaGroup;
import org.drools.spi.KnowledgeHelper;
import org.drools.spi.Tuple;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
public class SequentialKnowledgeHelper
implements
KnowledgeHelper {
@@ -41,8 +41,27 @@
private GroupElement subrule;
private Activation activation;
private Tuple tuple;
- private final InternalWorkingMemoryActions workingMemory;
+ private InternalWorkingMemoryActions workingMemory;
+ public SequentialKnowledgeHelper() {
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ rule = (Rule)in.readObject();
+ subrule = (GroupElement)in.readObject();
+ activation = (Activation)in.readObject();
+ tuple = (Tuple)in.readObject();
+ workingMemory = (InternalWorkingMemoryActions)in.readObject();
+
+ }
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(rule);
+ out.writeObject(subrule);
+ out.writeObject(activation);
+ out.writeObject(tuple);
+ out.writeObject(workingMemory);
+ }
+
public SequentialKnowledgeHelper(final WorkingMemory workingMemory) {
this.workingMemory = (InternalWorkingMemoryActions) workingMemory;
}
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 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractRuleBase.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -34,6 +34,7 @@
import org.drools.rule.MapBackedClassLoader;
import org.drools.rule.Package;
import org.drools.rule.Rule;
+import org.drools.rule.TypeDeclaration;
import org.drools.spi.ExecutorServiceFactory;
import org.drools.spi.FactHandleFactory;
import org.drools.util.ObjectHashSet;
@@ -115,6 +116,8 @@
private int additionsSinceLock;
private int removalsSinceLock;
+ private transient Map<Class< ? >, TypeDeclaration> classTypeDeclaration;
+
/**
* Default constructor - for Externalizable. This should never be used by a user, as it
* will result in an invalid state for the instance.
@@ -157,6 +160,8 @@
this.globals = new HashMap();
this.statefulSessions = new ObjectHashSet();
this.objenesis = createObjenesis();
+
+ this.classTypeDeclaration = new HashMap<Class< ? >, TypeDeclaration>();
}
// ------------------------------------------------------------
@@ -276,6 +281,15 @@
return new ObjenesisStd( true );
}
+ private void populateTypeDeclarationMaps() {
+ this.classTypeDeclaration = new HashMap<Class<?>, TypeDeclaration>();
+ for( Package pkg : this.pkgs.values() ) {
+ for( TypeDeclaration type : pkg.getTypeDeclarations().values() ) {
+ this.classTypeDeclaration.put( type.getTypeClass(), type );
+ }
+ }
+ }
+
/**
* @return the id
*/
@@ -429,6 +443,15 @@
}
this.globals.putAll( newGlobals );
+ // Add type declarations
+ for ( TypeDeclaration type : newPkg.getTypeDeclarations().values() ) {
+ // should we allow overrides?
+ if ( !this.classTypeDeclaration.containsKey( type.getTypeClass() ) ) {
+ this.classTypeDeclaration.put( type.getTypeClass(),
+ type );
+ }
+ }
+
final Rule[] rules = newPkg.getRules();
for ( int i = 0; i < rules.length; ++i ) {
@@ -483,14 +506,30 @@
}
globals.putAll( newPkg.getGlobals() );
+ // add type declarations
+ for ( TypeDeclaration type : newPkg.getTypeDeclarations().values() ) {
+ // should we allow overrides?
+ if ( !this.classTypeDeclaration.containsKey( type.getTypeClass() ) ) {
+ this.classTypeDeclaration.put( type.getTypeClass(),
+ type );
+ }
+ if ( !pkg.getTypeDeclarations().containsKey( type.getTypeName() ) ) {
+ pkg.addTypeDeclaration( type );
+ }
+ }
+
//Add rules into the RuleBase package
//as this is needed for individual rule removal later on
final Rule[] newRules = newPkg.getRules();
for ( int i = 0; i < newRules.length; i++ ) {
final Rule newRule = newRules[i];
- if ( pkg.getRule( newRule.getName() ) == null ) {
- pkg.addRule( newRule );
+
+ // remove the rule if it already exists
+ if ( pkg.getRule( newRule.getName() ) != null ) {
+ removeRule( pkg, pkg.getRule( newRule.getName() ) );
}
+
+ pkg.addRule( newRule );
}
//and now the rule flows
@@ -510,6 +549,10 @@
this.reloadPackageCompilationData.addDialectDatas( pkg.getDialectDatas() );
}
+ public TypeDeclaration getTypeDeclaration(Class< ? > clazz) {
+ return this.classTypeDeclaration.get( clazz );
+ }
+
private synchronized void addRule(final Package pkg,
final Rule rule) throws InvalidPatternException {
this.eventSupport.fireBeforeRuleAdded( pkg,
@@ -732,10 +775,9 @@
((InternalWorkingMemory) session).setRuleBase( this );
((InternalWorkingMemory) session).setId( (nextWorkingMemoryCounter()) );
+
ExecutorService executor = ExecutorServiceFactory.createExecutorService( this.config.getExecutorService() );
-
executor.setCommandExecutor( new CommandExecutor( session ) );
-
((InternalWorkingMemory) session).setExecutorService( executor );
if ( keepReference ) {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/AbstractWorkingMemory.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -16,31 +16,7 @@
* limitations under the License.
*/
-import java.beans.PropertyChangeEvent;
-import java.beans.PropertyChangeListener;
-import java.io.Serializable;
-import java.io.ObjectOutput;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.Externalizable;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Queue;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-
import org.drools.Agenda;
-import org.drools.EntryPointInterface;
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.ObjectFilter;
@@ -48,10 +24,11 @@
import org.drools.QueryResults;
import org.drools.RuleBase;
import org.drools.RuleBaseConfiguration;
-import org.drools.RuntimeDroolsException;
-import org.drools.WorkingMemory;
import org.drools.RuleBaseConfiguration.AssertBehaviour;
import org.drools.RuleBaseConfiguration.LogicalOverride;
+import org.drools.RuntimeDroolsException;
+import org.drools.WorkingMemory;
+import org.drools.WorkingMemoryEntryPoint;
import org.drools.base.MapGlobalResolver;
import org.drools.base.ShadowProxy;
import org.drools.concurrent.ExecutorService;
@@ -62,20 +39,18 @@
import org.drools.event.RuleFlowEventSupport;
import org.drools.event.WorkingMemoryEventListener;
import org.drools.event.WorkingMemoryEventSupport;
-import org.drools.facttemplates.Fact;
import org.drools.process.core.Process;
import org.drools.process.instance.ProcessInstance;
import org.drools.process.instance.ProcessInstanceFactory;
import org.drools.process.instance.WorkItemManager;
-import org.drools.reteoo.ClassObjectTypeConf;
import org.drools.reteoo.EntryPointNode;
-import org.drools.reteoo.FactTemplateTypeConf;
import org.drools.reteoo.LIANodePropagation;
import org.drools.reteoo.ObjectTypeConf;
import org.drools.rule.Declaration;
import org.drools.rule.EntryPoint;
import org.drools.rule.Rule;
import org.drools.rule.TimeMachine;
+import org.drools.rule.TypeDeclaration;
import org.drools.ruleflow.core.RuleFlowProcess;
import org.drools.ruleflow.instance.RuleFlowProcessInstanceFactory;
import org.drools.spi.Activation;
@@ -86,6 +61,28 @@
import org.drools.spi.GlobalResolver;
import org.drools.spi.PropagationContext;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
/**
* Implementation of <code>WorkingMemory</code>.
*
@@ -97,12 +94,12 @@
implements
InternalWorkingMemoryActions,
EventSupport,
- PropertyChangeListener, Externalizable {
+ PropertyChangeListener {
// ------------------------------------------------------------
// Constants
// ------------------------------------------------------------
- protected static final Class[] ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES = new Class[]{PropertyChangeListener.class};
- private static final int NODE_MEMORIES_ARRAY_GROWTH = 32;
+ protected static final Class[] ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES = new Class[]{PropertyChangeListener.class};
+ private static final int NODE_MEMORIES_ARRAY_GROWTH = 32;
// ------------------------------------------------------------
// Instance members
@@ -143,45 +140,49 @@
protected Queue<WorkingMemoryAction> actionQueue = new LinkedList<WorkingMemoryAction>();
- protected boolean evaluatingActionQueue;
+ protected boolean evaluatingActionQueue;
protected ReentrantLock lock = new ReentrantLock();
protected boolean discardOnLogicalOverride;
- protected long propagationIdCounter;
+ /**
+ * This must be thread safe as it is incremented and read via different EntryPoints
+ */
+ protected AtomicLong propagationIdCounter;
private boolean maintainTms;
private boolean sequential;
- private List liaPropagations = Collections.EMPTY_LIST;
+ private List liaPropagations = Collections.EMPTY_LIST;
/** Flag to determine if a rule is currently being fired. */
- protected boolean firing;
+ protected boolean firing;
- protected boolean halt;
+ protected boolean halt;
- private Map processInstances = new HashMap();
+ private Map processInstances = new HashMap();
- private int processCounter;
+ private int processCounter;
- private WorkItemManager workItemManager;
+ private WorkItemManager workItemManager;
- private Map<String, ProcessInstanceFactory> processInstanceFactories = new HashMap();
+ private Map<String, ProcessInstanceFactory> processInstanceFactories = new HashMap();
- private TimeMachine timeMachine = new TimeMachine();
+ private TimeMachine timeMachine = new TimeMachine();
- private Map<EntryPoint, Map<Object, ObjectTypeConf>> typeConfMap;
+ protected transient ObjectTypeConfigurationRegistry typeConfReg;
- private EntryPoint entryPoint;
- private EntryPointNode entryPointNode;
+ protected EntryPoint entryPoint;
+ protected transient EntryPointNode entryPointNode;
+ protected Map<String, WorkingMemoryEntryPoint> entryPoints;
+
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
public AbstractWorkingMemory() {
-
}
/**
* Construct.
@@ -191,7 +192,6 @@
*/
public AbstractWorkingMemory(final int id,
final InternalRuleBase ruleBase,
- //final EntryPoint entryPoint,
final FactHandleFactory handleFactory) {
this.id = id;
this.ruleBase = ruleBase;
@@ -210,6 +210,8 @@
final RuleBaseConfiguration conf = this.ruleBase.getConfiguration();
+ this.propagationIdCounter = new AtomicLong();
+
this.objectStore = new SingleThreadedObjectStore( conf,
this.lock );
@@ -223,18 +225,19 @@
this.workItemManager = new WorkItemManager( this );
this.processInstanceFactories.put( RuleFlowProcess.RULEFLOW_TYPE,
new RuleFlowProcessInstanceFactory() );
+ this.entryPoints = new ConcurrentHashMap();
+ this.entryPoints.put( "DEFAULT",
+ this );
- this.typeConfMap = new ConcurrentHashMap<EntryPoint, Map<Object, ObjectTypeConf>>();
-
-// this.entryPoint = entryPoint;
-// this.entryPointNode = this.ruleBase.getRete().getEntryPointNode( this.entryPoint );
+ this.entryPoint = EntryPoint.DEFAULT;
+ initTransient();
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
id = in.readLong();
evaluatingActionQueue = in.readBoolean();
discardOnLogicalOverride = in.readBoolean();
- propagationIdCounter = in.readLong();
+ propagationIdCounter = (AtomicLong)in.readObject();
maintainTms = in.readBoolean();
sequential = in.readBoolean();
firing = in.readBoolean();
@@ -260,7 +263,6 @@
workItemManager = (WorkItemManager)in.readObject();
processInstanceFactories = (Map<String, ProcessInstanceFactory>)in.readObject();
timeMachine = (TimeMachine)in.readObject();
- typeConfMap = (Map<EntryPoint, Map<Object, ObjectTypeConf>>)in.readObject();
entryPoint = (EntryPoint)in.readObject();
entryPointNode = (EntryPointNode)in.readObject();
}
@@ -269,7 +271,7 @@
out.writeLong(id);
out.writeBoolean(evaluatingActionQueue);
out.writeBoolean(discardOnLogicalOverride);
- out.writeLong(propagationIdCounter);
+ out.writeObject(propagationIdCounter);
out.writeBoolean(maintainTms);
out.writeBoolean(sequential);
out.writeBoolean(firing);
@@ -295,7 +297,6 @@
out.writeObject(workItemManager);
out.writeObject(processInstanceFactories);
out.writeObject(timeMachine);
- out.writeObject(typeConfMap);
out.writeObject(entryPoint);
out.writeObject(entryPointNode);
}
@@ -307,8 +308,16 @@
public void setRuleBase(final InternalRuleBase ruleBase) {
this.ruleBase = ruleBase;
this.nodeMemories.setRuleBaseReference( this.ruleBase );
+ initTransient();
}
+
+ private void initTransient() {
+ this.entryPointNode = this.ruleBase.getRete().getEntryPointNode( this.entryPoint );
+ this.typeConfReg = new ObjectTypeConfigurationRegistry( this.ruleBase );
+ }
+
+
public void setWorkingMemoryEventSupport(WorkingMemoryEventSupport workingMemoryEventSupport) {
this.workingMemoryEventSupport = workingMemoryEventSupport;
}
@@ -499,9 +508,7 @@
}
}
- if ( !this.actionQueue.isEmpty() ) {
- executeQueuedActions();
- }
+ executeQueuedActions();
boolean noneFired = true;
@@ -512,9 +519,7 @@
while ( continueFiring( fireLimit ) && this.agenda.fireNextItem( agendaFilter ) ) {
fireLimit = updateFireLimit( fireLimit );
noneFired = false;
- if ( !this.actionQueue.isEmpty() ) {
- executeQueuedActions();
- }
+ executeQueuedActions();
}
} finally {
this.firing = false;
@@ -674,161 +679,82 @@
*/
public FactHandle insert(final Object object) throws FactException {
return insert( object, /* Not-Dynamic */
- 0,
false,
false,
null,
null );
}
- public FactHandle insert(final Object object,
- final long duration) throws FactException {
- return insert( object, /* Not-Dynamic */
- duration,
- false,
- false,
- null,
- null );
- }
-
/**
* @see WorkingMemory
*/
public FactHandle insertLogical(final Object object) throws FactException {
return insert( object, //Not-Dynamic
- 0,
false,
true,
null,
null );
}
- public FactHandle insertLogical(final Object object,
- final long duration) throws FactException {
- return insert( object, /* Not-Dynamic */
- duration,
- false,
- true,
- null,
- null );
- }
-
public FactHandle insert(final Object object,
final boolean dynamic) throws FactException {
return insert( object,
- 0,
dynamic,
false,
null,
null );
}
- public FactHandle insert(final Object object,
- final long duration,
- final boolean dynamic) throws FactException {
- return insert( object,
- duration,
- dynamic,
- false,
- null,
- null );
- }
-
public FactHandle insertLogical(final Object object,
final boolean dynamic) throws FactException {
return insert( object,
- 0,
dynamic,
true,
null,
null );
}
- public FactHandle insertLogical(final Object object,
- final long duration,
- final boolean dynamic) throws FactException {
- return insert( object,
- duration,
- dynamic,
- true,
- null,
- null );
- }
+ // protected FactHandle insert(final EntryPoint entryPoint,
+ // final Object object,
+ // final boolean dynamic,
+ // boolean logical,
+ // final Rule rule,
+ // final Activation activation) throws FactException {
+ // return this.insert( entryPoint,
+ // object,
+ // 0,
+ // dynamic,
+ // logical,
+ // rule,
+ // activation );
+ // }
public FactHandle insert(final Object object,
final boolean dynamic,
boolean logical,
final Rule rule,
final Activation activation) throws FactException {
- return this.insert( EntryPoint.DEFAULT,
- object,
- 0,
- dynamic,
- logical,
- rule,
- activation );
-
- }
-
- public FactHandle insert(final Object object,
- final long duration,
- final boolean dynamic,
- boolean logical,
- final Rule rule,
- final Activation activation) throws FactException {
- return this.insert( EntryPoint.DEFAULT,
- object,
- duration,
- dynamic,
- logical,
- rule,
- activation );
- }
-
- protected FactHandle insert(final EntryPoint entryPoint,
- final Object object,
- final boolean dynamic,
- boolean logical,
- final Rule rule,
- final Activation activation) throws FactException {
- return this.insert( entryPoint,
- object,
- 0,
- dynamic,
- logical,
- rule,
- activation );
- }
-
- protected FactHandle insert(final EntryPoint entryPoint,
- final Object object,
- final long duration,
- final boolean dynamic,
- boolean logical,
- final Rule rule,
- final Activation activation) throws FactException {
if ( object == null ) {
// you cannot assert a null object
return null;
}
- ObjectTypeConf typeConf = getObjectTypeConf( entryPoint,
- object );
+ ObjectTypeConf typeConf = this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object );
InternalFactHandle handle = null;
if ( isSequential() ) {
handle = this.handleFactory.newFactHandle( object,
typeConf.isEvent(),
- duration,
this );
this.objectStore.addHandle( handle,
object );
- insert( entryPoint,
- handle,
+ insert( handle,
object,
rule,
- activation );
+ activation,
+ typeConf );
return handle;
}
@@ -879,7 +805,6 @@
// assert
handle = this.handleFactory.newFactHandle( object,
typeConf.isEvent(),
- duration,
this );
this.objectStore.addHandle( handle,
object );
@@ -910,7 +835,7 @@
key.setStatus( EqualityKey.STATED );
handle = key.getFactHandle();
- if ( AssertBehaviour.IDENTITY.equals(this.ruleBase.getConfiguration().getAssertBehaviour()) ) {
+ if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.IDENTITY ) {
// as assertMap may be using an "identity"
// equality comparator,
// we need to remove the handle from the map,
@@ -934,7 +859,6 @@
key.setStatus( EqualityKey.STATED );
handle = this.handleFactory.newFactHandle( object,
typeConf.isEvent(),
- duration,
this );
handle.setEqualityKey( key );
key.addFactHandle( handle );
@@ -946,7 +870,6 @@
} else {
handle = this.handleFactory.newFactHandle( object,
typeConf.isEvent(),
- duration,
this );
this.objectStore.addHandle( handle,
object );
@@ -978,7 +901,6 @@
}
handle = this.handleFactory.newFactHandle( object,
typeConf.isEvent(),
- duration,
this );
this.objectStore.addHandle( handle,
object );
@@ -989,11 +911,11 @@
addPropertyChangeListener( object );
}
- insert( entryPoint,
- handle,
+ insert( handle,
object,
rule,
- activation );
+ activation,
+ typeConf );
} finally {
this.lock.unlock();
@@ -1001,18 +923,18 @@
return handle;
}
- protected void insert(final EntryPoint entryPoint,
- final InternalFactHandle handle,
+ protected void insert(final InternalFactHandle handle,
final Object object,
final Rule rule,
- final Activation activation) {
+ final Activation activation,
+ ObjectTypeConf typeConf) {
this.ruleBase.executeQueuedActions();
if ( activation != null ) {
// release resources so that they can be GC'ed
activation.getPropagationContext().releaseResources();
}
- final PropagationContext propagationContext = new PropagationContextImpl( this.propagationIdCounter++,
+ final PropagationContext propagationContext = new PropagationContextImpl( getNextPropagationIdCounter(),
PropagationContext.ASSERTION,
rule,
activation,
@@ -1020,13 +942,12 @@
this.agenda.getDormantActivations(),
entryPoint );
- doInsert( handle,
- object,
- propagationContext );
+ this.entryPointNode.assertObject( handle,
+ propagationContext,
+ typeConf,
+ this );
- if ( !this.actionQueue.isEmpty() ) {
- executeQueuedActions();
- }
+ executeQueuedActions();
this.workingMemoryEventSupport.fireObjectInserted( propagationContext,
handle,
@@ -1057,14 +978,10 @@
}
}
- public abstract void doInsert(InternalFactHandle factHandle,
- Object object,
- PropagationContext propagationContext) throws FactException;
-
protected void removePropertyChangeListener(final FactHandle handle) {
Object object = null;
try {
- object = getObject( handle );
+ object = ((InternalFactHandle) handle).getObject();
if ( object != null ) {
final Method mehod = object.getClass().getMethod( "removePropertyChangeListener",
@@ -1100,32 +1017,11 @@
null );
}
- public abstract void doRetract(InternalFactHandle factHandle,
- PropagationContext propagationContext);
-
- /**
- * @see WorkingMemory
- */
public void retract(final FactHandle factHandle,
final boolean removeLogical,
final boolean updateEqualsMap,
final Rule rule,
final Activation activation) throws FactException {
- this.retract( EntryPoint.DEFAULT,
- factHandle,
- removeLogical,
- updateEqualsMap,
- rule,
- activation );
-
- }
-
- protected void retract(final EntryPoint entryPoint,
- final FactHandle factHandle,
- final boolean removeLogical,
- final boolean updateEqualsMap,
- final Rule rule,
- final Activation activation) throws FactException {
try {
this.lock.lock();
this.ruleBase.executeQueuedActions();
@@ -1141,17 +1037,22 @@
// release resources so that they can be GC'ed
activation.getPropagationContext().releaseResources();
}
- final PropagationContext propagationContext = new PropagationContextImpl( this.propagationIdCounter++,
+ final PropagationContext propagationContext = new PropagationContextImpl( getNextPropagationIdCounter(),
PropagationContext.RETRACTION,
rule,
activation,
this.agenda.getActiveActivations(),
this.agenda.getDormantActivations(),
- entryPoint );
+ this.entryPoint );
- doRetract( handle,
- propagationContext );
+ final Object object = handle.getObject();
+ this.entryPointNode.retractObject( handle,
+ propagationContext,
+ this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object ),
+ this );
+
if ( this.maintainTms ) {
// Update the equality key, which maintains a list of stated
// FactHandles
@@ -1173,8 +1074,6 @@
}
}
- final Object object = handle.getObject();
-
this.workingMemoryEventSupport.fireObjectRetracted( propagationContext,
handle,
object,
@@ -1184,32 +1083,12 @@
this.handleFactory.destroyFactHandle( handle );
- if ( !this.actionQueue.isEmpty() ) {
- executeQueuedActions();
- }
+ executeQueuedActions();
} finally {
this.lock.unlock();
}
}
- // private void addHandleToMaps(InternalFactHandle handle) {
- // this.assertMap.put( handle,
- // handle,
- // false );
- // if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
- // this.identityMap.put( handle,
- // handle,
- // false );
- // }
- // }
- //
- // private void removeHandleFromMaps(final InternalFactHandle handle) {
- // this.assertMap.remove( handle );
- // if ( this.ruleBase.getConfiguration().getAssertBehaviour() == AssertBehaviour.EQUALITY ) {
- // this.identityMap.remove( handle );
- // }
- // }
-
public void modifyRetract(final FactHandle factHandle) {
modifyRetract( factHandle,
null,
@@ -1219,16 +1098,6 @@
public void modifyRetract(final FactHandle factHandle,
final Rule rule,
final Activation activation) {
- this.modifyRetract( EntryPoint.DEFAULT,
- factHandle,
- rule,
- activation );
- }
-
- protected void modifyRetract(final EntryPoint entryPoint,
- final FactHandle factHandle,
- final Rule rule,
- final Activation activation) {
try {
this.lock.lock();
this.ruleBase.executeQueuedActions();
@@ -1255,16 +1124,20 @@
activation.getPropagationContext().releaseResources();
}
// Nowretract any trace of the original fact
- final PropagationContext propagationContext = new PropagationContextImpl( this.propagationIdCounter++,
+ final PropagationContext propagationContext = new PropagationContextImpl( getNextPropagationIdCounter(),
PropagationContext.MODIFICATION,
rule,
activation,
this.agenda.getActiveActivations(),
this.agenda.getDormantActivations(),
entryPoint );
- doRetract( handle,
- propagationContext );
+ this.entryPointNode.retractObject( handle,
+ propagationContext,
+ this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ handle.getObject() ),
+ this );
+
if ( this.maintainTms ) {
// the hashCode and equality has changed, so we must update the
@@ -1294,18 +1167,6 @@
final Object object,
final Rule rule,
final Activation activation) {
- this.modifyInsert( EntryPoint.DEFAULT,
- factHandle,
- object,
- rule,
- activation );
- }
-
- protected void modifyInsert(final EntryPoint entryPoint,
- final FactHandle factHandle,
- final Object object,
- final Rule rule,
- final Activation activation) {
try {
this.lock.lock();
this.ruleBase.executeQueuedActions();
@@ -1337,7 +1198,7 @@
activation.getPropagationContext().releaseResources();
}
// Nowretract any trace of the original fact
- final PropagationContext propagationContext = new PropagationContextImpl( this.propagationIdCounter++,
+ final PropagationContext propagationContext = new PropagationContextImpl( getNextPropagationIdCounter(),
PropagationContext.MODIFICATION,
rule,
activation,
@@ -1345,9 +1206,11 @@
this.agenda.getDormantActivations(),
entryPoint );
- doInsert( handle,
- object,
- propagationContext );
+ this.entryPointNode.assertObject( handle,
+ propagationContext,
+ this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object ),
+ this );
this.workingMemoryEventSupport.fireObjectUpdated( propagationContext,
factHandle,
@@ -1357,9 +1220,7 @@
propagationContext.clearRetractedTuples();
- if ( !this.actionQueue.isEmpty() ) {
- executeQueuedActions();
- }
+ executeQueuedActions();
} finally {
this.lock.unlock();
}
@@ -1373,29 +1234,10 @@
null );
}
- /**
- * modify is implemented as half way retract / assert due to the truth
- * maintenance issues.
- *
- * @see WorkingMemory
- */
public void update(final FactHandle factHandle,
final Object object,
final Rule rule,
final Activation activation) throws FactException {
- this.update( EntryPoint.DEFAULT,
- factHandle,
- object,
- rule,
- activation );
-
- }
-
- protected void update(final EntryPoint entryPoint,
- final FactHandle factHandle,
- final Object object,
- final Rule rule,
- final Activation activation) throws FactException {
try {
this.lock.lock();
this.ruleBase.executeQueuedActions();
@@ -1421,17 +1263,23 @@
activation.getPropagationContext().releaseResources();
}
// Nowretract any trace of the original fact
- final PropagationContext propagationContext = new PropagationContextImpl( this.propagationIdCounter++,
+ final PropagationContext propagationContext = new PropagationContextImpl( getNextPropagationIdCounter(),
PropagationContext.MODIFICATION,
rule,
activation,
this.agenda.getActiveActivations(),
this.agenda.getDormantActivations(),
entryPoint );
- doRetract( handle,
- propagationContext );
- if ( originalObject != object || !AssertBehaviour.IDENTITY.equals(this.ruleBase.getConfiguration().getAssertBehaviour()) ) {
+ ObjectTypeConf typeConf = this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object );
+
+ this.entryPointNode.retractObject( handle,
+ propagationContext,
+ typeConf,
+ this );
+
+ if ( (originalObject != object) || (this.ruleBase.getConfiguration().getAssertBehaviour() != AssertBehaviour.IDENTITY) ) {
this.objectStore.removeHandle( handle );
// set anyway, so that it updates the hashCodes
@@ -1468,9 +1316,10 @@
this.handleFactory.increaseFactHandleRecency( handle );
- doInsert( handle,
- object,
- propagationContext );
+ this.entryPointNode.assertObject( handle,
+ propagationContext,
+ typeConf,
+ this );
this.workingMemoryEventSupport.fireObjectUpdated( propagationContext,
factHandle,
@@ -1480,20 +1329,18 @@
propagationContext.clearRetractedTuples();
- if ( !this.actionQueue.isEmpty() ) {
- executeQueuedActions();
- }
+ executeQueuedActions();
} finally {
this.lock.unlock();
}
}
public void executeQueuedActions() {
- if( ! evaluatingActionQueue ) {
+ if ( !this.actionQueue.isEmpty() && !evaluatingActionQueue ) {
evaluatingActionQueue = true;
WorkingMemoryAction action = null;
- while ( ( action = actionQueue.poll() ) != null ) {
+ while ( (action = actionQueue.poll()) != null ) {
action.execute( this );
}
evaluatingActionQueue = false;
@@ -1575,7 +1422,7 @@
}
public long getNextPropagationIdCounter() {
- return this.propagationIdCounter++;
+ return this.propagationIdCounter.incrementAndGet();
}
public Lock getLock() {
@@ -1709,168 +1556,101 @@
// no executor service, so nothing to set
}
- /**
- * Returns the ObjectTypeConfiguration object for the given object or
- * creates a new one if none is found in the cache
- *
- * @param object
- * @return
- */
- public ObjectTypeConf getObjectTypeConf(EntryPoint entrypoint,
- Object object) {
- Map<Object, ObjectTypeConf> map = this.typeConfMap.get( entrypoint );
- if ( map == null ) {
- map = new ConcurrentHashMap<Object, ObjectTypeConf>();
- this.typeConfMap.put( entrypoint,
- map );
- }
- ObjectTypeConf objectTypeConf;
+ public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint(String name) {
+ WorkingMemoryEntryPoint wmEntryPoint = this.entryPoints.get( name );
+ if ( wmEntryPoint == null ) {
+ EntryPoint entryPoint = new EntryPoint( name );
+ EntryPointNode entryPointNode = this.ruleBase.getRete().getEntryPointNode( entryPoint );
- if ( object instanceof Fact ) {
- String key = ((Fact) object).getFactTemplate().getName();
- objectTypeConf = map.get( key );
- if ( objectTypeConf == null ) {
- objectTypeConf = new FactTemplateTypeConf( entrypoint,
- ((Fact) object).getFactTemplate(),
- this.ruleBase );
- this.addObjectTypeConf( entrypoint,
- key,
- objectTypeConf );
+ if ( entryPointNode != null ) {
+ wmEntryPoint = new NamedEntryPoint( entryPoint,
+ entryPointNode,
+ this );
}
- object = key;
- } else {
- Class cls = null;
- if ( object instanceof ShadowProxy ) {
- cls = ((ShadowProxy) object).getShadowedObject().getClass();
- } else {
- cls = object.getClass();
- }
- objectTypeConf = map.get( cls );
- if ( objectTypeConf == null ) {
-
- final boolean isEvent = this.ruleBase.isEvent( cls );
- objectTypeConf = new ClassObjectTypeConf( entrypoint,
- cls,
- isEvent,
- this.ruleBase );
- this.addObjectTypeConf( entrypoint,
- cls,
- objectTypeConf );
+ if ( wmEntryPoint != null ) {
+ this.entryPoints.put( name,
+ wmEntryPoint );
}
-
}
- return objectTypeConf;
+ return wmEntryPoint;
}
- public Map<Object, ObjectTypeConf> getObjectTypeConfMap(EntryPoint entryPoint) {
- Map<Object, ObjectTypeConf> map = this.typeConfMap.get( entryPoint );
- if ( map == null ) {
- map = Collections.emptyMap();
- }
- return map;
- }
+ // protected static class EntryPointInterfaceImpl
+ // implements
+ // EntryPointInterface {
+ //
+ // private static final long serialVersionUID = 2917871170743358801L;
+ //
+ // private final EntryPoint entryPoint;
+ // private final AbstractWorkingMemory wm;
+ //
+ // public EntryPointInterfaceImpl(EntryPoint entryPoint,
+ // AbstractWorkingMemory wm) {
+ // this.entryPoint = entryPoint;
+ // this.wm = wm;
+ // }
+ //
+ // public FactHandle insert(Object object) throws FactException {
+ // return wm.insert( this.entryPoint,
+ // object, /* Not-Dynamic */
+ // false,
+ // false,
+ // null,
+ // null );
+ // }
+ //
+ // public FactHandle insert(Object object,
+ // boolean dynamic) throws FactException {
+ // return wm.insert( this.entryPoint,
+ // object, /* Not-Dynamic */
+ // dynamic,
+ // false,
+ // null,
+ // null );
+ // }
+ //
+ // public void modifyInsert(FactHandle factHandle,
+ // Object object) {
+ // wm.modifyInsert( this.entryPoint,
+ // factHandle,
+ // object,
+ // null,
+ // null );
+ // }
+ //
+ // public void modifyRetract(FactHandle factHandle) {
+ // wm.modifyRetract( this.entryPoint,
+ // factHandle,
+ // null,
+ // null );
+ // }
+ //
+ // public void retract(FactHandle handle) throws FactException {
+ // wm.retract( this.entryPoint,
+ // handle,
+ // true,
+ // true,
+ // null,
+ // null );
+ // }
+ //
+ // public void update(FactHandle handle,
+ // Object object) throws FactException {
+ // wm.update( this.entryPoint,
+ // handle,
+ // object,
+ // null,
+ // null );
+ // }
+ //
+ // }
- private void addObjectTypeConf(EntryPoint entryPoint,
- Object key,
- ObjectTypeConf conf) {
- Map<Object, ObjectTypeConf> map = this.typeConfMap.get( entryPoint );
- if ( map == null ) {
- map = new ConcurrentHashMap<Object, ObjectTypeConf>();
- this.typeConfMap.put( entryPoint,
- map );
- }
- map.put( key,
- conf );
+ public ObjectTypeConfigurationRegistry getObjectTypeConfigurationRegistry() {
+ return this.typeConfReg;
}
- public EntryPointInterface getEntryPoint(String id) {
- EntryPoint ep = new EntryPoint( id );
- return new EntryPointInterfaceImpl( ep,
- this );
+ public TypeDeclaration getTypeDeclaration(Class<?> clazz) {
+ return null;
}
-
- public static class EntryPointInterfaceImpl
- implements
- EntryPointInterface {
-
- private static final long serialVersionUID = 2917871170743358801L;
-
- private EntryPoint entryPoint;
- private AbstractWorkingMemory wm;
-
- public EntryPointInterfaceImpl() {
-
- }
- public EntryPointInterfaceImpl(EntryPoint entryPoint,
- AbstractWorkingMemory wm) {
- this.entryPoint = entryPoint;
- this.wm = wm;
- }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
- entryPoint = (EntryPoint)in.readObject();
- wm = (AbstractWorkingMemory)in.readObject();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject(entryPoint);
- out.writeObject(wm);
- }
-
- public FactHandle insert(Object object) throws FactException {
- return wm.insert( this.entryPoint,
- object, /* Not-Dynamic */
- false,
- false,
- null,
- null );
- }
-
- public FactHandle insert(Object object,
- boolean dynamic) throws FactException {
- return wm.insert( this.entryPoint,
- object, /* Not-Dynamic */
- dynamic,
- false,
- null,
- null );
- }
-
- public void modifyInsert(FactHandle factHandle,
- Object object) {
- wm.modifyInsert( this.entryPoint,
- factHandle,
- object,
- null,
- null );
- }
-
- public void modifyRetract(FactHandle factHandle) {
- wm.modifyRetract( this.entryPoint,
- factHandle,
- null,
- null );
- }
-
- public void retract(FactHandle handle) throws FactException {
- wm.retract( this.entryPoint,
- handle,
- true,
- true,
- null,
- null );
- }
-
- public void update(FactHandle handle,
- Object object) throws FactException {
- wm.update( this.entryPoint,
- handle,
- object,
- null,
- null );
- }
-
- }
-
}
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 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalRuleBase.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -30,6 +30,7 @@
import org.drools.rule.CompositePackageClassLoader;
import org.drools.rule.MapBackedClassLoader;
import org.drools.rule.Package;
+import org.drools.rule.TypeDeclaration;
import org.drools.spi.FactHandleFactory;
import org.drools.spi.PropagationContext;
@@ -116,4 +117,12 @@
public Objenesis getObjenesis();
public int getNodeCount();
+
+ /**
+ * Returns the type declaration associated to the given class
+ *
+ * @param clazz
+ * @return
+ */
+ public TypeDeclaration getTypeDeclaration(Class<?> clazz);
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemory.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -1,8 +1,5 @@
package org.drools.common;
-import java.util.Map;
-import java.util.concurrent.locks.Lock;
-
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.WorkingMemory;
@@ -13,21 +10,21 @@
import org.drools.process.instance.ProcessInstance;
import org.drools.process.instance.ProcessInstanceFactory;
import org.drools.reteoo.LIANodePropagation;
-import org.drools.reteoo.ObjectTypeConf;
-import org.drools.rule.EntryPoint;
import org.drools.rule.Rule;
import org.drools.rule.TimeMachine;
import org.drools.spi.Activation;
import org.drools.spi.FactHandleFactory;
import org.drools.spi.PropagationContext;
+import java.util.concurrent.locks.Lock;
+
public interface InternalWorkingMemory
extends
WorkingMemory {
public long getId();
-
+
public void setId(long id);
-
+
void setRuleBase(final InternalRuleBase ruleBase);
public void setWorkingMemoryEventSupport(WorkingMemoryEventSupport workingMemoryEventSupport);
@@ -37,7 +34,7 @@
public void setAgendaEventSupport(AgendaEventSupport agendaEventSupport);
public void setRuleFlowEventSupport(RuleFlowEventSupport ruleFlowEventSupport);
-
+
public void registerProcessInstanceFactory(String type, ProcessInstanceFactory nodeInstanceFactory);
public Object getNodeMemory(NodeMemory node);
@@ -47,7 +44,7 @@
public long getNextPropagationIdCounter();
//public ObjectHashMap getFactHandleMap()
-
+
public ObjectStore getObjectStore();
public TruthMaintenanceSystem getTruthMaintenanceSystem();
@@ -57,7 +54,7 @@
public void queueWorkingMemoryAction(final WorkingMemoryAction action);
public FactHandleFactory getFactHandleFactory();
-
+
/**
* Looks for the fact handle associated to the given object
* by looking up the object IDENTITY (==), even if rule base
@@ -87,26 +84,10 @@
public TimeMachine getTimeMachine();
public void setTimeMachine(TimeMachine tm);
-
+
public void removeProcessInstance(ProcessInstance processInstance);
-
- /**
- * Returns the ObjectTypeConfiguration object for the given object
- * or creates a new one if none is found in the cache
- *
- * @param object
- * @return
- */
- public ObjectTypeConf getObjectTypeConf(EntryPoint entryPoint, Object object);
-
- /**
- * Returns the Map<Object key, ObjectTypeConf conf> of object type
- * confs in this working memory
- *
- * @return
- */
- public Map<Object, ObjectTypeConf> getObjectTypeConfMap(EntryPoint entryPoint);
+
public ExecutorService getExecutorService();
public void setExecutorService(ExecutorService executor);
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryActions.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryActions.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryActions.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -21,11 +21,10 @@
import org.drools.rule.Rule;
import org.drools.spi.Activation;
-import java.io.Externalizable;
-
public interface InternalWorkingMemoryActions
extends
- InternalWorkingMemory {
+ InternalWorkingMemory,
+ InternalWorkingMemoryEntryPoint {
public void update(FactHandle handle,
Object object,
Rule rule,
@@ -37,24 +36,13 @@
Rule rule,
Activation activation) throws FactException;
- /*FactHandle insert(Object object,
- boolean dynamic,
- boolean logical,
- Rule rule,
- Activation activation) throws FactException;*/
-
FactHandle insert(Object object,
- long duration,
boolean dynamic,
boolean logical,
Rule rule,
Activation activation) throws FactException;
- /*public FactHandle insertLogical(Object object,
- boolean dynamic) throws FactException;*/
-
public FactHandle insertLogical(Object object,
- long duration,
boolean dynamic) throws FactException;
public void modifyRetract(final FactHandle factHandle,
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryEntryPoint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryEntryPoint.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/InternalWorkingMemoryEntryPoint.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,7 @@
+package org.drools.common;
+
+import org.drools.WorkingMemoryEntryPoint;
+
+public interface InternalWorkingMemoryEntryPoint extends WorkingMemoryEntryPoint {
+ ObjectTypeConfigurationRegistry getObjectTypeConfigurationRegistry();
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/NamedEntryPoint.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/NamedEntryPoint.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/NamedEntryPoint.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,495 @@
+/**
+ *
+ */
+package org.drools.common;
+
+import org.drools.FactException;
+import org.drools.FactHandle;
+import org.drools.RuleBaseConfiguration.AssertBehaviour;
+import org.drools.RuntimeDroolsException;
+import org.drools.WorkingMemory;
+import org.drools.WorkingMemoryEntryPoint;
+import org.drools.base.ShadowProxy;
+import org.drools.reteoo.EntryPointNode;
+import org.drools.reteoo.ObjectTypeConf;
+import org.drools.rule.EntryPoint;
+import org.drools.rule.Rule;
+import org.drools.spi.Activation;
+import org.drools.spi.FactHandleFactory;
+import org.drools.spi.PropagationContext;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.concurrent.locks.ReentrantLock;
+
+public class NamedEntryPoint
+ implements
+ InternalWorkingMemoryEntryPoint {
+ /** The arguments used when adding/removing a property change listener. */
+ protected final Object[] addRemovePropertyChangeListenerArgs = new Object[]{this};
+
+ private static final long serialVersionUID = 500;
+
+ protected ObjectStore objectStore;
+
+ protected transient InternalRuleBase ruleBase;
+
+ protected EntryPoint entryPoint;
+ protected EntryPointNode entryPointNode;
+
+ private ObjectTypeConfigurationRegistry typeConfReg;
+
+ private AbstractWorkingMemory wm;
+
+ private FactHandleFactory handleFactory;
+
+ protected ReentrantLock lock;
+
+ public NamedEntryPoint() {
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ objectStore = (ObjectStore)in.readObject();
+ entryPoint = (EntryPoint)in.readObject();
+ entryPointNode = (EntryPointNode)in.readObject();
+ typeConfReg = (ObjectTypeConfigurationRegistry)in.readObject();
+ wm = (AbstractWorkingMemory)in.readObject();
+ handleFactory = (FactHandleFactory)in.readObject();
+ lock = (ReentrantLock)in.readObject();
+
+ ruleBase = (InternalRuleBase)wm.getRuleBase();
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeObject(objectStore);
+ out.writeObject(entryPoint);
+ out.writeObject(entryPointNode);
+ out.writeObject(typeConfReg);
+ out.writeObject(wm);
+ out.writeObject(handleFactory);
+ out.writeObject(lock);
+
+ }
+
+ public NamedEntryPoint(EntryPoint entryPoint,
+ EntryPointNode entryPointNode,
+ AbstractWorkingMemory wm) {
+ this( entryPoint,
+ entryPointNode,
+ wm,
+ new ReentrantLock() );
+ }
+
+ public NamedEntryPoint(EntryPoint entryPoint,
+ EntryPointNode entryPointNode,
+ AbstractWorkingMemory wm,
+ ReentrantLock lock) {
+ this.entryPoint = entryPoint;
+ this.entryPointNode = entryPointNode;
+ this.wm = wm;
+ this.ruleBase = (InternalRuleBase) this.wm.getRuleBase();
+ this.lock = lock;
+ this.typeConfReg = new ObjectTypeConfigurationRegistry( this.ruleBase );
+ this.handleFactory = this.wm.getFactHandleFactory();
+ this.objectStore = new SingleThreadedObjectStore( this.ruleBase.getConfiguration(),
+ this.lock );
+ }
+
+ /**
+ * @see WorkingMemory
+ */
+ public FactHandle insert(final Object object) throws FactException {
+ return insert( object, /* Not-Dynamic */
+ false,
+ false,
+ null,
+ null );
+ }
+
+ public FactHandle insert(final Object object,
+ final boolean dynamic) throws FactException {
+ return insert( object,
+ dynamic,
+ false,
+ null,
+ null );
+ }
+
+ protected FactHandle insert(final Object object,
+ final boolean dynamic,
+ boolean logical,
+ final Rule rule,
+ final Activation activation) throws FactException {
+ if ( object == null ) {
+ // you cannot assert a null object
+ return null;
+ }
+
+ ObjectTypeConf typeConf = this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object );
+
+ InternalFactHandle handle = this.handleFactory.newFactHandle( object,
+ typeConf.isEvent(),
+ wm );
+ this.objectStore.addHandle( handle,
+ object );
+
+ if ( dynamic ) {
+ addPropertyChangeListener( object );
+ }
+
+ try {
+ this.lock.lock();
+ insert( handle,
+ object,
+ rule,
+ activation );
+
+ } finally {
+ this.lock.unlock();
+ }
+ return handle;
+ }
+
+ protected void insert(final InternalFactHandle handle,
+ final Object object,
+ final Rule rule,
+ final Activation activation) {
+ this.ruleBase.executeQueuedActions();
+
+ if ( activation != null ) {
+ // release resources so that they can be GC'ed
+ activation.getPropagationContext().releaseResources();
+ }
+ final PropagationContext propagationContext = new PropagationContextImpl( this.wm.getNextPropagationIdCounter(),
+ PropagationContext.ASSERTION,
+ rule,
+ activation,
+ -1,
+ -1,
+ this.entryPoint );
+
+ this.entryPointNode.assertObject( handle,
+ propagationContext,
+ this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object ),
+ this.wm );
+
+ this.wm.executeQueuedActions();
+
+ this.wm.getWorkingMemoryEventSupport().fireObjectInserted( propagationContext,
+ handle,
+ object,
+ wm );
+ }
+
+ public void update(final FactHandle handle,
+ final Object object) throws FactException {
+ update( handle,
+ object,
+ null,
+ null );
+ }
+
+ protected void update(final FactHandle factHandle,
+ final Object object,
+ final Rule rule,
+ final Activation activation) throws FactException {
+ try {
+ this.lock.lock();
+ this.ruleBase.executeQueuedActions();
+
+ final InternalFactHandle handle = (InternalFactHandle) factHandle;
+ final Object originalObject = (handle.isShadowFact()) ? ((ShadowProxy) handle.getObject()).getShadowedObject() : handle.getObject();
+
+ if ( handle.getId() == -1 || object == null ) {
+ // the handle is invalid, most likely already retracted, so
+ // return
+ // and we cannot assert a null object
+ return;
+ }
+
+ ObjectTypeConf typeConf = this.typeConfReg.getObjectTypeConf( this.entryPoint,
+ object );
+
+ if ( activation != null ) {
+ // release resources so that they can be GC'ed
+ activation.getPropagationContext().releaseResources();
+ }
+ // Nowretract any trace of the original fact
+ final PropagationContext propagationContext = new PropagationContextImpl( this.wm.getNextPropagationIdCounter(),
+ PropagationContext.MODIFICATION,
+ rule,
+ activation,
+ -1,
+ -1,
+ this.entryPoint );
+
+ this.entryPointNode.retractObject( handle,
+ propagationContext,
+ typeConf,
+ this.wm );
+
+ if ( (originalObject != object) || (this.ruleBase.getConfiguration().getAssertBehaviour() != AssertBehaviour.IDENTITY) ) {
+ this.objectStore.removeHandle( handle );
+
+ // set anyway, so that it updates the hashCodes
+ handle.setObject( object );
+ this.objectStore.addHandle( handle,
+ object );
+ }
+
+ this.handleFactory.increaseFactHandleRecency( handle );
+
+ this.entryPointNode.assertObject( handle,
+ propagationContext,
+ typeConf,
+ this.wm );
+
+ this.wm.getWorkingMemoryEventSupport().fireObjectUpdated( propagationContext,
+ factHandle,
+ originalObject,
+ object,
+ this.wm );
+
+ propagationContext.clearRetractedTuples();
+
+ this.wm.executeQueuedActions();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void retract(final FactHandle handle) throws FactException {
+ retract( handle,
+ true,
+ true,
+ null,
+ null );
+ }
+
+ public void retract(final FactHandle factHandle,
+ final boolean removeLogical,
+ final boolean updateEqualsMap,
+ final Rule rule,
+ final Activation activation) throws FactException {
+ try {
+ this.lock.lock();
+ this.ruleBase.executeQueuedActions();
+
+ final InternalFactHandle handle = (InternalFactHandle) factHandle;
+ if ( handle.getId() == -1 ) {
+ // can't retract an already retracted handle
+ return;
+ }
+ removePropertyChangeListener( handle );
+
+ if ( activation != null ) {
+ // release resources so that they can be GC'ed
+ activation.getPropagationContext().releaseResources();
+ }
+ final PropagationContext propagationContext = new PropagationContextImpl( this.wm.getNextPropagationIdCounter(),
+ PropagationContext.RETRACTION,
+ rule,
+ activation,
+ -1,
+ -1,
+ this.entryPoint );
+
+ this.entryPointNode.retractObject( handle,
+ propagationContext,
+ this.wm );
+
+ final Object object = handle.getObject();
+
+ this.wm.getWorkingMemoryEventSupport().fireObjectRetracted( propagationContext,
+ handle,
+ object,
+ this.wm );
+
+ this.objectStore.removeHandle( handle );
+
+ this.handleFactory.destroyFactHandle( handle );
+
+ this.wm.executeQueuedActions();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void modifyRetract(final FactHandle factHandle) {
+ modifyRetract( factHandle,
+ null,
+ null );
+ }
+
+ public void modifyRetract(final FactHandle factHandle,
+ final Rule rule,
+ final Activation activation) {
+ try {
+ this.lock.lock();
+ this.ruleBase.executeQueuedActions();
+
+ final InternalFactHandle handle = (InternalFactHandle) factHandle;
+ // final Object originalObject = (handle.isShadowFact()) ?
+ // ((ShadowProxy) handle.getObject()).getShadowedObject() :
+ // handle.getObject();
+
+ if ( handle.getId() == -1 ) {
+ // the handle is invalid, most likely already retracted, so
+ // return
+ return;
+ }
+
+ if ( activation != null ) {
+ // release resources so that they can be GC'ed
+ activation.getPropagationContext().releaseResources();
+ }
+ // Nowretract any trace of the original fact
+ final PropagationContext propagationContext = new PropagationContextImpl( this.wm.getNextPropagationIdCounter(),
+ PropagationContext.MODIFICATION,
+ rule,
+ activation,
+ -1,
+ -1,
+ entryPoint );
+
+ this.entryPointNode.retractObject( handle,
+ propagationContext,
+ this.wm );
+
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void modifyInsert(final FactHandle factHandle,
+ final Object object) {
+ modifyInsert( factHandle,
+ object,
+ null,
+ null );
+ }
+
+ public void modifyInsert(final FactHandle factHandle,
+ final Object object,
+ final Rule rule,
+ final Activation activation) {
+ this.modifyInsert( EntryPoint.DEFAULT,
+ factHandle,
+ object,
+ rule,
+ activation );
+ }
+
+ protected void modifyInsert(final EntryPoint entryPoint,
+ final FactHandle factHandle,
+ final Object object,
+ final Rule rule,
+ final Activation activation) {
+ try {
+ this.lock.lock();
+ this.ruleBase.executeQueuedActions();
+
+ final InternalFactHandle handle = (InternalFactHandle) factHandle;
+ final Object originalObject = (handle.isShadowFact()) ? ((ShadowProxy) handle.getObject()).getShadowedObject() : handle.getObject();
+
+ this.handleFactory.increaseFactHandleRecency( handle );
+
+ if ( activation != null ) {
+ // release resources so that they can be GC'ed
+ activation.getPropagationContext().releaseResources();
+ }
+ // Nowretract any trace of the original fact
+ final PropagationContext propagationContext = new PropagationContextImpl( this.wm.getNextPropagationIdCounter(),
+ PropagationContext.MODIFICATION,
+ rule,
+ activation,
+ -1,
+ -1,
+ entryPoint );
+
+ this.entryPointNode.assertObject( handle,
+ propagationContext,
+ this.wm );
+
+ this.wm.getWorkingMemoryEventSupport().fireObjectUpdated( propagationContext,
+ factHandle,
+ originalObject,
+ object,
+ this.wm );
+
+ propagationContext.clearRetractedTuples();
+
+ this.wm.executeQueuedActions();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ protected void addPropertyChangeListener(final Object object) {
+ try {
+ final Method method = object.getClass().getMethod( "addPropertyChangeListener",
+ AbstractWorkingMemory.ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES );
+
+ method.invoke( object,
+ this.addRemovePropertyChangeListenerArgs );
+ } catch ( final NoSuchMethodException e ) {
+ System.err.println( "Warning: Method addPropertyChangeListener not found" + " on the class " + object.getClass() + " so Drools will be unable to process JavaBean" + " PropertyChangeEvents on the asserted Object" );
+ } catch ( final IllegalArgumentException e ) {
+ System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass() + " does not take" + " a simple PropertyChangeListener argument" + " so Drools will be unable to process JavaBean"
+ + " PropertyChangeEvents on the asserted Object" );
+ } catch ( final IllegalAccessException e ) {
+ System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass() + " is not public" + " so Drools will be unable to process JavaBean" + " PropertyChangeEvents on the asserted Object" );
+ } catch ( final InvocationTargetException e ) {
+ System.err.println( "Warning: The addPropertyChangeListener method" + " on the class " + object.getClass() + " threw an InvocationTargetException" + " so Drools will be unable to process JavaBean"
+ + " PropertyChangeEvents on the asserted Object: " + e.getMessage() );
+ } catch ( final SecurityException e ) {
+ System.err.println( "Warning: The SecurityManager controlling the class " + object.getClass() + " did not allow the lookup of a" + " addPropertyChangeListener method" + " so Drools will be unable to process JavaBean"
+ + " PropertyChangeEvents on the asserted Object: " + e.getMessage() );
+ }
+ }
+
+ protected void removePropertyChangeListener(final FactHandle handle) {
+ Object object = null;
+ try {
+ object = ((InternalFactHandle) handle).getObject();
+
+ if ( object != null ) {
+ final Method mehod = object.getClass().getMethod( "removePropertyChangeListener",
+ AbstractWorkingMemory.ADD_REMOVE_PROPERTY_CHANGE_LISTENER_ARG_TYPES );
+
+ mehod.invoke( object,
+ this.addRemovePropertyChangeListenerArgs );
+ }
+ } catch ( final NoSuchMethodException e ) {
+ // The removePropertyChangeListener method on the class
+ // was not found so Drools will be unable to
+ // stop processing JavaBean PropertyChangeEvents
+ // on the retracted Object
+ } catch ( final IllegalArgumentException e ) {
+ throw new RuntimeDroolsException( "Warning: The removePropertyChangeListener method on the class " + object.getClass() + " does not take a simple PropertyChangeListener argument so Drools will be unable to stop processing JavaBean"
+ + " PropertyChangeEvents on the retracted Object" );
+ } catch ( final IllegalAccessException e ) {
+ throw new RuntimeDroolsException( "Warning: The removePropertyChangeListener method on the class " + object.getClass() + " is not public so Drools will be unable to stop processing JavaBean PropertyChangeEvents on the retracted Object" );
+ } catch ( final InvocationTargetException e ) {
+ throw new RuntimeDroolsException( "Warning: The removePropertyChangeL istener method on the class " + object.getClass() + " threw an InvocationTargetException so Drools will be unable to stop processing JavaBean"
+ + " PropertyChangeEvents on the retracted Object: " + e.getMessage() );
+ } catch ( final SecurityException e ) {
+ throw new RuntimeDroolsException( "Warning: The SecurityManager controlling the class " + object.getClass() + " did not allow the lookup of a removePropertyChangeListener method so Drools will be unable to stop processing JavaBean"
+ + " PropertyChangeEvents on the retracted Object: " + e.getMessage() );
+ }
+ }
+
+ public WorkingMemoryEntryPoint getWorkingMemoryEntryPoint(String name) {
+ return this.wm.getWorkingMemoryEntryPoint( name );
+ }
+
+ public ObjectTypeConfigurationRegistry getObjectTypeConfigurationRegistry() {
+ return this.typeConfReg;
+ }
+
+}
\ No newline at end of file
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ObjectTypeConfigurationRegistry.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ObjectTypeConfigurationRegistry.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/ObjectTypeConfigurationRegistry.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,76 @@
+package org.drools.common;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.drools.base.ShadowProxy;
+import org.drools.facttemplates.Fact;
+import org.drools.reteoo.ClassObjectTypeConf;
+import org.drools.reteoo.FactTemplateTypeConf;
+import org.drools.reteoo.ObjectTypeConf;
+import org.drools.rule.EntryPoint;
+
+public class ObjectTypeConfigurationRegistry implements Serializable {
+ private InternalRuleBase ruleBase;
+ private Map<Object, ObjectTypeConf> typeConfMap;
+
+
+
+ public ObjectTypeConfigurationRegistry(InternalRuleBase ruleBase ) {
+ super();
+ this.ruleBase = ruleBase;
+ this.typeConfMap = new HashMap<Object, ObjectTypeConf>();
+ }
+
+
+
+ /**
+ * Returns the ObjectTypeConfiguration object for the given object or
+ * creates a new one if none is found in the cache
+ *
+ * @param object
+ * @return
+ */
+ public ObjectTypeConf getObjectTypeConf(EntryPoint entrypoint,
+ Object object) {
+
+ // first see if it's a ClassObjectTypeConf
+ Class cls = null;
+ if ( object instanceof ShadowProxy ) {
+ cls = ((ShadowProxy) object).getShadowedObject().getClass();
+ } else {
+ cls = object.getClass();
+ }
+ ObjectTypeConf objectTypeConf = this.typeConfMap.get( cls );
+
+ // Now see if it's something else
+ if ( objectTypeConf == null ) {
+ objectTypeConf = this.typeConfMap.get( object );
+ }
+
+
+ // it doesn't exist, so create it.
+ if ( objectTypeConf == null ) {
+ if ( object instanceof Fact ) {
+ objectTypeConf = new FactTemplateTypeConf( entrypoint,
+ ((Fact) object).getFactTemplate(),
+ this.ruleBase );
+ this.typeConfMap.put( object, objectTypeConf );
+ } else {
+ objectTypeConf = new ClassObjectTypeConf( entrypoint,
+ cls,
+ this.ruleBase );
+ this.typeConfMap.put( cls, objectTypeConf );
+ }
+ }
+
+ return objectTypeConf;
+ }
+
+
+ public Collection<ObjectTypeConf> values() {
+ return this.typeConfMap.values();
+ }
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedTemporalWorkingMemoryContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedTemporalWorkingMemoryContext.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedTemporalWorkingMemoryContext.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,33 @@
+package org.drools.common;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.base.MapGlobalResolver;
+import org.drools.event.AgendaEventSupport;
+import org.drools.event.RuleFlowEventSupport;
+import org.drools.event.WorkingMemoryEventSupport;
+import org.drools.process.instance.ProcessInstanceFactory;
+import org.drools.process.instance.WorkItemManager;
+import org.drools.rule.TimeMachine;
+import org.drools.spi.FactHandleFactory;
+import org.drools.spi.GlobalResolver;
+import org.drools.temporal.SessionClock;
+
+public class SharedTemporalWorkingMemoryContext<T extends SessionClock> extends SharedWorkingMemoryContext {
+ protected T sessionClock;
+
+ public SharedTemporalWorkingMemoryContext(FactHandleFactory handleFactory, T sessionClock) {
+ super( handleFactory );
+ this.sessionClock = sessionClock;
+ }
+
+ public T getSessionClock() {
+ return sessionClock;
+ }
+
+
+
+}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedWorkingMemoryContext.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedWorkingMemoryContext.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/common/SharedWorkingMemoryContext.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,119 @@
+package org.drools.common;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.drools.base.MapGlobalResolver;
+import org.drools.event.AgendaEventSupport;
+import org.drools.event.RuleFlowEventSupport;
+import org.drools.event.WorkingMemoryEventSupport;
+import org.drools.process.instance.ProcessInstanceFactory;
+import org.drools.process.instance.WorkItemManager;
+import org.drools.rule.TimeMachine;
+import org.drools.spi.FactHandleFactory;
+import org.drools.spi.GlobalResolver;
+import org.drools.temporal.SessionClock;
+
+public class SharedWorkingMemoryContext {
+ protected InternalRuleBase ruleBase;
+
+ protected FactHandleFactory handleFactory;
+
+ /** Global values which are associated with this memory. */
+ protected GlobalResolver globalResolver;
+
+ /** The eventSupport */
+ protected WorkingMemoryEventSupport workingMemoryEventSupport;
+
+ protected AgendaEventSupport agendaEventSupport;
+
+ protected RuleFlowEventSupport workflowEventSupport;
+
+ protected List __ruleBaseEventListeners;
+
+ protected long propagationIdCounter;
+
+ private Map processInstances;
+
+ private int processCounter;
+
+ private WorkItemManager workItemManager;
+
+ private Map<String, ProcessInstanceFactory> processInstanceFactories;
+
+ private TimeMachine timeMachine;
+
+ public SharedWorkingMemoryContext(FactHandleFactory handleFactory) {
+ this.handleFactory = handleFactory;
+
+ this.globalResolver = new MapGlobalResolver();
+
+ this.workingMemoryEventSupport = new WorkingMemoryEventSupport();
+ this.agendaEventSupport = new AgendaEventSupport();
+ this.workflowEventSupport = new RuleFlowEventSupport();
+ this.__ruleBaseEventListeners = new LinkedList();
+
+ processInstanceFactories = new HashMap();
+
+ timeMachine = new TimeMachine();
+ }
+
+ public Map getProcessInstances() {
+ return processInstances;
+ }
+
+ public void setProcessInstances(Map processInstances) {
+ this.processInstances = processInstances;
+ }
+
+ public WorkItemManager getWorkItemManager() {
+ return workItemManager;
+ }
+
+ public void setWorkItemManager(WorkItemManager workItemManager) {
+ this.workItemManager = workItemManager;
+ }
+
+ public TimeMachine getTimeMachine() {
+ return timeMachine;
+ }
+
+ public void setTimeMachine(TimeMachine timeMachine) {
+ this.timeMachine = timeMachine;
+ }
+
+ public FactHandleFactory getHandleFactory() {
+ return handleFactory;
+ }
+
+ public GlobalResolver getGlobalResolver() {
+ return globalResolver;
+ }
+
+ public WorkingMemoryEventSupport getWorkingMemoryEventSupport() {
+ return workingMemoryEventSupport;
+ }
+
+ public AgendaEventSupport getAgendaEventSupport() {
+ return agendaEventSupport;
+ }
+
+ public RuleFlowEventSupport getWorkflowEventSupport() {
+ return workflowEventSupport;
+ }
+
+ public List get__ruleBaseEventListeners() {
+ return __ruleBaseEventListeners;
+ }
+
+ public int getProcessCounter() {
+ return processCounter;
+ }
+
+ public Map<String, ProcessInstanceFactory> getProcessInstanceFactories() {
+ return processInstanceFactories;
+ }
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ClassObjectTypeConf.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ClassObjectTypeConf.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ClassObjectTypeConf.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -17,43 +17,45 @@
*/
package org.drools.reteoo;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.Serializable;
-import java.io.ObjectInput;
-import java.io.Externalizable;
-import java.io.ObjectOutput;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
import org.drools.FactException;
import org.drools.RuntimeDroolsException;
import org.drools.base.ClassObjectType;
import org.drools.base.DroolsQuery;
import org.drools.base.ShadowProxy;
import org.drools.base.ShadowProxyFactory;
-import org.drools.common.DroolsObjectInputStream;
+import org.drools.common.DroolsObjectInput;
import org.drools.common.InternalRuleBase;
-import org.drools.common.DroolsObjectInput;
import org.drools.objenesis.instantiator.ObjectInstantiator;
import org.drools.reteoo.builder.BuildContext;
import org.drools.reteoo.builder.PatternBuilder;
import org.drools.rule.EntryPoint;
+import org.drools.rule.TypeDeclaration;
import org.drools.spi.ObjectType;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutput;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
public class ClassObjectTypeConf
implements
ObjectTypeConf,
Externalizable {
- private Class cls;
+ private static final long serialVersionUID = 8218802585428841926L;
+
+ private Class<?> cls;
private transient InternalRuleBase ruleBase;
private ObjectTypeNode[] objectTypeNodes;
protected boolean shadowEnabled;
- protected Class shadowClass;
+ protected Class<ShadowProxy> shadowClass;
protected transient ObjectInstantiator instantiator;
private ObjectTypeNode concreteObjectTypeNode;
@@ -64,12 +66,13 @@
}
public ClassObjectTypeConf(final EntryPoint entryPoint,
- final Class clazz,
- final boolean isEvent,
+ final Class<?> clazz,
final InternalRuleBase ruleBase) {
this.cls = clazz;
this.ruleBase = ruleBase;
this.entryPoint = entryPoint;
+ TypeDeclaration type = ruleBase.getTypeDeclaration( clazz );
+ final boolean isEvent = type != null && type.getRole() == TypeDeclaration.Role.EVENT;
ObjectType objectType = new ClassObjectType( clazz,
isEvent );
@@ -177,7 +180,7 @@
* This will return the package name - if the package is null, it will
* work it out from the class name (this is in cases where funky classloading is used).
*/
- public static String getPackageName(Class clazz,
+ public static String getPackageName(Class<?> clazz,
Package pkg) {
String pkgName = "";
if ( pkg == null ) {
@@ -249,6 +252,12 @@
return ret;
}
+ private void readObject(ObjectInputStream stream) throws IOException,
+ ClassNotFoundException {
+ stream.defaultReadObject();
+ this.ruleBase = ((DroolsObjectInput) stream).getRuleBase();
+ }
+
/**
*
*/
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EntryPointNode.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EntryPointNode.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/EntryPointNode.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -18,17 +18,11 @@
* Created on January 8th, 2007
*/
-import java.io.Externalizable;
-import java.io.ObjectInput;
-import java.io.IOException;
-import java.io.ObjectOutput;
-import java.util.HashMap;
-import java.util.Map;
-
import org.drools.base.ShadowProxy;
import org.drools.common.BaseNode;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalWorkingMemory;
+import org.drools.common.InternalWorkingMemoryEntryPoint;
import org.drools.common.NodeMemory;
import org.drools.common.PropagationContextImpl;
import org.drools.reteoo.builder.BuildContext;
@@ -39,6 +33,13 @@
import org.drools.util.FactHashTable;
import org.drools.util.Iterator;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.HashMap;
+import java.util.Map;
+
/**
* A node that is an entry point into the Rete network.
*
@@ -119,25 +120,10 @@
return entryPoint;
}
- /**
- * This is the entry point into the network for all asserted Facts. Iterates a cache
- * of matching <code>ObjectTypdeNode</code>s asserting the Fact. If the cache does not
- * exist it first iterates and builds the cache.
- *
- * @param handle
- * The FactHandle of the fact to assert
- * @param context
- * The <code>PropagationContext</code> of the <code>WorkingMemory</code> action
- * @param workingMemory
- * The working memory session.
- */
public void assertObject(final InternalFactHandle handle,
final PropagationContext context,
+ final ObjectTypeConf objectTypeConf,
final InternalWorkingMemory workingMemory) {
-
- ObjectTypeConf objectTypeConf = workingMemory.getObjectTypeConf( this.entryPoint,
- handle.getObject() );
-
// checks if shadow is enabled
if ( objectTypeConf.isShadowEnabled() ) {
// need to improve this
@@ -160,6 +146,24 @@
}
/**
+ * This is the entry point into the network for all asserted Facts. Iterates a cache
+ * of matching <code>ObjectTypdeNode</code>s asserting the Fact. If the cache does not
+ * exist it first iterates and builds the cache.
+ *
+ * @param handle
+ * The FactHandle of the fact to assert
+ * @param context
+ * The <code>PropagationContext</code> of the <code>WorkingMemory</code> action
+ * @param workingMemory
+ * The working memory session.
+ */
+ public void assertObject(final InternalFactHandle handle,
+ final PropagationContext context,
+ final InternalWorkingMemory workingMemory) {
+ // do nothing, dummy method to impl the interface
+ }
+
+ /**
* Retract a fact object from this <code>RuleBase</code> and the specified
* <code>WorkingMemory</code>.
*
@@ -170,11 +174,10 @@
*/
public void retractObject(final InternalFactHandle handle,
final PropagationContext context,
+ final ObjectTypeConf objectTypeConf,
final InternalWorkingMemory workingMemory) {
final Object object = handle.getObject();
- ObjectTypeConf objectTypeConf = workingMemory.getObjectTypeConf( this.entryPoint,
- object );
ObjectTypeNode[] cachedNodes = objectTypeConf.getObjectTypeNodes();
if ( cachedNodes == null ) {
@@ -189,6 +192,12 @@
}
}
+ public void retractObject(final InternalFactHandle handle,
+ final PropagationContext context,
+ final InternalWorkingMemory workingMemory) {
+ // do nothing, dummy method to impl the interface
+ }
+
/**
* Adds the <code>ObjectSink</code> so that it may receive
* <code>Objects</code> propagated from this <code>ObjectSource</code>.
@@ -268,7 +277,9 @@
final ObjectTypeNode node = (ObjectTypeNode) sink;
final ObjectType newObjectType = node.getObjectType();
- for ( ObjectTypeConf objectTypeConf : workingMemory.getObjectTypeConfMap( this.entryPoint ).values() ) {
+ InternalWorkingMemoryEntryPoint wmEntryPoint = ( InternalWorkingMemoryEntryPoint ) workingMemory.getWorkingMemoryEntryPoint( this.entryPoint.getEntryPointId() );
+
+ for ( ObjectTypeConf objectTypeConf : wmEntryPoint.getObjectTypeConfigurationRegistry().values() ) {
if ( newObjectType.isAssignableFrom( objectTypeConf.getConcreteObjectTypeNode().getObjectType() ) ) {
objectTypeConf.resetCache();
ObjectTypeNode sourceNode = objectTypeConf.getConcreteObjectTypeNode();
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 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooRuleBase.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
-import java.io.Serializable;
import java.io.Externalizable;
import java.util.Iterator;
@@ -83,7 +82,7 @@
/**
* Construct.
*
- * @param id
+ * @param rete
* The rete network.
*/
public ReteooRuleBase(final String id) {
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooStatefulSession.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -5,6 +5,7 @@
import java.util.Iterator;
import java.util.List;
+import org.drools.WorkingMemoryEntryPoint;
import org.drools.FactHandle;
import org.drools.StatefulSession;
import org.drools.common.InternalRuleBase;
@@ -16,6 +17,7 @@
import org.drools.concurrent.RetractObject;
import org.drools.concurrent.UpdateObject;
import org.drools.event.RuleBaseEventListener;
+import org.drools.rule.EntryPoint;
import org.drools.spi.AgendaFilter;
import org.drools.spi.RuleBaseUpdateListener;
import org.drools.spi.RuleBaseUpdateListenerFactory;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/reteoo/ReteooWorkingMemory.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -33,6 +33,7 @@
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
+import org.drools.common.ObjectTypeConfigurationRegistry;
import org.drools.common.PropagationContextImpl;
import org.drools.common.WorkingMemoryAction;
import org.drools.event.RuleBaseEventListener;
@@ -49,7 +50,7 @@
* @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
* @author <a href="mailto:simon at redhillconsulting.com.au">Simon Harris</a>
*/
-public class ReteooWorkingMemory extends AbstractWorkingMemory implements Externalizable {
+public class ReteooWorkingMemory extends AbstractWorkingMemory {
/**
*
@@ -67,29 +68,13 @@
* The backing rule-base.
*/
public ReteooWorkingMemory(final int id,
- final InternalRuleBase ruleBase) {
+ final InternalRuleBase ruleBase ) {
super( id,
ruleBase,
ruleBase.newFactHandleFactory() );
this.agenda = new DefaultAgenda( this );
}
- public void doInsert(final InternalFactHandle handle,
- final Object object,
- final PropagationContext propagationContext) throws FactException {
- this.ruleBase.assertObject( handle,
- object,
- propagationContext,
- this );
- }
-
- public void doRetract(final InternalFactHandle handle,
- final PropagationContext propagationContext) {
- this.ruleBase.retractObject( handle,
- propagationContext,
- this );
- }
-
public QueryResults getQueryResults(final String query) {
return getQueryResults( query, null );
}
@@ -97,13 +82,13 @@
public QueryResults getQueryResults(final String query, final Object[] arguments) {
Object object = new DroolsQuery( query, arguments );
- InternalFactHandle handle = this.handleFactory.newFactHandle( object, false, 0, this );
+ InternalFactHandle handle = this.handleFactory.newFactHandle( object, false, this );
- insert( EntryPoint.DEFAULT, // query dummy objects always use default entry point
- handle,
+ insert( handle,
object,
null,
- null );
+ null,
+ this.typeConfReg.getObjectTypeConf( this.entryPoint, object ));
final QueryTerminalNode node = (QueryTerminalNode) this.queryResults.remove( query );
Query queryObj = null;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/Package.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -16,12 +16,18 @@
* limitations under the License.
*/
+import org.drools.common.DroolsObjectInput;
+import org.drools.common.DroolsObjectInputStream;
+import org.drools.common.DroolsObjectOutputStream;
+import org.drools.facttemplates.FactTemplate;
+import org.drools.process.core.Process;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
-import java.io.ByteArrayOutputStream;
-import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
@@ -29,12 +35,6 @@
import java.util.Map;
import java.util.Set;
-import org.drools.common.DroolsObjectInputStream;
-import org.drools.common.DroolsObjectOutputStream;
-import org.drools.common.DroolsObjectInput;
-import org.drools.facttemplates.FactTemplate;
-import org.drools.process.core.Process;
-
/**
* Collection of related <code>Rule</code>s.
*
@@ -81,6 +81,8 @@
// private JavaDialectData packageCompilationData;
private DialectDatas dialectDatas;
+ private Map<String, TypeDeclaration> typeDeclarations;
+
/** This is to indicate the the package has no errors during the compilation/building phase */
private boolean valid = true;
@@ -120,6 +122,7 @@
ClassLoader parentClassLoader) {
this.name = name;
this.imports = new HashMap<String, ImportDeclaration>();
+ this.typeDeclarations = new HashMap<String, TypeDeclaration>();
this.staticImports = Collections.EMPTY_SET;
this.rules = new LinkedHashMap();
this.ruleFlows = Collections.EMPTY_MAP;
@@ -157,6 +160,7 @@
out = new DroolsObjectOutputStream(bytes);
}
out.writeObject( this.dialectDatas );
+ out.writeObject( this.typeDeclarations );
out.writeObject( this.name );
out.writeObject( this.imports );
out.writeObject( this.staticImports );
@@ -194,6 +198,7 @@
if (!isDroolsStream)
((DroolsObjectInput)in).setClassLoader(this.dialectDatas.getClassLoader());
+ this.typeDeclarations = (Map)in.readObject();
this.name = (String) in.readObject();
this.imports = (Map<String, ImportDeclaration>) in.readObject();
this.staticImports = (Set) in.readObject();
@@ -239,6 +244,22 @@
return this.imports;
}
+ public void addTypeDeclaration( final TypeDeclaration typeDecl ) {
+ this.typeDeclarations.put( typeDecl.getTypeName(), typeDecl );
+ }
+
+ public void removeTypeDeclaration( final String type ) {
+ this.typeDeclarations.remove( type );
+ }
+
+ public Map<String, TypeDeclaration> getTypeDeclarations() {
+ return this.typeDeclarations;
+ }
+
+ public TypeDeclaration getTypeDeclaration( String type ) {
+ return this.typeDeclarations.get( type );
+ }
+
public void addStaticImport(final String functionImport) {
if ( this.staticImports == Collections.EMPTY_SET ) {
this.staticImports = new HashSet( 2 );
@@ -478,29 +499,20 @@
/**
* Returns true if clazz is imported as an Event class in this package
* @param clazz
- * @return true if clazz is imported as an Event class in this package
+ * @return
*/
public boolean isEvent(Class clazz) {
if ( clazz == null ) {
return false;
}
- // check if clazz is resolved by any of the import declarations
- for ( ImportDeclaration imp : this.imports.values() ) {
- if ( imp.isEvent() && imp.matches( clazz ) ) {
+
+ // check if clazz is resolved by any of the type declarations
+ for( TypeDeclaration type : this.typeDeclarations.values() ) {
+ if( type.matches( clazz ) && type.getRole() == TypeDeclaration.Role.EVENT ) {
return true;
}
}
- // if it is not resolved, try superclass
- if ( this.isEvent( clazz.getSuperclass() ) ) {
- return true;
- }
- // if it is no resolved, try interfaces
- for ( Class interf : clazz.getInterfaces() ) {
- if ( this.isEvent( interf ) ) {
- return true;
- }
- }
return false;
}
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/rule/TypeDeclaration.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,224 @@
+/*
+ * Copyright 2008 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.
+ *
+ * Created on Jan 23, 2008
+ */
+
+package org.drools.rule;
+
+import org.drools.facttemplates.FactTemplate;
+
+/**
+ * The type declaration class stores all type's metadata
+ * declared in source files.
+ *
+ * @author etirelli
+ */
+public class TypeDeclaration {
+
+ public static enum Role {
+ FACT,
+ EVENT;
+
+ public static Role parseRole( String role ) {
+ if( "event".equalsIgnoreCase( role ) ) {
+ return EVENT;
+ } else if( "fact".equalsIgnoreCase( role ) ) {
+ return FACT;
+ }
+ return null;
+ }
+ }
+
+ public static enum Format {
+ POJO,
+ TEMPLATE;
+
+ public static Format parseFormat( String format ) {
+ if( "pojo".equalsIgnoreCase( format ) ) {
+ return POJO;
+ } else if( "template".equalsIgnoreCase( format ) ) {
+ return TEMPLATE;
+ }
+ return null;
+ }
+ }
+
+ public static enum ClockStrategy {
+ NONE,
+ PSEUDO,
+ SYSTEM,
+ HEARTBEAT,
+ ATTRIBUTE;
+
+ public static ClockStrategy parseClockStrategy( String clockStrategy ) {
+ if( "none".equalsIgnoreCase( clockStrategy ) ) {
+ return NONE;
+ } else if( "pseudo".equalsIgnoreCase( clockStrategy ) ) {
+ return PSEUDO;
+ } else if( "system".equalsIgnoreCase( clockStrategy ) ) {
+ return SYSTEM;
+ } else if( "heartbeat".equalsIgnoreCase( clockStrategy ) ) {
+ return HEARTBEAT;
+ } else if( "attribute".equalsIgnoreCase( clockStrategy ) ) {
+ return ATTRIBUTE;
+ }
+ return null;
+ }
+ }
+
+ private final String typeName;
+ private Role role;
+ private Format format;
+ private ClockStrategy clockStrategy;
+ private String timestampAttribute;
+ private String durationAttribute;
+ private Class<?> typeClass;
+ private FactTemplate typeTemplate;
+
+ public TypeDeclaration( String typeName ) {
+ this.typeName = typeName;
+ this.role = Role.FACT;
+ this.format = Format.POJO;
+ this.clockStrategy = ClockStrategy.NONE;
+ this.durationAttribute = null;
+ this.timestampAttribute = null;
+ this.typeClass = null;
+ this.typeTemplate = null;
+ }
+
+ /**
+ * @return the type
+ */
+ public String getTypeName() {
+ return typeName;
+ }
+
+ /**
+ * @return the category
+ */
+ public Role getRole() {
+ return role;
+ }
+
+ /**
+ * @param role the category to set
+ */
+ public void setRole(Role role) {
+ this.role = role;
+ }
+
+ /**
+ * @return the format
+ */
+ public Format getFormat() {
+ return format;
+ }
+
+ /**
+ * @param format the format to set
+ */
+ public void setFormat(Format format) {
+ this.format = format;
+ }
+
+ /**
+ * @return the clockStrategy
+ */
+ public ClockStrategy getClockStrategy() {
+ return clockStrategy;
+ }
+
+ /**
+ * @param clockStrategy the clockStrategy to set
+ */
+ public void setClockStrategy(ClockStrategy clockStrategy) {
+ this.clockStrategy = clockStrategy;
+ }
+
+ /**
+ * @return the timestampAttribute
+ */
+ public String getTimestampAttribute() {
+ return timestampAttribute;
+ }
+
+ /**
+ * @param timestampAttribute the timestampAttribute to set
+ */
+ public void setTimestampAttribute(String timestampAttribute) {
+ this.timestampAttribute = timestampAttribute;
+ }
+
+ /**
+ * @return the durationAttribute
+ */
+ public String getDurationAttribute() {
+ return durationAttribute;
+ }
+
+ /**
+ * @param durationAttribute the durationAttribute to set
+ */
+ public void setDurationAttribute(String durationAttribute) {
+ this.durationAttribute = durationAttribute;
+ }
+
+ /**
+ * @return the typeClass
+ */
+ public Class< ? > getTypeClass() {
+ return typeClass;
+ }
+
+ /**
+ * @param typeClass the typeClass to set
+ */
+ public void setTypeClass(Class< ? > typeClass) {
+ this.typeClass = typeClass;
+ }
+
+ /**
+ * @return the typeTemplate
+ */
+ public FactTemplate getTypeTemplate() {
+ return typeTemplate;
+ }
+
+ /**
+ * @param typeTemplate the typeTemplate to set
+ */
+ public void setTypeTemplate(FactTemplate typeTemplate) {
+ this.typeTemplate = typeTemplate;
+ }
+
+ /**
+ * Returns true if the given parameter matches this type declaration
+ *
+ * @param clazz
+ * @return
+ */
+ public boolean matches( Object clazz ) {
+ boolean matches = false;
+ if( clazz instanceof FactTemplate ) {
+ matches = this.typeTemplate.equals( clazz );
+ } else {
+ matches = this.typeClass.isAssignableFrom( (Class<?>) clazz );
+ }
+ return matches;
+ }
+
+
+}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/spi/KnowledgeHelper.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -16,14 +16,14 @@
* limitations under the License.
*/
-import java.io.Serializable;
-
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.WorkingMemory;
import org.drools.rule.Declaration;
import org.drools.rule.Rule;
+import java.io.Externalizable;
+
/**
* KnowledgeHelper implementation types are injected into consequenses
* instrumented at compile time and instances passed at runtime. It provides
@@ -39,7 +39,7 @@
*/
public interface KnowledgeHelper
extends
- Serializable {
+ Externalizable {
public void setActivation(final Activation agendaItem);
@@ -58,17 +58,6 @@
void insert(Object object) throws FactException;
/**
- * Asserts an object, notice that it does not return the FactHandle
- *
- * @param object -
- * the object to be asserted
- * @throws FactException -
- * Exceptions can be thrown by conditions which are wrapped and
- * returned as a FactException
- */
- void insert(Object object, long duration) throws FactException;
-
- /**
* Asserts an object specifying that it implement the onPropertyChange
* listener, notice that it does not return the FactHandle.
*
@@ -83,33 +72,11 @@
void insert(Object object,
boolean dynamic) throws FactException;
- /**
- * Asserts an object specifying that it implement the onPropertyChange
- * listener, notice that it does not return the FactHandle.
- *
- * @param object -
- * the object to be asserted
- * @param dynamic -
- * specifies the object implements onPropertyChangeListener
- * @throws FactException -
- * Exceptions can be thrown by conditions which are wrapped and
- * returned as a FactException
- */
- void insert(Object object,
- long duration,
- boolean dynamic) throws FactException;
-
public void insertLogical(Object object) throws FactException;
- public void insertLogical(Object object, long duration) throws FactException;
-
public void insertLogical(Object object,
boolean dynamic) throws FactException;
- public void insertLogical(Object object,
- long duration,
- boolean dynamic) throws FactException;
-
void update(FactHandle handle,
Object newObject) throws FactException;
Added: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ArrayIterator.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ArrayIterator.java (rev 0)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/util/ArrayIterator.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.util;
+
+import java.lang.reflect.Array;
+import java.util.NoSuchElementException;
+
+/**
+ * Implements an {@link java.util.Iterator Iterator} over any array.
+ * <p>
+ * The array can be either an array of object or of primitives. If you know
+ * that you have an object array, the
+ * {@link org.apache.commons.collections.iterators.ObjectArrayIterator ObjectArrayIterator}
+ * class is a better choice, as it will perform better.
+ * <p>
+ * The iterator implements a {@link #reset} method, allowing the reset of
+ * the iterator back to the start if required.
+ *
+ * @since Commons Collections 1.0
+ * @version $Revision$ $Date$
+ *
+ * @author James Strachan
+ * @author Mauricio S. Moura
+ * @author Michael A. Smith
+ * @author Neil O'Toole
+ * @author Stephen Colebourne
+ */
+public class ArrayIterator implements java.util.Iterator {
+
+ /** The array to iterate over */
+ protected Object array;
+ /** The end index to loop to */
+ protected int endIndex = 0;
+ /** The current iterator index */
+ protected int index = 0;
+
+
+ /**
+ * Constructs an ArrayIterator that will iterate over the values in the
+ * specified array.
+ *
+ * @param array the array to iterate over.
+ * @throws IllegalArgumentException if <code>array</code> is not an array.
+ * @throws NullPointerException if <code>array</code> is <code>null</code>
+ */
+ public ArrayIterator(final Object array) {
+ setArray(array);
+ }
+
+ /**
+ * Checks whether the index is valid or not.
+ *
+ * @param bound the index to check
+ * @param type the index type (for error messages)
+ * @throws IndexOutOfBoundsException if the index is invalid
+ */
+ protected void checkBound(final int bound, final String type ) {
+ if (bound > this.endIndex) {
+ throw new ArrayIndexOutOfBoundsException(
+ "Attempt to make an ArrayIterator that " + type +
+ "s beyond the end of the array. "
+ );
+ }
+ if (bound < 0) {
+ throw new ArrayIndexOutOfBoundsException(
+ "Attempt to make an ArrayIterator that " + type +
+ "s before the start of the array. "
+ );
+ }
+ }
+
+ // Iterator interface
+ //-----------------------------------------------------------------------
+ /**
+ * Returns true if there are more elements to return from the array.
+ *
+ * @return true if there is a next element to return
+ */
+ public boolean hasNext() {
+ return (index < endIndex);
+ }
+
+ /**
+ * Returns the next element in the array.
+ *
+ * @return the next element in the array
+ * @throws NoSuchElementException if all the elements in the array
+ * have already been returned
+ */
+ public Object next() {
+ if (hasNext() == false) {
+ throw new NoSuchElementException();
+ }
+ return Array.get(array, index++);
+ }
+
+ /**
+ * Throws {@link UnsupportedOperationException}.
+ *
+ * @throws UnsupportedOperationException always
+ */
+ public void remove() {
+ throw new UnsupportedOperationException("remove() method is not supported");
+ }
+
+ // Properties
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the array that this iterator is iterating over.
+ *
+ * @return the array this iterator iterates over, or <code>null</code> if
+ * the no-arg constructor was used and {@link #setArray(Object)} has never
+ * been called with a valid array.
+ */
+ public Object getArray() {
+ return array;
+ }
+
+ /**
+ * Sets the array that the ArrayIterator should iterate over.
+ * <p>
+ * If an array has previously been set (using the single-arg constructor
+ * or this method) then that array is discarded in favour of this one.
+ * Iteration is restarted at the start of the new array.
+ * Although this can be used to reset iteration, the {@link #reset()} method
+ * is a more effective choice.
+ *
+ * @param array the array that the iterator should iterate over.
+ * @throws IllegalArgumentException if <code>array</code> is not an array.
+ * @throws NullPointerException if <code>array</code> is <code>null</code>
+ */
+ private void setArray(final Object array) {
+ // Array.getLength throws IllegalArgumentException if the object is not
+ // an array or NullPointerException if the object is null. This call
+ // is made before saving the array and resetting the index so that the
+ // array iterator remains in a consistent state if the argument is not
+ // an array or is null.
+ this.endIndex = Array.getLength(array);
+ this.array = array;
+ this.index = 0;
+ }
+
+}
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 2008-03-15 04:17:02 UTC (rev 19007)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/reteoo/ReteTest.java 2008-03-15 06:13:59 UTC (rev 19008)
@@ -16,14 +16,6 @@
* limitations under the License.
*/
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
import org.drools.Cheese;
import org.drools.DroolsTestCase;
import org.drools.FactException;
@@ -40,6 +32,13 @@
import org.drools.rule.EntryPoint;
import org.drools.spi.PropagationContext;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Properties;
+
/**
* @author mproctor
*
@@ -143,12 +142,11 @@
null ),
workingMemory );
- final Map map = workingMemory.getObjectTypeConfMap( EntryPoint.DEFAULT );
- ClassObjectTypeConf conf = (ClassObjectTypeConf) map.get( ArrayList.class );
+ ClassObjectTypeConf conf = ( ClassObjectTypeConf ) workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( this.entryPoint.getEntryPoint(), ArrayList.class );
assertLength( 3,
conf.getObjectTypeNodes() );
- conf = (ClassObjectTypeConf) map.get( LinkedList.class );
+ conf = ( ClassObjectTypeConf ) workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( this.entryPoint.getEntryPoint(), LinkedList.class );
assertLength( 3,
conf.getObjectTypeNodes() );
@@ -249,8 +247,7 @@
rete.getObjectTypeNodes( EntryPoint.DEFAULT ).get( new ClassObjectType( List.class ) ) );
// ArrayConf should match two ObjectTypenodes for List and ArrayList
- Map memory = workingMemory.getObjectTypeConfMap( EntryPoint.DEFAULT );
- ObjectTypeConf arrayConf = (ObjectTypeConf) memory.get( ArrayList.class );
+ ClassObjectTypeConf arrayConf = ( ClassObjectTypeConf ) workingMemory.getObjectTypeConfigurationRegistry().getObjectTypeConf( this.entryPoint.getEntryPoint(), ArrayList.class );
final ObjectTypeNode arrayOtn = arrayConf.getConcreteObjectTypeNode();
assertEquals( 2,
arrayConf.getObjectTypeNodes().length );
More information about the jboss-svn-commits
mailing list