Skip to content

Inference notebook

Gezim Sejdiu edited this page Feb 12, 2018 · 1 revision

The inference layer supports rule-based reasoning, i.e. given a set of rules it computes all possible inferences on the given dataset. Technically, forward-chaining is applied, i.e. it starts with the available data and uses inference rules to extract more data. This is sometimes also referred to as “materialization”.

Currently, three fixed rulesets are supported, namely RDFS, OWL-Horst, and OWL-EL. Later versions will contain a generic rule-based reasoner such that a user can define it’s own set of rules which will be used to materialize the given dataset.

SPARK 'rdfs' Reasoning example

import java.io.File
import java.net.URI

import net.sansa_stack.inference.rules.{RDFSLevel, ReasoningProfile}
import net.sansa_stack.inference.rules.ReasoningProfile._
import net.sansa_stack.inference.spark.data.loader.RDFGraphLoader
import net.sansa_stack.inference.spark.data.writer.RDFGraphWriter
import net.sansa_stack.inference.spark.forwardchaining.triples.{ForwardRuleReasonerOWLHorst, ForwardRuleReasonerRDFS, TransitiveReasoner}

// load triples from disk
val input = "hdfs://namenode:8020/data/rdf.nt"
val output = "hdfs://namenode:8020/data/output/"
val argprofile = "rdfs"

val profile = argprofile match {
      case "rdfs"        => ReasoningProfile.RDFS
      case "rdfs-simple" => ReasoningProfile.RDFS_SIMPLE
      case "owl-horst"   => ReasoningProfile.OWL_HORST
      case "transitive"  => ReasoningProfile.TRANSITIVE

}

// the degree of parallelism
val parallelism = 4

// load triples from disk
val graph = RDFGraphLoader.loadFromDisk(spark, URI.create(input), parallelism)
println(s"|G|=${graph.size()}")

// create reasoner
val reasoner = profile match {
   case TRANSITIVE => new TransitiveReasoner(spark.sparkContext, parallelism)
   case RDFS       => new ForwardRuleReasonerRDFS(spark.sparkContext, parallelism)
   case RDFS_SIMPLE =>
   var r = new ForwardRuleReasonerRDFS(spark.sparkContext, parallelism) //.level.+(RDFSLevel.SIMPLE)
     r.level = RDFSLevel.SIMPLE
     r
   case OWL_HORST => new ForwardRuleReasonerOWLHorst(spark.sparkContext)
}

// compute inferred graph
val inferredGraph = reasoner.apply(graph)
println(s"|G_inferred|=${inferredGraph.size()}")

// write triples to disk
//RDFGraphWriter.writeToDisk(inferredGraph, output)

val O_graph = "original graph" + "\t" + graph.size 
val I_Graph = "\n inferred graph" + "\t" + inferredGraph.size

println("%table graph\t size\n " + O_graph.union(I_Graph))