Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
handle 2 wrong cases raised from OWLAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
Hebaallahibrahim committed Jun 4, 2019
1 parent 88dddd5 commit 00a138c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 93 deletions.
Expand Up @@ -13,12 +13,10 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.SparkContext
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.model._
import org.semanticweb.owlapi.util._
import org.semanticweb.owlapi.util.OWLAPIStreamUtils.asList
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import net.sansa_stack.owl.spark.rdd.OWLAxiomsRDD

/**
* Object containing several constants used by the RDFXMLSyntaxParsing
Expand All @@ -40,10 +38,6 @@ object RDFXMLSyntaxParsing {

trait RDFXMLSyntaxParsing {

// private val logger = Logger(classOf[FunctionalSyntaxParsing])

// private def parser = new OWLXMLParserFactory().createParser()

private def man = OWLManager.createOWLOntologyManager()

private def dataFactory = man.getOWLDataFactory
Expand Down Expand Up @@ -138,8 +132,12 @@ trait RDFXMLSyntaxParsing {

axiomsRDD.cache()

// case 1: Handler for wrong domain axioms
val declaration = axiomsRDD.filter(a => a.getAxiomType.equals(AxiomType.DECLARATION))
/* Case 1: Handler for wrong domain axioms
* OWLDataPropertyDomain and OWLObjectPropertyDomain
* converted wrong to OWLAnnotationPropertyDomain
*/

val declaration = extractAxiom(axiomsRDD, AxiomType.DECLARATION)
.asInstanceOf[RDD[OWLDeclarationAxiom]]

val dataProperties = declaration.filter(a => a.getEntity.isOWLDataProperty)
Expand All @@ -154,93 +152,99 @@ trait RDFXMLSyntaxParsing {
val dataBC = sc.broadcast(dd)
val objBC = sc.broadcast(oo)

val annDomain = axiomsRDD.filter(a => a.getAxiomType.equals(AxiomType.ANNOTATION_PROPERTY_DOMAIN))
val annDomain = extractAxiom(axiomsRDD, AxiomType.ANNOTATION_PROPERTY_DOMAIN)

val domainTransform = annDomain.asInstanceOf[RDD[OWLAnnotationPropertyDomainAxiom]]
.map { a =>

val prop = a.getProperty.getIRI
val domain = a.getDomain

// Annotation property domain axiom turned to object property domain after parsing.
if (objBC.value.contains(prop)) {

val c = dataFactory.getOWLClass(domain)
val p = dataFactory.getOWLObjectProperty(prop.toString)
val obj = dataFactory.getOWLObjectPropertyDomainAxiom(p, c, asList(a.annotations))

obj

} else if (dataBC.value.contains(prop)) {

// Annotation property domain axiom turned to object property domain after parsing.
val c = dataFactory.getOWLClass(domain)
val p = dataFactory.getOWLDataProperty(prop.toString)
val obj = dataFactory.getOWLDataPropertyDomainAxiom(p, c, asList(a.annotations))

obj

} else {

val transform = annDomain.asInstanceOf[RDD[OWLAnnotationPropertyDomainAxiom]]
.map { a =>
val obj = dataFactory
.getOWLAnnotationPropertyDomainAxiom(a.getProperty.asOWLAnnotationProperty, domain, asList(a.annotations))
.asInstanceOf[OWLAxiom]
obj

}
}

/* Case 2: Handler for wrong subPropertyOf axioms
* OWLSubPropertyOf converted wrong to OWLSubAnnotationPropertyOf
* instead of OWLSubDataPropertyOf or OWLSubObjectPropertyOf
*/

val subAnnotationProperty = extractAxiom(axiomsRDD, AxiomType.SUB_ANNOTATION_PROPERTY_OF)

val prop = a.getProperty

// val prop1 = a.getProperty
val name = a.getProperty.getIRI
// println("name = " + prop1)
val domain = a.getDomain
val subPropertyTransform = subAnnotationProperty.asInstanceOf[RDD[OWLSubAnnotationPropertyOfAxiom]]
.map { a =>

if (objBC.value.contains(name)) {
val c = dataFactory.getOWLClass(domain)
val p = dataFactory.getOWLObjectProperty(name.toString)
val subProperty = a.getSubProperty.getIRI
val supProperty = a.getSuperProperty.getIRI

// LOGGER.warn("Annotation property domain axiom turned to object property domain after parsing. " +
// "This could introduce errors if the original domain was an anonymous expression: {} is the new domain.", domain)

val obj = dataFactory.getOWLObjectPropertyDomainAxiom(p, c, asList(a.annotations))
// SubAnnotationPropertyOf axiom turned to SubObjectPropertyOf after parsing.
if (objBC.value.contains(subProperty)) {

obj
val sub = dataFactory.getOWLObjectProperty(subProperty.toString)
val sup = dataFactory.getOWLObjectProperty(supProperty.toString)
val obj = dataFactory.getOWLSubObjectPropertyOfAxiom(sub, sup, asList(a.annotations))

} else if (dataBC.value.contains(name)) {
obj

val c = dataFactory.getOWLClass(domain)
val p = dataFactory.getOWLDataProperty(name.toString)
} else if (dataBC.value.contains(subProperty)) {

// LOGGER.warn("Annotation property domain axiom turned to object property domain after parsing. " +
// "This could introduce errors if the original domain was an anonymous expression: {} is the new domain.", domain)
// SubAnnotationPropertyOf axiom turned to SubDataPropertyOf after parsing.
val sub = dataFactory.getOWLDataProperty(subProperty.toString)
val sup = dataFactory.getOWLDataProperty(supProperty.toString)
val obj = dataFactory.getOWLSubDataPropertyOfAxiom(sub, sup, asList(a.annotations))

val obj = dataFactory.getOWLDataPropertyDomainAxiom(p, c, asList(a.annotations))
obj

obj
} else {
} else {

val obj = dataFactory.getOWLAnnotationPropertyDomainAxiom(prop.asOWLAnnotationProperty, domain, asList(a.annotations))
.asInstanceOf[OWLAxiom]
obj
}
}
val differenceRDD = axiomsRDD.subtract(annDomain)
val correctRDD = differenceRDD.union(transform)
val obj = dataFactory
.getOWLSubAnnotationPropertyOfAxiom(a.getSubProperty.asOWLAnnotationProperty,
a.getSuperProperty.asOWLAnnotationProperty(),
asList(a.annotations))
.asInstanceOf[OWLAxiom]
obj

}
}

val differenceRDD = axiomsRDD.subtract(annDomain).subtract(subAnnotationProperty)
val correctRDD = sc.union(differenceRDD, domainTransform, subPropertyTransform)

correctRDD.foreach(println(_))

correctRDD
}

// def visit(ax: OWLAnnotationPropertyDomainAxiom): OWLAxiom = {
//
// val prop = ax.getProperty
// val prop1 = prop.getIRI
// val domain = ax.getDomain
// println("\n prop: " + prop1)
//
// if (prop.isOWLObjectProperty) {
//
// // turn to object property domain
// val d = dataFactory.getOWLClass(domain)
//
// LOGGER.warn("Annotation property domain axiom turned to object property domain after parsing. " +
// "This could introduce errors if the original domain was an anonymous expression: {} is the new domain.", domain)
//
// var obj = dataFactory.getOWLObjectPropertyDomainAxiom(prop.asOWLObjectProperty, d, asList(ax.annotations))
// .asInstanceOf[OWLAxiom]
// return obj
//
// } else if (prop.isDataPropertyExpression) {
//
// // turn to data property domain
// val d = dataFactory.getOWLClass(domain)
//
// LOGGER.warn("Annotation property domain axiom turned to data property domain after parsing. " +
// "This could introduce errors if the original domain was an anonymous expression: {} is the new domain.", domain)
//
// var obj = dataFactory.getOWLDataPropertyDomainAxiom(prop.asOWLDataProperty, d, asList(ax.annotations))
// .asInstanceOf[OWLAxiom]
//
// return obj
//
// } else {
// var obj = dataFactory.getOWLAnnotationPropertyDomainAxiom(prop.asOWLAnnotationProperty, domain, asList(ax.annotations()))
// .asInstanceOf[OWLAxiom]
// return obj
// }
// }

def extractAxiom(axiom: RDD[OWLAxiom], T: AxiomType[_]): RDD[OWLAxiom] = {
axiom.filter(a => a.getAxiomType.equals(T))
}
}

/**
Expand Down
Expand Up @@ -6,11 +6,9 @@ import org.apache.hadoop.mapred.JobConf
import org.apache.log4j.{Level, Logger}
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession
import org.apache.spark.SparkContext
import org.semanticweb.owlapi.model.OWLAxiom
import net.sansa_stack.owl.common.parsing.{RDFXMLSyntaxParsing, RDFXMLSyntaxPrefixParsing}


import net.sansa_stack.owl.common.parsing.{RDFXMLSyntaxParsing, RDFXMLSyntaxPrefixParsing}

class RDFXMLSyntaxOWLExpressionsRDDBuilder extends Serializable with RDFXMLSyntaxPrefixParsing with RDFXMLSyntaxParsing {

Expand Down Expand Up @@ -84,15 +82,6 @@ class RDFXMLSyntaxOWLExpressionsRDDBuilder extends Serializable with RDFXMLSynta
val OWLAxiomsRDD: RDD[OWLAxiom] = OWLAxiomsList.flatMap(line => line.iterator().asScala)
.distinct()

// val builder = new RDFXMLSyntaxExpressionBuilder(prefixesMap)
//
// OWLAxiomsRDD.map(x => builder.clean(x.toString)).filter(_ != null)


// println("Axioms are : \n")
// OWLAxiomsRDD.foreach(println(_))
// println("Axioms count = " + OWLAxiomsRDD.count())

val refinedRDD = refineOWLAxioms (spark.sparkContext, OWLAxiomsRDD)
println("Axioms count = " + refinedRDD.count())

Expand All @@ -116,14 +105,9 @@ object RDFXMLSyntaxOWLExpressionsRDDBuilder {
.appName("RDF/XML Parser")
.getOrCreate()

// Logger.getLogger("org").setLevel(Level.OFF)
Logger.getLogger("akka").setLevel(Level.OFF)
Logger.getLogger(this.getClass).setLevel(Level.ERROR)

// val sc: SparkContext = sparkSession.sparkContext

// sparkSession.sparkContext.setLogLevel("ALL")

val RDFXMLBuilder = new RDFXMLSyntaxOWLExpressionsRDDBuilder
val rdd = RDFXMLBuilder.build(sparkSession, input)

Expand Down

0 comments on commit 00a138c

Please sign in to comment.