Skip to content

Commit f93cade

Browse files
committed
Fixed #178: Reset conditions if sameEntity is true
If a collector has an `sameEntity="true"` set, it is reset whenever the current entity changes. As described in issue #178 the current implementation fails to reset the condition during these reset operations. This commit fixes this. Additionally, the test cases for the if-condition have been moved into a separate xml-file as quite a number of tests have accumulated so that it makes sense to keep them in there on test file.
1 parent 12306f8 commit f93cade

File tree

4 files changed

+281
-189
lines changed

4 files changed

+281
-189
lines changed

src/main/java/org/culturegraph/mf/morph/collectors/AbstractCollect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ protected final void updateCounts(final int currentRecord, final int currentEnti
124124
oldRecord = currentRecord;
125125
}
126126
if (resetNeedFor(currentEntity)) {
127+
resetCondition();
127128
clear();
128129
}
129130
oldEntity = currentEntity;

src/test/java/org/culturegraph/mf/morph/collectors/CollectorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@
2525
*/
2626
@RunWith(TestSuite.class)
2727
@TestDefinitions({ "AllTest.xml", "AnyTest.xml", "NoneTest.xml", "CombineTest.xml", "GroupTest.xml", "ChooseTest.xml", "EntityTest.xml", "ConcatTest.xml",
28-
"Nested.xml", "NestedEntity.xml", "TuplesTest.xml", "Misc.xml", "SquareTest.xml", "RangeTest.xml", "EqualsFilterTest.xml" })
28+
"Nested.xml", "NestedEntity.xml", "TuplesTest.xml", "Misc.xml", "SquareTest.xml", "RangeTest.xml", "EqualsFilterTest.xml", "If.xml" })
2929
public final class CollectorTest {/* bind to xml test */
3030
}
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<metamorph-test version="1.0"
3+
xmlns="http://www.culturegraph.org/metamorph-test" xmlns:cgxml="http://www.culturegraph.org/cgxml">
4+
5+
<test-case name="should only fire if condition is met">
6+
<input type="text/x-cg+xml">
7+
<cgxml:cgxml version="1.0">
8+
<cgxml:records>
9+
<cgxml:record id="1">
10+
<cgxml:literal name="data1" value="a" />
11+
<cgxml:literal name="data2" value="b" />
12+
<cgxml:literal name="data3" value="c" />
13+
</cgxml:record>
14+
<cgxml:record id="2">
15+
<cgxml:literal name="data1" value="a" />
16+
<cgxml:literal name="data2" value="b" />
17+
<cgxml:literal name="data4" value="c" />
18+
</cgxml:record>
19+
</cgxml:records>
20+
</cgxml:cgxml>
21+
</input>
22+
23+
<transformation type="text/x-metamorph+xml">
24+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
25+
<rules>
26+
<combine name="combined" value="${data1}-${data2}">
27+
<if>
28+
<data source="data3" />
29+
</if>
30+
<data source="data1" />
31+
<data source="data2" />
32+
</combine>
33+
</rules>
34+
</metamorph>
35+
</transformation>
36+
37+
<result type="text/x-cg+xml">
38+
<cgxml:cgxml version="1.0">
39+
<cgxml:records>
40+
<cgxml:record id="1">
41+
<cgxml:literal name="combined" value="a-b" />
42+
</cgxml:record>
43+
<cgxml:record id="2">
44+
</cgxml:record>
45+
</cgxml:records>
46+
</cgxml:cgxml>
47+
</result>
48+
</test-case>
49+
50+
<test-case name="should allow to use same source in body and condition">
51+
<input type="text/x-cg+xml">
52+
<cgxml:cgxml version="1.0">
53+
<cgxml:records>
54+
<cgxml:record id="1">
55+
<cgxml:literal name="data1" value="a" />
56+
<cgxml:literal name="data2" value="b" />
57+
</cgxml:record>
58+
<cgxml:record id="2">
59+
<cgxml:literal name="data1" value="a" />
60+
<cgxml:literal name="data2" value="c" />
61+
</cgxml:record>
62+
</cgxml:records>
63+
</cgxml:cgxml>
64+
</input>
65+
66+
<transformation type="text/x-metamorph+xml">
67+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
68+
<rules>
69+
<combine name="combined" value="${data1}-${data2}">
70+
<if>
71+
<data source="data2">
72+
<equals string="b" />
73+
</data>
74+
</if>
75+
<data source="data1" />
76+
<data source="data2" />
77+
</combine>
78+
</rules>
79+
</metamorph>
80+
</transformation>
81+
82+
<result type="text/x-cg+xml">
83+
<cgxml:cgxml version="1.0">
84+
<cgxml:records>
85+
<cgxml:record id="1">
86+
<cgxml:literal name="combined" value="a-b" />
87+
</cgxml:record>
88+
<cgxml:record id="2">
89+
</cgxml:record>
90+
</cgxml:records>
91+
</cgxml:cgxml>
92+
</result>
93+
</test-case>
94+
95+
<test-case name="should allow quantors in if statements">
96+
<input type="text/x-cg+xml">
97+
<cgxml:cgxml version="1.0">
98+
<cgxml:records>
99+
<cgxml:record id="1">
100+
<cgxml:literal name="data1" value="a" />
101+
<cgxml:literal name="data2" value="b" />
102+
<cgxml:literal name="data3" value="c" />
103+
</cgxml:record>
104+
<cgxml:record id="2">
105+
<cgxml:literal name="data1" value="a" />
106+
<cgxml:literal name="data2" value="d" />
107+
<cgxml:literal name="data4" value="c" />
108+
</cgxml:record>
109+
<cgxml:record id="3">
110+
<cgxml:literal name="data1" value="a" />
111+
<cgxml:literal name="data2" value="b" />
112+
<cgxml:literal name="data5" value="c" />
113+
</cgxml:record>
114+
</cgxml:records>
115+
</cgxml:cgxml>
116+
</input>
117+
118+
<transformation type="text/x-metamorph+xml">
119+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
120+
<rules>
121+
<combine name="combined" value="${data1}-${data2}">
122+
<if>
123+
<any>
124+
<data source="data3" />
125+
<data source="data4" />
126+
</any>
127+
</if>
128+
<data source="data1" />
129+
<data source="data2" />
130+
</combine>
131+
</rules>
132+
</metamorph>
133+
</transformation>
134+
135+
<result type="text/x-cg+xml">
136+
<cgxml:cgxml version="1.0">
137+
<cgxml:records>
138+
<cgxml:record id="1">
139+
<cgxml:literal name="combined" value="a-b" />
140+
</cgxml:record>
141+
<cgxml:record id="2">
142+
<cgxml:literal name="combined" value="a-d" />
143+
</cgxml:record>
144+
<cgxml:record id="3">
145+
</cgxml:record>
146+
</cgxml:records>
147+
</cgxml:cgxml>
148+
</result>
149+
</test-case>
150+
151+
<test-case name="should reset condition with collector">
152+
<input type="text/x-cg+xml">
153+
<cgxml:cgxml version="1.0">
154+
<cgxml:records>
155+
<cgxml:record id="1">
156+
<cgxml:entity name="entity">
157+
<cgxml:literal name="data1" value="output" />
158+
<cgxml:literal name="data2" value="X" />
159+
</cgxml:entity>
160+
<cgxml:entity name="entity">
161+
<cgxml:literal name="data1" value="no-output" />
162+
<cgxml:literal name="data3" value="X" />
163+
</cgxml:entity>
164+
</cgxml:record>
165+
</cgxml:records>
166+
</cgxml:cgxml>
167+
</input>
168+
169+
<transformation type="text/x-metamorph+xml">
170+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
171+
<rules>
172+
<combine name="result" value="${VAL}" reset="true">
173+
<if>
174+
<data source="entity.data2" />
175+
</if>
176+
<data source="entity.data1" name="VAL" />
177+
</combine>
178+
</rules>
179+
</metamorph>
180+
</transformation>
181+
182+
<result type="text/x-cg+xml">
183+
<cgxml:cgxml version="1.0">
184+
<cgxml:records>
185+
<cgxml:record id="1">
186+
<cgxml:literal name="result" value="output" />
187+
</cgxml:record>
188+
</cgxml:records>
189+
</cgxml:cgxml>
190+
</result>
191+
</test-case>
192+
193+
<test-case name="should reset condition with collector on flushWith">
194+
<input type="text/x-cg+xml">
195+
<cgxml:cgxml version="1.0">
196+
<cgxml:records>
197+
<cgxml:record id="1">
198+
<cgxml:entity name="entity">
199+
<cgxml:literal name="data1" value="output" />
200+
<cgxml:literal name="data2" value="X" />
201+
</cgxml:entity>
202+
<cgxml:entity name="entity">
203+
<cgxml:literal name="data1" value="no-output" />
204+
<cgxml:literal name="data3" value="X" />
205+
</cgxml:entity>
206+
</cgxml:record>
207+
</cgxml:records>
208+
</cgxml:cgxml>
209+
</input>
210+
211+
<transformation type="text/x-metamorph+xml">
212+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
213+
<rules>
214+
<combine name="result" value="${VAL1}${VAL2}" reset="true" flushWith="entity">
215+
<if>
216+
<data source="entity.data2" />
217+
</if>
218+
<data source="entity.data1" name="VAL1" />
219+
<data source="entity.data4" name="VAL2" />
220+
</combine>
221+
</rules>
222+
</metamorph>
223+
</transformation>
224+
225+
<result type="text/x-cg+xml">
226+
<cgxml:cgxml version="1.0">
227+
<cgxml:records>
228+
<cgxml:record id="1">
229+
<cgxml:literal name="result" value="output" />
230+
</cgxml:record>
231+
</cgxml:records>
232+
</cgxml:cgxml>
233+
</result>
234+
</test-case>
235+
236+
<test-case name="should reset condition with collector on same entity">
237+
<input type="text/x-cg+xml">
238+
<cgxml:cgxml version="1.0">
239+
<cgxml:records>
240+
<cgxml:record id="1">
241+
<cgxml:entity name="entity">
242+
<cgxml:literal name="data1" value="output" />
243+
<cgxml:literal name="data2" value="X" />
244+
</cgxml:entity>
245+
<cgxml:entity name="entity">
246+
<cgxml:literal name="data1" value="no-output" />
247+
<cgxml:literal name="data3" value="X" />
248+
<cgxml:literal name="data4" value="extra-output" />
249+
</cgxml:entity>
250+
</cgxml:record>
251+
</cgxml:records>
252+
</cgxml:cgxml>
253+
</input>
254+
255+
<transformation type="text/x-metamorph+xml">
256+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
257+
<rules>
258+
<combine name="result" value="${VAL1}+${VAL2}" sameEntity="true">
259+
<if>
260+
<data source="entity.data2" />
261+
</if>
262+
<data source="entity.data1" name="VAL1" />
263+
<data source="entity.data4" name="VAL2" />
264+
</combine>
265+
</rules>
266+
</metamorph>
267+
</transformation>
268+
269+
<result type="text/x-cg+xml">
270+
<cgxml:cgxml version="1.0">
271+
<cgxml:records>
272+
<cgxml:record id="1">
273+
</cgxml:record>
274+
</cgxml:records>
275+
</cgxml:cgxml>
276+
</result>
277+
</test-case>
278+
279+
</metamorph-test>

0 commit comments

Comments
 (0)