📄 leapsworkingmemory.java
字号:
package org.drools.leaps;
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.drools.FactException;
import org.drools.FactHandle;
import org.drools.NoSuchFactObjectException;
import org.drools.QueryResults;
import org.drools.WorkingMemory;
import org.drools.common.AbstractWorkingMemory;
import org.drools.common.AgendaGroupImpl;
import org.drools.common.AgendaItem;
import org.drools.common.EqualityKey;
import org.drools.common.EventSupport;
import org.drools.common.InternalFactHandle;
import org.drools.common.InternalRuleBase;
import org.drools.common.PropagationContextImpl;
import org.drools.common.ScheduledAgendaItem;
import org.drools.leaps.conflict.DefaultConflictResolver;
import org.drools.leaps.util.TokenStack;
import org.drools.rule.Query;
import org.drools.rule.Rule;
import org.drools.spi.Activation;
import org.drools.spi.AgendaFilter;
import org.drools.spi.AgendaGroup;
import org.drools.spi.Duration;
import org.drools.spi.PropagationContext;
import org.drools.spi.Tuple;
import org.drools.util.FastMap;
import org.drools.util.IdentityMap;
import org.drools.util.IteratorChain;
/**
* Followed RETEOO implementation for interfaces.
*
* This class is a repository for leaps specific containers (fact factTables).
*
* @author Alexander Bagerman
*
* @see org.drools.WorkingMemory
* @see java.beans.PropertyChangeListener
* @see java.io.Serializable
*
*/
class LeapsWorkingMemory extends AbstractWorkingMemory implements EventSupport,
PropertyChangeListener {
private static final long serialVersionUID = -2524904474925421759L;
private final Map queryResults;
private final IdentityMap leapsRulesToHandlesMap = new IdentityMap( );
private final IdentityMap rulesActivationsMap = new IdentityMap( );
/**
* Construct.
*
* @param ruleBase
* The backing rule-base.
*/
public LeapsWorkingMemory(final InternalRuleBase ruleBase) {
super( ruleBase, ruleBase.newFactHandleFactory( ) );
this.queryResults = new HashMap( );
this.agenda = new LeapsAgenda( this );
}
public void doAssertObject( final InternalFactHandle factHandle,
final Object object,
final PropagationContext propagationContext )
throws FactException {
this.pushTokenOnStack( factHandle, new Token( this, factHandle, propagationContext ) );
// determine what classes it belongs to put it into the "table" on
// class name key
final Class objectClass = object.getClass();
for ( final Iterator tables = this.getFactTablesList( objectClass ).iterator(); tables.hasNext(); ) {
final FactTable factTable = (FactTable) tables.next();
// adding fact to container
factTable.add( factHandle );
// inspect all tuples for exists and not conditions and activate
// /
// deactivate agenda items
for (final Iterator tuples = factTable.getTuplesIterator( ); tuples.hasNext( );) {
final LeapsTuple tuple = (LeapsTuple) tuples.next( );
boolean tupleWasReadyForActivation = tuple.isReadyForActivation( );
if (!tuple.isActivationNull( )) {
// check not constraints only on activated tuples to see
// if
// we need to deactivate
final ColumnConstraints[] not = tuple.getLeapsRule( )
.getNotColumnConstraints( );
for (int i = 0, length = not.length; i < length; i++) {
final ColumnConstraints constraint = not[i];
if (!tuple.isBlockingNotFactHandle( i )
&& constraint.getClassType( )
.isAssignableFrom( objectClass )
&& constraint.isAllowed( factHandle, tuple, this )) {
tuple.setBlockingNotFactHandle( (LeapsFactHandle) factHandle, i );
( (LeapsFactHandle) factHandle ).addNotTuple( tuple, i );
}
}
// check and see if we need de-activate
if (!tuple.isReadyForActivation( )) {
if (tuple.getLeapsRule( ).getRule( ) instanceof Query) {
// put query results to the working memory
// location
removeFromQueryResults( tuple.getLeapsRule( )
.getRule( )
.getName( ), tuple );
}
else {
// time to pull from agenda
invalidateActivation( tuple );
}
}
}
else {
// check exists constraints and activate constraints
final ColumnConstraints[] exists = tuple.getLeapsRule( )
.getExistsColumnConstraints( );
for (int i = 0, length = exists.length; i < length; i++) {
final ColumnConstraints constraint = exists[i];
if (!tuple.isExistsFactHandle( i )
&& constraint.getClassType( )
.isAssignableFrom( objectClass )
&& constraint.isAllowed( factHandle, tuple, this )) {
tuple.setExistsFactHandle( (LeapsFactHandle) factHandle, i );
( (LeapsFactHandle) factHandle ).addExistsTuple( tuple, i );
}
}
// check and see if we need activate
// activate only if tuple was not ready for it before
if (!tupleWasReadyForActivation && tuple.isReadyForActivation( )) {
// ready to activate
tuple.setContext( new PropagationContextImpl( nextPropagationIdCounter( ),
PropagationContext.ASSERTION,
tuple.getLeapsRule( ).getRule( ),
null ) );
this.assertTuple( tuple );
}
}
}
}
}
/**
* copies reteoo behaviour in regards to logical assertion
* and does checking on available tuples to see if any needs
* invalidation / activation as a result of this retraction
*
* @see WorkingMemory
*/
public void doRetract( final InternalFactHandle factHandle,
final PropagationContext propagationContext ) {
/*
* leaps specific actions
*/
// remove fact from all relevant fact tables container
for (final Iterator it = this.getFactTablesList( factHandle.getObject( ).getClass( ) )
.iterator( ); it.hasNext( );) {
( (FactTable) it.next( ) ).remove( factHandle );
}
// 0. remove activated tuples
final Iterator tuples = ( (LeapsFactHandle) factHandle ).getActivatedTuples( );
for (; tuples != null && tuples.hasNext( );) {
final LeapsTuple tuple = (LeapsTuple) tuples.next( );
if (tuple.getLeapsRule( ).getRule( ) instanceof Query) {
// put query results to the working memory location
removeFromQueryResults( tuple.getLeapsRule( ).getRule( ).getName( ), tuple );
}
else {
// time to pull from agenda
invalidateActivation( tuple );
}
}
// 1. remove fact for nots and exists tuples
final IdentityMap tuplesNotReadyForActivation = new IdentityMap( );
FactHandleTupleAssembly assembly;
LeapsTuple tuple;
Iterator it;
it = ( (LeapsFactHandle) factHandle ).getNotTupleAssemblies( );
if (it != null) {
for (; it.hasNext( );) {
assembly = (FactHandleTupleAssembly) it.next( );
tuple = assembly.getTuple( );
if (!tuple.isReadyForActivation( )) {
tuplesNotReadyForActivation.put( tuple, tuple );
}
tuple.removeBlockingNotFactHandle( assembly.getIndex( ) );
TokenEvaluator.evaluateNotCondition( (LeapsFactHandle) factHandle,
// TokenEvaluator.evaluateNotCondition( new LeapsFactHandle( factHandle.getRecency( ) + 1,
// new Object( ) ),
assembly.getIndex( ),
tuple,
this );
}
}
it = ((LeapsFactHandle) factHandle).getExistsTupleAssemblies();
if ( it != null ) {
for ( ; it.hasNext(); ) {
assembly = (FactHandleTupleAssembly) it.next();
tuple = assembly.getTuple();
if ( !tuple.isReadyForActivation() ) {
tuplesNotReadyForActivation.put( tuple,
tuple );
}
tuple.removeExistsFactHandle( assembly.getIndex( ) );
TokenEvaluator.evaluateExistsCondition( (LeapsFactHandle)factHandle,
// TokenEvaluator.evaluateExistsCondition( new LeapsFactHandle( factHandle.getRecency( ) + 1,
// null ),
assembly.getIndex( ),
tuple,
this );
}
}
// 2. assert all tuples that are ready for activation or cancel ones
// that are no longer
final IteratorChain chain = new IteratorChain();
it = ((LeapsFactHandle) factHandle).getNotTupleAssemblies();
if ( it != null ) {
chain.addIterator( it );
}
it = ((LeapsFactHandle) factHandle).getExistsTupleAssemblies();
if ( it != null ) {
chain.addIterator( it );
}
for ( ; chain.hasNext(); ) {
tuple = ( (FactHandleTupleAssembly) chain.next( ) ).getTuple( );
// can assert only tuples that were not eligible for activation
// before retraction
if (tuple.isReadyForActivation( ) && tuple.isActivationNull( )
&& tuplesNotReadyForActivation.containsKey( tuple )) {
// ready to activate
tuple.setContext( new PropagationContextImpl( nextPropagationIdCounter( ),
PropagationContext.ASSERTION,
tuple.getLeapsRule( )
.getRule( ),
null ) );
this.assertTuple( tuple );
}
else {
if (tuple.getLeapsRule( ).getRule( ) instanceof Query) {
// put query results to the working memory location
removeFromQueryResults( tuple.getLeapsRule( ).getRule( ).getName( ),
tuple );
}
else {
// time to pull from agenda
invalidateActivation( tuple );
}
}
}
// remove it from stack
this.removeTokenFromStack( (LeapsFactHandle) factHandle );
}
/**
* used when assertion / retraction adds invalidating conditions that
* make tuple ineligible for firing
*
* @param tuple
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -