package parameterrules; import java.util.Vector; import java.util.Enumeration; import java.io.*; import main.BetterTokenizer; import main.GeneralInput; import parameters.ParameterSet; public class ParameterRuleSet { protected Vector rules = new Vector(5); /** The reference parameter set is needed by the parameter rules and is passed in when making a new rule. */ ParameterSet refParams = null; public ParameterRuleSet(ParameterSet paramset) { refParams = paramset; } public ParameterRuleSet copy() { ParameterRuleSet set = new ParameterRuleSet(refParams); Enumeration enum = rules.elements(); while(enum.hasMoreElements()) { set.addRule(((ParameterRule)enum.nextElement()).copy()); } return set; } public boolean areParametersGood(float [] params, int mode, ParameterSet set) { boolean good = changeParameters(params, mode, set); if(!good) good = checkParameters(params); return good; /* boolean good = true; Enumeration enum = rules.elements(); while(good && enum.hasMoreElements()) { ParameterRule rule = (ParameterRule)enum.nextElement(); good &= rule.isGood(params); } return good; */ } private boolean changeParameters(float [] params, int mode, ParameterSet set) { boolean good = true; Enumeration enum = rules.elements(); while(enum.hasMoreElements()) { ParameterRule rule = (ParameterRule)enum.nextElement(); good &= rule.changeParameters(params, mode, set); } return good; } private boolean checkParameters(float [] params) { boolean good = true; Enumeration enum = rules.elements(); while(good && enum.hasMoreElements()) { ParameterRule rule = (ParameterRule)enum.nextElement(); good &= rule.isGood(params); } return good; } public float getFuzzyParametersScore(float [] params) { float score = 0; Enumeration enum = rules.elements(); while(enum.hasMoreElements()) { ParameterRule rule = (ParameterRule)enum.nextElement(); score += rule.getFuzzyScore(params); } return score; } /** Loads a set of parameter rules from a stream. Will stop at the first endParamRules tag. It assumes any other tags are names of a parameter rules and calls loadParameter to try and make a rule of that type and load its parameters. */ public void loadParameters(BetterTokenizer tokenizer) throws Exception { GeneralInput.indent++; String info = GeneralInput.findNextIDToken(tokenizer); while(!info.equals("endParameterRules") && !info.equals("endParameterManipulations")) { loadParameter(info, tokenizer); info = GeneralInput.findNextIDToken(tokenizer); } GeneralInput.indent--; } /** Loads a parameter rule from a stream. Tries to make a parameter rule with the given name and then asks that rule to load its own parameters. If everything is succesful, adds the new rule to the rules vector. */ protected void loadParameter(String rule_name, BetterTokenizer tokenizer) throws Exception { try { Class c = Class.forName("parameterrules." + rule_name); ParameterRule newPR = (ParameterRule)c.newInstance(); newPR.init(refParams); newPR.loadParameters(tokenizer); addRule(newPR); } catch(Exception e) { System.out.println("Problem loading parameter rule " + rule_name + ":" + e.toString()); } } public void addRule(ParameterRule rule) { rules.addElement(rule); } }