[8780cbec] | 1 | package edu.stanford.peer.rbtm.engine; |
---|
| 2 | |
---|
| 3 | import java.util.*; |
---|
| 4 | |
---|
| 5 | import edu.stanford.peer.rbtm.credential.*; |
---|
| 6 | import edu.stanford.peer.rbtm.util.*; |
---|
| 7 | |
---|
| 8 | /* |
---|
| 9 | */ |
---|
| 10 | public abstract class AbstractProofNode implements ProofNode |
---|
| 11 | { |
---|
| 12 | /** The EntityExpression this node is about. */ |
---|
| 13 | EntityExpression _roleExp; |
---|
| 14 | |
---|
| 15 | /** The proof _graph this node is in, this reference is used while processing |
---|
| 16 | this node */ |
---|
| 17 | ProofGraph _graph; |
---|
| 18 | |
---|
| 19 | /** Nodes that can reach this node directly, evidences are credentials. */ |
---|
| 20 | protected HashSet parents; |
---|
| 21 | |
---|
| 22 | /** Nodes that this node can reach directly, evidecens are credentials. */ |
---|
| 23 | protected HashSet children; |
---|
| 24 | |
---|
| 25 | /** Backward solutions on this node */ |
---|
| 26 | protected HashSet backwardSolutions; |
---|
| 27 | |
---|
| 28 | /** Forward solutions on this node */ |
---|
| 29 | protected HashSet forwardSolutions; |
---|
| 30 | |
---|
| 31 | protected ArrayList backwardListeners; |
---|
| 32 | protected ArrayList forwardListeners; |
---|
| 33 | |
---|
| 34 | AbstractProofNode (ProofGraph graph, EntityExpression re, boolean trackAll) |
---|
| 35 | { |
---|
| 36 | _graph = graph; |
---|
| 37 | _roleExp = re; |
---|
| 38 | Debug.debugInfo("A new node is created for " + re); |
---|
| 39 | |
---|
| 40 | parents = new ResultEvidenceMap(trackAll); |
---|
| 41 | children = new ResultEvidenceMap(trackAll); |
---|
| 42 | backwardSolutions = new ResultEvidenceMap(trackAll); |
---|
| 43 | forwardSolutions = new ResultEvidenceMap(trackAll); |
---|
| 44 | |
---|
| 45 | backwardListeners = new HashSet(); |
---|
| 46 | forwardListeners = new HashSet(); |
---|
| 47 | |
---|
| 48 | // Call this in constructor instead of process methods, because |
---|
| 49 | // we want to record the fact that this node has been reached even |
---|
| 50 | // without further processing |
---|
| 51 | finishInit(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * This method adds the node itself as both a forward solution and a backward |
---|
| 56 | * aolution to itself. If this behaviour is not appropriate, this method |
---|
| 57 | * should be overriden. |
---|
| 58 | */ |
---|
| 59 | protected void finishInit() |
---|
| 60 | { |
---|
| 61 | ForwardSolution fs = _graph.getForwardSolution(_roleExp); |
---|
| 62 | if (fs != null) { |
---|
| 63 | forwardSolutionAdded(this, fs); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | BackwardSolution bs = _graph.getBackwardSolution(_roleExp); |
---|
| 67 | if (bs != null) { |
---|
| 68 | backwardSolutionAdded(this, bs); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | public ProofGraph getGraph() { |
---|
| 73 | return _graph; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | public EntityExpression getRoleExp() { |
---|
| 77 | return _roleExp; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | public ResultEvidenceMap getForwardSolutions() { |
---|
| 81 | return forwardSolutions; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | public ResultEvidenceMap getBackwardSolutions() { |
---|
| 85 | return backwardSolutions; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // The method that does the work when we visit a node |
---|
| 89 | public void forwardProcess() |
---|
| 90 | { |
---|
| 91 | // first step: go over all credentials that have this as subject |
---|
| 92 | Iterator credIt = _graph.findCredentialsBySubject(_roleExp); |
---|
| 93 | while (credIt.hasNext()) { |
---|
| 94 | StaticCredential credential = (StaticCredential) credIt.next(); |
---|
| 95 | ProofNode node = _graph.addForwardNode(credential.getDefinedRole()); |
---|
| 96 | node.addParent(this, credential); |
---|
| 97 | //_graph.addProofEdge(this, node, credential); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | // second step: find partial usage of this EntityExpression |
---|
| 101 | /* |
---|
| 102 | if (_roleExp instanceof Intersection) { |
---|
| 103 | return; |
---|
| 104 | } |
---|
| 105 | credIt = _graph.findCredentialsByPartialSubject(_roleExp); |
---|
| 106 | while (credIt.hasNext()) { |
---|
| 107 | StaticCredential credential = (StaticCredential) credIt.next(); |
---|
| 108 | ProofNode node = _graph.addForwardNode(credential.getDefinedRole()); |
---|
| 109 | int j = ((Intersection) credential.getSubject()).getIndexOf(_roleExp); |
---|
| 110 | forward.solutionAdded(new PartialSolution(_roleExp, j)); |
---|
| 111 | } |
---|
| 112 | */ |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public void addBackwardListener(BackwardSolutionListener sl) { |
---|
| 116 | backwardListeners.add(sl); |
---|
| 117 | propagateBackwardSolutionsTo(sl); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | public void addForwardListener(ForwardSolutionListener sl) { |
---|
| 121 | forwardListeners.add(sl); |
---|
| 122 | propagateForwardSolutionsTo(sl); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Add a node as a parent to this node |
---|
| 127 | * @param node: the parent node |
---|
| 128 | */ |
---|
| 129 | public void addParent(ProofNode node, Object evidence) { |
---|
| 130 | if (! parents.contains(node.getRoleExp()) { |
---|
| 131 | parents.add(node); |
---|
| 132 | addForwardListener(node); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | public void addChild(ProofNode node, Object evidence) { |
---|
| 137 | if (children.putResultEvidence(node.getRoleExp(), evidence)) { |
---|
| 138 | addBackwardListener(node); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | public void backwardSolutionAdded(ProofNode s, BackwardSolution r) |
---|
| 143 | { |
---|
| 144 | if (backwardSolutions.putResultEvidence(r, s)) { // when solution r is new |
---|
| 145 | Debug.debugInfo("Solution added: " + getRoleExp() + "; " + s.getRoleExp() + "; " + r); |
---|
| 146 | propagateBackwardSolution(r); |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | public void forwardSolutionAdded(ProofNode s, ForwardSolution r) { |
---|
| 151 | /* |
---|
| 152 | if (r instanceof PartialSolution) { |
---|
| 153 | Intersection re = r.getIntersection(); |
---|
| 154 | Integer index = r.getNumber(); |
---|
| 155 | HashMap value = (HashMap) partialSols.get(re); |
---|
| 156 | if (value == null) { // first piece |
---|
| 157 | value = new HashMap(); |
---|
| 158 | partialSols.put(re, value); |
---|
| 159 | } |
---|
| 160 | SmallSet evidences = value.get(index); |
---|
| 161 | if (evidences == null) { // A new piece |
---|
| 162 | evidences = new SmallSet(s); |
---|
| 163 | value.put(index, evidences); |
---|
| 164 | if (value.size() == re.getK()) { // have all pieces |
---|
| 165 | // Add an edge from current node to re |
---|
| 166 | // addEdge(); |
---|
| 167 | } |
---|
| 168 | } else if (trackAll) { |
---|
| 169 | evidences.add(s); |
---|
| 170 | } |
---|
| 171 | return; |
---|
| 172 | } |
---|
| 173 | */ |
---|
| 174 | if (forwardSolutions.putResultEvidence(r, s)) { |
---|
| 175 | Debug.debugInfo("Forward solution added to: " + getRoleExp() + "; " + s.getRoleExp() + "; " + r); |
---|
| 176 | propagateForwardSolution(r); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | protected void propagateBackwardSolutionsTo(BackwardSolutionListener listener) |
---|
| 181 | { |
---|
| 182 | Object[] sols = backwardSolutions.resultSet().toArray(); |
---|
| 183 | for (int i=0; i<sols.length; i++) { |
---|
| 184 | listener.backwardSolutionAdded(this, (BackwardSolution)(sols[i])); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | protected void propagateForwardSolutionsTo(ForwardSolutionListener listener) |
---|
| 189 | { |
---|
| 190 | Object[] sols = forwardSolutions.resultSet().toArray(); |
---|
| 191 | for (int i=0; i<sols.length; i++) { |
---|
| 192 | listener.forwardSolutionAdded(this, (ForwardSolution)(sols[i])); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | protected void propagateBackwardSolution(BackwardSolution g) |
---|
| 197 | { |
---|
| 198 | Object[] listeners = backwardListeners.toArray(); |
---|
| 199 | for (int i=0; i<listeners.length; i++) { |
---|
| 200 | ((BackwardSolutionListener)listeners[i]).backwardSolutionAdded(this, g); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | protected void propagateForwardSolution(ForwardSolution g) |
---|
| 205 | { |
---|
| 206 | Object[] listeners = forwardListeners.toArray(); |
---|
| 207 | for (int i=0; i<listeners.length; i++) { |
---|
| 208 | ((ForwardSolutionListener)listeners[i]).forwardSolutionAdded(this, g); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | // boolean hasParent(ProofNode node) { return parents.containsResult(node._roleExp); } |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | //////////// code for SelectiveProofNode |
---|
| 217 | /* |
---|
| 218 | * The following two fields store desired goals. Only desired goals are |
---|
| 219 | * passed through edges. Setting them to null means all goals are |
---|
| 220 | * desired. |
---|
| 221 | * In backward search, if child A.r only wants to see one solution, while |
---|
| 222 | * child A.r1.r2 wants to see all solutions. All goals are desired, so |
---|
| 223 | * they are passed to A.r as well. But A.r should have only one desired |
---|
| 224 | * solution, and so A.r will not propagate them further. |
---|
| 225 | */ |
---|
| 226 | // SmallSet backwardGoals = new SmallSet(); |
---|
| 227 | // SmallSet forwardGoals = new SmallSet(); |
---|
| 228 | |
---|
| 229 | /* |
---|
| 230 | void enableBackwardGoal(EntityExpression g) |
---|
| 231 | { |
---|
| 232 | if (backwardGoals != null && ! backwardGoals.containsGoal(g)) { |
---|
| 233 | // has not been enabled before |
---|
| 234 | backwardGoals.addGoal(g); |
---|
| 235 | if (backwardSoltuions.containsResult(g)) { |
---|
| 236 | propagateBackwardSolution(g); |
---|
| 237 | } |
---|
| 238 | // Enable this backward goal for all parents |
---|
| 239 | Object[] list = parents.toArray(); // inform listeners |
---|
| 240 | for (int i=0; i<list.length; i++) { |
---|
| 241 | ((ProofNode)list[i]).enableBackwardGoal(g); |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void enableAllBackwardGoals() |
---|
| 247 | { |
---|
| 248 | if (backwardGoals == null ) { |
---|
| 249 | backwardGoals = null; |
---|
| 250 | // if have been procesed, loop over all parents, and enable them |
---|
| 251 | // if haven't been processed, just enable it |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | */ |
---|
| 255 | } // End of class ProofNode |
---|
| 256 | |
---|