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:
021: import org.drools.rule.GroupElement;
022: import org.drools.rule.Rule;
023: import org.drools.spi.Activation;
024: import org.drools.spi.AgendaGroup;
025: import org.drools.spi.PropagationContext;
026: import org.drools.spi.Tuple;
027: import org.drools.util.LinkedList;
028: import org.drools.util.Queue;
029: import org.drools.util.Queueable;
030:
031: /**
032: * Item entry in the <code>Agenda</code>.
033: *
034: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
035: * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
036: */
037: public class AgendaItem implements Activation, Queueable, Serializable {
038: // ------------------------------------------------------------
039: // Instance members
040: // ------------------------------------------------------------
041:
042: /**
043: *
044: */
045: private static final long serialVersionUID = 400L;
046:
047: /** The tuple. */
048: private final Tuple tuple;
049:
050: /** The rule. */
051: private final Rule rule;
052:
053: /** The salience */
054: private final int salience;
055:
056: /** Used for sequential mode */
057: private int sequenence;
058:
059: /** The subrule */
060: private final GroupElement subrule;
061:
062: /** The propagation context */
063: private final PropagationContext context;
064:
065: /** The activation number */
066: private final long activationNumber;
067:
068: /** A reference to the PriorityQeue the item is on */
069: private Queue queue;
070:
071: private int index;
072:
073: private LinkedList justified;
074:
075: private boolean activated;
076:
077: private InternalAgendaGroup agendaGroup;
078:
079: private ActivationGroupNode activationGroupNode;
080:
081: private RuleFlowGroupNode ruleFlowGroupNode;
082:
083: // ------------------------------------------------------------
084: // Constructors
085: // ------------------------------------------------------------
086:
087: /**
088: * Construct.
089: *
090: * @param tuple
091: * The tuple.
092: * @param rule
093: * The rule.
094: */
095: public AgendaItem(final long activationNumber, final Tuple tuple,
096: final int salience, final PropagationContext context,
097: final Rule rule, final GroupElement subrule) {
098: this .tuple = tuple;
099: this .context = context;
100: this .rule = rule;
101: this .salience = salience;
102: this .subrule = subrule;
103: this .activationNumber = activationNumber;
104: }
105:
106: // ------------------------------------------------------------
107: // Instance methods
108: // ------------------------------------------------------------
109: public PropagationContext getPropagationContext() {
110: return this .context;
111: }
112:
113: /**
114: * Retrieve the rule.
115: *
116: * @return The rule.
117: */
118: public Rule getRule() {
119: return this .rule;
120: }
121:
122: /**
123: * Retrieve the tuple.
124: *
125: * @return The tuple.
126: */
127: public Tuple getTuple() {
128: return this .tuple;
129: }
130:
131: public int getSalience() {
132: return this .salience;
133: }
134:
135: public int getSequenence() {
136: return sequenence;
137: }
138:
139: public void setSequenence(int sequenence) {
140: this .sequenence = sequenence;
141: }
142:
143: /*
144: * (non-Javadoc)
145: *
146: * @see org.drools.spi.Activation#getActivationNumber()
147: */
148: public long getActivationNumber() {
149: return this .activationNumber;
150: }
151:
152: public void addLogicalDependency(final LogicalDependency node) {
153: if (this .justified == null) {
154: this .justified = new LinkedList();
155: }
156:
157: this .justified.add(node);
158: }
159:
160: public LinkedList getLogicalDependencies() {
161: return this .justified;
162: }
163:
164: public void setLogicalDependencies(LinkedList justified) {
165: this .justified = justified;
166: }
167:
168: public boolean isActivated() {
169: return this .activated;
170: }
171:
172: public void setActivated(final boolean activated) {
173: this .activated = activated;
174: }
175:
176: public String toString() {
177: return "[Activation rule=" + this .rule.getName() + ", tuple="
178: + this .tuple + "]";
179: }
180:
181: /*
182: * (non-Javadoc)
183: *
184: * @see java.lang.Object#equals(java.lang.Object)
185: */
186: public boolean equals(final Object object) {
187: if (object == this ) {
188: return true;
189: }
190:
191: if (object == null || !(object instanceof AgendaItem)) {
192: return false;
193: }
194:
195: final AgendaItem otherItem = (AgendaItem) object;
196:
197: return (this .rule.equals(otherItem.getRule()) && this .tuple
198: .equals(otherItem.getTuple()));
199: }
200:
201: /**
202: * Return the hashCode of the
203: * <code>TupleKey<code> as the hashCode of the AgendaItem
204: * @return
205: */
206: public int hashCode() {
207: return this .tuple.hashCode();
208: }
209:
210: public void enqueued(final Queue queue, final int index) {
211: this .queue = queue;
212: this .index = index;
213: }
214:
215: public void dequeue() {
216: if (this .queue != null) {
217: // will only be null if the AgendaGroup is locked when the activation add was attempted
218: this .queue.dequeue(this .index);
219: }
220: this .activated = false;
221: }
222:
223: public void remove() {
224: dequeue();
225: }
226:
227: public ActivationGroupNode getActivationGroupNode() {
228: return this .activationGroupNode;
229: }
230:
231: public void setActivationGroupNode(
232: final ActivationGroupNode activationNode) {
233: this .activationGroupNode = activationNode;
234: }
235:
236: public AgendaGroup getAgendaGroup() {
237: return this .agendaGroup;
238: }
239:
240: public void setAgendaGroup(final InternalAgendaGroup agendaGroup) {
241: this .agendaGroup = agendaGroup;
242: }
243:
244: public RuleFlowGroupNode getRuleFlowGroupNode() {
245: return this .ruleFlowGroupNode;
246: }
247:
248: public void setRuleFlowGroupNode(
249: final RuleFlowGroupNode ruleFlowGroupNode) {
250: this .ruleFlowGroupNode = ruleFlowGroupNode;
251: }
252:
253: public GroupElement getSubRule() {
254: return this.subrule;
255: }
256: }
|