![JAR search and dependency download from the Maven repository](/logo.png)
jadex.rules.state.javaimpl.RingBufferQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-rules Show documentation
Show all versions of jadex-rules Show documentation
Jadex Rules is a small lightweight rule engine, which currently
employs the well-known Rete algorithm for highly efficient rule
matching. Jadex rules is therefore similar to other rule engines
like JESS and Drools. Despite the similarities there are also
important differences between these systems:
* Jadex Rules is very small and
intended to be used as component
of other software. Even though rules can be specified in a Java
dialect as well as (a small variation of) the CLIPS language
its primary usage is on the API level. Jadex Rules is currently
the core component of the Jadex BDI reasoning engine.
* Jadex Rules cleanly separates between state and rule representation.
This allows the state implementation as well as the matcher to be
flexibly exchanged. Some experiments have e.g. been conducted with
a Jena representation. Regarding the matcher, it is planned to
support also the Treat algorithm, which has a lower memory footprint
than Rete.
* Jadex Rules pays close attention to rule debugging. The state as
well as the rete engine can be observed at runtime. The rule debugger
provides functionalities to execute a rule program stepwise and also
use rule breakpoints to stop the execution at those points.
package jadex.rules.state.javaimpl;
/**
* Container for queue attribute values.
*/
class RingBufferQueue
{
//-------- constants --------
/** The default inital capacity of the agenda buffer. */
protected static final int DEFAULT_INITIAL_CAPACITY = 16;
//-------- attributes --------
/**
* The list of values.
* For efficiency we use a custom non-blocking, non-synchronized queue.
* The implementation is based on an array used as buffer, an integer for
* the current size, and a cursor indicating the current start of the
* queue. When the cursor moves beyond the end of the array it starts
* again from the beginning. When the size gets larger than the buffer,
* the array will be resized. Shrinking of the array has not been
* implemented, and is probably not needed(?).
*/
protected Object[] values;
/** The size of the queue. */
protected int size;
/** The pointer to the currently first value. */
protected int current;
//-------- constructors --------
/**
* Create a new ring buffer queue.
*/
public RingBufferQueue()
{
this.values = new Object[DEFAULT_INITIAL_CAPACITY];
this.size = 0;
this.current = 0;
}
//-------- methods --------
/**
* Remove the first value from the queue, if any.
* @return The first value from the queue, or null if the queue is empty.
*/
public Object removeFirstValue()
{
Object ret = null;
// Check if there is a current value.
if(size>0)
{
// Save the return value.
ret = values[current];
// Move to next element in buffer.
values[current] = null; // To allow garbage collection.
current = (current+1)%values.length;
size--;
}
return ret;
}
/**
* Add a value to the queue.
* @param value The value to add.
*/
public void addValue(Object value)
{
// Resize buffer, if necessary.
if(size==values.length)
{
// Create new buffer with doubled size.
Object[] newvalues = new Object[values.length*2];
// Copy into new buffer the elements from cursor to the end of the old buffer.
System.arraycopy(values, current, newvalues, 0, values.length-current);
// When there ere elements to the left of the cursor...
if(current>0)
{
// Copy remaining elements to new buffer.
System.arraycopy(values, 0, newvalues, values.length-current, current);
}
// Switch to new buffer, which starts at index 0.
values = newvalues;
current = 0;
// System.out.println("New buffersize: "+values.length);
}
// Add entry to end of the buffer, and increment size.
this.values[(current+(size++))%values.length] = value;
}
/**
* Test if the queue is empty.
* @return True, if the queue is empty.
*/
public boolean isEmpty()
{
return size==0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy