1 | package com.nailabs.abac.process; |
---|
2 | |
---|
3 | import edu.stanford.peer.rbtm.credential.*; |
---|
4 | import java.util.*; |
---|
5 | import java.io.*; |
---|
6 | |
---|
7 | public class CountTable { |
---|
8 | |
---|
9 | protected Hashtable table = new Hashtable(20); |
---|
10 | |
---|
11 | public float SAT = 4.0f; |
---|
12 | |
---|
13 | public float INIT = 7.0f; |
---|
14 | |
---|
15 | public CountTable(String file) { load(file); } |
---|
16 | |
---|
17 | public void load(String filename) { |
---|
18 | try { |
---|
19 | BufferedReader in = new BufferedReader(new FileReader(filename)); |
---|
20 | while(true) { |
---|
21 | String line = in.readLine(); |
---|
22 | StringTokenizer st = new StringTokenizer(line, "="); |
---|
23 | EntityExpression expr = |
---|
24 | StaticCredential.getEntityExpression(st.nextToken()); |
---|
25 | Float weight = new Float(st.nextToken()); |
---|
26 | table.put(expr, weight); |
---|
27 | } |
---|
28 | } catch(Exception ex) { |
---|
29 | //ex.printStackTrace(); |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | public void save(String filename) { |
---|
34 | try { |
---|
35 | PrintStream out = new PrintStream(new FileOutputStream(filename)); |
---|
36 | Iterator i = table.keySet().iterator(); |
---|
37 | while(i.hasNext()) { |
---|
38 | EntityExpression key = (EntityExpression)i.next(); |
---|
39 | Float entry = (Float)table.get(key); |
---|
40 | out.println(key + "=" + getWeight(key)); |
---|
41 | } |
---|
42 | } catch(Exception ex) { |
---|
43 | ex.printStackTrace(); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | public float getWeight(EntityExpression r) { |
---|
48 | Float val = (Float)table.get(r); |
---|
49 | if(val == null) { |
---|
50 | val = new Float(INIT); |
---|
51 | table.put(r, val); |
---|
52 | } |
---|
53 | return val.floatValue(); |
---|
54 | } |
---|
55 | |
---|
56 | public void addSatisfied(EntityExpression r) { |
---|
57 | Float val = (Float)table.get(r); |
---|
58 | if(val == null) { |
---|
59 | val = new Float(INIT); |
---|
60 | } |
---|
61 | val = new Float(SAT); |
---|
62 | table.put(r, val); |
---|
63 | } |
---|
64 | |
---|
65 | public void addFailed(EntityExpression r) { |
---|
66 | Float val = (Float)table.get(r); |
---|
67 | float f = (val == null)? INIT: val.floatValue(); |
---|
68 | f = (f <= 1.0f)? 0.0f: (f - 1.0f); |
---|
69 | table.put(r, new Float(f)); |
---|
70 | } |
---|
71 | } |
---|