001: package org.drools.base;
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 org.drools.FactException;
020: import org.drools.FactHandle;
021: import org.drools.WorkingMemory;
022: import org.drools.common.InternalWorkingMemoryActions;
023: import org.drools.rule.Declaration;
024: import org.drools.rule.GroupElement;
025: import org.drools.rule.Rule;
026: import org.drools.spi.Activation;
027: import org.drools.spi.KnowledgeHelper;
028: import org.drools.spi.Tuple;
029:
030: public class DefaultKnowledgeHelper implements KnowledgeHelper {
031:
032: private static final long serialVersionUID = 400L;
033:
034: private Rule rule;
035: private GroupElement subrule;
036: private Activation activation;
037: private Tuple tuple;
038: private final InternalWorkingMemoryActions workingMemory;
039:
040: public DefaultKnowledgeHelper(final WorkingMemory workingMemory) {
041: this .workingMemory = (InternalWorkingMemoryActions) workingMemory;
042: }
043:
044: public void setActivation(final Activation agendaItem) {
045: this .rule = agendaItem.getRule();
046: this .subrule = agendaItem.getSubRule();
047: this .activation = agendaItem;
048: this .tuple = agendaItem.getTuple();
049: }
050:
051: public void insert(final Object object) throws FactException {
052: insert(object, false);
053: }
054:
055: public void insert(final Object object, final boolean dynamic)
056: throws FactException {
057: this .workingMemory.insert(object, dynamic, false, this .rule,
058: this .activation);
059: }
060:
061: public void insertLogical(final Object object) throws FactException {
062: insertLogical(object, false);
063: }
064:
065: public void insertLogical(final Object object, final boolean dynamic)
066: throws FactException {
067: this .workingMemory.insert(object, dynamic, true, this .rule,
068: this .activation);
069: }
070:
071: public void update(final FactHandle handle, final Object newObject)
072: throws FactException {
073: // only update if this fact exists in the wm
074: this .workingMemory.update(handle, newObject, this .rule,
075: this .activation);
076: }
077:
078: public void update(final Object object) throws FactException {
079: FactHandle handle = this .workingMemory.getFactHandle(object);
080: if (handle == null) {
081: throw new FactException(
082: "Update error: handle not found for object: "
083: + object + ". Is it in the working memory?");
084: }
085: // only update if this fact exists in the wm
086: this .workingMemory.update(handle, object, this .rule,
087: this .activation);
088: }
089:
090: public void retract(final FactHandle handle) throws FactException {
091: this .workingMemory.retract(handle, true, true, this .rule,
092: this .activation);
093: }
094:
095: public void retract(final Object object) throws FactException {
096: FactHandle handle = this .workingMemory.getFactHandle(object);
097: if (handle == null) {
098: throw new FactException(
099: "Retract error: handle not found for object: "
100: + object + ". Is it in the working memory?");
101: }
102: this .workingMemory.retract(handle, true, true, this .rule,
103: this .activation);
104: }
105:
106: public void modifyRetract(final Object object) {
107: FactHandle handle = this .workingMemory.getFactHandle(object);
108: this .workingMemory.modifyRetract(handle, rule, activation);
109: }
110:
111: public void modifyRetract(final FactHandle factHandle) {
112: this .workingMemory.modifyRetract(factHandle, rule, activation);
113: }
114:
115: public void modifyInsert(final Object object) {
116: FactHandle handle = this .workingMemory.getFactHandle(object);
117: this .workingMemory.modifyInsert(handle, object, rule,
118: activation);
119: }
120:
121: public void modifyInsert(final FactHandle factHandle,
122: final Object object) {
123: this .workingMemory.modifyInsert(factHandle, object, rule,
124: activation);
125: }
126:
127: public Rule getRule() {
128: return this .rule;
129: }
130:
131: // public List getObjects() {
132: // return null; //this.workingMemory.getObjects();
133: // }
134: //
135: // public List getObjects(final Class objectClass) {
136: // return null; //this.workingMemory.getObjects( objectClass );
137: // }
138: //
139: // public void clearAgenda() {
140: // this.workingMemory.clearAgenda();
141: // }
142: //
143: // public void clearAgendaGroup(final String group) {
144: // this.workingMemory.clearAgendaGroup( group );
145: // }
146:
147: public Tuple getTuple() {
148: return this .tuple;
149: }
150:
151: public WorkingMemory getWorkingMemory() {
152: return this .workingMemory;
153: }
154:
155: public Activation getActivation() {
156: return this .activation;
157: }
158:
159: // public QueryResults getQueryResults(final String query) {
160: // return this.workingMemory.getQueryResults( query );
161: // }
162: //
163: // public AgendaGroup getFocus() {
164: // return this.workingMemory.getFocus();
165: // }
166: //
167: public void setFocus(final String focus) {
168: this .workingMemory.setFocus(focus);
169: }
170:
171: //
172: // public void setFocus(final AgendaGroup focus) {
173: // this.workingMemory.setFocus( focus );
174: // }
175:
176: public Object get(final Declaration declaration) {
177: return declaration.getValue(workingMemory, this .tuple.get(
178: declaration).getObject());
179: }
180:
181: public Declaration getDeclaration(final String identifier) {
182: return (Declaration) this .subrule.getOuterDeclarations().get(
183: identifier);
184: }
185:
186: public void halt() {
187: this.workingMemory.halt();
188: }
189: }
|