/** * Generates new simulated observations and adds them to the dialogue state. The method returns * true when a new user input has been generated, and false otherwise. * * @return whether a user input has been generated @ */ private boolean addNewObservations() { List<String> newObsVars = new ArrayList<String>(); for (String var : simulatorState.getChanceNodeIds()) { if (var.contains("^o'")) { newObsVars.add(var); } } if (!newObsVars.isEmpty()) { MultivariateDistribution newObs = simulatorState.queryProb(newObsVars); for (String newObsVar : newObsVars) { newObs.modifyVariableId(newObsVar, newObsVar.replace("^o'", "")); } while (system.isPaused()) { try { Thread.sleep(50); } catch (InterruptedException e) { } } if (!newObs.getValues().isEmpty()) { if (newObs.getVariables().contains(system.getSettings().userInput)) { log.fine("Simulator output: " + newObs + "\n --------------"); system.addContent(newObs); return true; } else { log.fine("Contextual variables: " + newObs); system.addContent(newObs); } } } return false; }
/** * public void testTemplateOr() { Template t1 = new Template("var({X})"); Template t2 = new * Template("var3"); Template t3 = new Template("bli"); Template or = new * Template(Arrays.asList(t1, t2, t3)); assertTrue(or.match("var3").isMatching()); * assertTrue(or.match("var(blo)").isMatching()); assertTrue(or.match("bli").isMatching()); * assertFalse(or.match("var3bli").isMatching()); assertFalse(or.match("var").isMatching()); } */ @Test public void testTemplateQuick() { Domain domain = XMLDomainReader.extractDomain("test/domains/quicktest.xml"); DialogueSystem system = new DialogueSystem(domain); system.getSettings().showGUI = false; system.startSystem(); assertEquals(system.getContent("caught").getProb(false), 1.0, 0.01); assertEquals(system.getContent("caught2").getProb(true), 1.0, 0.01); }
/** Adds an empty action to the dialogue system to start the interaction. */ @Override public void start() { Assignment emptyAction = new Assignment(system.getSettings().systemOutput, ValueFactory.none()); if (system.isPaused()) { system.getState().addToState(emptyAction); } else { system.addContent(emptyAction); } system.attachModule(RewardLearner.class); }
private void performTurn() { DialogueState systemState = system.getState(); final String outputVar = system.getSettings().systemOutput; try { Value systemAction = ValueFactory.none(); if (systemState.hasChanceNode(outputVar)) { systemAction = systemState.queryProb(outputVar).getBest(); } log.fine("Simulator input: " + systemAction); boolean turnPerformed = performTurn(systemAction); int repeat = 0; while (!turnPerformed && repeat < 5 && system.getModules().contains(this)) { turnPerformed = performTurn(systemAction); repeat++; } } catch (RuntimeException e) { log.fine("cannot update simulator: " + e); } }
/** * 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; }
/** * Triggers the simulator by updating the simulator state and generating new observations and user * inputs. * * @param systemState the dialogue state of the main dialogue system * @param updatedVars the updated variables in the dialogue system */ @Override public void trigger(final DialogueState systemState, Collection<String> updatedVars) { if (updatedVars.contains(system.getSettings().systemOutput)) { (new Thread(() -> performTurn())).start(); } }
// @Test public void parsingTest() throws InterruptedException { DialogueSystem system = new DialogueSystem(XMLDomainReader.extractDomain(DOMAIN_FILE)); system.getSettings().showGUI = false; system.getSettings().params.setProperty("taggingmodel", TAGGING_MODEL); system.getSettings().params.setProperty("parsingmodel", PARSING_MODEL); system.attachModule(MaltParser.class); system.startSystem(); system.addUserInput("move to the left"); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertTrue(system.getState().hasChanceNode("a_u")); assertEquals(system.getContent("a_u").toDiscrete().getBest().toString(), "Move(left)"); system.addUserInput("what do you see now?"); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertEquals(system.getContent("a_u").toDiscrete().getBest().toString(), "Move(left)"); Map<String, Double> table = new HashMap<String, Double>(); table.put("move a little bit to the left", 0.7); table.put("move a bit to the left", 0.1); system.addUserInput(table); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertTrue(system.getState().hasChanceNode("a_u")); assertEquals(system.getContent("a_u").toDiscrete().getProb("Move(left)"), 0.8, 0.01); table = new HashMap<String, Double>(); table.put("now move a bit to the right please", 0.6); system.addUserInput(table); assertTrue(system.getState().hasChanceNode("parse(u_u)")); assertTrue(system.getState().hasChanceNode("a_u")); assertEquals(system.getContent("a_u").toDiscrete().getProb("Move(right)"), 0.6, 0.01); ParseValue pv = (ParseValue) system .getContent("parse(u_u)") .getValues() .stream() .filter(v -> v instanceof ParseValue) .findFirst() .get(); assertTrue(pv.contains(ValueFactory.create("TO DT JJ"))); assertFalse(pv.contains(ValueFactory.create("DT TT JJ"))); assertTrue(pv.contains(ValueFactory.create("(*,the,*,det,7)"))); assertTrue(pv.contains(ValueFactory.create("(7,*,JJ,*,*)"))); assertTrue(pv.contains(ValueFactory.create("(*,*,DT,det,7)"))); assertTrue(pv.contains(ValueFactory.create("(7,right,*,*,*)"))); assertFalse(pv.contains(ValueFactory.create("(7,left,*,*,*)"))); assertTrue(pv.contains(ValueFactory.create("TO the JJ"))); assertTrue(pv.contains(ValueFactory.create("to the JJ"))); assertTrue(pv.contains(ValueFactory.create("TO DT right"))); assertFalse(pv.contains(ValueFactory.create("TO DT left"))); assertTrue(pv.contains(ValueFactory.create("to/TO the/DT right/JJ"))); assertFalse(pv.contains(ValueFactory.create("to/JJ the/DT right/JJ"))); assertTrue(pv.contains(ValueFactory.create("(*,the,DT,det,7)"))); assertTrue(pv.contains(ValueFactory.create("(7,right,JJ,*,*)"))); assertTrue(pv.contains(ValueFactory.create("JJ"))); assertFalse(pv.contains(ValueFactory.create("RBR"))); table = new HashMap<String, Double>(); table.put("this is a gnome", 0.6); system.addUserInput(table); pv = (ParseValue) system .getContent("parse(u_u)") .getValues() .stream() .filter(v -> v instanceof ParseValue) .findFirst() .get(); assertTrue(pv.contains(ValueFactory.create("DT VBZ DT NN"))); assertEquals("Test successful", system.getContent("i_u").getBest().toString()); }