Skip to content

Instantly share code, notes, and snippets.

@berezovskyi
Created June 11, 2019 17:20
Show Gist options
  • Save berezovskyi/bcf75008263444bd410ac2ffa7aa00f4 to your computer and use it in GitHub Desktop.
Save berezovskyi/bcf75008263444bd410ac2ffa7aa00f4 to your computer and use it in GitHub Desktop.
import org.apache.jena.rdf.listeners.ChangedListener
import org.apache.jena.rdf.model.InfModel
import org.apache.jena.rdf.model.Model
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.reasoner.rulesys.RDFSRuleReasonerFactory
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
import org.apache.jena.util.FileUtils
import org.apache.jena.vocabulary.RDF
import org.apache.jena.vocabulary.ReasonerVocabulary
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.StringReader
import java.io.StringWriter
fun main() {
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
val log: Logger = LoggerFactory.getLogger("main")
val model = ModelFactory.createDefaultModel()
val reasoner = RDFSRuleReasonerFactory.theInstance()
.create(null)
reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel,
ReasonerVocabulary.RDFS_SIMPLE)
// NPE at org.apache.jena.reasoner.rulesys.RDFSRuleReasoner.bind(RDFSRuleReasoner.java:150)
// reasoner.setParameter(ReasonerVocabulary.PROPruleMode,
// "forward")
val infModel: InfModel = ModelFactory.createInfModel(reasoner, model)
infModel.setDerivationLogging(true)
infModel.read(StringReader("""
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix : <http://example.com/ns/#>.
:Publication a rdfs:Class .
:Book a rdfs:Class;
rdfs:subClassOf :Publication .
""".trimIndent()), "http://example.com/ns/#", FileUtils.langTurtle)
val rawChangedListener = ChangedListener()
val deductionsChangedListener = ChangedListener()
infModel.rawModel.register(rawChangedListener)
infModel.deductionsModel.register(deductionsChangedListener)
log.info("Changes before creation: raw={}, deductions={}", rawChangedListener.hasChanged(), deductionsChangedListener.hasChanged())
infModel.createResource("http://example.com/books/1")
.addProperty(RDF.type, model.createResource("http://example.com/ns/#Book"))
log.info("Changes after creation : raw={}, deductions={}", rawChangedListener.hasChanged(), deductionsChangedListener.hasChanged())
infModel.prepare()
log.info("Changes after prepare : raw={}, deductions={}", rawChangedListener.hasChanged(), deductionsChangedListener.hasChanged())
log.info(modelToString(infModel))
}
fun modelToString(model: Model, rdfFormat: RDFFormat = RDFFormat.TURTLE_PRETTY): String {
val writer = StringWriter()
RDFDataMgr.write(writer, model, rdfFormat)
return writer.toString()
}
@berezovskyi
Copy link
Author

The output produced is the following:

[main] INFO main - Changes before creation: raw=false, deductions=false
[main] INFO main - Changes after creation : raw=true, deductions=false
[main] INFO main - Changes after prepare  : raw=false, deductions=false
[main] INFO main - @prefix :      <http://example.com/ns/#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

:Publication  a          rdfs:Class ;
        rdfs:subClassOf  rdfs:Resource , :Publication .

:Book   a                rdfs:Class ;
        rdfs:subClassOf  :Publication , :Book , rdfs:Resource .

<http://example.com/books/1>
        a       :Book , :Publication , rdfs:Resource .

rdfs:Resource  rdfs:subClassOf  rdfs:Resource .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment