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 WeightTable { |
---|
8 | |
---|
9 | private int serialNo = 0; |
---|
10 | |
---|
11 | protected Hashtable table = new Hashtable(20); |
---|
12 | |
---|
13 | protected float p = 0.50f; |
---|
14 | |
---|
15 | protected final float SAT = 1.0f; |
---|
16 | |
---|
17 | protected final float FAIL = 0.0f; |
---|
18 | |
---|
19 | public float DEFAULT = 1.0f; |
---|
20 | |
---|
21 | public WeightTable(String file, float p) { |
---|
22 | this(file); |
---|
23 | this.p = p; |
---|
24 | } |
---|
25 | |
---|
26 | public WeightTable(String file) { load(file); } |
---|
27 | |
---|
28 | public void load(String filename) { |
---|
29 | try { |
---|
30 | BufferedReader in = new BufferedReader(new FileReader(filename)); |
---|
31 | while(true) { |
---|
32 | String line = in.readLine(); |
---|
33 | StringTokenizer st = new StringTokenizer(line, "="); |
---|
34 | EntityExpression expr = |
---|
35 | StaticCredential.getEntityExpression(st.nextToken()); |
---|
36 | Float weight = new Float(st.nextToken()); |
---|
37 | table.put(expr, weight); |
---|
38 | } |
---|
39 | } catch(Exception ex) { |
---|
40 | //ex.printStackTrace(); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | public void save(String filename) { |
---|
45 | try { |
---|
46 | PrintStream out = new PrintStream(new FileOutputStream(filename)); |
---|
47 | Iterator i = table.keySet().iterator(); |
---|
48 | while(i.hasNext()) { |
---|
49 | EntityExpression key = (EntityExpression)i.next(); |
---|
50 | Float entry = (Float)table.get(key); |
---|
51 | out.println(key + "=" + getWeight(key)); |
---|
52 | } |
---|
53 | } catch(Exception ex) { |
---|
54 | ex.printStackTrace(); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | public float getWeight(EntityExpression r) { |
---|
59 | Float val = (Float)table.get(r); |
---|
60 | if(val == null) { |
---|
61 | val = new Float(DEFAULT); |
---|
62 | table.put(r, val); |
---|
63 | } |
---|
64 | return val.floatValue(); |
---|
65 | } |
---|
66 | |
---|
67 | public void addSatisfied(EntityExpression r) { |
---|
68 | Float val = (Float)table.get(r); |
---|
69 | if(val == null) { |
---|
70 | val = new Float(DEFAULT); |
---|
71 | } |
---|
72 | val = new Float(update(val.floatValue(), SAT)); |
---|
73 | table.put(r, val); |
---|
74 | } |
---|
75 | |
---|
76 | public void addFailed(EntityExpression r) { |
---|
77 | Float val = (Float)table.get(r); |
---|
78 | if(val == null) { |
---|
79 | val = new Float(DEFAULT); |
---|
80 | } |
---|
81 | val = new Float(update(val.floatValue(), FAIL)); |
---|
82 | table.put(r, val); |
---|
83 | } |
---|
84 | |
---|
85 | protected float update(float val, float u) { |
---|
86 | if(u == 1.0f) |
---|
87 | return 1.0f; |
---|
88 | else |
---|
89 | return (val * (1.0f - p) + u * p); |
---|
90 | } |
---|
91 | |
---|
92 | } |
---|