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 org.drools.conflict.DepthConflictResolver;
020: import org.drools.spi.Activation;
021: import org.drools.spi.AgendaGroup;
022: import org.drools.spi.ConflictResolver;
023: import org.drools.util.BinaryHeapQueue;
024: import org.drools.util.Queueable;
025:
026: /**
027: * <code>AgendaGroup</code> implementation that uses a <code>PriorityQueue</code> to prioritise the evaluation of added
028: * <code>ActivationQueue</code>s. The <code>AgendaGroup</code> also maintains a <code>Map</code> of <code>ActivationQueues</code>
029: * for requested salience values.
030: *
031: * @see PriorityQueue
032: * @see ActivationQueue
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: */
038: public class BinaryHeapQueueAgendaGroup implements InternalAgendaGroup {
039:
040: private static final long serialVersionUID = 400L;
041:
042: private final String name;
043:
044: /** Items in the agenda. */
045: private final BinaryHeapQueue queue;
046:
047: private boolean active;
048:
049: /**
050: * Construct an <code>AgendaGroup</code> with the given name.
051: *
052: * @param name
053: * The <AgendaGroup> name.
054: */
055:
056: public BinaryHeapQueueAgendaGroup(final String name,
057: final InternalRuleBase ruleBase) {
058: this .name = name;
059: this .queue = new BinaryHeapQueue(ruleBase.getConfiguration()
060: .getConflictResolver());
061: }
062:
063: /* (non-Javadoc)
064: * @see org.drools.spi.AgendaGroup#getName()
065: */
066: public String getName() {
067: return this .name;
068: }
069:
070: public void clear() {
071: this .queue.clear();
072: }
073:
074: /* (non-Javadoc)
075: * @see org.drools.spi.AgendaGroup#size()
076: */
077: public int size() {
078: return this .queue.size();
079: }
080:
081: public void add(final Activation activation) {
082: this .queue.enqueue((Queueable) activation);
083: }
084:
085: public Activation getNext() {
086: return (Activation) this .queue.dequeue();
087: }
088:
089: public boolean isActive() {
090: return this .active;
091: }
092:
093: public void setActive(final boolean activate) {
094: this .active = activate;
095: }
096:
097: /**
098: * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
099: * otherwise it returns false;
100: *
101: * @param priorityQueue
102: * @return
103: */
104: public boolean isEmpty() {
105: return this .queue.isEmpty();
106: }
107:
108: public Activation[] getActivations() {
109: return (Activation[]) this .queue
110: .toArray(new AgendaItem[this .queue.size()]);
111: }
112:
113: public Activation[] getQueue() {
114: return this .queue.getQueueable();
115: }
116:
117: public String toString() {
118: return "AgendaGroup '" + this .name + "'";
119: }
120:
121: public boolean equal(final Object object) {
122: if ((object == null)
123: || !(object instanceof BinaryHeapQueueAgendaGroup)) {
124: return false;
125: }
126:
127: if (((BinaryHeapQueueAgendaGroup) object).name
128: .equals(this .name)) {
129: return true;
130: }
131:
132: return false;
133: }
134:
135: public int hashCode() {
136: return this.name.hashCode();
137: }
138: }
|