package com.nailabs.abac.process; import edu.stanford.peer.rbtm.credential.*; import java.util.*; import java.io.*; public class WeightTable { private int serialNo = 0; protected Hashtable table = new Hashtable(20); protected float p = 0.50f; protected final float SAT = 1.0f; protected final float FAIL = 0.0f; public float DEFAULT = 1.0f; public WeightTable(String file, float p) { this(file); this.p = p; } public WeightTable(String file) { load(file); } public void load(String filename) { try { BufferedReader in = new BufferedReader(new FileReader(filename)); while(true) { String line = in.readLine(); StringTokenizer st = new StringTokenizer(line, "="); EntityExpression expr = StaticCredential.getEntityExpression(st.nextToken()); Float weight = new Float(st.nextToken()); table.put(expr, weight); } } catch(Exception ex) { //ex.printStackTrace(); } } public void save(String filename) { try { PrintStream out = new PrintStream(new FileOutputStream(filename)); Iterator i = table.keySet().iterator(); while(i.hasNext()) { EntityExpression key = (EntityExpression)i.next(); Float entry = (Float)table.get(key); out.println(key + "=" + getWeight(key)); } } catch(Exception ex) { ex.printStackTrace(); } } public float getWeight(EntityExpression r) { Float val = (Float)table.get(r); if(val == null) { val = new Float(DEFAULT); table.put(r, val); } return val.floatValue(); } public void addSatisfied(EntityExpression r) { Float val = (Float)table.get(r); if(val == null) { val = new Float(DEFAULT); } val = new Float(update(val.floatValue(), SAT)); table.put(r, val); } public void addFailed(EntityExpression r) { Float val = (Float)table.get(r); if(val == null) { val = new Float(DEFAULT); } val = new Float(update(val.floatValue(), FAIL)); table.put(r, val); } protected float update(float val, float u) { if(u == 1.0f) return 1.0f; else return (val * (1.0f - p) + u * p); } }