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: /**
020: * The base class for all Rete nodes.
021: *
022: * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
023: * @author <a href="mailto:bob@werken.com">Bob McWhirter</a>
024: *
025: */
026: public abstract class BaseNode implements NetworkNode {
027: protected final int id;
028:
029: protected boolean hasMemory = false;
030:
031: protected int sharedCount = 0;
032:
033: /**
034: * All nodes have a unique id, set in the constructor.
035: *
036: * @param id
037: * The unique id
038: */
039: public BaseNode(final int id) {
040: super ();
041: this .id = id;
042: }
043:
044: /* (non-Javadoc)
045: * @see org.drools.spi.ReteooNode#getId()
046: */
047: public int getId() {
048: return this .id;
049: }
050:
051: /**
052: * Specifies with the node has any memory.
053: *
054: * @param hasMemory
055: */
056: protected void setHasMemory(final boolean hasMemory) {
057: this .hasMemory = hasMemory;
058: }
059:
060: /**
061: *
062: * Indicates whether the node has any memory.
063: * @return
064: */
065: public boolean hasMemory() {
066: return this .hasMemory;
067: }
068:
069: /**
070: * Attaches the node into the network. Usually to the parent <code>ObjectSource</code> or <code>TupleSource</code>
071: */
072: public abstract void attach();
073:
074: public abstract void attach(InternalWorkingMemory[] workingMemories);
075:
076: /**
077: * Removes the node from teh network. Usually from the parent <code>ObjectSource</code> or <code>TupleSource</code>
078: *
079: */
080: public abstract void remove(BaseNode node,
081: InternalWorkingMemory[] workingMemories);
082:
083: // /**
084: // * When nodes are added to the network that already has data. that existing data must be repropagated to the new node.
085: // * This new propagation may result in one or more assertions, so a PropagationContext and the workingMemory for the facts
086: // * must be provided.
087: // *
088: // * @param workingMemory
089: // * The WorkingMemory
090: // * @param context
091: // * The PropagationContext
092: // *
093: // */
094: // public abstract void updateNewNode(InternalWorkingMemory workingMemory,
095: // PropagationContext context);
096:
097: /**
098: * Each time a node is shared a counter is increased.
099: *
100: */
101: public void addShare() {
102: ++this .sharedCount;
103: }
104:
105: /**
106: * Each time a node is unshared a counter is decreased.
107: *
108: */
109: public void removeShare() {
110: --this .sharedCount;
111: }
112:
113: /**
114: * Indicates whether the node is shared.
115: * @return
116: */
117: public boolean isShared() {
118: return this .sharedCount > 0;
119: }
120:
121: /**
122: * Returns true in case the current node is in use (is referenced by any other node)
123: * @return
124: */
125: public boolean isInUse() {
126: return this .sharedCount >= 0;
127: }
128:
129: /**
130: * Returns the number of times the node is shared
131: * @return
132: * int value indicating the share count.
133: */
134: public int getSharedCount() {
135: return this .sharedCount;
136: }
137:
138: /**
139: * The hashCode return is simply the unique id of the node. It is expected that base classes will also implement equals(Object object).
140: */
141: public int hashCode() {
142: return this .id;
143: }
144:
145: public String toString() {
146: return "[" + this .getClass().getName() + "(" + this .id + ")]";
147: }
148: }
|