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