Skip to content

Commit 994edae

Browse files
committed
Added an <any> statement to metamorph.
The statement implements a existential quantor or disjunction operation (logical or-operation). It allows for checking whether at least one of the statements contained in the <any> statement generated an output. Using the <any> statement disjunctions can be written in a more expressive way than (ab)using the <combine> statement allows. By default the <any> statement outputs an unamed literal with "true" as value. However, this can be customized. <any name="MyCustomName" value="one-fired"> <data source="data1" /> <data source="data2" /> </any> The <any> statement supports reset and flushWith. Conflicts: src/test/java/org/culturegraph/mf/morph/collectors/CollectorTest.java
1 parent 272cb53 commit 994edae

File tree

17 files changed

+416
-64
lines changed

17 files changed

+416
-64
lines changed

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

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
*/
2828
public abstract class AbstractCollect extends AbstractNamedValuePipeHead implements Collect {
2929

30-
//private static final String FLUSH = "_flush";
31-
// private static final Logger LOG = LoggerFactory.getLogger(AbstractCollect.class);
32-
3330
private int oldRecord;
3431
private int oldEntity;
3532
private boolean resetAfterEmit;
@@ -39,13 +36,12 @@ public abstract class AbstractCollect extends AbstractNamedValuePipeHead impleme
3936
private final Metamorph metamorph;
4037
private boolean waitForFlush;
4138

42-
4339
public AbstractCollect(final Metamorph metamorph) {
4440
super();
4541
this.metamorph = metamorph;
4642
}
47-
48-
protected final Metamorph getMetamorph(){
43+
44+
protected final Metamorph getMetamorph() {
4945
return metamorph;
5046
}
5147

@@ -63,33 +59,30 @@ public final void setWaitForFlush(final boolean waitForFlush) {
6359
//metamorph.addEntityEndListener(this, flushEntity);
6460
}
6561

66-
67-
6862
@Override
6963
public final void setSameEntity(final boolean sameEntity) {
7064
this.sameEntity = sameEntity;
7165
}
72-
66+
67+
public final boolean getReset() {
68+
return resetAfterEmit;
69+
}
7370

7471
@Override
7572
public final void setReset(final boolean reset) {
7673
this.resetAfterEmit = reset;
7774
}
7875

79-
80-
8176
@Override
8277
public final String getName() {
8378
return name;
8479
}
8580

86-
8781
@Override
8882
public final void setName(final String name) {
8983
this.name = name;
9084
}
9185

92-
9386
public final String getValue() {
9487
return value;
9588
}
@@ -102,7 +95,6 @@ public final void setValue(final String value) {
10295
this.value = value;
10396
}
10497

105-
10698
private void updateCounts(final int currentRecord, final int currentEntity) {
10799
if (!isSameRecord(currentRecord)) {
108100
clear();
@@ -118,56 +110,48 @@ private boolean resetNeedFor(final int currentEntity) {
118110
return sameEntity && oldEntity != currentEntity;
119111
}
120112

121-
private boolean isSameRecord(final int currentRecord) {
113+
protected final boolean isSameRecord(final int currentRecord) {
122114
return currentRecord == oldRecord;
123115
}
124116

125117
@Override
126-
public final void receive(final String name, final String value, final NamedValueSource source, final int recordCount, final int entityCount) {
127-
updateCounts(recordCount, entityCount);
128-
129-
// if(FLUSH.equals(name)){
130-
// flush(name, recordCount, entityCount);
131-
// }
132-
133-
receive(name, value, source);
134-
135-
if (!waitForFlush && isComplete()) {
136-
emit();
137-
if (resetAfterEmit) {
138-
clear();
139-
}
140-
}
141-
}
142-
118+
public final void receive(final String name, final String value, final NamedValueSource source,
119+
final int recordCount, final int entityCount) {
120+
updateCounts(recordCount, entityCount);
121+
122+
// if(FLUSH.equals(name)){
123+
// flush(name, recordCount, entityCount);
124+
// }
125+
126+
receive(name, value, source);
127+
128+
if (!waitForFlush && isComplete()) {
129+
emit();
130+
if (resetAfterEmit) {
131+
clear();
132+
}
133+
}
134+
}
143135

144136
@Override
145137
public final void addNamedValueSource(final NamedValueSource namedValueSource) {
146138
onNamedValueSourceAdded(namedValueSource);
147139
}
148140

149-
protected void onNamedValueSourceAdded(final NamedValueSource namedValueSource){
150-
//nothing to do
141+
protected void onNamedValueSourceAdded(final NamedValueSource namedValueSource) {
142+
//nothing to do
151143
}
152144

153-
@Override
154-
public final void flush(final int recordCount, final int entityCount) {
155-
if (isSameRecord(recordCount) && sameEntityConstraintSatisfied(entityCount)) {
156-
emit();
157-
if (resetAfterEmit) {
158-
clear();
159-
}
160-
}
161-
}
162-
163-
private boolean sameEntityConstraintSatisfied(final int entityCount) {
145+
protected final boolean sameEntityConstraintSatisfied(final int entityCount) {
164146
return !sameEntity || oldEntity == entityCount;
165147
}
166148

167149
protected abstract void receive(final String name, final String value, final NamedValueSource source);
150+
168151
protected abstract boolean isComplete();
152+
169153
protected abstract void clear();
154+
170155
protected abstract void emit();
171156

172-
173-
}
157+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2013 Deutsche Nationalbibliothek
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.morph.collectors;
17+
18+
import org.culturegraph.mf.morph.Metamorph;
19+
20+
/**
21+
* Common basis for {@link Entity}, {@link Combine} etc.
22+
*
23+
* @author Markus Michael Geipel
24+
25+
*/
26+
public abstract class AbstractFlushingCollect extends AbstractCollect {
27+
28+
//private static final String FLUSH = "_flush";
29+
// private static final Logger LOG = LoggerFactory.getLogger(AbstractCollect.class);
30+
31+
public AbstractFlushingCollect(final Metamorph metamorph) {
32+
super(metamorph);
33+
}
34+
35+
@Override
36+
public final void flush(final int recordCount, final int entityCount) {
37+
if (isSameRecord(recordCount) && sameEntityConstraintSatisfied(entityCount)) {
38+
emit();
39+
if (getReset()) {
40+
clear();
41+
}
42+
}
43+
}
44+
45+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @author Christoph Böhme <[email protected]>
2929
*
3030
*/
31-
public final class All extends AbstractCollect {
31+
public final class All extends AbstractFlushingCollect {
3232

3333
private static final String DEFAULT_NAME = "";
3434
private static final String DEFAULT_VALUE = "true";
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2013 Christoph Böhme
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.morph.collectors;
17+
18+
import org.culturegraph.mf.morph.Metamorph;
19+
import org.culturegraph.mf.morph.NamedValueSource;
20+
import org.culturegraph.mf.util.StringUtil;
21+
22+
/**
23+
* Corresponds to the <code>&lt;any&gt;</code> tag.
24+
*
25+
* @author Christoph Böhme <[email protected]>
26+
*
27+
*/
28+
public final class Any extends AbstractCollect {
29+
30+
private static final String DEFAULT_NAME = "";
31+
private static final String DEFAULT_VALUE = "true";
32+
33+
private boolean receivedInput;
34+
private boolean emittedResult;
35+
36+
public Any(final Metamorph metamorph) {
37+
super(metamorph);
38+
setNamedValueReceiver(metamorph);
39+
}
40+
41+
@Override
42+
protected void receive(final String name, final String value, final NamedValueSource source) {
43+
receivedInput = true;
44+
}
45+
46+
@Override
47+
protected boolean isComplete() {
48+
return receivedInput;
49+
}
50+
51+
@Override
52+
protected void clear() {
53+
receivedInput = false;
54+
emittedResult = false;
55+
}
56+
57+
@Override
58+
protected void emit() {
59+
if (receivedInput && !emittedResult) {
60+
final String name = StringUtil.fallback(getName(), DEFAULT_NAME);
61+
final String value = StringUtil.fallback(getValue(), DEFAULT_VALUE);
62+
getNamedValueReceiver().receive(name, value, this, getRecordCount(), getEntityCount());
63+
emittedResult = true;
64+
}
65+
}
66+
67+
@Override
68+
public void flush(final int recordCount, final int entityCount) {
69+
emit();
70+
receivedInput = false;
71+
emittedResult = false;
72+
}
73+
74+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @author Christoph Böhme <[email protected]>, Markus Michael Geipel
3131
*
3232
*/
33-
public final class Choose extends AbstractCollect{
33+
public final class Choose extends AbstractFlushingCollect{
3434

3535
private String value;
3636
private String name;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @author Markus Michael Geipel
3333
*/
34-
public final class Combine extends AbstractCollect{
34+
public final class Combine extends AbstractFlushingCollect{
3535
private final Map<String, String> variables = new HashMap<String, String>();
3636
private final Set<NamedValueSource> sources = new HashSet<NamedValueSource>();
3737
private final Set<NamedValueSource> sourcesLeft = new HashSet<NamedValueSource>();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Markus Michael Geipel
2525
*/
26-
public final class Concat extends AbstractCollect{
26+
public final class Concat extends AbstractFlushingCollect{
2727
private final StringBuilder builder = new StringBuilder();
2828
private String prefix = "";
2929
private String postfix = "";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Markus Michael Geipel
3636
*/
37-
public final class Entity extends AbstractCollect {
37+
public final class Entity extends AbstractFlushingCollect {
3838
// private static final Logger LOG = LoggerFactory.getLogger(Entity.class);
3939

4040
// public static final String ENTITY_NAME = "_entity_name";

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727

2828

2929
/**
30-
* Corresponds to the <code>&lt;equalsFilter-literal&gt;</code> tag. Emits data if values in variables are all equal.
31-
*
30+
* Corresponds to the <code>&lt;equalsFilter-literal&gt;</code> tag. Emits data if values in variables are all equal.
31+
*
3232
* @author Thomas Haidlas
3333
*/
34-
public final class EqualsFilter extends AbstractCollect{
34+
public final class EqualsFilter extends AbstractFlushingCollect{
3535
private final Map<String, String> variables = new HashMap<String, String>();
3636
private final Set<NamedValueSource> sources = new HashSet<NamedValueSource>();
3737
private final Set<NamedValueSource> sourcesLeft = new HashSet<NamedValueSource>();
38-
private boolean isEqual = true;
39-
38+
private boolean isEqual = true;
39+
4040
public EqualsFilter(final Metamorph metamorph) {
4141
super(metamorph);
4242
setNamedValueReceiver(metamorph);
@@ -64,10 +64,10 @@ protected void receive(final String name, final String value, final NamedValueSo
6464
this.isEqual = false;
6565
}
6666
this.variables.put(name, value);
67-
this.sourcesLeft.remove(source);
67+
this.sourcesLeft.remove(source);
6868
}
6969

70-
70+
7171
@Override
7272
public void onNamedValueSourceAdded(final NamedValueSource namedValueSource) {
7373
this.sources.add(namedValueSource);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* @author Markus Michael Geipel
2727
*/
28-
public final class Group extends AbstractCollect{
28+
public final class Group extends AbstractFlushingCollect{
2929

3030

3131
public Group(final Metamorph metamorph) {

0 commit comments

Comments
 (0)