com.bladecoder.ink.runtime.Story.getVariablesState() - java examples

Here are the examples of the java api com.bladecoder.ink.runtime.Story.getVariablesState() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

10 Examples 7

19 View Complete Implementation : StoryManager.java
Copyright Apache License 2.0
Author : bladecoder
public boolean compareVariable(String name, String value) {
    if (story.getVariablesState().get(name) instanceof InkList) {
        return ((InkList) story.getVariablesState().get(name)).ContainsItemNamed(value);
    } else {
        return story.getVariablesState().get(name).toString().equals(value);
    }
}

19 View Complete Implementation : StoryManager.java
Copyright Apache License 2.0
Author : bladecoder
public String getVariable(String name) {
    return story.getVariablesState().get(name).toString();
}

19 View Complete Implementation : InkManager.java
Copyright Apache License 2.0
Author : bladecoder
public boolean compareVariable(String name, String value) {
    waitIfNotLoaded();
    if (story.getVariablesState().get(name) instanceof InkList) {
        return ((InkList) story.getVariablesState().get(name)).ContainsItemNamed(value);
    } else {
        return story.getVariablesState().get(name).toString().equals(value == null ? "" : value);
    }
}

19 View Complete Implementation : InkBot.java
Copyright Apache License 2.0
Author : rabidgremlin
private void setSlotValuesInInk(Collection<SlotMatch> slotMatches, Story story) throws Exception {
    for (SlotMatch slotMatch : slotMatches) {
        if (slotMatch.getValue() instanceof Number) {
            story.getVariablesState().set(slotMatch.getSlot().getName().toLowerCase(), slotMatch.getValue());
        } else {
            story.getVariablesState().set(slotMatch.getSlot().getName().toLowerCase(), slotMatch.getValue().toString());
        }
    }
}

18 View Complete Implementation : StoryManager.java
Copyright Apache License 2.0
Author : bladecoder
public void setVariable(String name, String value) throws Exception {
    if (story.getVariablesState().get(name) instanceof InkList) {
        InkList rawList = (InkList) story.getVariablesState().get(name);
        if (rawList.getOrigins() == null) {
            List<String> names = rawList.getOriginNames();
            if (names != null) {
                ArrayList<ListDefinition> origins = new ArrayList<ListDefinition>();
                for (String n : names) {
                    ListDefinition def = story.getListDefinitions().getDefinition(n);
                    if (!origins.contains(def))
                        origins.add(def);
                }
                rawList.setOrigins(origins);
            }
        }
        rawList.addItem(value);
    } else
        story.getVariablesState().set(name, value);
}

17 View Complete Implementation : GetLongTermAttributeFunction.java
Copyright Apache License 2.0
Author : rabidgremlin
/*
   * (non-Javadoc)
   * 
   * @see com.rabidgremlin.mutters.bot.ink.InkBotFunction#respondexecute(
   * CurrentResponse currentResponse, Session session, IntentMatch intentMatch,
   * Story story, String param)
   */
@Override
public void execute(CurrentResponse currentResponse, Session session, IntentMatch intentMatch, Story story, String param) {
    FunctionDetails details = FunctionHelper.parseFunctionString(param);
    if (details.getFunctionParams() == null) {
        throw new IllegalArgumentException("Missing name and variable value for GET_LONG_TERM_ATTR");
    }
    String name = details.getFunctionParams().get("name");
    if (name == null) {
        throw new IllegalArgumentException("Missing name value for GET_LONG_TERM_ATTR");
    }
    String var = details.getFunctionParams().get("var");
    if (var == null) {
        throw new IllegalArgumentException("Missing var value for GET_LONG_TERM_ATTR");
    }
    try {
        Object value = session.getLongTermAttribute(name);
        if (value == null) {
            story.getVariablesState().set(var, "");
        } else {
            story.getVariablesState().set(var, value);
        }
    } catch (Exception e) {
        throw new RuntimeException("Failed to get long term attribute", e);
    }
}

16 View Complete Implementation : MiscTest.java
Copyright MIT License
Author : bladecoder
/**
 * Issue: https://github.com/bladecoder/blade-ink/issues/15
 */
@Test
public void issue15() throws Exception {
    String json = TestUtils.getJsonString("inkfiles/misc/issue15.ink.json");
    Story story = new Story(json);
    replacedert.replacedertEquals("This is a test\n", story.Continue());
    while (story.canContinue()) {
        // System.out.println(story.buildStringOfHierarchy());
        String line = story.Continue();
        if (line.startsWith("SET_X:")) {
            story.getVariablesState().set("x", 100);
        } else {
            replacedert.replacedertEquals("X is set\n", line);
        }
    }
}

15 View Complete Implementation : RuntimeSpecTest.java
Copyright MIT License
Author : bladecoder
/**
 * Test set/get variables from code.
 */
@Test
public void setAndGetVariable() throws Exception {
    List<String> text = new ArrayList<String>();
    String json = TestUtils.getJsonString("inkfiles/runtime/set-get-variables.ink.json");
    Story story = new Story(json);
    TestUtils.nextAll(story, text);
    replacedert.replacedertEquals(10, (int) story.getVariablesState().get("x"));
    story.getVariablesState().set("x", 15);
    replacedert.replacedertEquals(15, (int) story.getVariablesState().get("x"));
    story.chooseChoiceIndex(0);
    text.clear();
    TestUtils.nextAll(story, text);
    replacedert.replacedertEquals("OK", text.get(0));
}

15 View Complete Implementation : InkManager.java
Copyright Apache License 2.0
Author : bladecoder
public void setVariable(String name, String value) throws Exception {
    waitIfNotLoaded();
    if (story.getVariablesState().get(name) instanceof InkList) {
        InkList rawList = (InkList) story.getVariablesState().get(name);
        if (rawList.getOrigins() == null) {
            List<String> names = rawList.getOriginNames();
            if (names != null) {
                ArrayList<ListDefinition> origins = new ArrayList<>();
                for (String n : names) {
                    ListDefinition def = story.getListDefinitions().getListDefinition(n);
                    if (!origins.contains(def))
                        origins.add(def);
                }
                rawList.setOrigins(origins);
            }
        }
        rawList.addItem(value);
    } else
        story.getVariablesState().set(name, value);
}

14 View Complete Implementation : RuntimeSpecTest.java
Copyright MIT License
Author : bladecoder
/**
 * Test non existant variable.
 */
@Test
public void testSetNonExistantVariable() throws Exception {
    List<String> text = new ArrayList<String>();
    String json = TestUtils.getJsonString("inkfiles/runtime/set-get-variables.ink.json");
    Story story = new Story(json);
    TestUtils.nextAll(story, text);
    try {
        story.getVariablesState().set("y", "earth");
        replacedert.fail("Setting non existant variable.");
    } catch (StoryException e) {
    }
    replacedert.replacedertEquals(10, (int) story.getVariablesState().get("x"));
    story.getVariablesState().set("x", 15);
    replacedert.replacedertEquals(15, (int) story.getVariablesState().get("x"));
    story.chooseChoiceIndex(0);
    text.clear();
    TestUtils.nextAll(story, text);
    replacedert.replacedertEquals("OK", text.get(0));
}