001: package org.drools.reteoo;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.ObjectInput;
021: import java.io.ObjectOutput;
022: import java.io.Serializable;
023: import java.util.HashMap;
024: import java.util.WeakHashMap;
025:
026: import org.drools.FactException;
027: import org.drools.FactHandle;
028: import org.drools.RuleBase;
029: import org.drools.RuleBaseConfiguration;
030: import org.drools.StatefulSession;
031: import org.drools.StatelessSession;
032: import org.drools.common.AbstractRuleBase;
033: import org.drools.common.DefaultFactHandle;
034: import org.drools.common.InternalFactHandle;
035: import org.drools.common.InternalWorkingMemory;
036: import org.drools.concurrent.CommandExecutor;
037: import org.drools.concurrent.DefaultExecutorService;
038: import org.drools.concurrent.ExecutorService;
039: import org.drools.rule.CompositePackageClassLoader;
040: import org.drools.rule.InvalidPatternException;
041: import org.drools.rule.Rule;
042: import org.drools.reteoo.ReteooWorkingMemory.WorkingMemoryReteAssertAction;
043: import org.drools.spi.FactHandleFactory;
044: import org.drools.spi.PropagationContext;
045: import org.drools.util.ObjectHashSet;
046:
047: import sun.security.x509.IssuerAlternativeNameExtension;
048:
049: /**
050: * Implementation of <code>RuleBase</code>.
051: *
052: * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
053: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
054: *
055: * @version $Id: RuleBaseImpl.java,v 1.5 2005/08/14 22:44:12 mproctor Exp $
056: */
057: public class ReteooRuleBase extends AbstractRuleBase {
058: /**
059: * DO NOT CHANGE BELLOW SERIAL_VERSION_ID UNLESS YOU ARE CHANGING DROOLS VERSION
060: * SERIAL_VERSION_ID=320 stands for version 3.2.0
061: */
062: private static final long serialVersionUID = 400L;
063:
064: /** The root Rete-OO for this <code>RuleBase</code>. */
065: private Rete rete;
066:
067: private ReteooBuilder reteooBuilder;
068:
069: // ------------------------------------------------------------
070: // Constructors
071: // ------------------------------------------------------------
072:
073: /**
074: * Default constructor - for Externalizable. This should never be used by a user, as it
075: * will result in an invalid state for the instance.
076: */
077: public ReteooRuleBase() {
078:
079: }
080:
081: /**
082: * Construct.
083: *
084: * @param rete
085: * The rete network.
086: */
087: public ReteooRuleBase(final String id) {
088: this (id, null, new ReteooFactHandleFactory());
089: }
090:
091: /**
092: * @param factHandleFactory
093: */
094: public ReteooRuleBase(final String id,
095: final FactHandleFactory factHandleFactory) {
096: this (id, null, factHandleFactory);
097: }
098:
099: public ReteooRuleBase(final String id,
100: final RuleBaseConfiguration config) {
101: this (id, config, new ReteooFactHandleFactory());
102: }
103:
104: /**
105: * @param config
106: */
107: public ReteooRuleBase(final RuleBaseConfiguration config) {
108: this (null, config, new ReteooFactHandleFactory());
109: }
110:
111: /**
112: * Construct.
113: *
114: * @param rete
115: * The rete network.
116: */
117: public ReteooRuleBase(final String id,
118: final RuleBaseConfiguration config,
119: final FactHandleFactory factHandleFactory) {
120: super (id, config, factHandleFactory);
121: this .rete = new Rete(this );
122: this .reteooBuilder = new ReteooBuilder(this );
123: }
124:
125: /**
126: * Handles the write serialization of the Package. Patterns in Rules may reference generated data which cannot be serialized by default methods.
127: * The Package uses PackageCompilationData to hold a reference to the generated bytecode. The generated bytecode must be restored before any Rules.
128: *
129: */
130: public void writeExternal(final ObjectOutput stream)
131: throws IOException {
132: final Object[] objects = new Object[] { this .rete,
133: this .reteooBuilder };
134: doWriteExternal(stream, objects);
135: }
136:
137: /**
138: * Handles the read serialization of the Package. Patterns in Rules may reference generated data which cannot be serialized by default methods.
139: * The Package uses PackageCompilationData to hold a reference to the generated bytecode; which must be restored before any Rules.
140: * A custom ObjectInputStream, able to resolve classes against the bytecode in the PackageCompilationData, is used to restore the Rules.
141: *
142: */
143: public void readExternal(final ObjectInput stream)
144: throws IOException, ClassNotFoundException {
145: final Object[] objects = new Object[2];
146: doReadExternal(stream, objects);
147:
148: this .rete = (Rete) objects[0];
149: this .reteooBuilder = (ReteooBuilder) objects[1];
150: }
151:
152: // ------------------------------------------------------------
153: // Instance methods
154: // ------------------------------------------------------------
155:
156: /**
157: * Retrieve the Rete-OO network for this <code>RuleBase</code>.
158: *
159: * @return The RETE-OO network.
160: */
161: public Rete getRete() {
162: return this .rete;
163: }
164:
165: public ReteooBuilder getReteooBuilder() {
166: return this .reteooBuilder;
167: }
168:
169: /**
170: * Assert a fact object.
171: *
172: * @param handle
173: * The handle.
174: * @param object
175: * The fact.
176: * @param workingMemory
177: * The working-memory.
178: *
179: * @throws FactException
180: * If an error occurs while performing the assertion.
181: */
182: public void assertObject(final FactHandle handle,
183: final Object object, final PropagationContext context,
184: final InternalWorkingMemory workingMemory)
185: throws FactException {
186: getRete().assertObject((DefaultFactHandle) handle, context,
187: workingMemory);
188: }
189:
190: /**
191: * Retract a fact object.
192: *
193: * @param handle
194: * The handle.
195: * @param workingMemory
196: * The working-memory.
197: *
198: * @throws FactException
199: * If an error occurs while performing the retraction.
200: */
201: public void retractObject(final FactHandle handle,
202: final PropagationContext context,
203: final ReteooWorkingMemory workingMemory)
204: throws FactException {
205: getRete().retractObject((InternalFactHandle) handle, context,
206: workingMemory);
207: }
208:
209: /**
210: * @see RuleBase
211: */
212: public synchronized StatefulSession newStatefulSession(
213: final boolean keepReference) {
214: if (this .config.isSequential()) {
215: throw new RuntimeException(
216: "Cannot have a stateful rule session, with sequential configuration set to true");
217: }
218: ReteooStatefulSession session = null;
219:
220: synchronized (this .pkgs) {
221: ExecutorService executor = this .config.getExecutorService();
222: session = new ReteooStatefulSession(
223: nextWorkingMemoryCounter(), this , executor);
224:
225: executor.setCommandExecutor(new CommandExecutor(session));
226:
227: if (keepReference) {
228: super .addStatefulSession(session);
229: }
230:
231: final InitialFactHandle handle = new InitialFactHandle(
232: session.getFactHandleFactory().newFactHandle(
233: new InitialFactHandleDummyObject()));
234:
235: session
236: .queueWorkingMemoryAction(new WorkingMemoryReteAssertAction(
237: handle, false, true, null, null));
238: }
239: return session;
240: }
241:
242: public StatelessSession newStatelessSession() {
243:
244: //orders the rules
245: if (this .config.isSequential()) {
246: this .reteooBuilder.order();
247: }
248:
249: synchronized (this .pkgs) {
250: return new ReteooStatelessSession(this );
251: }
252: }
253:
254: protected synchronized void addRule(final Rule rule)
255: throws InvalidPatternException {
256: super .addRule(rule);
257:
258: // This adds the rule. ReteBuilder has a reference to the WorkingMemories and will propagate any existing facts.
259: this .reteooBuilder.addRule(rule);
260: }
261:
262: protected synchronized void removeRule(final Rule rule) {
263: this .reteooBuilder.removeRule(rule);
264: }
265:
266: public static class InitialFactHandleDummyObject implements
267: Serializable {
268: private static final long serialVersionUID = 400L;
269: }
270: }
|