1   package eu.fbk.rdfpro;
2   
3   import java.io.File;
4   import java.net.URISyntaxException;
5   import java.util.concurrent.atomic.AtomicInteger;
6   
7   import org.junit.Assert;
8   import org.junit.Test;
9   import org.openrdf.model.Statement;
10  import org.openrdf.model.URI;
11  import org.openrdf.model.ValueFactory;
12  import org.openrdf.model.impl.URIImpl;
13  import org.openrdf.model.vocabulary.FOAF;
14  import org.openrdf.rio.RDFHandler;
15  import org.openrdf.rio.RDFHandlerException;
16  import org.openrdf.rio.helpers.RDFHandlerBase;
17  
18  import eu.fbk.rdfpro.util.Statements;
19  
20  public class GroovyProcessorTest {
21  
22      @Test
23      public void testPerformances() throws Throwable {
24  
25          final String script = "" //
26                  + "def init(args) { println args[0]; println args[1]; }; " //
27                  + "def start(x) { println 'start ' + x; i = 0 }; " //
28                  + "def end(x) { println 'end ' + x + ' ' + i }; " //
29                  + "emitIf(p == <ex:b> || p == <ex:p> || p == foaf:name) ";
30  
31          final RDFProcessor processor = RDFProcessors.parse(true, "@transform -p \"" + script
32                  + "\" arg1 arg2");
33  
34          final AtomicInteger n = new AtomicInteger(0);
35          final RDFHandler sink = new RDFHandlerBase() {
36  
37              @Override
38              public void handleStatement(final Statement st) throws RDFHandlerException {
39                  n.incrementAndGet();
40              }
41          };
42          final RDFHandler handler = processor.wrap(sink);
43  
44          // final RDFHandler handler = new Baseline(sink);
45  
46          final int c = 20000000;
47          final long ts = System.currentTimeMillis();
48          handler.startRDF();
49          for (int i = 0; i < c; ++i) {
50              handler.handleStatement(newStatement("ex:s", FOAF.NAME.stringValue(), "ex:o" + i,
51                      "ex:c"));
52          }
53          handler.endRDF();
54          System.out.println(1000L * c / (System.currentTimeMillis() - ts));
55          System.out.println(n);
56      }
57  
58      @Test
59      public void testValueSet() throws RDFHandlerException, URISyntaxException {
60          final File target = new File(GroovyProcessorTest.class.getResource("file.nt").toURI());
61          final String script = "def init(args) { valMatchCount = 0; statMatchCount = 0; valueset = loadSet('"
62                  + target.getAbsolutePath()
63                  + "', 'spo'); };"
64                  + "def end(x) { log('# Matched statements: ' + statMatchCount + ' Matched values: ' + valMatchCount); };"
65                  + "if( valueset.match(q, 'spo') ) statMatchCount++;"
66                  + "if( valueset.match(s) ) valMatchCount++;";
67  
68          final GroovyProcessor processor = (GroovyProcessor) RDFProcessors.parse(true,
69                  "@transform \"" + script + "\" arg1 arg2");
70          final RDFHandler handler = processor.wrap(RDFHandlers.NIL);
71          final int c = 500000;
72          final long ts = System.currentTimeMillis();
73          handler.startRDF();
74          for (int i = 0; i < c; ++i) {
75              handler.handleStatement(newStatement("ex:s", "ex:p", "ex:o" + i, "ex:c"));
76          }
77          handler.handleStatement(newStatement("http://s11", "http://PP", "http://OO", "ex:c"));
78          handler.handleStatement(newStatement("http://SS", "http://sy", "http://OO", "ex:c"));
79          handler.handleStatement(newStatement("http://SS", "http://PP", "http://o33", "ex:c"));
80          for (int i = c; i < c + c; ++i) {
81              handler.handleStatement(newStatement("ex:s", "ex:p", "ex:o" + i, "ex:c"));
82          }
83          handler.endRDF();
84          System.out.println("Statements per sec: " + 1000L * c / (System.currentTimeMillis() - ts));
85  
86          Assert.assertEquals(3, processor.getProperty(handler, "statMatchCount"));
87          Assert.assertEquals(1, processor.getProperty(handler, "valMatchCount"));
88      }
89  
90      private Statement newStatement(final String s, final String p, final String o, final String c) {
91          final ValueFactory vf = Statements.VALUE_FACTORY;
92          return vf.createStatement(vf.createURI(s), vf.createURI(p), vf.createURI(o),
93                  vf.createURI(c));
94      }
95  
96      static class Baseline implements RDFHandler {
97  
98          private final RDFHandler handler;
99  
100         public Baseline(final RDFHandler handler) {
101             this.handler = handler;
102         }
103 
104         @Override
105         public void startRDF() throws RDFHandlerException {
106             this.handler.startRDF();
107         }
108 
109         @Override
110         public void handleComment(final String comment) throws RDFHandlerException {
111             this.handler.handleComment(comment);
112         }
113 
114         @Override
115         public void handleNamespace(final String prefix, final String uri)
116                 throws RDFHandlerException {
117             this.handler.handleNamespace(prefix, uri);
118         }
119 
120         private final URI a = new URIImpl("ex:a");
121 
122         private final URI b = new URIImpl("ex:b");
123 
124         private final URI p = new URIImpl("ex:p");
125 
126         @Override
127         public void handleStatement(final Statement st) throws RDFHandlerException {
128             final URI p = st.getPredicate();
129             if (p.equals(this.a) || p.equals(this.b) || p.equals(this.p)) {
130                 // this.handler.handleStatement(new StatementImpl(st.getSubject(),
131                 // st.getPredicate(),
132                 // st.getObject()));
133                 this.handler.handleStatement(st);
134             }
135         }
136 
137         @Override
138         public void endRDF() throws RDFHandlerException {
139             this.handler.endRDF();
140         }
141 
142     }
143 
144 }