📄 abstractrulebase.java
字号:
package org.drools.common;
/*
* 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.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.drools.FactException;
import org.drools.PackageIntegrationException;
import org.drools.RuleBase;
import org.drools.RuleBaseConfiguration;
import org.drools.RuleIntegrationException;
import org.drools.WorkingMemory;
import org.drools.rule.CompositePackageClassLoader;
import org.drools.rule.InvalidPatternException;
import org.drools.rule.Package;
import org.drools.rule.PackageCompilationData;
import org.drools.rule.Rule;
import org.drools.spi.FactHandleFactory;
/**
* Implementation of <code>RuleBase</code>.
*
* @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
* @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
*
* @version $Id: RuleBaseImpl.java,v 1.5 2005/08/14 22:44:12 mproctor Exp $
*/
abstract public class AbstractRuleBase
implements
InternalRuleBase,
Externalizable {
// ------------------------------------------------------------
// Instance members
// ------------------------------------------------------------
protected RuleBaseConfiguration config;
protected Map pkgs;
protected transient CompositePackageClassLoader packageClassLoader;
/** The fact handle factory. */
protected FactHandleFactory factHandleFactory;
protected Map globals;
/**
* WeakHashMap to keep references of WorkingMemories but allow them to be
* garbage collected
*/
protected transient Map workingMemories;
/** Special value when adding to the underlying map. */
protected static final Object PRESENT = new Object();
// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
/**
* Construct.
*
* @param rete
* The rete network.
*/
public AbstractRuleBase(final RuleBaseConfiguration config,
final FactHandleFactory factHandleFactory) {
this.config = (config != null) ? config : new RuleBaseConfiguration();
this.config.makeImmutable();
this.factHandleFactory = factHandleFactory;
this.packageClassLoader = new CompositePackageClassLoader( Thread.currentThread().getContextClassLoader() );
this.pkgs = new HashMap();
this.globals = new HashMap();
this.workingMemories = new WeakHashMap();
}
// ------------------------------------------------------------
// Instance methods
// ------------------------------------------------------------
/**
* Handles the write serialization of the Package. Patterns in Rules may reference generated data which cannot be serialized by default methods.
* The Package uses PackageCompilationData to hold a reference to the generated bytecode. The generated bytecode must be restored before any Rules.
*
*/
public void doWriteExternal(final ObjectOutput stream,
final Object[] objects) throws IOException {
stream.writeObject( this.pkgs );
// Rules must be restored by an ObjectInputStream that can resolve using a given ClassLoader to handle seaprately by storing as
// a byte[]
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ObjectOutput out = new ObjectOutputStream( bos );
out.writeObject( this.factHandleFactory );
out.writeObject( this.globals );
out.writeObject( this.config );
for ( int i = 0, length = objects.length; i < length; i++ ) {
out.writeObject( objects[i] );
}
stream.writeObject( bos.toByteArray() );
}
/**
* Handles the read serialization of the Package. Patterns in Rules may reference generated data which cannot be serialized by default methods.
* The Package uses PackageCompilationData to hold a reference to the generated bytecode; which must be restored before any Rules.
* A custom ObjectInputStream, able to resolve classes against the bytecode in the PackageCompilationData, is used to restore the Rules.
*
*/
public void doReadExternal(final ObjectInput stream,
final Object[] objects) throws IOException,
ClassNotFoundException {
// PackageCompilationData must be restored before Rules as it has the ClassLoader needed to resolve the generated code references in Rules
this.pkgs = (Map) stream.readObject();
this.packageClassLoader = new CompositePackageClassLoader( Thread.currentThread().getContextClassLoader() );
for ( final Iterator it = this.pkgs.values().iterator(); it.hasNext(); ) {
this.packageClassLoader.addClassLoader( ((Package) it.next()).getPackageCompilationData().getClassLoader() );
}
// Return the rules stored as a byte[]
final byte[] bytes = (byte[]) stream.readObject();
// Use a custom ObjectInputStream that can resolve against a given classLoader
final ObjectInputStreamWithLoader streamWithLoader = new ObjectInputStreamWithLoader( new ByteArrayInputStream( bytes ),
this.packageClassLoader );
this.factHandleFactory = (FactHandleFactory) streamWithLoader.readObject();
this.globals = (Map) streamWithLoader.readObject();
this.config = (RuleBaseConfiguration) streamWithLoader.readObject();
this.workingMemories = new WeakHashMap();
for ( int i = 0, length = objects.length; i < length; i++ ) {
objects[i] = streamWithLoader.readObject();
}
}
/**
* @see RuleBase
*/
public WorkingMemory newWorkingMemory() {
return newWorkingMemory( true );
}
/**
* @see RuleBase
*/
abstract public WorkingMemory newWorkingMemory(boolean keepReference);
public void disposeWorkingMemory(final WorkingMemory workingMemory) {
this.workingMemories.remove( workingMemory );
}
/**
* @see RuleBase
*/
public FactHandleFactory getFactHandleFactory() {
return this.factHandleFactory;
}
public FactHandleFactory newFactHandleFactory() {
return this.factHandleFactory.newInstance();
}
public Package[] getPackages() {
return (Package[]) this.pkgs.values().toArray( new Package[this.pkgs.size()] );
}
public Map getGlobals() {
return this.globals;
}
/**
* Add a <code>Package</code> to the network. Iterates through the
* <code>Package</code> adding Each individual <code>Rule</code> to the
* network. Before update network each referenced <code>WorkingMemory</code>
* is locked.
*
* @param pkg
* The package to add.
* @throws PackageIntegrationException
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -