1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package eu.fbk.rdfpro;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.junit.Assert;
21 import org.junit.Test;
22 import org.openrdf.model.Statement;
23 import org.openrdf.model.URI;
24 import org.openrdf.model.ValueFactory;
25 import org.openrdf.model.impl.URIImpl;
26 import org.openrdf.model.impl.ValueFactoryImpl;
27 import org.openrdf.model.vocabulary.RDF;
28 import org.openrdf.model.vocabulary.RDFS;
29 import org.openrdf.rio.RDFHandler;
30 import org.openrdf.rio.RDFHandlerException;
31
32
33
34
35
36
37 public class TransformProcessorTest {
38
39 @Test
40 public void testFilter() throws RDFHandlerException {
41
42
43
44
45
46 URI dboCompany = new URIImpl("http://dbpedia.org/ontology/Company");
47
48 RDFSource aboxSource = RDFSources.read(true, true, null, null, "dbpedia.tbox.owl");
49 RDFSource tboxSource = RDFSources.read(true, true, null, null, "dbpedia.abox.nt.gz");
50
51 RDFHandler labelsSink = RDFHandlers.write(null, 0, "labels.nt.gz");
52
53 final RDFProcessor processor = RDFProcessors.sequence(
54 RDFProcessors.rdfs(aboxSource, null, false, false),
55 RDFProcessors.transform(Transformer.filter((final Statement s) -> {
56 final URI p = s.getPredicate();
57 return p.equals(RDF.TYPE) || p.equals(RDFS.LABEL);
58 })),
59 RDFProcessors.mapReduce(Mapper.select("s"), Reducer.filter(Reducer.IDENTITY, (
60 final Statement s) -> s.getObject().equals(dboCompany), null), true));
61
62 processor.apply(tboxSource, labelsSink, 1);
63
64
65
66 final RDFProcessor filter = RDFProcessors.parse(true, "@filter 'sou +[./file.nt]p -*'");
67 Assert.assertEquals(0, filter.getExtraPasses());
68 final int[] flags = new int[2];
69 final List<Statement> statements = new ArrayList<>();
70 final RDFHandler handler = filter.wrap(new RDFHandler() {
71
72 @Override
73 public void startRDF() throws RDFHandlerException {
74 flags[0]++;
75 }
76
77 @Override
78 public void endRDF() throws RDFHandlerException {
79 flags[1]++;
80 }
81
82 @Override
83 public void handleNamespace(final String pref, final String name)
84 throws RDFHandlerException {
85 throw new IllegalStateException();
86 }
87
88 @Override
89 public void handleStatement(final Statement s) throws RDFHandlerException {
90 statements.add(s);
91 }
92
93 @Override
94 public void handleComment(final String comment) throws RDFHandlerException {
95 throw new IllegalStateException();
96 }
97 });
98
99 final ValueFactory factory = new ValueFactoryImpl();
100 handler.startRDF();
101 handler.handleStatement(factory.createStatement(factory.createURI("http://sx"),
102 factory.createURI("http://p1"), factory.createURI("http://sx")));
103 handler.handleStatement(factory.createStatement(factory.createURI("http://sF"),
104 factory.createURI("http://pF"), factory.createURI("http://sF")));
105 handler.handleStatement(factory.createStatement(factory.createURI("http://sy"),
106 factory.createURI("http://p2"), factory.createURI("http://sy")));
107 handler.endRDF();
108
109 Assert.assertEquals(1, flags[0]);
110 Assert.assertEquals(1, flags[1]);
111 Assert.assertEquals(2, statements.size());
112 Assert.assertEquals("(http://sx, http://p1, http://sx)", statements.get(0).toString());
113 Assert.assertEquals("(http://sy, http://p2, http://sy)", statements.get(1).toString());
114 }
115 }