1 | package com.framsticks.parsers; |
---|
2 | |
---|
3 | import org.testng.annotations.*; |
---|
4 | |
---|
5 | import com.framsticks.model.*; |
---|
6 | import com.framsticks.model.f0.Schema; |
---|
7 | import com.framsticks.model.f0.SchemaBuilder; |
---|
8 | import com.framsticks.params.*; |
---|
9 | import com.framsticks.params.types.FloatParam; |
---|
10 | import com.framsticks.params.types.StringParam; |
---|
11 | import com.framsticks.test.TestConfiguration; |
---|
12 | import com.framsticks.util.math.Point3d; |
---|
13 | |
---|
14 | import java.io.IOException; |
---|
15 | import java.text.ParseException; |
---|
16 | import java.util.List; |
---|
17 | import static org.fest.assertions.Assertions.*; |
---|
18 | import static org.fest.assertions.Delta.*; |
---|
19 | |
---|
20 | /** |
---|
21 | * Author: Piotr Śniegowski |
---|
22 | */ |
---|
23 | public class F0ParserTest extends TestConfiguration { |
---|
24 | |
---|
25 | private Schema schema; |
---|
26 | private List<AccessInterface> accesses; |
---|
27 | private List<ModelComponent> components; |
---|
28 | private Model model; |
---|
29 | |
---|
30 | @BeforeClass |
---|
31 | public void setUp() throws Exception { |
---|
32 | schema = new SchemaBuilder().stream(Schema.getDefaultDefinitionAsStream()).finish(); |
---|
33 | } |
---|
34 | |
---|
35 | @Test |
---|
36 | public void checkFramsClasses() { |
---|
37 | FramsClass modelClass = schema.getRegistry().getFramsClass("Model"); |
---|
38 | assertThat(modelClass).isNotNull(); |
---|
39 | assertThat(modelClass.getParam("se")).isInstanceOf(FloatParam.class); |
---|
40 | // assertThat(modelClass.getParam("energ0")).isInstanceOf(FloatParam.class); |
---|
41 | assertThat(modelClass.getParam("Vstyle")).isInstanceOf(StringParam.class); |
---|
42 | assertThat(modelClass.getParamCount()).isEqualTo(6); |
---|
43 | // assertThat(); |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | @Test(dependsOnMethods = "checkFramsClasses") |
---|
48 | public void primitiveParam() { |
---|
49 | FramsClass joint = schema.getFramsClass("j"); |
---|
50 | PrimitiveParam<?> dx = joint.getParamEntry("dx", PrimitiveParam.class); |
---|
51 | assertThat(dx).isInstanceOf(FloatParam.class); |
---|
52 | assertThat(schema.getNeuroClasses().size()).isEqualTo(21); |
---|
53 | assertThat(dx.getName()).isEqualTo("delta.x"); |
---|
54 | assertThat(dx.getMin(Double.class)).isEqualTo(-2.0, delta(0.0)); |
---|
55 | |
---|
56 | assertThat(schema.getRegistry().getFramsClass("n").getParamEntry("d", StringParam.class).getDef(String.class)).isEqualTo("N"); |
---|
57 | } |
---|
58 | |
---|
59 | @Test(dependsOnMethods = "primitiveParam") |
---|
60 | public void readF0() throws IOException, ParseException { |
---|
61 | assertThat(schema.getFramsClass("p")).isInstanceOf(FramsClass.class); |
---|
62 | assertThat(schema.getRegistry().getFramsClass("p").getParamEntry("as", FloatParam.class).getDef(Double.class)).isEqualTo(0.25, delta(0.0)); |
---|
63 | |
---|
64 | accesses = new F0Parser(schema, F0ParserTest.class.getResourceAsStream("/parsers/f0_example.txt")).parse(); |
---|
65 | |
---|
66 | assertThat(accesses.size()).isEqualTo(19); |
---|
67 | assertThat(accesses.get(0).getSelected()).isInstanceOf(Model.class); |
---|
68 | assertThat(accesses.get(5).get("i", String.class)).isEqualTo("1,2,3,\"dsadsa,,,,"); |
---|
69 | assertThat(accesses.get(7).get("d", String.class)).isEqualTo("|:p=0.25,r=1"); |
---|
70 | assertThat(accesses.get(10).get("d", String.class)).isEqualTo("@:p=0.25"); |
---|
71 | } |
---|
72 | |
---|
73 | @Test(dependsOnMethods = {"readF0"}) |
---|
74 | public void stripAccessInterface() { |
---|
75 | components = Util.stripAccessInterface(accesses, ModelComponent.class); |
---|
76 | |
---|
77 | assertThat(components.get(1)).isInstanceOf(Part.class); |
---|
78 | assertThat(components.get(4)).isInstanceOf(Joint.class); |
---|
79 | assertThat(components.get(6)).isInstanceOf(NeuroDefinition.class); |
---|
80 | assertThat(components.get(12)).isInstanceOf(NeuroConnection.class); |
---|
81 | } |
---|
82 | |
---|
83 | @Test(dependsOnMethods = {"stripAccessInterface"}) |
---|
84 | public void buildModel() { |
---|
85 | model = new ModelBuilder().addComponents(components).finish(); |
---|
86 | |
---|
87 | assertThat(model.getParts().size()).isEqualTo(3); |
---|
88 | assertThat(model.getNeuroDefinitions().size()).isEqualTo(6); |
---|
89 | assertThat(model.getJoints().size()).isEqualTo(2); |
---|
90 | assertThat(model.getNeuroConnections().size()).isEqualTo(7); |
---|
91 | |
---|
92 | assertThat(model.getParts().get(1).getPosition().x).isEqualTo(2.0, delta(0.0)); |
---|
93 | assertThat(model.getParts().get(2).getPosition().sub(new Point3d(2.27236, -0.0792596, -0.958924)).length()).isLessThan(0.0001); |
---|
94 | assertThat(model.getParts().get(2).getOrientation().y.sub(new Point3d(0.870277, -0.404792, 0.280644)).length()).isLessThan(0.0001); |
---|
95 | |
---|
96 | assertThat(model.getJoints().get(0).part1).isEqualTo(0); |
---|
97 | assertThat(model.getJoints().get(0).part2).isEqualTo(1); |
---|
98 | |
---|
99 | assertThat(model.getNeuroDefinitions().get(0).part).isEqualTo(1); |
---|
100 | assertThat(model.getNeuroDefinitions().get(0).joint).isEqualTo(-1); |
---|
101 | assertThat(model.getNeuroDefinitions().get(1).details).isEqualTo("|:p=0.25,r=1"); |
---|
102 | assertThat(model.getNeuroDefinitions().get(3).details).isEqualTo("N"); |
---|
103 | assertThat(model.getNeuroDefinitions().get(4).part).isEqualTo(-1); |
---|
104 | |
---|
105 | assertThat(model.getNeuroConnections().get(2).connectedNeuro).isEqualTo(0); |
---|
106 | assertThat(model.getNeuroConnections().get(5).weight).isEqualTo(5.6, delta(0.0)); |
---|
107 | |
---|
108 | } |
---|
109 | |
---|
110 | @Test(dependsOnMethods = {"buildModel"}) |
---|
111 | public void print() throws Exception { |
---|
112 | ListSink sink = new ListSink(); |
---|
113 | |
---|
114 | new F0Writer(schema, model, sink).write(); |
---|
115 | |
---|
116 | assertThat(sink.getOut()).containsExactly( |
---|
117 | "p:", |
---|
118 | "p:2.0,i=,Vstyle=", |
---|
119 | "p:2.272364001928095,-0.07925961087140347,-0.9589242746631385,i=bla", |
---|
120 | "j:0,1,dx=2.0", |
---|
121 | "j:1,2,rx=8.0,5.0,6.0,dx=1.0,i=\"1,2,3,\\\"dsadsa,,,,\"", |
---|
122 | "n:p=1", |
---|
123 | "n:j=0,d=\"|:p=0.25,r=1\"", |
---|
124 | "n:j=0,d=G", |
---|
125 | "n:p=1", |
---|
126 | "n:j=0,d=@:p=0.25", |
---|
127 | "n:p=1,d=Nu", |
---|
128 | "c:0,2", |
---|
129 | "c:0,2,5.4", |
---|
130 | "c:1,0", |
---|
131 | "c:2,0,3.4", |
---|
132 | "c:0,1,4.5", |
---|
133 | "c:1,0,5.6", |
---|
134 | "c:2,1", |
---|
135 | "m:" |
---|
136 | ); |
---|
137 | |
---|
138 | } |
---|
139 | } |
---|