Skip to content

Commit 29ee8ed

Browse files
committed
Resolve #49, #210, #250: reset and sameEntity in choose
This commit enables the `reset` and `sameEntity` flags for the `choose` collector (resolves #210 and resolves #250). In order for the `reset` flag to work, the `choose` collector is changed to no longer always clear its value after emitting it (fixes #49). To minimize the impact of this modification on existing metamorph scripts, the `reset` flag is set to true by default. Despite this the behaviour of `choose` is different if it contains an `if`-condition. In the old implementation the chosen value was only cleared after it was actually emitted (so only if the condition was met). Now it is also cleared if it was attempted to be emitted but was not due to the condition not being met. This is in-line with the behaviour of the other collectors.
1 parent aa24cca commit 29ee8ed

File tree

3 files changed

+149
-7
lines changed

3 files changed

+149
-7
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public final class Choose extends AbstractFlushingCollect{
3535
private String value;
3636
private String name;
3737
private int priority = Integer.MAX_VALUE;
38-
private final Map<NamedValueSource, Integer> priorities = new HashMap<NamedValueSource, Integer>();
38+
private final Map<NamedValueSource, Integer> priorities =
39+
new HashMap<NamedValueSource, Integer>();
3940
private int nextPriority;
4041

4142
public Choose(final Metamorph metamorph) {
@@ -45,13 +46,14 @@ public Choose(final Metamorph metamorph) {
4546
@Override
4647
protected void emit() {
4748
if(!isEmpty()){
48-
getNamedValueReceiver().receive(StringUtil.fallback(getName(), name), StringUtil.fallback(getValue(), value), this, getRecordCount(), getEntityCount());
49+
getNamedValueReceiver().receive(StringUtil.fallback(getName(), name),
50+
StringUtil.fallback(getValue(), value), this, getRecordCount(),
51+
getEntityCount());
4952
}
50-
clear();
5153
}
5254

5355
private boolean isEmpty() {
54-
return name==null;
56+
return name == null;
5557
}
5658

5759
@Override
@@ -67,9 +69,9 @@ protected void clear() {
6769
}
6870

6971
@Override
70-
protected void receive(final String name, final String value, final NamedValueSource source) {
72+
protected void receive(final String name, final String value,
73+
final NamedValueSource source) {
7174
final int sourcePriority = priorities.get(source).intValue();
72-
7375
if (sourcePriority <= priority) {
7476
this.value = value;
7577
this.name = name;

src/main/resources/schemata/metamorph.xsd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@
374374
<attribute name="name" type="string" use="optional" />
375375
<attribute name="value" type="string" use="optional" />
376376
<!-- <attribute name="defaultValue" type="string" use="optional" /> -->
377+
<attribute name="reset" type="boolean" use="optional"
378+
default="true" />
379+
<attribute name="sameEntity" type="boolean" use="optional"
380+
default="false" />
377381
<attribute name="flushWith" type="string" use="optional"
378382
default="record" />
379383
</complexType>

src/test/java/org/culturegraph/mf/morph/collectors/ChooseTest.xml

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
</result>
9999
</test-case>
100100

101-
<test-case name="issue110">
101+
<test-case name="issue110_shouldOutputFallBackIfFlushedWithEntity">
102102
<input type="text/x-cg+xml">
103103
<cgxml:cgxml version="1.0">
104104
<cgxml:records>
@@ -130,4 +130,140 @@
130130
</cgxml:cgxml>
131131
</result>
132132
</test-case>
133+
134+
<test-case name="issue210_issue49_shouldRepeatedlyEmitNamedValueIfResetIsFalse">
135+
<input type="text/x-formeta">
136+
{
137+
lit1: data1,
138+
flush: first,
139+
flush: second
140+
}
141+
</input>
142+
<transformation type="text/x-metamorph+xml">
143+
<mm:metamorph version="1">
144+
<mm:rules>
145+
<mm:choose flushWith="flush" reset="false">
146+
<mm:data source="lit1" />
147+
</mm:choose>
148+
</mm:rules>
149+
</mm:metamorph>
150+
</transformation>
151+
<result type="text/x-formeta">
152+
{
153+
lit1: data1,
154+
lit1: data1
155+
}
156+
</result>
157+
</test-case>
158+
159+
<test-case name="issue210_shouldResetAfterEmittingNamedValueIfResetIsTrue">
160+
<input type="text/x-formeta">
161+
{
162+
lit1: data1,
163+
flush: first,
164+
flush: second
165+
}
166+
</input>
167+
<transformation type="text/x-metamorph+xml">
168+
<mm:metamorph version="1">
169+
<mm:rules>
170+
<mm:choose flushWith="flush" reset="true">
171+
<mm:data source="lit1" />
172+
</mm:choose>
173+
</mm:rules>
174+
</mm:metamorph>
175+
</transformation>
176+
<result type="text/x-formeta">
177+
{ lit1: data1 }
178+
</result>
179+
</test-case>
180+
181+
<test-case name="issue210_shouldResetAfterEmittingNamedValueByDefault">
182+
<input type="text/x-formeta">
183+
{
184+
lit1: data1,
185+
flush: first,
186+
flush: second
187+
}
188+
</input>
189+
<transformation type="text/x-metamorph+xml">
190+
<mm:metamorph version="1">
191+
<mm:rules>
192+
<mm:choose flushWith="flush">
193+
<mm:data source="lit1" />
194+
</mm:choose>
195+
</mm:rules>
196+
</mm:metamorph>
197+
</transformation>
198+
<result type="text/x-formeta">
199+
{ lit1: data1 }
200+
</result>
201+
</test-case>
202+
203+
<test-case name="issue250_shouldResetOnEntityChangeIfSameEntityIsTrue">
204+
<input type="text/x-formeta">
205+
{
206+
entity { lit1: data1 }
207+
entity { lit2: data2 }
208+
}
209+
</input>
210+
<transformation type="text/x-metamorph+xml">
211+
<mm:metamorph version="1">
212+
<mm:rules>
213+
<mm:choose sameEntity="true">
214+
<mm:data source="entity.lit1" />
215+
<mm:data source="entity.lit2" />
216+
</mm:choose>
217+
</mm:rules>
218+
</mm:metamorph>
219+
</transformation>
220+
<result type="text/x-formeta">
221+
{ entity.lit2: data2 }
222+
</result>
223+
</test-case>
224+
225+
<test-case name="issue250_shouldNotResetOnEntityChangeIfSameEntityIsFalse">
226+
<input type="text/x-formeta">
227+
{
228+
entity { lit1: data1 }
229+
entity { lit2: data2 }
230+
}
231+
</input>
232+
<transformation type="text/x-metamorph+xml">
233+
<mm:metamorph version="1">
234+
<mm:rules>
235+
<mm:choose sameEntity="false">
236+
<mm:data source="entity.lit1" />
237+
<mm:data source="entity.lit2" />
238+
</mm:choose>
239+
</mm:rules>
240+
</mm:metamorph>
241+
</transformation>
242+
<result type="text/x-formeta">
243+
{ entity.lit1: data1 }
244+
</result>
245+
</test-case>
246+
247+
<test-case name="issue250_shouldNotResetOnEntityChangeByDefault">
248+
<input type="text/x-formeta">
249+
{
250+
entity { lit1: data1 }
251+
entity { lit2: data2 }
252+
}
253+
</input>
254+
<transformation type="text/x-metamorph+xml">
255+
<mm:metamorph version="1">
256+
<mm:rules>
257+
<mm:choose>
258+
<mm:data source="entity.lit1" />
259+
<mm:data source="entity.lit2" />
260+
</mm:choose>
261+
</mm:rules>
262+
</mm:metamorph>
263+
</transformation>
264+
<result type="text/x-formeta">
265+
{ entity.lit1: data1 }
266+
</result>
267+
</test-case>
268+
133269
</metamorph-test>

0 commit comments

Comments
 (0)