source: dialogmanager/src/test/java/tudelft/dialogmanager/UpdateFunctionTest.java@ 44

Last change on this file since 44 was 44, checked in by wouter, 5 years ago

#30 added processing 'with black box' by adding UpdateFunction to the phases that can do arbitrary java computation.

File size: 2.0 KB
RevLine 
[44]1package tudelft.dialogmanager;
2
3import static org.junit.Assert.assertEquals;
4
5import java.io.IOException;
6import java.util.Arrays;
7import java.util.List;
8
9import org.junit.Before;
10import org.junit.Test;
11
12import com.fasterxml.jackson.annotation.JsonCreator;
13import com.fasterxml.jackson.core.JsonParseException;
14import com.fasterxml.jackson.core.JsonProcessingException;
15import com.fasterxml.jackson.databind.JsonMappingException;
16import com.fasterxml.jackson.databind.ObjectMapper;
17
18import tudelft.dialogmanager.parameters.DoubleValue;
19import tudelft.dialogmanager.parameters.ParameterValue;
20
21public class UpdateFunctionTest {
22
23 private final ObjectMapper jackson = new ObjectMapper();
24 private final String serialized = "{\"AddFunction\":[[\"a\",\"b\"],[\"sum\"]]}";;
25 private final AddFunction testf = new AddFunction(
26 Arrays.asList(Arrays.asList("a", "b"), Arrays.asList("sum")));
27
28 @Before
29 public void before() {
30 jackson.registerSubtypes(AddFunction.class);
31 }
32
33 @Test
34 public void testSerialize() throws JsonProcessingException {
35
36 String text = jackson.writeValueAsString(testf);
37 System.out.println(text);
38
39 assertEquals(serialized, text);
40 }
41
42 @Test
43 public void testDeserialize()
44 throws JsonParseException, JsonMappingException, IOException {
45 UpdateFunction func = jackson.readValue(serialized,
46 UpdateFunction.class);
47 assertEquals(testf, func);
48 }
49
50 @Test
51 public void testFunctionCall() {
52 DoubleValue a = new DoubleValue(3d);
53 DoubleValue b = new DoubleValue(4d);
54 DoubleValue sum = new DoubleValue(7d);
55 assertEquals(Arrays.asList(sum), testf.call(Arrays.asList(a, b)));
56 }
57
58}
59
60/**
61 * Function that computes sum of argumets
62 */
63class AddFunction extends UpdateFunction {
64
65 @JsonCreator
66 public AddFunction(List<List<String>> inout) {
67 super(inout);
68 }
69
70 @Override
71 public List<ParameterValue> call(List<ParameterValue> inargs) {
72 Double a = ((DoubleValue) inargs.get(0)).getValue();
73 Double b = ((DoubleValue) inargs.get(1)).getValue();
74 return Arrays.asList(new DoubleValue(a + b));
75 }
76
77}
Note: See TracBrowser for help on using the repository browser.