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 | * A credential graph node to represent an intersection. |
---|
10 | */ |
---|
11 | public class IntersectionProofNode extends AbstractProofNode |
---|
12 | { |
---|
13 | /** Initial capacity for a new intersection proof node */ |
---|
14 | private int CAPACITY = 10; |
---|
15 | |
---|
16 | /** |
---|
17 | * Backward solution monitor for this node. |
---|
18 | * @see IntersectionMonitor |
---|
19 | */ |
---|
20 | private IntersectionMonitor iMonitor; |
---|
21 | |
---|
22 | /** The number sub-part expressions which belong to this intersection */ |
---|
23 | private int k = 0; |
---|
24 | |
---|
25 | /** |
---|
26 | * Generic constructor for creating a new intersection proof node in a |
---|
27 | * credential graph. |
---|
28 | * @param g The credential graph to which the node should belong |
---|
29 | * @param i The intersection role expression the node represents |
---|
30 | * @param type The track type used for result evidence maps |
---|
31 | */ |
---|
32 | protected IntersectionProofNode(ProofGraph g, Intersection i, int type) { |
---|
33 | super(g, i, type); |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | public HashSet getChain(int size, EntityExpression target) { |
---|
38 | Iterator parts = ((Intersection)getRoleExp()).getParts(); |
---|
39 | ProofGraph graph = getGraph(); |
---|
40 | HashSet results = new HashSet(); |
---|
41 | |
---|
42 | if(getRoleExp().equals(target)) { |
---|
43 | return(new HashSet(size++)); |
---|
44 | } |
---|
45 | while(parts.hasNext()) { |
---|
46 | EntityExpression part = (EntityExpression)parts.next(); |
---|
47 | //System.out.println("&&&&& Getting chain from " + |
---|
48 | //part + " to " + target); |
---|
49 | results.addAll(graph.getChain(part, target)); |
---|
50 | } |
---|
51 | return results; |
---|
52 | } |
---|
53 | |
---|
54 | public void additionalBackwardProcess() { |
---|
55 | if (iMonitor != null)return; |
---|
56 | Intersection i = (Intersection)getRoleExp(); |
---|
57 | iMonitor = new IntersectionMonitor(); |
---|
58 | Iterator parts = i.getParts(); |
---|
59 | k = i.getPartsCount(); |
---|
60 | while(parts.hasNext()) { |
---|
61 | EntityExpression expr = (EntityExpression)parts.next(); |
---|
62 | ProofNode node = getGraph().addBackwardNode(expr); |
---|
63 | node.addBackwardListener(iMonitor); |
---|
64 | Debug.debugInfo(iMonitor.toString() + ": adding expr = " + expr ); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * This method derives from RoleProofNode. |
---|
70 | */ |
---|
71 | public void additionalForwardProcess() { |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * A listener for monitoring possible backward solutions. |
---|
77 | */ |
---|
78 | class IntersectionMonitor implements BackwardSolutionListener { |
---|
79 | /** |
---|
80 | * a partial solutions map |
---|
81 | */ |
---|
82 | HashMap results = new HashMap(CAPACITY); |
---|
83 | |
---|
84 | /** |
---|
85 | * Solutions are stored internally until all child nodes return a |
---|
86 | * specific solution at which time the solution is propagted to the |
---|
87 | * parent. |
---|
88 | * @param source child node which propagates the answer to its parent |
---|
89 | * @param soln an entity expression instance which is a partial sol'n |
---|
90 | */ |
---|
91 | public void backwardSolutionAdded(ProofNode source, BackwardSolution soln) { |
---|
92 | // list of sources for this soln |
---|
93 | SolutionCounter counter = (SolutionCounter)results.get(soln); |
---|
94 | // if this is a new solution then create a new list |
---|
95 | if(counter == null) { |
---|
96 | counter = new SolutionCounter(); // use k even if k grows |
---|
97 | results.put(soln, counter); // add new counter to table |
---|
98 | } |
---|
99 | counter.increment(); |
---|
100 | if(counter.isDone()) { |
---|
101 | // AbstractProofNode |
---|
102 | IntersectionProofNode.super.backwardSolutionAdded(source,soln); |
---|
103 | } |
---|
104 | else |
---|
105 | Debug.debugInfo(toString() + ": part soln " + soln + " = " + |
---|
106 | counter); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Creates a human readable version of ther current intersection |
---|
111 | * instance. Expression parts are separated with the '^' operator. |
---|
112 | */ |
---|
113 | public String toString() { |
---|
114 | return "Monitor for (" + getRoleExp() + " w/k = " + k + ")"; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * A class to monitor the number of solution matches received |
---|
119 | * by an intersection's proof node. |
---|
120 | */ |
---|
121 | class SolutionCounter { |
---|
122 | /** |
---|
123 | * The counter integer |
---|
124 | */ |
---|
125 | private int count = 0; |
---|
126 | |
---|
127 | /** |
---|
128 | * Increment the counter by one. |
---|
129 | */ |
---|
130 | public void increment() { count += 1; } |
---|
131 | |
---|
132 | /** |
---|
133 | * A test method to see if the counter has max'ed out. |
---|
134 | * |
---|
135 | * @return true if count reaches k otherwise false. |
---|
136 | */ |
---|
137 | public boolean isDone() { return (count >= k && k != 0); } |
---|
138 | |
---|
139 | /** |
---|
140 | * To see what value the counter has set. |
---|
141 | */ |
---|
142 | public String toString() { return Integer.toString(count); } |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | |
---|