001: package org.drools.base;
002:
003: import org.drools.FactHandle;
004: import org.drools.WorkingMemory;
005: import org.drools.spi.JavaFact;
006:
007: public class DelegateJavaFactHandler implements JavaFact {
008: private JavaFactRegistryEntry[] entries;
009:
010: public JavaFactRegistryEntry[] listWorkingMemories() {
011: return this .entries;
012: }
013:
014: public boolean register(final WorkingMemory workingMemory) {
015: if (workingMemory == null) {
016: return false;
017: }
018:
019: if (isRegistered(workingMemory)) {
020: return false;
021: }
022:
023: final JavaFactRegistryEntry[] newEntries;
024: int position;
025: if (this .entries == null) {
026: newEntries = new JavaFactRegistryEntry[1];
027: position = 0;
028: } else {
029: final int newLength = this .entries.length + 1;
030: newEntries = new JavaFactRegistryEntry[newLength];
031: System.arraycopy(this .entries, 0, newEntries, 0,
032: newLength - 1);
033: position = this .entries.length;
034: }
035:
036: final FactHandle handle = workingMemory.insert(this );
037:
038: newEntries[position] = new JavaFactRegistryEntry(workingMemory,
039: handle);
040:
041: this .entries = newEntries;
042: return true;
043: }
044:
045: public void unregisterAll() {
046: for (int i = 0, length = this .entries.length; i < length; i++) {
047: final WorkingMemory workingMemory = this .entries[i]
048: .getWorkingMemory();
049: final FactHandle handle = this .entries[i].getFactHandle();
050: workingMemory.retract(handle);
051: }
052:
053: }
054:
055: public boolean unregister(final WorkingMemory workingMemory) {
056: if (this .entries == null) {
057: return false;
058: }
059:
060: // If there is only one entry, see if it matched and if so null
061: if (this .entries.length == 1
062: && this .entries[0].getWorkingMemory() == workingMemory) {
063: this .entries = null;
064: return true;
065: }
066:
067: // try the first
068: if (this .entries[0].getWorkingMemory() == workingMemory) {
069: final JavaFactRegistryEntry[] newEntries = new JavaFactRegistryEntry[this .entries.length - 1];
070: System.arraycopy(this .entries, 1, newEntries, 0,
071: newEntries.length);
072: this .entries = newEntries;
073: return true;
074: }
075:
076: // try the last
077: if (this .entries[this .entries.length - 1].getWorkingMemory() == workingMemory) {
078: final JavaFactRegistryEntry[] newEntries = new JavaFactRegistryEntry[this .entries.length - 1];
079: System.arraycopy(this .entries, 0, newEntries, 0,
080: newEntries.length);
081: this .entries = newEntries;
082: return true;
083: }
084:
085: // try middle
086: for (int i = 0, length = this .entries.length; i < length; i++) {
087: if (this .entries[i].getWorkingMemory() == workingMemory) {
088: final JavaFactRegistryEntry[] newEntries = new JavaFactRegistryEntry[this .entries.length - 1];
089: System.arraycopy(this .entries, 0, newEntries, 0, i);
090: System.arraycopy(this .entries, i + 1, newEntries, i,
091: newEntries.length - 1);
092: this .entries = newEntries;
093: return true;
094: }
095: }
096: return false;
097: }
098:
099: public boolean isRegistered(final WorkingMemory workingMemory) {
100: if (this .entries == null) {
101: return false;
102: }
103:
104: for (int i = 0, length = this .entries.length; i < length; i++) {
105: if (this .entries[i].getWorkingMemory() == workingMemory) {
106: return true;
107: }
108: }
109: return false;
110: }
111:
112: public int[] getChanges() {
113: // TODO Auto-generated method stub
114: return null;
115: }
116:
117: }
|