KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > RETEQueue


1 /******************************************************************
2  * File: RETEQueue.java
3  * Created by: Dave Reynolds
4  * Created on: 09-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: RETEQueue.java,v 1.8 2005/02/21 12:17:58 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.graph.*;
13
14 import java.util.*;
15
16 /**
17  * Represents one input left of a join node. The queue points to
18  * a sibling queue representing the other leg which should be joined
19  * against.
20  *
21  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
22  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:17:58 $
23  */

24 public class RETEQueue implements RETESinkNode, RETESourceNode {
25     
26     /** A multi-set of partially bound envionments */
27     protected HashMap queue = new HashMap();
28     
29     /** A set of variable indices which should match between the two inputs */
30     protected byte[] matchIndices;
31     
32     /** The sibling queue which forms the other half of the join node */
33     protected RETEQueue sibling;
34     
35     /** The node that results should be passed on to */
36     protected RETESinkNode continuation;
37     
38     /**
39      * Constructor. The queue is not usable until it has been bound
40      * to a sibling and a continuation node.
41      * @param A set of variable indices which should match between the two inputs
42      */

43     public RETEQueue(byte[] matchIndices) {
44         this.matchIndices = matchIndices;
45     }
46     
47     /**
48      * Constructor. The queue is not usable until it has been bound
49      * to a sibling and a continuation node.
50      * @param A List of variable indices which should match between the two inputs
51      */

52     public RETEQueue(List matchIndexList) {
53         int len = matchIndexList.size();
54         matchIndices = new byte[len];
55         for (int i = 0; i < len; i++) {
56             matchIndices[i] = (byte) ((Number JavaDoc)matchIndexList.get(i)).intValue();
57         }
58     }
59     
60     /**
61      * Set the sibling for this node.
62      */

63     public void setSibling(RETEQueue sibling) {
64         this.sibling = sibling;
65     }
66     
67     /**
68      * Set the continuation node for this node (and any sibling)
69      */

70     public void setContinuation(RETESinkNode continuation) {
71         this.continuation = continuation;
72         if (sibling != null) sibling.continuation = continuation;
73     }
74
75     /**
76      * Propagate a token to this node.
77      * @param env a set of variable bindings for the rule being processed.
78      * @param isAdd distinguishes between add and remove operations.
79      */

80     public void fire(BindingVector env, boolean isAdd) {
81         // Store the new token in this store
82
Count count = (Count)queue.get(env);
83         if (count == null) {
84             // no entry yet
85
if (!isAdd) return;
86             queue.put(env, new Count(1));
87         } else {
88             if (isAdd) {
89                 count.inc();
90             } else {
91                 count.dec();
92                 if (count.getCount() == 0) {
93                     queue.remove(env);
94                 }
95             }
96         }
97         
98         // Cross match new token against the entries in the sibling queue
99
for (Iterator i = sibling.queue.keySet().iterator(); i.hasNext(); ) {
100             Node[] candidate = ((BindingVector)i.next()).getEnvironment();
101             Node[] envNodes = env.getEnvironment();
102             boolean matchOK = true;
103             for (int j = 0; j < matchIndices.length; j++) {
104                 int index = matchIndices[j];
105                 if ( ! candidate[index].sameValueAs(envNodes[index])) {
106                     matchOK = false;
107                     break;
108                 }
109             }
110             if (matchOK) {
111                 // Instantiate a new extended environment
112
Node[] newNodes = new Node[candidate.length];
113                 for (int j = 0; j < candidate.length; j++) {
114                     Node n = candidate[j];
115                     newNodes[j] = (n == null) ? envNodes[j] : n;
116                 }
117                 BindingVector newEnv = new BindingVector(newNodes);
118                 // Fire the successor processing
119
continuation.fire(newEnv, isAdd);
120             }
121         }
122     }
123
124     /**
125      * Inner class used to represent an updatable count.
126      */

127     protected static class Count {
128         /** the count */
129         int count;
130         
131         /** Constructor */
132         public Count(int count) {
133             this.count = count;
134         }
135         
136         /** Access count value */
137         public int getCount() {
138             return count;
139         }
140         
141         /** Increment the count value */
142         public void inc() {
143             count++;
144         }
145         
146         /** Decrement the count value */
147         public void dec() {
148             count--;
149         }
150         
151         /** Set the count value */
152         public void setCount(int count) {
153             this.count = count;
154         }
155     }
156     
157     /**
158      * Clone this node in the network.
159      * @param context the new context to which the network is being ported
160      */

161     public RETENode clone(Map netCopy, RETERuleContext context) {
162         RETEQueue clone = (RETEQueue)netCopy.get(this);
163         if (clone == null) {
164             clone = new RETEQueue(matchIndices);
165             netCopy.put(this, clone);
166             clone.setSibling((RETEQueue)sibling.clone(netCopy, context));
167             clone.setContinuation((RETESinkNode)continuation.clone(netCopy, context));
168             clone.queue.putAll(queue);
169         }
170         return clone;
171     }
172 }
173
174
175
176 /*
177     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
178     All rights reserved.
179
180     Redistribution and use in source and binary forms, with or without
181     modification, are permitted provided that the following conditions
182     are met:
183
184     1. Redistributions of source code must retain the above copyright
185        notice, this list of conditions and the following disclaimer.
186
187     2. Redistributions in binary form must reproduce the above copyright
188        notice, this list of conditions and the following disclaimer in the
189        documentation and/or other materials provided with the distribution.
190
191     3. The name of the author may not be used to endorse or promote products
192        derived from this software without specific prior written permission.
193
194     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
195     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
197     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
199     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
201     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
202     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
203     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
204 */
Popular Tags