1 package eu.fbk.rdfpro.util;
2
3 import java.io.IOException;
4 import java.util.concurrent.atomic.AtomicInteger;
5
6 import org.openrdf.model.Statement;
7 import org.openrdf.rio.RDFHandler;
8 import org.openrdf.rio.RDFHandlerException;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import eu.fbk.rdfpro.AbstractRDFHandlerWrapper;
13 import eu.fbk.rdfpro.RDFHandlers;
14 import eu.fbk.rdfpro.RDFProcessor;
15 import eu.fbk.rdfpro.util.Dictionary;
16
17
18
19 public class DictionaryProcessor implements RDFProcessor {
20
21 private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryProcessor.class);
22
23 static RDFProcessor create(final String name, final String... args) throws IOException,
24 RDFHandlerException {
25 return new DictionaryProcessor();
26 }
27
28 @Override
29 public RDFHandler wrap(final RDFHandler handler) {
30 return RDFHandlers.decouple(new AbstractRDFHandlerWrapper(handler) {
31
32 private Dictionary dictionary;
33
34 private AtomicInteger counter;
35
36 @Override
37 public void startRDF() throws RDFHandlerException {
38 super.startRDF();
39 this.dictionary = Dictionary.newMemoryDictionary();
40 this.counter = new AtomicInteger();
41 }
42
43 @Override
44 public void handleStatement(final Statement stmt) throws RDFHandlerException {
45 final int sc = this.dictionary.encode(stmt.getSubject());
46 final int pc = this.dictionary.encode(stmt.getPredicate());
47 final int oc = this.dictionary.encode(stmt.getObject());
48 final int cc = this.dictionary.encode(stmt.getContext());
49 this.counter.addAndGet(sc + pc + oc + cc);
50 super.handleStatement(stmt);
51 }
52
53 @Override
54 public void endRDF() throws RDFHandlerException {
55 LOGGER.info(this.counter + " - " + this.dictionary.toString());
56 this.dictionary = null;
57 super.endRDF();
58 }
59
60 });
61 }
62
63 }