@Test public void testTemplate7() throws Exception { Template template1 = new Template("here we have slot {A} and slot {B}"); Assignment fillers = new Assignment(); fillers.addPair("A", "apple"); fillers.addPair("B", "banana"); assertEquals("here we have slot apple and slot banana", template1.fillSlots(fillers)); fillers.removePair("B"); assertEquals("B", new Template(template1.fillSlots(fillers)).getSlots().iterator().next()); }
/** * Grounds the parameter by assigning the values in the assignment to the unknown variables * * @param input the grounding assignment * @return the grounded parameter */ public Parameter ground(Assignment input) { if (input.containsVars(expression.getVariables())) { try { double result = expression.evaluate(input); return new FixedParameter(result); } catch (IllegalArgumentException e) { log.warning("cannot ground " + expression + " with " + input); } } String filled = expression.toString(); for (String u : input.getVariables()) { filled.replaceAll(u, input.getValue(u).toString()); } return new ComplexParameter(filled); }
/** * 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; }
/** * 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; }
/** * Performs the dialogue turn in the simulator. * * @param systemAction the last system action. @ */ private synchronized boolean performTurn(Value systemAction) { boolean turnPerformed = false; simulatorState.setParameters(domain.getParameters()); Assignment systemAssign = new Assignment(system.getSettings().systemOutput, systemAction); simulatorState.addToState(systemAssign); while (!simulatorState.getNewVariables().isEmpty()) { Set<String> toProcess = simulatorState.getNewVariables(); simulatorState.reduce(); for (Model model : domain.getModels()) { if (model.isTriggered(simulatorState, toProcess)) { boolean change = model.trigger(simulatorState); if (change && model.isBlocking()) { break; } } } if (!simulatorState.getUtilityNodeIds().isEmpty()) { double reward = simulatorState.queryUtil(); String comment = "Reward: " + StringUtils.getShortForm(reward); system.displayComment(comment); system .getState() .addEvidence(new Assignment("R(" + systemAssign.addPrimes() + ")", reward)); simulatorState.removeNodes(simulatorState.getUtilityNodeIds()); } if (addNewObservations()) { turnPerformed = true; } simulatorState.addEvidence(simulatorState.getSample()); } return turnPerformed; }