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.util.Collections;
020: import java.util.HashMap;
021: import java.util.List;
022:
023: import org.drools.FactException;
024: import org.drools.FactHandle;
025: import org.drools.QueryResults;
026: import org.drools.base.DroolsQuery;
027: import org.drools.common.AbstractWorkingMemory;
028: import org.drools.common.DefaultAgenda;
029: import org.drools.common.InternalFactHandle;
030: import org.drools.common.InternalRuleBase;
031: import org.drools.common.InternalWorkingMemory;
032: import org.drools.common.PropagationContextImpl;
033: import org.drools.common.WorkingMemoryAction;
034: import org.drools.rule.Query;
035: import org.drools.rule.Rule;
036: import org.drools.spi.Activation;
037: import org.drools.spi.PropagationContext;
038:
039: /**
040: * Implementation of <code>WorkingMemory</code>.
041: *
042: * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
043: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
044: * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
045: */
046: public class ReteooWorkingMemory extends AbstractWorkingMemory {
047:
048: /**
049: *
050: */
051: private static final long serialVersionUID = 400L;
052:
053: /**
054: * Construct.
055: *
056: * @param ruleBase
057: * The backing rule-base.
058: */
059: public ReteooWorkingMemory(final int id,
060: final InternalRuleBase ruleBase) {
061: super (id, ruleBase, ruleBase.newFactHandleFactory());
062: this .agenda = new DefaultAgenda(this );
063: }
064:
065: public void doInsert(final InternalFactHandle handle,
066: final Object object,
067: final PropagationContext propagationContext)
068: throws FactException {
069: this .ruleBase.assertObject(handle, object, propagationContext,
070: this );
071: }
072:
073: public void doRetract(final InternalFactHandle handle,
074: final PropagationContext propagationContext) {
075: this .ruleBase.retractObject(handle, propagationContext, this );
076: }
077:
078: public QueryResults getQueryResults(final String query) {
079: return getQueryResults(query, null);
080: }
081:
082: public QueryResults getQueryResults(final String query,
083: final Object[] arguments) {
084: final FactHandle handle = insert(new DroolsQuery(query,
085: arguments));
086: final QueryTerminalNode node = (QueryTerminalNode) this .queryResults
087: .remove(query);
088: Query queryObj = null;
089: List list = null;
090:
091: if (node == null) {
092: // There are no results, first check the query object actually exists and then retract the DroolsQuery object
093: final org.drools.rule.Package[] pkgs = this .ruleBase
094: .getPackages();
095: for (int i = 0; i < pkgs.length; i++) {
096: final Rule rule = pkgs[i].getRule(query);
097: if ((rule != null) && (rule instanceof Query)) {
098: queryObj = (Query) rule;
099: break;
100: }
101: }
102: retract(handle);
103: if (queryObj == null) {
104: throw new IllegalArgumentException("Query '" + query
105: + "' does not exist");
106: }
107: list = Collections.EMPTY_LIST;
108: } else {
109: list = (List) this .nodeMemories.remove(node.getId());
110:
111: retract(handle);
112: if (list == null) {
113: list = Collections.EMPTY_LIST;
114: }
115: queryObj = (Query) node.getRule();
116: }
117:
118: return new QueryResults(list, queryObj, this );
119: }
120:
121: void setQueryResults(final String query,
122: final QueryTerminalNode node) {
123: if (this .queryResults == Collections.EMPTY_MAP) {
124: this .queryResults = new HashMap();
125: }
126: this .queryResults.put(query, node);
127: }
128:
129: public static class WorkingMemoryReteAssertAction implements
130: WorkingMemoryAction {
131: private InternalFactHandle factHandle;
132:
133: private boolean removeLogical;
134:
135: private boolean updateEqualsMap;
136:
137: private Rule ruleOrigin;
138:
139: private Activation activationOrigin;
140:
141: public WorkingMemoryReteAssertAction(
142: final InternalFactHandle factHandle,
143: final boolean removeLogical,
144: final boolean updateEqualsMap, final Rule ruleOrigin,
145: final Activation activationOrigin) {
146: super ();
147: this .factHandle = factHandle;
148: this .removeLogical = removeLogical;
149: this .updateEqualsMap = updateEqualsMap;
150: this .ruleOrigin = ruleOrigin;
151: this .activationOrigin = activationOrigin;
152: }
153:
154: public void execute(InternalWorkingMemory workingMemory) {
155:
156: final PropagationContext context = new PropagationContextImpl(
157: workingMemory.getNextPropagationIdCounter(),
158: PropagationContext.ASSERTION, this .ruleOrigin,
159: this .activationOrigin);
160: ReteooRuleBase ruleBase = (ReteooRuleBase) workingMemory
161: .getRuleBase();
162: ruleBase.assertObject(this.factHandle, this.factHandle
163: .getObject(), context, workingMemory);
164: }
165: }
166: }
|