/** Returns a string representation of the rule case. */ @Override public String toString() { String str = ""; if (!(condition instanceof VoidCondition)) { str += "if (" + condition.toString() + ") then "; } else { str += " "; } str += output.toString(); return str; }
/** * Returns the first rule output whose condition matches the input assignment provided as * argument. The output contains the grounded list of effects associated with the satisfied * condition. * * @param input the input assignment * @return the matched rule output. */ public RuleOutput getOutput(Assignment input) { RuleOutput output = new RuleOutput(ruleType); RuleGrounding groundings = getGroundings(input); for (Assignment g : groundings.getAlternatives()) { Assignment full = !(g.isEmpty()) ? new Assignment(input, g) : input; RuleOutput match = cases .stream() .filter(c -> c.condition.isSatisfiedBy(full)) .map(c -> c.output) .findFirst() .orElse(new RuleOutput(ruleType)); match = match.ground(full); output.addOutput(match); } return output; }
/** * Returns the groundings associated with the rule case, given the input assignment * * @param input the input assignment * @return the resulting groundings */ public RuleGrounding getGroundings(Assignment input) { RuleGrounding groundings = new RuleGrounding(); groundings.add(condition.getGroundings(input)); if (ruleType == RuleType.UTIL) { boolean actionVars = input.containsVars(output.getOutputVariables()); for (Effect e : getEffects()) { if (actionVars) { Condition co = e.convertToCondition(); RuleGrounding effectGrounding = co.getGroundings(input); groundings.add(effectGrounding); } else { Set<String> slots = e.getValueSlots(); slots.removeAll(input.getVariables()); groundings.add(Assignment.createOneValue(slots, "")); } } } return groundings; }
/** * Adds a new case to the abstract rule * * @param condition the condition * @param output the corresponding output */ public void addCase(Condition condition, RuleOutput output) { if (!cases.isEmpty() && cases.get(cases.size() - 1).condition instanceof VoidCondition) { log.warning("new case for rule " + id + " is unreachable (previous case is trivially true)"); } // ensuring that the probability values are between 0 and 1 if (ruleType == RuleType.PROB) { double totalMass = 0; for (Parameter p : output.getParameters()) { if (p instanceof FixedParameter) { double v = ((FixedParameter) p).getValue(); if (v < 0.0) { throw new RuntimeException("probability value must be >=0"); } totalMass += v; } } if (totalMass > 1) { throw new RuntimeException("probability value must be <=1"); } } cases.add(new RuleCase(condition, output)); }
/** * Returns the set of effects for the rule case * * @return the set of effects */ public Set<Effect> getEffects() { return output.getEffects(); }