Skip to content

Commit 498cc44

Browse files
author
Markus M. Geipel
committed
Merge pull request #101 from cboehme/add-triple-filter
Added a filter for triples
2 parents a4ba148 + 9dff3b1 commit 498cc44

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.stream.pipe;
17+
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
20+
21+
import org.culturegraph.mf.framework.DefaultObjectPipe;
22+
import org.culturegraph.mf.framework.ObjectReceiver;
23+
import org.culturegraph.mf.framework.annotations.Description;
24+
import org.culturegraph.mf.framework.annotations.In;
25+
import org.culturegraph.mf.framework.annotations.Out;
26+
import org.culturegraph.mf.types.Triple;
27+
28+
/**
29+
* Filters triples. The patterns for subject, predicate and object are disjunctive.
30+
*
31+
* @author Christoph Böhme
32+
*
33+
*/
34+
@Description("Filters triple. The patterns for subject, predicate and object are disjunctive.")
35+
@In(Triple.class)
36+
@Out(Triple.class)
37+
public final class TripleFilter extends
38+
DefaultObjectPipe<Triple, ObjectReceiver<Triple>> {
39+
40+
// A regexp that is guaranteed to never match ( an `a` after the end
41+
// of the string, see http://stackoverflow.com/a/1723225 for details):
42+
private static final Matcher MATCH_NOTHING = Pattern.compile("$a").matcher("");
43+
44+
private Matcher subjectMatcher = MATCH_NOTHING;
45+
private Matcher predicateMatcher = MATCH_NOTHING;
46+
private Matcher objectMatcher = MATCH_NOTHING;
47+
private boolean passMatches = true;
48+
49+
public String getSubjectPattern() {
50+
return subjectMatcher.pattern().pattern();
51+
}
52+
53+
public void setSubjectPattern(final String pattern) {
54+
subjectMatcher = Pattern.compile(pattern).matcher("");
55+
}
56+
57+
public String getPredicatePattern() {
58+
return predicateMatcher.pattern().pattern();
59+
}
60+
61+
public void setPredicatePattern(final String pattern) {
62+
predicateMatcher = Pattern.compile(pattern).matcher("");
63+
}
64+
65+
public String getObjectPattern() {
66+
return objectMatcher.pattern().pattern();
67+
}
68+
69+
public void setObjectPattern(final String pattern) {
70+
objectMatcher = Pattern.compile(pattern).matcher("");
71+
}
72+
73+
public boolean isPassMatches() {
74+
return passMatches;
75+
}
76+
77+
public void setPassMatches(final boolean passMatches) {
78+
this.passMatches = passMatches;
79+
}
80+
81+
@Override
82+
public void process(final Triple obj) {
83+
subjectMatcher.reset(obj.getSubject());
84+
predicateMatcher.reset(obj.getPredicate());
85+
objectMatcher.reset(obj.getObject());
86+
87+
final boolean matches = subjectMatcher.matches() || predicateMatcher.matches() || objectMatcher.matches();
88+
89+
if ((matches && passMatches) || (!matches && !passMatches)) {
90+
getReceiver().process(obj);
91+
}
92+
}
93+
94+
}

src/main/resources/flux-commands.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ sort-triples org.culturegraph.mf.stream.pipe.sort.TripleSort
1414
count-triples org.culturegraph.mf.stream.pipe.sort.TripleCount
1515
collect-triples org.culturegraph.mf.stream.pipe.sort.TripleCollect
1616
stream-to-triples org.culturegraph.mf.stream.converter.StreamToTriples
17-
17+
filter-triples org.culturegraph.mf.stream.pipe.TripleFilter
1818

1919
jscript org.culturegraph.mf.stream.pipe.JScriptObjectPipe
2020

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.stream.pipe;
17+
18+
import static org.mockito.Mockito.verify;
19+
import static org.mockito.Mockito.verifyNoMoreInteractions;
20+
21+
import org.culturegraph.mf.framework.ObjectReceiver;
22+
import org.culturegraph.mf.types.Triple;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.mockito.Mock;
27+
import org.mockito.MockitoAnnotations;
28+
29+
/**
30+
* Tests for {@link TripleFilter},
31+
*
32+
* @author Christoph Böhme
33+
*
34+
*/
35+
public final class TripleFilterTest {
36+
37+
private static final Triple TRIPLE1 = new Triple("sA", "pA", "oA");
38+
private static final Triple TRIPLE2 = new Triple("sB", "pB", "oB");
39+
private static final Triple TRIPLE3 = new Triple("sC", "pC", "oC");
40+
41+
private TripleFilter tripleFilter;
42+
43+
@Mock
44+
private ObjectReceiver<Triple> receiver;
45+
46+
@Before
47+
public void setup() {
48+
MockitoAnnotations.initMocks(this);
49+
tripleFilter = new TripleFilter();
50+
tripleFilter.setReceiver(receiver);
51+
}
52+
53+
@After
54+
public void cleanup() {
55+
tripleFilter.closeStream();
56+
}
57+
58+
@Test
59+
public void testShouldPassMatchingTripleByDefault() {
60+
tripleFilter.setSubjectPattern("sA");
61+
62+
tripleFilter.process(TRIPLE1);
63+
tripleFilter.process(TRIPLE2);
64+
65+
verify(receiver).process(TRIPLE1);
66+
verifyNoMoreInteractions(receiver);
67+
}
68+
69+
@Test
70+
public void testShouldPassNonMatchingTripleIfPassMatchesIsFalse() {
71+
tripleFilter.setSubjectPattern("sA");
72+
tripleFilter.setPassMatches(false);
73+
74+
tripleFilter.process(TRIPLE1);
75+
tripleFilter.process(TRIPLE2);
76+
77+
verify(receiver).process(TRIPLE2);
78+
verifyNoMoreInteractions(receiver);
79+
}
80+
81+
@Test
82+
public void testShouldUseDisjunctionForPatterns() {
83+
tripleFilter.setSubjectPattern("sA");
84+
tripleFilter.setObjectPattern("oC");
85+
86+
tripleFilter.process(TRIPLE1);
87+
tripleFilter.process(TRIPLE2);
88+
tripleFilter.process(TRIPLE3);
89+
90+
verify(receiver).process(TRIPLE1);
91+
verify(receiver).process(TRIPLE3);
92+
verifyNoMoreInteractions(receiver);
93+
}
94+
95+
}

0 commit comments

Comments
 (0)