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

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

There is a newer version: 4.7.6
Show 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 java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
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.OWLObject;
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.OWLOntology;
import org.semanticweb.owlapi.model.OWLPropertyExpression;
import org.semanticweb.owlapi.model.OWLRuntimeException;
import org.semanticweb.owlapi.model.OWLSameIndividualAxiom;
import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
import org.semanticweb.owlapi.search.Filters;
import org.semanticweb.owlapi.util.CollectionFactory;
import org.semanticweb.owlapi.util.OWLObjectVisitorAdapter;

/**
 * A {@code KRSSObjectRenderer} renders an OWLOntology in the original KRSS syntax. Note that only a
 * subset of OWL can be expressed in KRSS. 
* Abbreviations * * * * * * * * * * * * * * * * * *
CNconcept name
C,D,Econcept expression
RNrole name
R, R1, R2,...role expressions, i.e. role name or inverse role
*
* 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 * * * * * * * * * *
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.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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 extends OWLObjectVisitorAdapter { @Nonnull private static final String OPEN_BRACKET = "("; @Nonnull private static final String CLOSE_BRACKET = ")"; @Nonnull private static final String NEWLINE = "\n"; @Nonnull protected final OWLOntology ont; @Nonnull protected final Writer writer; private int pos = 0; private int lastNewLinePos = 0; /** * @param ontology ontology * @param writer writer */ public KRSSObjectRenderer(@Nonnull OWLOntology ontology, @Nonnull Writer writer) { ont = checkNotNull(ontology); this.writer = new PrintWriter(writer); } @Nonnull protected static List sort(@Nonnull Collection objects) { return CollectionFactory.sortOptionally(objects); } @Nonnull protected static List sort(@Nonnull Iterable objects) { Collection sortedDescriptions = new ArrayList<>(); for (T t : objects) { sortedDescriptions.add(t); } return CollectionFactory.sortOptionally(sortedDescriptions); } protected void writeOpenBracket() { write(OPEN_BRACKET); } protected void writeCloseBracket() { write(CLOSE_BRACKET); } protected void write(int i) { write(" " + i); } protected void write(@Nonnull IRI iri) { write(iri.toString()); } protected void write(@Nonnull KRSSVocabulary v) { write(v.toString()); } protected void writeSpace() { write(" "); } protected void write(@Nonnull String s) { try { int newLineIndex = s.indexOf('\n'); if (newLineIndex != -1) { lastNewLinePos = pos + newLineIndex; } pos += s.length(); writer.write(s); } catch (IOException e) { throw new OWLRuntimeException(e); } } 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(@Nonnull OWLClassExpression obj) { writeSpace(); obj.accept(this); } protected void write(@Nonnull OWLIndividual ind) { writeSpace(); ind.accept(this); } protected void write(@Nonnull OWLPropertyExpression obj) { writeSpace(); obj.accept(this); } protected void write(@Nonnull OWLDataRange obj) { writeSpace(); obj.accept(this); } protected void flattenProperties(@Nonnull Iterable properties, @Nullable KRSSVocabulary junctor) { List props = sort(properties); int size = props.size(); if (size == 0) { return; } if (size == 1) { write(properties.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(@Nonnull Iterable description, @Nonnull KRSSVocabulary junctor) { List descs = sort(description); int size = descs.size(); if (size == 0) { return; } write(descs.get(0)); if (size == 1) { return; } writeOpenBracket(); write(junctor); int indent = getIndent(); for (int i = 1; i < size; i++) { writeln(); writeIndent(indent); write(descs.get(i)); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLOntology ontology) { Set classes = ontology.getClassesInSignature(); classes.remove(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLThing()); classes.remove(ontology.getOWLOntologyManager().getOWLDataFactory().getOWLNothing()); for (OWLClass eachClass : sort(classes)) { boolean primitive = !isDefined(eachClass, ontology); if (primitive) { writeOpenBracket(); write(DEFINE_PRIMITIVE_CONCEPT); write(eachClass); writeSpace(); Iterable supclasses = sup(ontology.getSubClassAxiomsForSubClass(eachClass), OWLClassExpression.class); flatten(supclasses, AND); writeCloseBracket(); writeln(); } else { writeOpenBracket(); write(DEFINE_CONCEPT); write(eachClass); Iterable equivalentClasses = equivalent(ontology.getEquivalentClassesAxioms(eachClass)); flatten(equivalentClasses, AND); writeCloseBracket(); writeln(); } } for (OWLObjectProperty property : sort(ontology.getObjectPropertiesInSignature())) { writeOpenBracket(); Collection properties = equivalent(ontology.getEquivalentObjectPropertiesAxioms(property)); 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(); Collection axioms = ontology.filterAxioms(Filters.subObjectPropertyWithSub, property, INCLUDED); properties = sup(axioms, OWLObjectPropertyExpression.class); if (!properties.isEmpty()) { write(properties.iterator().next()); } } writeCloseBracket(); writeln(); } for (OWLAxiom axiom : ontology.getAxioms()) { axiom.accept(this); } try { writer.flush(); } catch (IOException io) { throw new OWLRuntimeException(io); } } @Override public void visit(@Nonnull OWLDisjointClassesAxiom axiom) { List classes = sort(axiom.getClassExpressions()); int size = classes.size(); if (size <= 1) { return; } for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { writeOpenBracket(); write(DISJOINT); write(classes.get(i)); write(classes.get(j)); writeCloseBracket(); writeln(); } } } @Override public void visit(@Nonnull OWLObjectPropertyDomainAxiom axiom) { writeOpenBracket(); write(DOMAIN); write(axiom.getProperty()); write(axiom.getDomain()); writeCloseBracket(); writeln(); } @Override public void visit(@Nonnull OWLDifferentIndividualsAxiom axiom) { List individuals = sort(axiom.getIndividuals()); int size = individuals.size(); if (size <= 1) { return; } for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { writeOpenBracket(); write(DISTINCT); write(individuals.get(i)); write(individuals.get(j)); writeCloseBracket(); writeln(); } } } @Override public void visit(@Nonnull OWLObjectPropertyRangeAxiom axiom) { writeOpenBracket(); write(RANGE); write(axiom.getProperty()); write(axiom.getRange()); writeCloseBracket(); writeln(); } @Override public void visit(@Nonnull OWLObjectPropertyAssertionAxiom axiom) { writeOpenBracket(); write(RELATED); write(axiom.getSubject()); write(axiom.getProperty()); write(axiom.getObject()); writeCloseBracket(); writeln(); } @Override public void visit(@Nonnull OWLClassAssertionAxiom axiom) { writeOpenBracket(); write(INSTANCE); write(axiom.getIndividual()); write(axiom.getClassExpression()); writeCloseBracket(); writeln(); } @Override public void visit(@Nonnull OWLTransitiveObjectPropertyAxiom axiom) { writeOpenBracket(); write(TRANSITIVE); write(axiom.getProperty()); writeCloseBracket(); writeln(); } @Override public void visit(@Nonnull OWLSameIndividualAxiom axiom) { List individuals = sort(axiom.getIndividuals()); int size = individuals.size(); if (size <= 1) { return; } for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { writeOpenBracket(); write(EQUAL); write(individuals.get(i)); write(individuals.get(j)); writeCloseBracket(); writeln(); } } } @Override public void visit(@Nonnull OWLClass ce) { write(ce.getIRI()); } @Override public void visit(@Nonnull OWLObjectIntersectionOf ce) { writeOpenBracket(); write(AND); List operands = sort(ce.getOperands()); 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(@Nonnull OWLObjectUnionOf ce) { writeOpenBracket(); write(OR); List operands = sort(ce.getOperands()); 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(@Nonnull OWLObjectComplementOf ce) { writeOpenBracket(); write(NOT); write(ce.getOperand()); writeCloseBracket(); } @Override public void visit(@Nonnull OWLObjectSomeValuesFrom ce) { writeOpenBracket(); write(SOME); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(@Nonnull OWLObjectAllValuesFrom ce) { writeOpenBracket(); write(ALL); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(@Nonnull OWLObjectMinCardinality ce) { writeOpenBracket(); write(AT_LEAST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLObjectExactCardinality ce) { writeOpenBracket(); write(EXACTLY); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLObjectMaxCardinality ce) { writeOpenBracket(); write(AT_MOST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLDataSomeValuesFrom ce) { writeOpenBracket(); write(SOME); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(@Nonnull OWLDataAllValuesFrom ce) { writeOpenBracket(); write(ALL); write(ce.getProperty()); write(ce.getFiller()); writeCloseBracket(); } @Override public void visit(@Nonnull OWLDataMinCardinality ce) { writeOpenBracket(); write(AT_LEAST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLDataExactCardinality ce) { writeOpenBracket(); write(EXACTLY); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLDataMaxCardinality ce) { writeOpenBracket(); write(AT_MOST); write(ce.getCardinality()); write(ce.getProperty()); if (ce.isQualified()) { write(ce.getFiller()); } writeCloseBracket(); } @Override public void visit(@Nonnull OWLLiteral node) { write(node.getLiteral()); } @Override public void visit(@Nonnull OWLObjectProperty property) { write(property.getIRI()); } @Override public void visit(@Nonnull OWLObjectInverseOf property) { writeOpenBracket(); write(INVERSE); writeSpace(); property.getInverse().accept(this); writeCloseBracket(); } @Override public void visit(@Nonnull OWLDataProperty property) { write(property.getIRI()); } @Override public void visit(@Nonnull OWLNamedIndividual individual) { write(individual.getIRI()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy