All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.semanticweb.owlapi.krss2.renderer.KRSSObjectRenderer Maven / Gradle / Ivy

The newest version!
/* This file is part of the OWL API.
 * The contents of this file are subject to the LGPL License, Version 3.0.
 * Copyright 2011, Ulm University
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with this program.  If not, see http://www.gnu.org/licenses/.
 *
 * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package org.semanticweb.owlapi.krss2.renderer;

import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.ALL;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.AND;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.AT_LEAST;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.AT_MOST;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DEFINE_CONCEPT;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DEFINE_PRIMITIVE_CONCEPT;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DEFINE_PRIMITIVE_ROLE;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DEFINE_ROLE;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DISJOINT;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DISTINCT;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.DOMAIN;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.EQUAL;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.EXACTLY;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.INSTANCE;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.INVERSE;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.NOT;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.OR;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.RANGE;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.RELATED;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.SOME;
import static org.semanticweb.owlapi.krss2.renderer.KRSSVocabulary.TRANSITIVE;
import static org.semanticweb.owlapi.model.parameters.Imports.INCLUDED;
import static org.semanticweb.owlapi.search.EntitySearcher.isDefined;
import static org.semanticweb.owlapi.search.Searcher.equivalent;
import static org.semanticweb.owlapi.search.Searcher.sup;
import static org.semanticweb.owlapi.util.OWLAPIPreconditions.checkNotNull;
import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.asList;
import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.pairs;

import java.io.PrintWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataAllValuesFrom;
import org.semanticweb.owlapi.model.OWLDataExactCardinality;
import org.semanticweb.owlapi.model.OWLDataMaxCardinality;
import org.semanticweb.owlapi.model.OWLDataMinCardinality;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataRange;
import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom;
import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom;
import org.semanticweb.owlapi.model.OWLObjectComplementOf;
import org.semanticweb.owlapi.model.OWLObjectExactCardinality;
import org.semanticweb.owlapi.model.OWLObjectIntersectionOf;
import org.semanticweb.owlapi.model.OWLObjectInverseOf;
import org.semanticweb.owlapi.model.OWLObjectMaxCardinality;
import org.semanticweb.owlapi.model.OWLObjectMinCardinality;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom;
import org.semanticweb.owlapi.model.OWLObjectUnionOf;
import org.semanticweb.owlapi.model.OWLObjectVisitor;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLPropertyExpression;
import org.semanticweb.owlapi.model.OWLSameIndividualAxiom;
import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
import org.semanticweb.owlapi.search.Filters;

/**
 * A {@code KRSSObjectRenderer} renders an OWLOntology in the original KRSS syntax. Note that only a
 * subset of OWL can be expressed in KRSS. 
* Abbreviations * * * * * * * * * * * * * * * * * * *
Abbreviations
CNconcept name
C,D,Econcept expression
RNrole name
R, R1, R2,...role expressions, i.e. role name or inverse role
*
* KRSS concept language * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
KRSS concept language
KRSSOWLClassExpression
(at-least n R C)(OWLObjectMinCardinality R n C)
(at-most n R C)(OWLObjectMaxCardinality R n C)
(exactly n R C)(OWLObjectExactCardinality R n C)
(some R C)(OWLObjectSomeValuesFrom R C)
(all R C)(OWLObjectAllValuesFrom R C)
(not C)(OWLObjectComplementOf C)
(and C D E)(OWLObjectIntersectionOf C D E)
(or C D E)(OWLObjectUnionOf C D E)
*
* KRSS role language * * * * * * * * * * *
KRSS role language
KRSSOWLObjectPropertyExpression
(inv R)(OWLInverseObjectPropertiesAxiom R)
*
* Each referenced class, object property as well as individual is defined using * define-concept resp. define-primitive-concept, define-role and * define-individual. In addition, axioms are translated as follows.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Remarks
OWLAxiomKRSS syntaxRemarks
OWLEquivalentClasses(define-concept C D)OWLEquivalentClasses C D1 D2...Dn will be translated to:
* (define-concept C (and D1 D2...Dn))
* Only applicable if there is no OWLSubClassOf axiom.
*
OWLDisjointClassesAxiom(disjoint C D)multiple pairwise disjoint statements are added in case of more than 2 disjoint * expressions
OWLSubClassOf(define-primitive-concept C D)Multiple OWLSubClassOf axioms for C will be combined:
* (define-primitive-concept C (and D1...Dn))
* Only applicable if there is no OWLEquivalentClasses axiom.
* KRSS does not allow both define-concept C and define-primitive-concept C. GCIs not supported in * KRSS (see KRSS2)
OWLEquivalentObjectPropertiesAxiom(define-role R S)Only applicable if the is no OWLSubObjectPropertyOf for R and the number of the involved * properties must be two
OWLObjectPropertyDomainAxiom(domain P D)
OWLObjectPropertyRangeAxiom(range P D)
OWLSubObjectPropertyOf(define-primitive-role R S)Only applicable if the is no OWLEquivalentObjectPropertiesAxiom for R and only one * OWLSubObjectPropertyOf axiom for a given property is allowed. If there are more one is randomly * chosen.
OWLTransitiveObjectPropertyAxiom(transitive P)
OWLClassAssertionAxiom(instance i D)
OWLDifferentIndividualsAxiom(distinct i1 i2)OWLDifferentIndividualsAxiom i1 i2 ... in will be split into:
* { (distinct i(j) i(j+k)) | 1 <= j <=n, j<k<=n, j=|=k}
OWLObjectPropertyAssertionAxiom(related i1 P i2)i1: subject, i2: object
OWLSameIndividualsAxiom(equal i1 i2)OWLSameIndividual i1 i2 ...i(n-1) in will be split into:
* { (equal i(j) i(j+k)) | 1 <= j <=n, j<k<=n, j=|=k}
*
* * @author Olaf Noppens, Ulm University, Institute of Artificial Intelligence */ public class KRSSObjectRenderer implements OWLObjectVisitor { protected static final String OPEN_BRACKET = "("; protected static final String CLOSE_BRACKET = ")"; protected static final String NEWLINE = "\n"; protected final OWLOntology ont; protected final PrintWriter writer; private int pos = 0; private int lastNewLinePos = 0; /** * @param ontology ontology * @param writer writer */ public KRSSObjectRenderer(OWLOntology ontology, Writer writer) { ont = checkNotNull(ontology); this.writer = new PrintWriter(writer); } protected void writeOpenBracket() { write(OPEN_BRACKET); } protected void writeCloseBracket() { write(CLOSE_BRACKET); } protected void write(int i) { write(" " + i); } protected void write(IRI iri) { write(iri.toString()); } protected void write(KRSSVocabulary v) { write(v.toString()); } protected void write(KRSS2Vocabulary v) { write(v.toString()); } protected void writeSpace() { write(" "); } protected void write(String s) { int newLineIndex = s.indexOf('\n'); if (newLineIndex != -1) { lastNewLinePos = pos + newLineIndex; } pos += s.length(); writer.write(s); } protected int getIndent() { return pos - lastNewLinePos - 1; } protected void writeIndent(int indent) { for (int i = 0; i < indent; i++) { writeSpace(); } } protected void writeln() { write(NEWLINE); } protected void write(OWLClassExpression obj) { writeSpace(); obj.accept(this); } protected void write(OWLIndividual ind) { writeSpace(); ind.accept(this); } protected void write(OWLPropertyExpression obj) { writeSpace(); obj.accept(this); } protected void write(OWLDataRange obj) { writeSpace(); obj.accept(this); } protected void flattenProperties(List props, @Nullable KRSSVocabulary junctor) { int size = props.size(); if (size == 0) { return; } if (size == 1) { write(props.iterator().next()); return; } if (junctor != null) { writeOpenBracket(); write(junctor); } write(props.get(0)); int indent = getIndent(); for (int i = 1; i < size; i++) { writeln(); writeIndent(indent); write(props.get(i)); } if (junctor != null) { writeCloseBracket(); } } protected void flatten(List descs) { int size = descs.size(); if (size == 0) { return; } write(descs.get(0)); if (size == 1) { return; } writeOpenBracket(); write(AND); int indent = getIndent(); for (int i = 1; i < size; i++) { writeln(); writeIndent(indent); write(descs.get(i)); } writeCloseBracket(); } @Override public void visit(OWLOntology ontology) { List classes = asList(ontology.classesInSignature()); classes.remove(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLThing()); classes.remove(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLNothing()); classes.sort(null); for (OWLClass eachClass : classes) { boolean primitive = !isDefined(eachClass, ontology); if (primitive) { writeOpenBracket(); write(DEFINE_PRIMITIVE_CONCEPT); write(eachClass); writeSpace(); flatten(asList( sup(ontology.subClassAxiomsForSubClass(eachClass), OWLClassExpression.class))); writeCloseBracket(); writeln(); } else { writeOpenBracket(); write(DEFINE_CONCEPT); write(eachClass); flatten(asList(equivalent(ontology.equivalentClassesAxioms(eachClass)))); writeCloseBracket(); writeln(); } } ontology.objectPropertiesInSignature().forEach(property -> { writeOpenBracket(); Stream pStream = equivalent(ontology.equivalentObjectPropertiesAxioms(property)); Collection properties = asList(pStream); boolean isDefined = !properties.isEmpty(); if (isDefined) { write(DEFINE_ROLE); write(property); // choose randomly one property (KRSS restriction) properties.remove(property); if (!properties.isEmpty()) { write(properties.iterator().next()); } } else { write(DEFINE_PRIMITIVE_ROLE); write(property); writeSpace(); Iterator i = sup(ontology.axioms(Filters.subObjectPropertyWithSub, property, INCLUDED), OWLObjectPropertyExpression.class).iterator(); if (i.hasNext()) { write(i.next()); } } writeCloseBracket(); writeln(); }); ontology.axioms().forEach(a -> a.accept(this)); writer.flush(); } @Override public void visit(OWLDisjointClassesAxiom axiom) { pairs(axiom.classExpressions()).forEach(v -> { writeOpenBracket(); write(DISJOINT); write(v.i); write(v.j); writeCloseBracket(); writeln(); }); } @Override public void visit(OWLObjectPropertyDomainAxiom axiom) { writeOpenBracket(); write(DOMAIN); write(axiom.getProperty()); write(axiom.getDomain()); writeCloseBracket(); writeln(); } @Override public void visit(OWLDifferentIndividualsAxiom axiom) { pairs(axiom.individuals()).forEach(v -> { writeOpenBracket(); write(DISTINCT); write(v.i); write(v.j); writeCloseBracket(); writeln(); }); } @Override public void visit(OWLObjectPropertyRangeAxiom axiom) { writeOpenBracket(); write(RANGE); write(axiom.getProperty()); write(axiom.getRange()); writeCloseBracket(); writeln(); } @Override public void visit(OWLObjectPropertyAssertionAxiom axiom) { writeOpenBracket(); write(RELATED); write(axiom.getSubject()); write(axiom.getProperty()); write(axiom.getObject()); writeCloseBracket(); writeln(); } @Override public void visit(OWLClassAssertionAxiom axiom) { writeOpenBracket(); write(INSTANCE); write(axiom.getIndividual()); write(axiom.getClassExpression()); writeCloseBracket(); writeln(); } @Override public void visit(OWLTransitiveObjectPropertyAxiom axiom) { writeOpenBracket(); write(TRANSITIVE); write(axiom.getProperty()); writeCloseBracket(); writeln(); } @Override public void visit(OWLSameIndividualAxiom axiom) { pairs(axiom.individuals()).forEach(v -> { writeOpenBracket(); write(EQUAL); write(v.i); write(v.j); writeCloseBracket(); writeln(); }); } @Override public void visit(OWLClass ce) { write(ce.getIRI()); } @Override public void visit(OWLObjectIntersectionOf ce) { writeOpenBracket(); write(AND); List operands = asList(ce.operands()); int size = operands.size(); if (size > 0) { int indent = getIndent(); write(operands.get(0)); for (int i = 1; i < size; i++) { writeln(); writeIndent(indent); write(operands.get(i)); } } writeCloseBracket(); } @Override public void visit(OWLObjectUnionOf ce) { writeOpenBracket(); write(OR); List operands = asList(ce.operands()); int size = operands.size(); if (size > 0) { int indent = getIndent(); write(operands.get(0)); for (int i = 1; i < size; i++) { writeln(); writeIndent(indent); write(operands.get(i)); } } writeCloseBracket(); } @Override public void visit(OWLObjectComplementOf ce) { writeOpenBracket(); write(NOT); write(ce.getOperand()); writeCloseBracket(); } @Override public void visit(OWLObjectSomeValuesFrom ce) { writeOpenBracket(); write(SOME); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(OWLObjectAllValuesFrom ce) { writeOpenBracket(); write(ALL); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(OWLObjectMinCardinality ce) { writeOpenBracket(); write(AT_LEAST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(OWLObjectExactCardinality ce) { writeOpenBracket(); write(EXACTLY); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(OWLObjectMaxCardinality ce) { writeOpenBracket(); write(AT_MOST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(OWLDataSomeValuesFrom ce) { writeOpenBracket(); write(SOME); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(OWLDataAllValuesFrom ce) { writeOpenBracket(); write(ALL); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(OWLDataMinCardinality ce) { writeOpenBracket(); write(AT_LEAST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(OWLDataExactCardinality ce) { writeOpenBracket(); write(EXACTLY); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(OWLDataMaxCardinality ce) { writeOpenBracket(); write(AT_MOST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(OWLLiteral node) { write(node.getLiteral()); } @Override public void visit(OWLObjectProperty property) { write(property.getIRI()); } @Override public void visit(OWLObjectInverseOf property) { writeOpenBracket(); write(INVERSE); writeSpace(); property.getInverse().accept(this); writeCloseBracket(); } @Override public void visit(OWLDataProperty property) { write(property.getIRI()); } @Override public void visit(OWLNamedIndividual individual) { write(individual.getIRI()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy