}
@Test
public void testResolve_Trigger() {
System.out.println("Triggers resolving a set of references mixed with text like 'temperature is @event.temperature'");
Trigger c = new Trigger();
c.setName("say something using TTS");
c.getPayload().addStatement("zero", "@event.temperature");
c.getPayload().addStatement("one", "temperature is @event.temperature.");
c.getPayload().addStatement("two", "temperature is @event.temperature#celsius degree.");
c.getPayload().addStatement("three", "temperature in @event.zone is @event.temperature.");
c.getPayload().addStatement("four", "temperature in @event.zone is @event.temperature celsius degree.");
c.getPayload().addStatement("five", "temperature in @event.zone is @event.temperature celsius degree. @event.zone# is hot because temperature is +@event.temperature°C.");
c.getPayload().addStatement("six", "temperature in @event.zone is managed by object @event.object.name#.");
c.getPayload().addStatement("seven", "= seven=\"Current temperature is @event.temperature celsius degrees. In fahrenheit is \" + Math.floor(((@event.temperature+40)*1.8)-40) + \" degrees.\";");
c.getPayload().addStatement("eight", "= eight=10+5;"); //this always returns a double
c.getPayload().addStatement("nine", "= nine=Math.floor(10+5).toString();"); //print the number as is to avoid conversion to double
c.getPayload().addStatement("SET", "behaviorValue", "EQUALS", "= if (@event.temperature > 20) behaviorValue=\"it's hot\"; else behaviorValue=\"it's cold\";");
GenericEvent event = new GenericEvent(this);
event.addProperty("zone", "Kitchen");
event.addProperty("temperature", "25");
event.addProperty("object.name", "Indoor Thermometer");
Resolver resolver = new Resolver();
resolver.addContext("event.", event.getPayload());
Trigger result = null;
try {
result = resolver.resolve(c);
} catch (VariableResolutionException ex) {
Assert.fail(ex.getMessage());
}
XStream x = new XStream();
assertEquals("25", result.getPayload().getStatements("zero").get(0).getValue());
assertEquals("temperature is 25.", result.getPayload().getStatements("one").get(0).getValue());
assertEquals("temperature is 25celsius degree.", result.getPayload().getStatements("two").get(0).getValue());
assertEquals("temperature in Kitchen is 25.", result.getPayload().getStatements("three").get(0).getValue());
assertEquals("temperature in Kitchen is 25 celsius degree.", result.getPayload().getStatements("four").get(0).getValue());
assertEquals("temperature in Kitchen is 25 celsius degree. Kitchen is hot because temperature is +25°C.", result.getPayload().getStatements("five").get(0).getValue());
assertEquals("temperature in Kitchen is managed by object Indoor Thermometer.", result.getPayload().getStatements("six").get(0).getValue());
//testing scripting
assertEquals("Current temperature is 25 celsius degrees. In fahrenheit is 77 degrees.", result.getPayload().getStatements("seven").get(0).getValue());
//assertEquals("15.0", result.getPayload().getStatements("eight").get(0).getValue());
//assertEquals("15", result.getPayload().getStatements("nine").get(0).getValue());
assertEquals("it's hot", result.getPayload().getStatements("behaviorValue").get(0).getValue());
}