1   package eu.fbk.dkm.pikes.rdf.api;
2   
3   import eu.fbk.rdfpro.RDFHandlers;
4   import eu.fbk.rdfpro.RDFProcessor;
5   import eu.fbk.rdfpro.RDFSource;
6   import eu.fbk.rdfpro.RDFSources;
7   import eu.fbk.rdfpro.util.QuadModel;
8   import org.eclipse.rdf4j.model.Statement;
9   import org.eclipse.rdf4j.rio.RDFHandler;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  public interface Mapper {
15  
16      void map(QuadModel model) throws Exception;
17  
18      public static Mapper concat(final Mapper... mappers) {
19          return new Mapper() {
20  
21              @Override
22              public void map(final QuadModel model) throws Exception {
23                  for (final Mapper mapper : mappers) {
24                      mapper.map(model);
25                  }
26              }
27  
28          };
29      }
30  
31      public static Mapper forProcessor(final RDFProcessor processor) {
32          return new Mapper() {
33  
34              @Override
35              public void map(final QuadModel model) throws Exception {
36                  final List<Statement> statements = new ArrayList<>();
37                  final RDFSource source = RDFSources.wrap(model);
38                  final RDFHandler handler = RDFHandlers.wrap(statements);
39                  processor.wrap(source).emit(handler, 1);
40                  model.addAll(statements);
41              }
42  
43          };
44      }
45  
46  }