|
| 1 | +package net.sansa_stack.owl.common.parsing |
| 2 | + |
| 3 | +import scala.compat.java8.StreamConverters._ |
| 4 | + |
| 5 | +import com.typesafe.scalalogging.Logger |
| 6 | +import org.semanticweb.owlapi.apibinding.OWLManager |
| 7 | +import org.semanticweb.owlapi.formats.OWLXMLDocumentFormat |
| 8 | +import org.semanticweb.owlapi.io.StringDocumentSource |
| 9 | +import org.semanticweb.owlapi.model._ |
| 10 | + |
| 11 | + /** |
| 12 | + * Singleton instance of type OWLXMLSyntaxParsing |
| 13 | + */ |
| 14 | + |
| 15 | +object OWLXMLSyntaxParsing extends Serializable { |
| 16 | + |
| 17 | + private val logger = Logger(classOf[OWLXMLSyntaxParsing]) |
| 18 | + |
| 19 | + private def manager = OWLManager.createOWLOntologyManager() |
| 20 | + |
| 21 | + // map to define owlXml schema syntax pattern |
| 22 | + var OWLXMLSyntaxPattern: Map[String, Map[String, String]] = Map( |
| 23 | + "versionPattern" -> Map( |
| 24 | + "beginTag" -> "<?xml", |
| 25 | + "endTag" -> "?>" |
| 26 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 27 | + "prefixPattern" -> Map( |
| 28 | + "beginTag" -> "<rdf:RDF", |
| 29 | + "endTag" -> ">" |
| 30 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 31 | + "owlClassPattern" -> Map( |
| 32 | + "beginTag" -> "<owl:Class", |
| 33 | + "endTag" -> "</owl:Class>" |
| 34 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 35 | + "owlClassPattern2" -> Map( |
| 36 | + "beginTag" -> "<owl:Class", |
| 37 | + "endTag" -> "/>" |
| 38 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 39 | + "owlDataTypePropertyPattern" -> Map( |
| 40 | + "beginTag" -> "<owl:DatatypeProperty", |
| 41 | + "endTag" -> "</owl:DatatypeProperty>" |
| 42 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 43 | + "owlDataTypePropertyPattern2" -> Map( |
| 44 | + "beginTag" -> "<owl:DatatypeProperty", |
| 45 | + "endTag" -> "/>" |
| 46 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 47 | + "owlObjectPropertyPattern" -> Map( |
| 48 | + "beginTag" -> "<owl:ObjectProperty", |
| 49 | + "endTag" -> "</owl:ObjectProperty>" |
| 50 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 51 | + "owlObjectPropertyPattern2" -> Map( |
| 52 | + "beginTag" -> "<owl:ObjectProperty", |
| 53 | + "endTag" -> "/>" |
| 54 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 55 | + "owlNamedIndividualPattern" -> Map( |
| 56 | + "beginTag" -> "<owl:NamedIndividual", |
| 57 | + "endTag" -> "</owl:NamedIndividual>" |
| 58 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 59 | + "owlAnnotationPropertyPattern" -> Map( |
| 60 | + "beginTag" -> "<owl:AnnotationProperty", |
| 61 | + "endTag" -> "</owl:AnnotationProperty>" |
| 62 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 63 | + "owlAnnotationPropertyPattern2" -> Map( |
| 64 | + "beginTag" -> "<owl:AnnotationProperty", |
| 65 | + "endTag" -> "/>" |
| 66 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 67 | + "owlAxiomPattern" -> Map( |
| 68 | + "beginTag" -> "<owl:Axiom", |
| 69 | + "endTag" -> "</owl:Axiom>" |
| 70 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 71 | + "TransitivePropertyPattern" -> Map( |
| 72 | + "beginTag" -> "<owl:TransitiveProperty", |
| 73 | + "endTag" -> "</owl:TransitiveProperty>" |
| 74 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 75 | + "rdfsDatatypePattern" -> Map( |
| 76 | + "beginTag" -> "<rdfs:Datatype", |
| 77 | + "endTag" -> ">" |
| 78 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 79 | + "rdfDescription" -> Map( |
| 80 | + "beginTag" -> "<rdf:Description", |
| 81 | + "endTag" -> "</rdf:Description>" |
| 82 | + ).withDefaultValue("Pattern for begin and end tags not found"), |
| 83 | + "owlRestriction" -> Map( |
| 84 | + "beginTag" -> "<owl:Restriction", |
| 85 | + "endTag" -> "</owl:Restriction>" |
| 86 | + ).withDefaultValue("Pattern for begin and end tags not found") |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | + /** definition to build Owl Axioms out of expressions |
| 91 | + * |
| 92 | + * @param xmlString xmlVersion string |
| 93 | + * @param prefixString owlxml prefix string |
| 94 | + * @param expression owl expressions of owlxml syntax |
| 95 | + * @return A set consisting OWL Axioms built out of owl expressions |
| 96 | + */ |
| 97 | + |
| 98 | + def makeAxiom(xmlString: String, prefixString: String, expression: String): Set[OWLAxiom] = { |
| 99 | + |
| 100 | + // compose a string consisting of xml version, owlXml prefixes, owlXml expressions |
| 101 | + val axiomString = xmlString + "\n" + prefixString + "\n" + expression + "\n" + "</rdf:RDF>" |
| 102 | + |
| 103 | + // load an ontology from file |
| 104 | + val ontology = try { |
| 105 | + // load an ontology from an input stream |
| 106 | + manager.loadOntologyFromOntologyDocument(new StringDocumentSource(axiomString)) |
| 107 | + } catch { |
| 108 | + case e: Exception => null |
| 109 | + } |
| 110 | + |
| 111 | + val extractedAxioms = if (ontology != null) { |
| 112 | + |
| 113 | + // get the ontology format |
| 114 | + val format = manager.getOntologyFormat(ontology) |
| 115 | + val owlXmlFormat = new OWLXMLDocumentFormat |
| 116 | + |
| 117 | + // copy all the prefixes from OWL document format to OWL XML format |
| 118 | + if (format != null && format.isPrefixOWLDocumentFormat) { |
| 119 | + owlXmlFormat.copyPrefixesFrom(format.asPrefixOWLDocumentFormat) |
| 120 | + } |
| 121 | + |
| 122 | + // get axioms from the loaded ontology |
| 123 | + val axioms: Set[OWLAxiom] = ontology.axioms().toScala[Set] |
| 124 | + axioms |
| 125 | + |
| 126 | + } else { |
| 127 | + // logger.warn("No ontology was created for expression \n" + expression + "\n") |
| 128 | + null |
| 129 | + } |
| 130 | + extractedAxioms |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Trait to support the parsing of input OWL files in OWLXML syntax. |
| 135 | + * This trait mainly defines how to make axioms from input OWLXML |
| 136 | + * syntax expressions. |
| 137 | + */ |
| 138 | + |
| 139 | + trait OWLXMLSyntaxParsing extends Serializable { |
| 140 | + } |
| 141 | + } |
0 commit comments