001: package org.drools.common;
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.Serializable;
020: import java.util.TimerTask;
021:
022: import org.drools.rule.GroupElement;
023: import org.drools.rule.Rule;
024: import org.drools.spi.Activation;
025: import org.drools.spi.AgendaGroup;
026: import org.drools.spi.PropagationContext;
027: import org.drools.spi.Tuple;
028: import org.drools.util.LinkedList;
029: import org.drools.util.LinkedListNode;
030:
031: /**
032: * Item entry in the <code>Agenda</code>.
033: *
034: * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a>
035: */
036: public class ScheduledAgendaItem extends TimerTask implements
037: Activation, Serializable, LinkedListNode {
038: // ------------------------------------------------------------
039: // Instance members
040: // ------------------------------------------------------------
041:
042: /**
043: *
044: */
045: private static final long serialVersionUID = 400L;
046:
047: private LinkedListNode previous;
048:
049: private LinkedListNode next;
050:
051: /** The tuple. */
052: private final Tuple tuple;
053:
054: /** The rule. */
055: private final Rule rule;
056:
057: /** The subrule */
058: private final GroupElement subrule;
059:
060: private final InternalAgenda agenda;
061:
062: private final PropagationContext context;
063:
064: private final long activationNumber;
065:
066: private LinkedList justified;
067:
068: private boolean activated;
069:
070: private ActivationGroupNode activationGroupNode;
071:
072: // ------------------------------------------------------------
073: // Constructors
074: // ------------------------------------------------------------
075:
076: /**
077: * Construct.
078: *
079: * @param tuple
080: * The tuple.
081: * @param rule
082: * The rule.
083: */
084: public ScheduledAgendaItem(final long activationNumber,
085: final Tuple tuple, final InternalAgenda agenda,
086: final PropagationContext context, final Rule rule,
087: final GroupElement subrule) {
088: this .tuple = tuple;
089: this .context = context;
090: this .rule = rule;
091: this .subrule = subrule;
092: this .agenda = agenda;
093: this .activationNumber = activationNumber;
094: }
095:
096: // ------------------------------------------------------------
097: // Instance methods
098: // ------------------------------------------------------------
099: public PropagationContext getPropagationContext() {
100: return this .context;
101: }
102:
103: public int getSalience() {
104: throw new UnsupportedOperationException(
105: "salience is now application to scheduled activations");
106: }
107:
108: /**
109: * Retrieve the rule.
110: *
111: * @return The rule.
112: */
113: public Rule getRule() {
114: return this .rule;
115: }
116:
117: /**
118: * Retrieve the tuple.
119: *
120: * @return The tuple.
121: */
122: public Tuple getTuple() {
123: return this .tuple;
124: }
125:
126: /**
127: * Handle the firing of an alarm.
128: */
129: public void run() {
130: this .agenda.fireActivation(this );
131: this .agenda.getWorkingMemory().fireAllRules();
132: }
133:
134: public long getActivationNumber() {
135: return this .activationNumber;
136: }
137:
138: public LinkedListNode getNext() {
139: return this .next;
140: }
141:
142: public void setNext(final LinkedListNode next) {
143: this .next = next;
144: }
145:
146: public LinkedListNode getPrevious() {
147: return this .previous;
148: }
149:
150: public void setPrevious(final LinkedListNode previous) {
151: this .previous = previous;
152: }
153:
154: public void remove() {
155: this .agenda.removeScheduleItem(this );
156: }
157:
158: public String toString() {
159: return "[Activation rule=" + this .rule.getName() + ", tuple="
160: + this .tuple + "]";
161: }
162:
163: public void addLogicalDependency(final LogicalDependency node) {
164: if (this .justified == null) {
165: this .justified = new LinkedList();
166: }
167:
168: this .justified.add(node);
169: }
170:
171: public LinkedList getLogicalDependencies() {
172: return this .justified;
173: }
174:
175: public boolean isActivated() {
176: return this .activated;
177: }
178:
179: public void setActivated(final boolean activated) {
180: this .activated = activated;
181: }
182:
183: public ActivationGroupNode getActivationGroupNode() {
184: return this .activationGroupNode;
185: }
186:
187: public void setActivationGroupNode(
188: final ActivationGroupNode activationGroupNode) {
189: this .activationGroupNode = activationGroupNode;
190: }
191:
192: public RuleFlowGroupNode getRuleFlowGroupNode() {
193: return null;
194: }
195:
196: public void setRuleFlowGroupNode(
197: final RuleFlowGroupNode ruleFlowGroupNode) {
198: throw new UnsupportedOperationException(
199: "Scheduled activations cannot be in a Rule Flow Group");
200: }
201:
202: public AgendaGroup getAgendaGroup() {
203: return null;
204: }
205:
206: /*
207: * (non-Javadoc)
208: *
209: * @see java.lang.Object#equals(java.lang.Object)
210: */
211: public boolean equals(final Object object) {
212: if (object == this ) {
213: return true;
214: }
215:
216: if ((object == null) || !(object instanceof AgendaItem)) {
217: return false;
218: }
219:
220: final AgendaItem otherItem = (AgendaItem) object;
221:
222: return (this .rule.equals(otherItem.getRule()) && this .tuple
223: .equals(otherItem.getTuple()));
224: }
225:
226: /**
227: * Return the hashode of the
228: * <code>TupleKey<code> as the hashCode of the AgendaItem
229: * @return
230: */
231: public int hashCode() {
232: return this .tuple.hashCode();
233: }
234:
235: public GroupElement getSubRule() {
236: return this .subrule;
237: }
238:
239: public void setLogicalDependencies(LinkedList justified) {
240: this.justified = justified;
241: }
242: }
|