1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.graph.*; 13 14 import java.util.*; 15 16 24 public class RETEQueue implements RETESinkNode, RETESourceNode { 25 26 27 protected HashMap queue = new HashMap(); 28 29 30 protected byte[] matchIndices; 31 32 33 protected RETEQueue sibling; 34 35 36 protected RETESinkNode continuation; 37 38 43 public RETEQueue(byte[] matchIndices) { 44 this.matchIndices = matchIndices; 45 } 46 47 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 )matchIndexList.get(i)).intValue(); 57 } 58 } 59 60 63 public void setSibling(RETEQueue sibling) { 64 this.sibling = sibling; 65 } 66 67 70 public void setContinuation(RETESinkNode continuation) { 71 this.continuation = continuation; 72 if (sibling != null) sibling.continuation = continuation; 73 } 74 75 80 public void fire(BindingVector env, boolean isAdd) { 81 Count count = (Count)queue.get(env); 83 if (count == null) { 84 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 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 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 continuation.fire(newEnv, isAdd); 120 } 121 } 122 } 123 124 127 protected static class Count { 128 129 int count; 130 131 132 public Count(int count) { 133 this.count = count; 134 } 135 136 137 public int getCount() { 138 return count; 139 } 140 141 142 public void inc() { 143 count++; 144 } 145 146 147 public void dec() { 148 count--; 149 } 150 151 152 public void setCount(int count) { 153 this.count = count; 154 } 155 } 156 157 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 | Popular Tags |