1 | package edu.stanford.peer.rbtm.engine; |
---|
2 | |
---|
3 | import java.util.*; |
---|
4 | |
---|
5 | import edu.stanford.peer.rbtm.RBTMConstants; |
---|
6 | import edu.stanford.peer.rbtm.credential.*; |
---|
7 | import edu.stanford.peer.rbtm.util.*; |
---|
8 | |
---|
9 | /** |
---|
10 | * A graph engine is a proof graph which may optionally contain a predicate |
---|
11 | * function. This implementation is a tightly coupled directed credential |
---|
12 | * graph and credential index. |
---|
13 | * |
---|
14 | * @deprecated |
---|
15 | */ |
---|
16 | public class CredentialManager |
---|
17 | implements java.io.Serializable, ProofGraph, RBTMConstants |
---|
18 | { |
---|
19 | static final public Iterator emptyIt = new ArrayList(1).iterator(); |
---|
20 | static final public Collection emptyCollection = new ArrayList(1); |
---|
21 | |
---|
22 | /** stores all the credentials */ |
---|
23 | private HashSet credentials = new HashSet(); |
---|
24 | |
---|
25 | /** an index from issuers to credential collections */ |
---|
26 | private HashMap indexByIssuer = new HashMap(); |
---|
27 | |
---|
28 | /** an index from roles to credential collections */ |
---|
29 | private HashMap indexByRole = new HashMap(); |
---|
30 | |
---|
31 | /** an index from subjects to collections of credentials */ |
---|
32 | private HashMap indexBySubject = new HashMap(); |
---|
33 | |
---|
34 | /** predicate function which could be null */ |
---|
35 | private Predicate pred = null; |
---|
36 | |
---|
37 | private int track; |
---|
38 | |
---|
39 | private SolutionFilter solutionFilter; |
---|
40 | |
---|
41 | // a proof queue |
---|
42 | transient private ProofQueue proofQueue = new SimpleProofQueue(); |
---|
43 | |
---|
44 | transient private HashMap nodeTable = new HashMap(); |
---|
45 | |
---|
46 | public CredentialManager(Set credSet, int trackType, |
---|
47 | SolutionFilter solFilter) |
---|
48 | { |
---|
49 | this(trackType, solFilter); |
---|
50 | addCredentials(credentials); |
---|
51 | } |
---|
52 | |
---|
53 | public CredentialManager(int trackType, SolutionFilter solFilter) { |
---|
54 | track = trackType; |
---|
55 | solutionFilter = solFilter; |
---|
56 | } |
---|
57 | |
---|
58 | public CredentialManager() { |
---|
59 | this(TRACK_NONE, new DefaultSolutionFilter()); |
---|
60 | } |
---|
61 | |
---|
62 | public void reinit() { |
---|
63 | proofQueue = new SimpleProofQueue(); |
---|
64 | nodeTable = new HashMap(); |
---|
65 | } |
---|
66 | |
---|
67 | synchronized public void addCredentials(Collection credentials) { |
---|
68 | reinit(); |
---|
69 | Iterator credIt = credentials.iterator(); |
---|
70 | while (credIt.hasNext()) { |
---|
71 | addCredential((StaticCredential)credIt.next()); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | synchronized public void removeCredentials(Collection credentials) { |
---|
76 | reinit(); |
---|
77 | Iterator credIt = credentials.iterator(); |
---|
78 | while (credIt.hasNext()) { |
---|
79 | removeCredential((StaticCredential)credIt.next()); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | synchronized public boolean addCredential(StaticCredential cred) { |
---|
84 | // Make sure that no duplicate Credential exists |
---|
85 | if (credentials.contains(cred)) { |
---|
86 | Debug.debugInfo("trying to add existing credential: " + cred); |
---|
87 | return false; |
---|
88 | } |
---|
89 | Debug.debugInfo("adding credential: " + cred); |
---|
90 | credentials.add(cred); |
---|
91 | addIndexFor(cred); |
---|
92 | ProofNode node = getNode(cred.getDefinedRole()); |
---|
93 | if (node != null) { |
---|
94 | node.invalidateBackward(); |
---|
95 | } |
---|
96 | node = getNode(cred.getSubject()); |
---|
97 | if (node != null) { |
---|
98 | node.invalidateForward(); |
---|
99 | } |
---|
100 | return true; |
---|
101 | } |
---|
102 | |
---|
103 | synchronized public void removeCredential(StaticCredential cred) { |
---|
104 | if (credentials.contains(cred)) { |
---|
105 | Debug.debugInfo("removing credential: " + cred); |
---|
106 | reinit(); |
---|
107 | credentials.remove(cred); |
---|
108 | removeIndexFor(cred); |
---|
109 | } else { |
---|
110 | Debug.debugInfo("trying to remove nonexistent credential: " + cred); |
---|
111 | //// throw Exception |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | private void addIndexFor(StaticCredential cred) |
---|
116 | { |
---|
117 | Entity e = cred.getIssuer(); |
---|
118 | ArrayList credList = (ArrayList)indexByIssuer.get(e); |
---|
119 | if (credList == null) { |
---|
120 | credList = new ArrayList(40); |
---|
121 | indexByIssuer.put(e, credList); |
---|
122 | } |
---|
123 | credList.add(cred); |
---|
124 | |
---|
125 | // Code to deal with indexByRole |
---|
126 | Role r = cred.getDefinedRole(); |
---|
127 | credList = (ArrayList) indexByRole.get(r); |
---|
128 | if (credList == null) { |
---|
129 | credList = new ArrayList(10); |
---|
130 | indexByRole.put(r, credList); |
---|
131 | } |
---|
132 | credList.add(cred); |
---|
133 | |
---|
134 | EntityExpression subject = cred.getSubject(); |
---|
135 | if(subject instanceof Intersection) { |
---|
136 | Iterator parts = ((Intersection)subject).getParts(); |
---|
137 | while(parts.hasNext()) { |
---|
138 | EntityExpression partialSolution = |
---|
139 | (EntityExpression)parts.next(); |
---|
140 | credList = (ArrayList) indexBySubject.get(subject); |
---|
141 | if (credList == null) { |
---|
142 | credList = new ArrayList(10); |
---|
143 | indexBySubject.put(partialSolution, credList); |
---|
144 | } |
---|
145 | credList.add(cred); |
---|
146 | } |
---|
147 | } else { |
---|
148 | credList = (ArrayList) indexBySubject.get(subject); |
---|
149 | if (credList == null) { |
---|
150 | credList = new ArrayList(10); |
---|
151 | indexBySubject.put(subject, credList); |
---|
152 | } |
---|
153 | credList.add(cred); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | private void removeIndexFor(StaticCredential cred) |
---|
158 | { |
---|
159 | ((ArrayList)indexByIssuer.get(cred.getIssuer())).remove(cred); |
---|
160 | ((ArrayList)indexByRole.get(cred.getDefinedRole())).remove(cred); |
---|
161 | ((ArrayList)indexBySubject.get(cred.getSubject())).remove(cred); |
---|
162 | } |
---|
163 | |
---|
164 | /* |
---|
165 | public void addRole(Role role) { |
---|
166 | if (! indexByRole.containsKey(role)) { |
---|
167 | indexByRole.put(role, new ArrayList()); |
---|
168 | } |
---|
169 | } |
---|
170 | */ |
---|
171 | |
---|
172 | synchronized public Collection getCredentialsIssuedBy(Entity e) { |
---|
173 | Collection credentials = (Collection)indexByIssuer.get(e); |
---|
174 | if (credentials == null) { |
---|
175 | return emptyCollection; |
---|
176 | } else { |
---|
177 | return credentials; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | synchronized public Iterator findCredentialsDefiningRole(Role r) |
---|
182 | { |
---|
183 | ArrayList credList = (ArrayList) indexByRole.get(r); |
---|
184 | if (credList == null) { |
---|
185 | return emptyIt; |
---|
186 | } else { |
---|
187 | return credList.iterator(); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | synchronized public Iterator findCredentialsBySubject(EntityExpression subject) |
---|
192 | { |
---|
193 | ArrayList credList = (ArrayList) indexBySubject.get(subject); |
---|
194 | if (credList == null) { |
---|
195 | return emptyIt; |
---|
196 | } else { |
---|
197 | return credList.iterator(); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | synchronized public Iterator findCredentialsByPartialSubject(EntityExpression subject) |
---|
202 | { |
---|
203 | // Using a vector copies the arraylist so we don't remove from it |
---|
204 | ArrayList credList = (ArrayList)indexBySubject.get(subject); |
---|
205 | if (credList == null) { |
---|
206 | return emptyIt; |
---|
207 | } |
---|
208 | credList = (ArrayList)credList.clone(); |
---|
209 | Iterator i = credList.iterator(); |
---|
210 | while(i.hasNext()) { // remove credential which aren't intersections |
---|
211 | StaticCredential cred = (StaticCredential)i.next(); |
---|
212 | if(!(cred.getSubject() instanceof Intersection)) { |
---|
213 | i.remove(); |
---|
214 | } |
---|
215 | } |
---|
216 | return credList.iterator(); |
---|
217 | } |
---|
218 | |
---|
219 | /* |
---|
220 | boolean hasCredentialsForSubject(EntityExpression re) { |
---|
221 | return findCredentialsBySubject(re).hasNext(); |
---|
222 | } |
---|
223 | */ |
---|
224 | |
---|
225 | // Find out all Roles that roleExp belongs to |
---|
226 | synchronized public ResultEvidenceMap forwardSearch(EntityExpression roleExp) { |
---|
227 | ProofNode goalNode = addForwardNode(roleExp); |
---|
228 | while (proofQueue.hasUnexploredForwardNodes()) { |
---|
229 | proofQueue.nextUnexploredForwardNode().forwardProcess(); |
---|
230 | } |
---|
231 | return goalNode.getForwardSolutions(); |
---|
232 | } |
---|
233 | |
---|
234 | // Find out all members of roleExp |
---|
235 | synchronized public ResultEvidenceMap backwardSearch(EntityExpression roleExp) { |
---|
236 | ProofNode goalNode = addBackwardNode(roleExp); |
---|
237 | while (proofQueue.hasUnexploredBackwardNodes()) { |
---|
238 | proofQueue.nextUnexploredBackwardNode().backwardProcess(); |
---|
239 | } |
---|
240 | return goalNode.getBackwardSolutions(); |
---|
241 | } |
---|
242 | |
---|
243 | public ProofNode addForwardNode(EntityExpression roleExp) { |
---|
244 | ProofNode node = addNode(roleExp); |
---|
245 | proofQueue.addForwardNode(node); |
---|
246 | return node; |
---|
247 | } |
---|
248 | |
---|
249 | public ProofNode addBackwardNode (EntityExpression roleExp) { |
---|
250 | ProofNode node = addNode(roleExp); |
---|
251 | proofQueue.addBackwardNode(node); |
---|
252 | return node; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Add a node corresponding to a role expression, make sure that the |
---|
257 | * node is not added twice. |
---|
258 | */ |
---|
259 | private ProofNode addNode (EntityExpression roleExp) { |
---|
260 | ProofNode node = (ProofNode)nodeTable.get(roleExp); |
---|
261 | if (node == null) { |
---|
262 | node = createProofNode(roleExp); |
---|
263 | nodeTable.put(roleExp, node); |
---|
264 | } |
---|
265 | return node; |
---|
266 | } |
---|
267 | |
---|
268 | private ProofNode getNode(EntityExpression roleExp) { |
---|
269 | return (ProofNode)nodeTable.get(roleExp); |
---|
270 | } |
---|
271 | |
---|
272 | private ProofNode createProofNode (EntityExpression roleExp) { |
---|
273 | if (roleExp instanceof Role) { |
---|
274 | return new RoleProofNode(this, (Role)roleExp, track); |
---|
275 | } else if (roleExp instanceof LinkedRole) { |
---|
276 | return new LinkedRoleProofNode(this, (LinkedRole)roleExp, track); |
---|
277 | } else if (roleExp instanceof Intersection) { |
---|
278 | return new IntersectionProofNode(this,(Intersection)roleExp,track); |
---|
279 | } else { |
---|
280 | return new EntityProofNode(this, (Entity)roleExp, track); |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | public ForwardSolution getForwardSolution(EntityExpression re) { |
---|
285 | return solutionFilter.getForwardSolution(re); |
---|
286 | } |
---|
287 | |
---|
288 | public BackwardSolution getBackwardSolution(EntityExpression re) { |
---|
289 | return solutionFilter.getBackwardSolution(re); |
---|
290 | } |
---|
291 | |
---|
292 | public String toString() { |
---|
293 | return "credentials=" + credentials.toString() + "\n" + |
---|
294 | "indexByIssuer=" + indexByIssuer.toString() + "\n" + |
---|
295 | "indexByRole=" + indexByRole.toString() + "\n" + |
---|
296 | "indexBySubject=" + indexBySubject.toString(); |
---|
297 | } |
---|
298 | |
---|
299 | private String getSubjectIndexs() |
---|
300 | { |
---|
301 | StringBuffer outBuf = new StringBuffer(); |
---|
302 | Iterator keyIt = indexBySubject.keySet().iterator(); |
---|
303 | while (keyIt.hasNext()) { |
---|
304 | Object o = keyIt.next(); |
---|
305 | if (o instanceof Role) { |
---|
306 | outBuf.append("Role ").append(o.toString()).append("\n"); |
---|
307 | Role r = (Role) o; |
---|
308 | outBuf.append("Base is ").append(r.getBase().getClass()).append("\n"); |
---|
309 | outBuf.append("RoleName is ").append(r.getName().getClass()).append("\n"); |
---|
310 | } |
---|
311 | } |
---|
312 | return outBuf.toString(); |
---|
313 | } |
---|
314 | |
---|
315 | public HashSet getChain(EntityExpression source, EntityExpression target) { |
---|
316 | return new HashSet(); |
---|
317 | } |
---|
318 | |
---|
319 | public Predicate getPredicate() { return pred; } |
---|
320 | } |
---|