Skip to content

Commit c70a179

Browse files
committed
Fixed #177: Condition was not reset when collector was reset
If a collector has an reset="true" attribute one would expect that this also resets the state of an if-condition in the collector. However, this was not the case. This commits fixes this. Please note that this may change the behaviour of existing scripts if they relied on the condition not being reset with the reset of the collector.
1 parent cbaadff commit c70a179

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* Common basis for {@link Entity}, {@link Combine} etc.
24-
*
24+
*
2525
* @author Markus Michael Geipel
2626
* @author Christoph Böhme
2727
*
@@ -37,9 +37,9 @@ public abstract class AbstractCollect extends AbstractNamedValuePipeHead impleme
3737
private final Metamorph metamorph;
3838
private boolean waitForFlush;
3939
private boolean conditionMet;
40-
40+
4141
private NamedValueSource conditionSource;
42-
42+
4343
public AbstractCollect(final Metamorph metamorph) {
4444
super();
4545
this.metamorph = metamorph;
@@ -60,11 +60,15 @@ protected final int getEntityCount() {
6060
protected final boolean isConditionMet() {
6161
return conditionMet;
6262
}
63-
63+
6464
protected final void setConditionMet(final boolean conditionMet) {
6565
this.conditionMet = conditionMet;
6666
}
67-
67+
68+
protected final void resetCondition() {
69+
setConditionMet(conditionSource == null);
70+
}
71+
6872
@Override
6973
public final void setWaitForFlush(final boolean waitForFlush) {
7074
this.waitForFlush = waitForFlush;
@@ -75,7 +79,7 @@ public final void setWaitForFlush(final boolean waitForFlush) {
7579
public final void setSameEntity(final boolean sameEntity) {
7680
this.sameEntity = sameEntity;
7781
}
78-
82+
7983
public final boolean getReset() {
8084
return resetAfterEmit;
8185
}
@@ -98,9 +102,9 @@ public final void setName(final String name) {
98102
@Override
99103
public final void setConditionSource(final NamedValueSource conditionSource) {
100104
this.conditionSource = conditionSource;
101-
this.conditionMet = conditionSource == null;
105+
resetCondition();
102106
}
103-
107+
104108
public final String getValue() {
105109
return value;
106110
}
@@ -115,7 +119,7 @@ public final void setValue(final String value) {
115119

116120
protected final void updateCounts(final int currentRecord, final int currentEntity) {
117121
if (!isSameRecord(currentRecord)) {
118-
conditionMet = conditionSource == null;
122+
resetCondition();
119123
clear();
120124
oldRecord = currentRecord;
121125
}
@@ -138,16 +142,17 @@ public final void receive(final String name, final String value, final NamedValu
138142
final int recordCount, final int entityCount) {
139143

140144
updateCounts(recordCount, entityCount);
141-
145+
142146
if (source == conditionSource) {
143147
conditionMet = true;
144148
} else {
145149
receive(name, value, source);
146150
}
147-
151+
148152
if (!waitForFlush && isConditionMet() && isComplete()) {
149153
emit();
150154
if (resetAfterEmit) {
155+
resetCondition();
151156
clear();
152157
}
153158
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/**
2121
* Common basis for {@link Entity}, {@link Combine} etc.
22-
*
22+
*
2323
* @author Markus Michael Geipel
2424
* @author Christoph Böhme
2525
*
@@ -32,12 +32,13 @@ public abstract class AbstractFlushingCollect extends AbstractCollect {
3232
public AbstractFlushingCollect(final Metamorph metamorph) {
3333
super(metamorph);
3434
}
35-
35+
3636
@Override
3737
public final void flush(final int recordCount, final int entityCount) {
3838
if (isSameRecord(recordCount) && sameEntityConstraintSatisfied(entityCount) && isConditionMet()) {
3939
emit();
4040
if (getReset()) {
41+
resetCondition();
4142
clear();
4243
}
4344
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* Corresponds to the <code>&lt;none&gt;</code> tag.
24-
*
24+
*
2525
* @author Christoph Böhme <c.boehme@dnb.de>
2626
*
2727
*/
@@ -32,7 +32,7 @@ public final class None extends AbstractCollect {
3232

3333
private boolean receivedInput;
3434
private boolean emittedResult;
35-
35+
3636
public None(final Metamorph metamorph) {
3737
super(metamorph);
3838
setNamedValueReceiver(metamorph);

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,46 @@
267267
</result>
268268
</test-case>
269269

270+
<test-case name="should reset condition with collector">
271+
<input type="text/x-cg+xml">
272+
<cgxml:cgxml version="1.0">
273+
<cgxml:records>
274+
<cgxml:record id="1">
275+
<cgxml:entity name="entity">
276+
<cgxml:literal name="data1" value="output" />
277+
<cgxml:literal name="data2" value="X" />
278+
</cgxml:entity>
279+
<cgxml:entity name="entity">
280+
<cgxml:literal name="data1" value="no-output" />
281+
<cgxml:literal name="data3" value="X" />
282+
</cgxml:entity>
283+
</cgxml:record>
284+
</cgxml:records>
285+
</cgxml:cgxml>
286+
</input>
287+
288+
<transformation type="text/x-metamorph+xml">
289+
<metamorph version="1" xmlns="http://www.culturegraph.org/metamorph">
290+
<rules>
291+
<combine name="result" value="${VAL}" reset="true">
292+
<if>
293+
<data source="entity.data2" />
294+
</if>
295+
<data source="entity.data1" name="VAL" />
296+
</combine>
297+
</rules>
298+
</metamorph>
299+
</transformation>
300+
301+
<result type="text/x-cg+xml">
302+
<cgxml:cgxml version="1.0">
303+
<cgxml:records>
304+
<cgxml:record id="1">
305+
<cgxml:literal name="result" value="output" />
306+
</cgxml:record>
307+
</cgxml:records>
308+
</cgxml:cgxml>
309+
</result>
310+
</test-case>
311+
270312
</metamorph-test>

0 commit comments

Comments
 (0)