[jboss-svn-commits] JBL Code SVN: r37004 - in labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590: drools-compiler/src/test/java/org/drools/core and 7 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat May 14 00:16:32 EDT 2011
Author: tsurdilovic
Date: 2011-05-14 00:16:31 -0400 (Sat, 14 May 2011)
New Revision: 37004
Added:
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/reteoo/
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/reteoo/BinaryHeapQueueTest.java
Removed:
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/SimpleAgendaGroup.java
Modified:
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/integrationtests/StreamsTest.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/core/util/BinaryHeapQueue.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/runtime/rule/impl/AgendaGroupImpl.java
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/guvnor-importer/.classpath
labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/sample-model/.classpath
Log:
BRMS-590: Need hotfix for BRMS-586 for BRMS 5.1.0.GA
Added: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/reteoo/BinaryHeapQueueTest.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/reteoo/BinaryHeapQueueTest.java (rev 0)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/reteoo/BinaryHeapQueueTest.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -0,0 +1,411 @@
+package org.drools.core.reteoo;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import static org.junit.Assert.*;
+
+import java.util.*;
+
+import junit.framework.TestCase;
+
+import org.drools.common.ActivationGroupNode;
+import org.drools.common.ActivationNode;
+import org.drools.common.InternalFactHandle;
+import org.drools.common.LogicalDependency;
+import org.drools.core.util.BinaryHeapQueue;
+import org.drools.core.util.LinkedList;
+import org.drools.core.util.Queueable;
+import org.drools.reteoo.LeftTuple;
+import org.drools.rule.GroupElement;
+import org.drools.rule.Rule;
+import org.drools.runtime.rule.FactHandle;
+import org.drools.spi.Activation;
+import org.drools.spi.AgendaGroup;
+import org.drools.spi.ConflictResolver;
+import org.drools.spi.PropagationContext;
+
+/**
+ * Thes test class uses auxiliary test classes in org.drools.util:
+ * Group and Item as a mock-up for the corresponding Agenda classes.
+ *
+ * The test testShuffled uses a sequence of shuffled Item arrays, inserts
+ * them in the "random" order, retracts and reinserts the Items at an even
+ * position. Finally, Items are retrieved and the order is checked.
+ *
+ * Experience has shown that at least 6 Items are required to demonstrate
+ * a certain bug, so don't reduce the max parameter.
+ *
+ */
+public class BinaryHeapQueueTest extends TestCase {
+
+ private List<Integer[]> perms = new ArrayList<Integer[]>();
+ private final static int max = 6;
+
+ // NOT really permutations, just some shuffling
+ private void shuffle(Integer[] a,
+ int lim) {
+ if ( lim == 0 ) {
+ Integer[] p = a.clone();
+ perms.add( p );
+ } else {
+ shuffle( a,
+ lim - 1 );
+ Integer h = a[lim];
+ a[lim] = a[lim - 1];
+ a[lim - 1] = h;
+ shuffle( a,
+ lim - 1 );
+ }
+ }
+
+ public void setUp() throws Exception {
+ System.out.println( "Running setup" );
+ Integer[] a = new Integer[max];
+ for ( int i = 0; i < max; i++ ) {
+ a[i] = i;
+ }
+ shuffle( a,
+ max - 1 );
+ // System.out.println( "The size is " + perms.size() );
+ }
+
+ public void testShuffled() {
+
+ for ( Integer[] perm : perms ) {
+ Group group = new Group( "group" );
+
+ for ( Integer i : perm ) {
+ Item item = new Item( group,
+ i );
+ group.add( item );
+ }
+
+ Queueable[] elems = group.getQueue();
+ for ( Queueable elem : elems ) {
+ Item item = (Item) elem;
+ // System.out.print( " " + item.getSalience() + "/" + item.getActivationNumber() + "/" + item.getIndex() );
+ if ( item.getIndex() % 2 == 0 ) {
+ group.remove( item );
+ group.add( item );
+ }
+ }
+ boolean ok = true;
+ StringBuilder sb = new StringBuilder( "queue:" );
+ for ( int i = max - 1; i >= 0; i-- ) {
+ int sal = group.getNext().getSalience();
+ sb.append( " " ).append( sal );
+ if ( sal != i ) ok = false;
+ }
+ assertTrue( "incorrect order in " + sb.toString(),
+ ok );
+ // System.out.println( sb.toString() );
+ }
+ }
+
+ public static class Group {
+
+ private static final long serialVersionUID = 510l;
+
+ private String name;
+
+ /** Items in the agenda. */
+ private BinaryHeapQueue queue;
+
+ /**
+ * Construct an <code>AgendaGroup</code> with the given name.
+ *
+ * @param name
+ * The <AgendaGroup> name.
+ */
+ public Group() {
+ }
+
+ public Group(final String name) {
+ this.name = name;
+ this.queue = new BinaryHeapQueue( ItemConflictResolver.INSTANCE );
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void clear() {
+ this.queue.clear();
+ }
+
+ /* (non-Javadoc)
+ * @see org.drools.spi.AgendaGroup#size()
+ */
+ public int size() {
+ return this.queue.size();
+ }
+
+ public void add(final Item item) {
+ this.queue.enqueue( (Queueable) item );
+ }
+
+ public Item getNext() {
+ return (Item) this.queue.dequeue();
+ }
+
+ /**
+ * Iterates a PriorityQueue removing empty entries until it finds a populated entry and return true,
+ * otherwise it returns false;
+ *
+ * @param priorityQueue
+ * @return
+ */
+ public boolean isEmpty() {
+ return this.queue.isEmpty();
+ }
+
+ public String toString() {
+ return "AgendaGroup '" + this.name + "'";
+ }
+
+ public boolean equal(final Object object) {
+ if ( (object == null) || !(object instanceof Group) ) {
+ return false;
+ }
+
+ if ( ((Group) object).name.equals( this.name ) ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public int hashCode() {
+ return this.name.hashCode();
+ }
+
+ public void remove(Item agendaItem) {
+ this.queue.dequeue( agendaItem.getIndex() );
+ }
+
+ public Queueable[] getQueue() {
+ return (Queueable[]) this.queue.toArray( new Queueable[size()] );
+ }
+ }
+
+ public static class IsTuple extends BaseMatcher<List<InternalFactHandle>> {
+ private final InternalFactHandle[] expected;
+
+ public IsTuple(List<InternalFactHandle> tupleAsList) {
+ expected = tupleAsList.toArray( new InternalFactHandle[tupleAsList.size()] );
+ }
+
+ public IsTuple(InternalFactHandle[] tuple) {
+ expected = tuple;
+ }
+
+ public boolean matches(Object arg) {
+ if ( arg == null || !(arg.getClass().isArray() && InternalFactHandle.class.isAssignableFrom( arg.getClass().getComponentType() )) ) {
+ return false;
+ }
+ InternalFactHandle[] actual = (InternalFactHandle[]) arg;
+ return Arrays.equals( expected,
+ actual );
+ }
+
+ public void describeTo(Description description) {
+ description.appendValue( expected );
+ }
+
+ /**
+ * Is the value equal to another value, as tested by the
+ * {@link java.lang.Object#equals} invokedMethod?
+ */
+ @Factory
+ public static Matcher<List<InternalFactHandle>> isTuple(List<InternalFactHandle> operand) {
+ return new IsTuple( operand );
+ }
+
+ public static Matcher< ? super List<InternalFactHandle>> isTuple(InternalFactHandle... operands) {
+ return new IsTuple( operands );
+ }
+ }
+
+ public static class Item
+ implements
+ Queueable,
+ Activation {
+
+ private static int actNo = 1;
+
+ private int index;
+ private long activationNumber;
+ private Group group;
+ private int salience;
+
+ public Item(Group group,
+ int salience) {
+ this.group = group;
+ this.salience = salience;
+ this.activationNumber = actNo++;
+ }
+
+ public void dequeue() {
+ if ( this.group != null ) {
+ this.group.remove( this );
+ }
+ this.index = -1;
+ }
+
+ public void enqueued(int index) {
+ this.index = index;
+ }
+
+ public int getIndex() {
+ return index;
+ }
+
+ public int getSalience() {
+ return salience;
+ }
+
+ public long getActivationNumber() {
+ return activationNumber;
+ }
+
+ public void addLogicalDependency(LogicalDependency arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public ActivationGroupNode getActivationGroupNode() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ActivationNode getActivationNode() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public AgendaGroup getAgendaGroup() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public LinkedList getLogicalDependencies() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public PropagationContext getPropagationContext() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Rule getRule() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public GroupElement getSubRule() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public LeftTuple getTuple() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public boolean isActivated() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ public void remove() {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setActivated(boolean arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setActivationGroupNode(ActivationGroupNode arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setActivationNode(ActivationNode arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setLogicalDependencies(LinkedList arg0) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public List<String> getDeclarationIDs() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Object getDeclarationValue(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public List< ? extends FactHandle> getFactHandles() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public List<Object> getObjects() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+ }
+
+ public static class ItemConflictResolver
+ implements
+ ConflictResolver {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 1L;
+ public static final ItemConflictResolver INSTANCE = new ItemConflictResolver();
+
+ public static ItemConflictResolver getInstance() {
+ return ItemConflictResolver.INSTANCE;
+ }
+
+ /**
+ * @see ConflictResolver
+ */
+ public final int compare(final Object existing,
+ final Object adding) {
+ return compare( (Item) existing,
+ (Item) adding );
+ }
+
+ public final int compare(final Item existing,
+ final Item adding) {
+ final int s1 = existing.getSalience();
+ final int s2 = adding.getSalience();
+
+ if ( s1 != s2 ) {
+ return s1 - s2;
+ }
+
+ // we know that no two activations will have the same number
+ return (int) (existing.getActivationNumber() - adding.getActivationNumber());
+ }
+
+ public int compare(Activation arg0,
+ Activation arg1) {
+ return 0;
+ }
+
+ }
+}
Property changes on: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/core/reteoo/BinaryHeapQueueTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/integrationtests/StreamsTest.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/integrationtests/StreamsTest.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-compiler/src/test/java/org/drools/integrationtests/StreamsTest.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -429,6 +429,6 @@
ksession.fireAllRules();
assertThat( ksession.getObjects().size(),
- equalTo( 0 ) );
+ equalTo( 2 ) );
}
}
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/ArrayAgendaGroup.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -24,6 +24,7 @@
import org.drools.core.util.Iterator;
import org.drools.core.util.LinkedList;
import org.drools.core.util.LinkedListEntry;
+import org.drools.core.util.Queueable;
import org.drools.spi.Activation;
/**
@@ -119,6 +120,12 @@
public int size() {
return this.size;
}
+
+ public Activation[] getAndClear() {
+ Activation[] queue = getActivations();
+ clear();
+ return queue;
+ }
public void add(final Activation activation) {
AgendaItem item = (AgendaItem) activation;
@@ -196,10 +203,6 @@
return activations;
}
- public Activation[] getQueue() {
- return getActivations();
- }
-
public String toString() {
return "AgendaGroup '" + this.name + "'";
}
@@ -228,3 +231,4 @@
throw new UnsupportedOperationException();
}
}
+
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/BinaryHeapQueueAgendaGroup.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -89,6 +89,10 @@
public void clear() {
this.queue.clear();
}
+
+ public Activation[] getAndClear() {
+ return this.queue.getAndClear();
+ }
/* (non-Javadoc)
* @see org.drools.spi.AgendaGroup#size()
@@ -128,10 +132,6 @@
return (Activation[]) this.queue.toArray( new AgendaItem[this.queue.size()] );
}
- public Activation[] getQueue() {
- return this.queue.getQueueable();
- }
-
public String toString() {
return "AgendaGroup '" + this.name + "'";
}
@@ -160,3 +160,4 @@
this.queue.dequeue( agendaItem.getIndex() );
}
}
+
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/DefaultAgenda.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/DefaultAgenda.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -722,7 +722,9 @@
public void clearAndCancelAgendaGroup(final AgendaGroup agendaGroup) {
final EventSupport eventsupport = (EventSupport) this.workingMemory;
- final Activation[] queueable = ((InternalAgendaGroup) agendaGroup).getQueue();
+ // this is thread safe for BinaryHeapQueue
+ // Binary Heap locks while it returns the array and reset's it's own internal array. Lock is released afer getAndClear()
+ final Activation[] queueable = ((InternalAgendaGroup) agendaGroup).getAndClear();
for ( int i = 0, length = queueable.length; i < length; i++ ) {
final AgendaItem item = (AgendaItem) queueable[i];
if ( item == null ) {
@@ -747,7 +749,6 @@
this.workingMemory,
ActivationCancelledCause.CLEAR );
}
- ((InternalAgendaGroup) agendaGroup).clear();
}
/*
@@ -924,33 +925,32 @@
this.workingMemory,
e );
} else if ( this.consequenceExceptionHandler != null ) {
- this.consequenceExceptionHandler.handleException( activation,
- new StatefulKnowledgeSessionImpl( (ReteooWorkingMemory) this.workingMemory ),
+ this.consequenceExceptionHandler.handleException( activation, this.workingMemory.getKnowledgeRuntime(),
e );
} else {
throw new RuntimeException( e );
}
- }
-
- if( ruleFlowGroup != null ) {
- ruleFlowGroup.deactivateIfEmpty();
- }
-
- // if the tuple contains expired events
- for ( LeftTuple tuple = (LeftTuple) activation.getTuple(); tuple != null; tuple = tuple.getParent() ) {
- if ( tuple.getLastHandle().isEvent() ) {
- EventFactHandle handle = (EventFactHandle) tuple.getLastHandle();
- // handles "expire" only in stream mode.
- if ( handle.isExpired() ) {
+ } finally {
+ // if the tuple contains expired events
+ for ( LeftTuple tuple = (LeftTuple) activation.getTuple(); tuple != null; tuple = tuple.getParent() ) {
+ if ( tuple.getLastHandle().isEvent() ) {
+ EventFactHandle handle = (EventFactHandle) tuple.getLastHandle();
// decrease the activation count for the event
handle.decreaseActivationsCount();
- if ( handle.getActivationsCount() == 0 ) {
- // and if no more activations, retract the handle
- handle.getEntryPoint().retract( handle );
+ // handles "expire" only in stream mode.
+ if ( handle.isExpired() ) {
+ if ( handle.getActivationsCount() <= 0 ) {
+ // and if no more activations, retract the handle
+ handle.getEntryPoint().retract( handle );
+ }
}
}
}
}
+
+ if( ruleFlowGroup != null ) {
+ ruleFlowGroup.deactivateIfEmpty();
+ }
eventsupport.getAgendaEventSupport().fireAfterActivationFired( activation,
this.workingMemory );
@@ -1104,3 +1104,4 @@
return this.legacyConsequenceExceptionHandler;
}
}
+
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/InternalAgendaGroup.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -16,6 +16,7 @@
package org.drools.common;
+import org.drools.core.util.Queueable;
import org.drools.spi.Activation;
import org.drools.spi.AgendaGroup;
@@ -26,7 +27,7 @@
public void setActive(boolean activate);
- public Activation[] getQueue();
+ public Activation[] getAndClear();
public void clear();
Deleted: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/SimpleAgendaGroup.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/SimpleAgendaGroup.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/common/SimpleAgendaGroup.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -1,277 +0,0 @@
-/**
- * Copyright 2005 JBoss Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.drools.common;
-
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.PriorityQueue;
-
-import org.drools.core.util.AbstractBaseLinkedListNode;
-import org.drools.core.util.BinaryHeapQueue;
-import org.drools.core.util.LinkedList;
-import org.drools.core.util.Queueable;
-import org.drools.spi.Activation;
-
-/**
- * <code>AgendaGroup</code> implementation that uses a
- * <code>ActivationQueue</code>s. The <code>AgendaGroup</code> also maintains a
- * <code>Map</code> of <code>ActivationQueues</code> for requested salience
- * values.
- *
- * @see PriorityQueue
- * @see ActivationQueue
- *
- * @author <a href="mailto:mark.proctor at jboss.com">Mark Proctor</a>
- * @author <a href="mailto:bob at werken.com">Bob McWhirter</a>
- *
- */
-public class SimpleAgendaGroup
- implements
- InternalAgendaGroup {
-
- private static final long serialVersionUID = 510l;
-
- private String name;
-
- /** Items in the agenda. */
- private LinkedList salienceGroups;
-
- private int size;
-
- private boolean active;
-
- public static class SalienceGroup extends AbstractBaseLinkedListNode {
- private int salience;
- private LinkedList list;
-
- public SalienceGroup(int salience) {
- this.salience = salience;
- this.list = new LinkedList();
- }
-
- public int getSalience() {
- return salience;
- }
-
- public LinkedList getList() {
- return this.list;
- }
- }
-
- /**
- * Construct an <code>AgendaGroup</code> with the given name.
- *
- * @param name
- * The <AgendaGroup> name.
- */
- public SimpleAgendaGroup() {
-
- }
-
- public SimpleAgendaGroup(final String name,
- final InternalRuleBase ruleBase) {
- this.name = name;
- this.salienceGroups = new LinkedList();
- }
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- name = (String) in.readObject();
- //queue = (BinaryHeapQueue) in.readObject();
- active = in.readBoolean();
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeObject( name );
- //out.writeObject(queue);
- out.writeBoolean( active );
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.drools.spi.AgendaGroup#getName()
- */
- public String getName() {
- return this.name;
- }
-
- public void clear() {
- this.salienceGroups.clear();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.drools.spi.AgendaGroup#size()
- */
- public int size() {
- return this.size;
- }
-
- public void add(final Activation activation) {
- int salience = activation.getSalience();
- SalienceGroup group = null;
-
- if ( !this.salienceGroups.isEmpty() ) {
- SalienceGroup lastGroup = (SalienceGroup) this.salienceGroups.getLast();
-
- // optimisation to find out it's the lowest, and thus add get or add it.
- if ( salience <= lastGroup.getSalience() ) {
- if ( salience == lastGroup.getSalience() ) {
- // get
- group = (SalienceGroup) lastGroup;
- } else {
- // create and add
- SalienceGroup newGroup = new SalienceGroup( salience );
- this.salienceGroups.insertAfter( lastGroup, newGroup );
- group = newGroup;
- }
- //add or get to end
- }
-
-
- if ( group == null ) {
- // we know this won't iterate to the end returning null, as we checked the end already.
- for ( group = (SalienceGroup) this.salienceGroups.getFirst(); group != null && salience < group.getSalience(); group = (SalienceGroup) group.getNext() ) {
- }
-
- if ( salience == group.getSalience() ) {
- // get
- group = (SalienceGroup) group;
- } else {
- // create and add before, as must be larger
- SalienceGroup newGroup = new SalienceGroup( salience );
- this.salienceGroups.insertAfter( group.getPrevious(), newGroup );
- group = newGroup;
- }
- }
-
- } else {
- //no groups so add
- SalienceGroup newGroup = new SalienceGroup( salience );
- this.salienceGroups.add( newGroup );
- group = newGroup;
- }
-
- group.getList().add( new ActivationNode( activation,
- this ) );
-
- }
-
- public void remove(AgendaItem agendaItem) {
- int salience = agendaItem.getSalience();
-
- SalienceGroup group = null;
-
- SalienceGroup lastGroup = (SalienceGroup) this.salienceGroups.getLast();
-
- // optimisation to find out it's the lowest, and thus add get or add it.
- if ( salience == lastGroup.getSalience() ) {
- group = lastGroup;
- } else {
- // don't check for !
- for ( group = (SalienceGroup) this.salienceGroups.getFirst(); group != null && group.getSalience() != salience; group = (SalienceGroup) group.getNext() ) {
- }
- }
-
- if ( group == null ) {
- throw new RuntimeException( "SalienceGroup does not exist, This should not be possible." );
- }
-
- group.getList().remove( agendaItem.getActivationNode() );
- }
-
- public Activation getNext() {
- SalienceGroup group = (SalienceGroup) this.salienceGroups.getFirst();
-
- while ( !this.salienceGroups.isEmpty() && ( group == null || ( group != null && group.getList().isEmpty() ) ) ) {
- this.salienceGroups.removeFirst();
- group = (SalienceGroup) this.salienceGroups.getFirst();
- }
-
- if ( group != null ) {
- ActivationNode node = (ActivationNode) group.getList().removeFirst();
- if ( group.getList().isEmpty() ) {
- this.salienceGroups.removeFirst();
- }
-
- return node.getActivation();
-
- }
-
- return null;
- }
-
- public boolean isActive() {
- return this.active;
- }
-
- public void setActive(final boolean activate) {
- this.active = activate;
- }
-
- /**
- * Iterates a PriorityQueue removing empty entries until it finds a
- * populated entry and return true, otherwise it returns false;
- *
- * @param priorityQueue
- * @return
- */
- public boolean isEmpty() {
- return this.salienceGroups.isEmpty();
- }
-
- public Activation[] getActivations() {
- return null;
- // return (Activation[]) this.queue.toArray(new AgendaItem[this.queue
- // .size()]);
- }
-
- public Activation[] getQueue() {
- return null;
- //return this.queue.getQueueable();
- }
-
- public String toString() {
- return "AgendaGroup '" + this.name + "'";
- }
-
- public boolean equal(final Object object) {
- if ( (object == null) || !(object instanceof SimpleAgendaGroup) ) {
- return false;
- }
-
- if ( ((SimpleAgendaGroup) object).name.equals( this.name ) ) {
- return true;
- }
-
- return false;
- }
-
- public int hashCode() {
- return this.name.hashCode();
- }
-
- public void setFocus() {
- throw new UnsupportedOperationException();
- }
-
-}
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/core/util/BinaryHeapQueue.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/core/util/BinaryHeapQueue.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/core/util/BinaryHeapQueue.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,13 +16,34 @@
package org.drools.core.util;
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Comparator;
import java.util.NoSuchElementException;
+import java.util.concurrent.locks.ReentrantLock;
+import org.drools.spi.Activation;
+
public class BinaryHeapQueue
implements
Queue,
@@ -38,6 +59,8 @@
/** The elements in this heap. */
private Queueable[] elements;
+
+ private ReentrantLock lock;
public BinaryHeapQueue() {
@@ -71,6 +94,7 @@
//+1 as 0 is noop
this.elements = new Queueable[capacity + 1];
this.comparator = comparator;
+ this.lock = new ReentrantLock();
}
//-----------------------------------------------------------------------
@@ -90,9 +114,26 @@
* Clears all elements from queue.
*/
public void clear() {
- this.elements = new Queueable[this.elements.length]; // for gc
- this.size = 0;
+ try {
+ this.lock.lock();
+ this.elements = new Queueable[this.elements.length]; // for gc
+ this.size = 0;
+ } finally {
+ this.lock.unlock();
+ }
}
+
+ public Activation[] getAndClear() {
+ try {
+ this.lock.lock();
+ Activation[] queue = ( Activation[] )this.elements;
+ this.elements = new Queueable[this.elements.length]; // for gc
+ this.size = 0;
+ return queue;
+ } finally {
+ this.lock.unlock();
+ }
+ }
/**
* Tests if queue is empty.
@@ -129,12 +170,17 @@
*
* @param element the Queueable to be inserted
*/
- public synchronized void enqueue(final Queueable element) {
- if ( isFull() ) {
- grow();
+ public void enqueue(final Queueable element) {
+ try {
+ this.lock.lock();
+ if ( isFull() ) {
+ grow();
+ }
+
+ percolateUpMaxHeap( element );
+ } finally {
+ this.lock.unlock();
}
-
- percolateUpMaxHeap( element );
}
/**
@@ -143,56 +189,56 @@
* @return the Queueable at top of heap
* @throws NoSuchElementException if <code>isEmpty() == true</code>
*/
- public synchronized Queueable dequeue() throws NoSuchElementException {
- if ( isEmpty() ) {
- return null;
- }
-
- final Queueable result = this.elements[1];
- result.dequeue();
-
- // Code bellow was removed because it is already executed
- // inside result.dequeue()
- //
- // setElement(1, this.elements[this.size--]);
- // this.elements[this.size + 1] = null;
- //
- // if (this.size != 0) {
- // percolateDownMinHeap(1);
- // }
-
- return result;
+ public Queueable dequeue() throws NoSuchElementException {
+ try {
+ this.lock.lock();
+ if ( isEmpty() ) {
+ return null;
+ }
+
+ final Queueable result = this.elements[1];
+ result.dequeue();
+
+ return result;
+ } finally {
+ this.lock.unlock();
+ }
}
/**
*
* @param index
*/
- public synchronized Queueable dequeue(final int index) {
- if ( index < 1 || index > this.size ) {
- //throw new NoSuchElementException();
- return null;
- }
-
- final Queueable result = this.elements[index];
- setElement( index,
- this.elements[this.size] );
- this.elements[this.size] = null;
- this.size--;
- if ( this.size != 0 && index <= this.size ) {
- int compareToParent = 0;
- if ( index > 1 ) {
- compareToParent = compare( this.elements[index],
- this.elements[index / 2] );
+ public Queueable dequeue(final int index) {
+ try {
+ this.lock.lock();
+ if ( index < 1 || index > this.size ) {
+ //throw new NoSuchElementException();
+ return null;
}
- if ( index > 1 && compareToParent < 0 ) {
- percolateUpMaxHeap( index );
- } else {
- percolateDownMaxHeap( index );
+
+ final Queueable result = this.elements[index];
+ setElement( index,
+ this.elements[this.size] );
+ this.elements[this.size] = null;
+ this.size--;
+ if ( this.size != 0 && index <= this.size ) {
+ int compareToParent = 0;
+ if ( index > 1 ) {
+ compareToParent = compare( this.elements[index],
+ this.elements[index / 2] );
+ }
+ if ( index > 1 && compareToParent > 0 ) {
+ percolateUpMaxHeap( index );
+ } else {
+ percolateDownMaxHeap( index );
+ }
}
- }
-
- return result;
+
+ return result;
+ } finally {
+ this.lock.unlock();
+ }
}
// /**
@@ -373,36 +419,29 @@
element.enqueued( index );
}
- public Queueable[] getQueueable() {
- return this.elements;
- }
-
- public Object[] toArray() {
- final Object[] result = new Object[this.size];
- System.arraycopy( this.elements,
- 1,
- result,
- 0,
- this.size );
- return result;
- }
-
public Object[] toArray(Object a[]) {
- if ( a.length < this.size ) {
- a = (Object[]) java.lang.reflect.Array.newInstance( a.getClass().getComponentType(),
- this.size );
+ try {
+ this.lock.lock();
+
+ if ( a.length < this.size ) {
+ a = (Object[]) java.lang.reflect.Array.newInstance( a.getClass().getComponentType(),
+ this.size );
+ }
+
+ System.arraycopy( this.elements,
+ 1,
+ a,
+ 0,
+ this.size );
+
+ if ( a.length > this.size ) {
+ a[this.size] = null;
+ }
+
+ return a;
+ } finally {
+ this.lock.unlock();
}
-
- System.arraycopy( this.elements,
- 1,
- a,
- 0,
- this.size );
-
- if ( a.length > this.size ) {
- a[this.size] = null;
- }
-
- return a;
}
}
+
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/runtime/rule/impl/AgendaGroupImpl.java
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/runtime/rule/impl/AgendaGroupImpl.java 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-core/src/main/java/org/drools/runtime/rule/impl/AgendaGroupImpl.java 2011-05-14 04:16:31 UTC (rev 37004)
@@ -1,63 +1,71 @@
-/**
- * Copyright 2010 JBoss Inc
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.drools.runtime.rule.impl;
-
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-
-import org.drools.common.InternalAgenda;
-import org.drools.runtime.rule.AgendaGroup;
-
-public class AgendaGroupImpl implements AgendaGroup, Externalizable {
-
- private String name;
-
- private InternalAgenda agenda;
-
- AgendaGroupImpl() {
-
- }
-
- AgendaGroupImpl(org.drools.spi.AgendaGroup agendaGroup, InternalAgenda agenda) {
- this.name = agendaGroup.getName();
- this.agenda = agenda;
- }
-
- public void writeExternal(ObjectOutput out) throws IOException {
- out.writeUTF( this.name );
- }
-
- public void readExternal(ObjectInput in) throws IOException,
- ClassNotFoundException {
- this.name = in.readUTF();
- }
-
- public String getName() {
- return this.name;
- }
-
- public void clear() {
- this.agenda.clearAndCancelAgendaGroup( this.name );
- }
-
- public void setFocus() {
- this.agenda.setFocus( this.name );
- }
-
-}
+/**
+ * Copyright 2010 JBoss Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.drools.runtime.rule.impl;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.drools.common.InternalAgenda;
+import org.drools.runtime.rule.Activation;
+import org.drools.runtime.rule.AgendaGroup;
+
+public class AgendaGroupImpl implements AgendaGroup, Externalizable {
+
+ private String name;
+
+ private InternalAgenda agenda;
+
+ AgendaGroupImpl() {
+
+ }
+
+ AgendaGroupImpl(org.drools.spi.AgendaGroup agendaGroup, InternalAgenda agenda) {
+ this.name = agendaGroup.getName();
+ this.agenda = agenda;
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeUTF( this.name );
+ }
+
+ public void readExternal(ObjectInput in) throws IOException,
+ ClassNotFoundException {
+ this.name = in.readUTF();
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public void clear() {
+ this.agenda.clearAndCancelAgendaGroup( this.name );
+ }
+
+ public void setFocus() {
+ this.agenda.setFocus( this.name );
+ }
+
+// public Collection<Activation> getActivations() {
+// return this.agenda.getAgendaGroup( this.name ).getActivations();
+//}
+
+
+}
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/guvnor-importer/.classpath
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/guvnor-importer/.classpath 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/guvnor-importer/.classpath 2011-05-14 04:16:31 UTC (rev 37004)
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
- <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
- <classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
- <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
Modified: labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/sample-model/.classpath
===================================================================
--- labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/sample-model/.classpath 2011-05-14 03:48:10 UTC (rev 37003)
+++ labs/jbossrules/soa_branches/BRMS-5.1-GA_BRMS-590/drools-guvnor/bulk-importer-util/sample-model/.classpath 2011-05-14 04:16:31 UTC (rev 37004)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
More information about the jboss-svn-commits
mailing list