This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Triples2RdfModel.java
117 lines (107 loc) · 4.03 KB
/
Triples2RdfModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Copyright 2013 Pascal Christoph.
* Licensed under the Eclipse Public License 1.0 */
package org.lobid.lodmill;
import java.io.StringReader;
import org.culturegraph.mf.framework.DefaultObjectPipe;
import org.culturegraph.mf.framework.ObjectReceiver;
import org.culturegraph.mf.framework.annotations.Description;
import org.culturegraph.mf.framework.annotations.In;
import org.culturegraph.mf.framework.annotations.Out;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
/**
* Encodes triples into a jena RDF Model. Usage of inferencing by applying an
* ontology is possible.
*
* @author Pascal Christoph
*/
@Description("Encodes triples into an RDF Model. Usage of inferencing by applying "
+ "an ontology is possible. Predefined values for input are"
+ " 'RDF/XML', 'N-TRIPLE', 'TURTLE' (or 'TTL') and 'N3'. null represents the "
+ "default language, 'RDF/XML'. 'RDF/XML-ABBREV' is a synonym for 'RDF/XML'."
+ "Default is 'TURTLE'.")
@In(String.class)
@Out(Model.class)
public class Triples2RdfModel
extends DefaultObjectPipe<String, ObjectReceiver<Model>> {
private int count = 0;
private String serialization = "TURTLE";
private static final Logger LOG =
LoggerFactory.getLogger(Triples2RdfModel.class);
Reasoner boundReasoner;
Property propertyIdentifyingTheNodeForInferencing;
boolean inferencing;
/**
* Sets the serialization format of the incoming triples .
*
* @param serialization one of 'RDF/XML', 'N-TRIPLE', 'TURTLE' (or 'TTL') and
* 'N3'. null represents the default language, 'RDF/XML'.
* 'RDF/XML-ABBREV' is a synonym for 'RDF/XML'.")
*/
public void setInput(final String serialization) {
this.serialization = serialization;
}
/**
* Sets the filename of the ontology. This ontology will be loaded into an
* inference model to gain statements about resources part of that ontology.
* The reasoner being used is the most simple one and considers only RDFS.
*
* @param ontologyFilename the filename of the ontology
*/
public void setInferenceModel(String ontologyFilename) {
Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(null);
reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel,
ReasonerVocabulary.RDFS_SIMPLE);
boundReasoner =
reasoner.bindSchema(FileManager.get().loadModel(ontologyFilename));
this.inferencing = true;
}
/**
* Sets the property to identify the node for inferencing. As we want only
* statements about the "main" resource and discard all other resources which
* may be added by inferencing, this resource needs to be set.
*
* @param property the property
*/
public void setPropertyIdentifyingTheNodeForInferencing(String property) {
this.propertyIdentifyingTheNodeForInferencing =
ResourceFactory.createProperty(property);
}
@Override
public void process(final String str) {
Model model = ModelFactory.createDefaultModel();
try {
model.read(new StringReader(str), "test:uri" + count++, serialization);
if (inferencing)
reasoning(model);
getReceiver().process(model);
} catch (Exception e) {
LOG.error("Exception in " + str, e);
}
}
private void reasoning(Model model) {
ExtendedIterator<Triple> it =
ModelFactory.createInfModel(boundReasoner, model).getGraph().find(
model.listSubjectsWithProperty(
propertyIdentifyingTheNodeForInferencing).next().asNode(),
null, null);
Model model1 = ModelFactory.createDefaultModel();
while (it.hasNext()) {
Triple triple = it.next().asTriple();
if (!triple.getObject().isBlank()) {
model1.add(model.asStatement(triple));
}
}
model.add(model1);
}
}