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

it.unife.ml.probowlapi.renderer.LogManchesterSyntaxObjectRenderer Maven / Gradle / Ivy

Go to download

This package encapsulates OWL API adding tools for managing DISPONTE probabilistic semantics. Used by the reasoner BUNDLE.

The newest version!
/**
 *  This file is part of BUNDLE.
 * 
 *  BUNDLE is a probabilistic reasoner for OWL 2 ontologies.
 * 
 *  BUNDLE can be used both as module and as standalone.
 * 
 *  LEAP was implemented as a plugin of DL-Learner http://dl-learner.org, 
 *  but some components can be used as stand-alone.
 * 
 *  BUNDLE and all its parts are 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 .
 * 
 */
package it.unife.ml.probowlapi.renderer;

import com.clarkparsia.owlapi.explanation.io.manchester.DescriptionSorter;
import com.clarkparsia.owlapi.explanation.io.manchester.Keyword;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAnnotationPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
import org.semanticweb.owlapi.model.OWLAsymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLCardinalityRestriction;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataAllValuesFrom;
import org.semanticweb.owlapi.model.OWLDataComplementOf;
import org.semanticweb.owlapi.model.OWLDataExactCardinality;
import org.semanticweb.owlapi.model.OWLDataHasValue;
import org.semanticweb.owlapi.model.OWLDataIntersectionOf;
import org.semanticweb.owlapi.model.OWLDataMaxCardinality;
import org.semanticweb.owlapi.model.OWLDataMinCardinality;
import org.semanticweb.owlapi.model.OWLDataOneOf;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom;
import org.semanticweb.owlapi.model.OWLDataUnionOf;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom;
import org.semanticweb.owlapi.model.OWLDatatypeRestriction;
import org.semanticweb.owlapi.model.OWLDeclarationAxiom;
import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owlapi.model.OWLDisjointClassesAxiom;
import org.semanticweb.owlapi.model.OWLDisjointDataPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLDisjointObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLDisjointUnionAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLFacetRestriction;
import org.semanticweb.owlapi.model.OWLFunctionalDataPropertyAxiom;
import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLHasKeyAxiom;
import org.semanticweb.owlapi.model.OWLInverseFunctionalObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLIrreflexiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom;
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.OWLObjectHasSelf;
import org.semanticweb.owlapi.model.OWLObjectHasValue;
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.OWLObjectOneOf;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
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.OWLQuantifiedRestriction;
import org.semanticweb.owlapi.model.OWLReflexiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLSameIndividualAxiom;
import org.semanticweb.owlapi.model.OWLSubAnnotationPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubPropertyChainOfAxiom;
import org.semanticweb.owlapi.model.OWLSymmetricObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLTransitiveObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLUnaryPropertyAxiom;
import org.semanticweb.owlapi.model.SWRLArgument;
import org.semanticweb.owlapi.model.SWRLAtom;
import org.semanticweb.owlapi.model.SWRLBuiltInAtom;
import org.semanticweb.owlapi.model.SWRLClassAtom;
import org.semanticweb.owlapi.model.SWRLDataPropertyAtom;
import org.semanticweb.owlapi.model.SWRLDataRangeAtom;
import org.semanticweb.owlapi.model.SWRLDifferentIndividualsAtom;
import org.semanticweb.owlapi.model.SWRLIndividualArgument;
import org.semanticweb.owlapi.model.SWRLLiteralArgument;
import org.semanticweb.owlapi.model.SWRLObjectPropertyAtom;
import org.semanticweb.owlapi.model.SWRLRule;
import org.semanticweb.owlapi.model.SWRLSameIndividualAtom;
import org.semanticweb.owlapi.model.SWRLVariable;
import org.semanticweb.owlapi.vocab.XSDVocabulary;
import org.slf4j.Logger;

/**
 *
 * @author Riccardo Zese , Giuseppe Cota
 * 
 */
public class LogManchesterSyntaxObjectRenderer implements OWLObjectVisitor {

    private final List indentations;
    private int totalIndentation;
    private int indentationIndex;
    private String to_write;

    protected Logger logger;

    private final int MAX_LENGTH = 150;

    /**
     * @param logger
     */
    public LogManchesterSyntaxObjectRenderer(Logger logger) {
        this.logger = logger;
        indentations = new ArrayList();
        totalIndentation = 0;
        indentationIndex = 0;
        to_write = "";
    }

    /**
     * Return the short form (local name) for a URI identifier
     *
     * @param theIRI the URI
     * @return the local name part of the URI identifier
     */
    protected String shortForm(IRI theIRI) {
        String fragment = theIRI.getFragment();
        if (fragment != null) {
            return fragment;
        }
        String str = theIRI.toString();
        int lastSlashIndex = str.lastIndexOf('/');
        if (lastSlashIndex != -1) {
            return str.substring(lastSlashIndex + 1, str.length());
        }
        return str;
    }

    public void flush() {
        logger.debug(to_write);
        to_write = "";
        writeIndent();
    }

    public void flush(int ind) {
        logger.debug(to_write);
        to_write = "";
        writeIndent(ind, true);
    }

    public void visit(OWLAsymmetricObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.ASYMMETRIC_PROPERTY);
    }

    public void visit(OWLClass theOWLClass) {
        write(theOWLClass.getIRI());
    }

    public void visit(OWLClassAssertionAxiom theAxiom) {
        writeKeywordInfix(Keyword.TYPE, theAxiom.getIndividual(), theAxiom.getClassExpression());
    }

    public void visit(OWLAnnotation theAnnotation) {
        write(Keyword.ANNOTATION);
        writeSpace();
        write("(");
        write(theAnnotation.getProperty());
        write(" ");
        write(theAnnotation.getValue());
        write(")");
    }

    public void visit(OWLDataAllValuesFrom theDescription) {
        writeQuantifiedRestriction(theDescription, Keyword.ONLY);
    }

    public void visit(OWLDataComplementOf theDescription) {
        writeKeywordPrefix(Keyword.NOT, theDescription.getDataRange());
    }

    public void visit(OWLDataExactCardinality theDescription) {
        writeCardinalityRestriction(theDescription, Keyword.EXACTLY);
    }

    public void visit(OWLDataMaxCardinality theDescription) {
        writeCardinalityRestriction(theDescription, Keyword.MAX);
    }

    public void visit(OWLDataMinCardinality theDescription) {
        writeCardinalityRestriction(theDescription, Keyword.MIN);
    }

    public void visit(OWLDataOneOf theDescription) {
        writeEnumeration(theDescription.getValues());
    }

    public void visit(OWLDataProperty theProperty) {
        write(theProperty.getIRI());
    }

    public void visit(OWLDataPropertyAssertionAxiom theAxiom) {
        write(theAxiom.getSubject());
        writeSpace();
        write(theAxiom.getProperty());
        writeSpace();
        write(theAxiom.getObject());
    }

    public void visit(OWLDataPropertyDomainAxiom theAxiom) {
        writeKeywordInfix(Keyword.DOMAIN, theAxiom.getProperty(), theAxiom.getDomain());
    }

    public void visit(OWLDataPropertyRangeAxiom theAxiom) {
        writeKeywordInfix(Keyword.RANGE, theAxiom.getProperty(), theAxiom.getRange());
    }

    public void visit(OWLFacetRestriction theRestriction) {
        write(theRestriction.getFacet().getSymbolicForm());
        writeSpace();
        write(theRestriction.getFacetValue());
    }

    public void visit(OWLDatatypeRestriction theRestriction) {

        write(theRestriction.getDatatype());
        write("[");
        boolean first = true;
        for (OWLFacetRestriction restriction : theRestriction.getFacetRestrictions()) {
            if (first) {
                first = false;
            } else {
                write(",");
                writeSpace();
            }
            write(restriction);
        }
        write("]");

    }

    public void visit(OWLDataSomeValuesFrom theDescription) {
        writeQuantifiedRestriction(theDescription, Keyword.SOME);
    }

    public void visit(OWLSubDataPropertyOfAxiom theAxiom) {
        writeKeywordInfix(Keyword.SUB_PROPERTY_OF, theAxiom.getSubProperty(), theAxiom
                .getSuperProperty());
    }

    public void visit(OWLDatatype node) {
        write(node.getIRI());
    }

    public void visit(OWLDataHasValue theDescription) {
        writeRestriction(theDescription.getProperty(), Keyword.VALUE, theDescription.getValue());
    }

    public void visit(OWLDeclarationAxiom theAxiom) {
        writeKeywordPrefix(Keyword.DECLARATION, theAxiom.getEntity());
    }

    public void visit(OWLDifferentIndividualsAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getIndividuals(), Keyword.DIFFERENT_INDIVIDUAL,
                Keyword.DIFFERENT_INDIVIDUALS);
    }

    public void visit(OWLDisjointClassesAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getClassExpressions(), Keyword.DISJOINT_CLASS,
                Keyword.DISJOINT_CLASSES);
    }

    public void visit(OWLDisjointDataPropertiesAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getProperties(), Keyword.DISJOINT_PROPERTY,
                Keyword.DISJOINT_PROPERTIES);
    }

    public void visit(OWLDisjointObjectPropertiesAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getProperties(), Keyword.DISJOINT_PROPERTY,
                Keyword.DISJOINT_PROPERTIES);
    }

    public void visit(OWLDisjointUnionAxiom theAxiom) {
        write(theAxiom.getOWLClass());
        writeSpace();
        write(Keyword.DISJOINT_UNION);
        writeSpace();
        writeNaryKeyword(Keyword.OR, theAxiom.getClassExpressions());
    }

    public void visit(OWLEquivalentClassesAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getClassExpressions(), Keyword.EQUIVALENT_TO,
                Keyword.EQUIVALENT_CLASSES);
    }

    public void visit(OWLEquivalentDataPropertiesAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getProperties(), Keyword.EQUIVALENT_TO,
                Keyword.EQUIVALENT_PROPERTIES);
    }

    public void visit(OWLEquivalentObjectPropertiesAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getProperties(), Keyword.EQUIVALENT_TO,
                Keyword.EQUIVALENT_PROPERTIES);
    }

    public void visit(OWLFunctionalDataPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.FUNCTIONAL);
    }

    public void visit(OWLFunctionalObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.FUNCTIONAL);
    }

    public void visit(OWLAnonymousIndividual theIndividual) {
        write(theIndividual.getID().getID());
    }

    public void visit(OWLNamedIndividual theIndividual) {
        write(theIndividual.getIRI());
    }

    public void visit(OWLInverseFunctionalObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.INVERSE_FUNCTIONAL);
    }

    public void visit(OWLInverseObjectPropertiesAxiom theAxiom) {
        writeKeywordInfix(Keyword.INVERSE_OF, theAxiom.getFirstProperty(), theAxiom
                .getSecondProperty());
    }

    public void visit(OWLIrreflexiveObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.IRREFLEXIVE);
    }

    public void visit(OWLNegativeDataPropertyAssertionAxiom theAxiom) {
        write(Keyword.NOT_RELATIONSHIP);
        writeSpace();
        write("(");
        write(theAxiom.getSubject());
        writeSpace();
        write(theAxiom.getProperty());
        writeSpace();
        write(theAxiom.getObject());
        write(")");
    }

    public void visit(OWLNegativeObjectPropertyAssertionAxiom theAxiom) {
        write(Keyword.NOT_RELATIONSHIP);
        writeSpace();
        write("(");
        write(theAxiom.getSubject());
        writeSpace();
        write(theAxiom.getProperty());
        writeSpace();
        write(theAxiom.getObject());
        write(")");
    }

    public void visit(OWLObjectAllValuesFrom theDescription) {
        writeQuantifiedRestriction(theDescription, Keyword.ONLY);
    }

    public void visit(OWLObjectComplementOf theDescription) {
        writeKeywordPrefix(Keyword.NOT, theDescription.getOperand());
    }

    public void visit(OWLObjectExactCardinality theDescription) {
        writeCardinalityRestriction(theDescription, Keyword.EXACTLY);
    }

    public void visit(OWLObjectIntersectionOf theDescription) {
        writeNaryKeyword(Keyword.AND, theDescription.getOperands());
    }

    public void visit(OWLObjectMaxCardinality theDescription) {
        writeCardinalityRestriction(theDescription, Keyword.MAX);
    }

    public void visit(OWLObjectMinCardinality theDescription) {
        writeCardinalityRestriction(theDescription, Keyword.MIN);
    }

    public void visit(OWLObjectOneOf theDescription) {
        writeEnumeration(theDescription.getIndividuals());
    }

    public void visit(OWLObjectProperty theProperty) {
        write(theProperty.getIRI());
    }

    public void visit(OWLObjectPropertyAssertionAxiom theAxiom) {
        write(theAxiom.getSubject());
        writeSpace();
        write(theAxiom.getProperty());
        writeSpace();
        write(theAxiom.getObject());
    }

    public void visit(OWLSubPropertyChainOfAxiom theAxiom) {
        writeCollection(theAxiom.getPropertyChain(), " o", false);
        writeSpace();
        write(Keyword.SUB_PROPERTY_OF);
        writeSpace();
        write(theAxiom.getSuperProperty());
    }

    public void visit(OWLObjectPropertyDomainAxiom theAxiom) {
        writeKeywordInfix(Keyword.DOMAIN, theAxiom.getProperty(), theAxiom.getDomain());
    }

    public void visit(OWLObjectInverseOf theInverse) {
        writeKeywordPrefix(Keyword.INVERSE, theInverse.getInverse());
    }

    public void visit(OWLObjectPropertyRangeAxiom theAxiom) {
        writeKeywordInfix(Keyword.RANGE, theAxiom.getProperty(), theAxiom.getRange());
    }

    public void visit(OWLObjectHasSelf theRestriction) {
        writeRestriction(theRestriction.getProperty(), Keyword.SELF);
    }

    public void visit(OWLObjectSomeValuesFrom theDescription) {
        writeQuantifiedRestriction(theDescription, Keyword.SOME);
    }

    public void visit(OWLSubObjectPropertyOfAxiom theAxiom) {
        writeKeywordInfix(Keyword.SUB_PROPERTY_OF, theAxiom.getSubProperty(), theAxiom
                .getSuperProperty());
    }

    public void visit(OWLObjectUnionOf theDescription) {
        writeNaryKeyword(Keyword.OR, theDescription.getOperands());
    }

    public void visit(OWLObjectHasValue theDescription) {
        writeRestriction(theDescription.getProperty(), Keyword.VALUE, theDescription.getValue());
    }

    public void visit(OWLOntology ontology) {
        write(ontology.getOntologyID().getOntologyIRI().get());
    }

    public void visit(OWLReflexiveObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.REFLEXIVE_PROPERTY);
    }

    public void visit(OWLSameIndividualAxiom theAxiom) {
        writeNaryAxiom(theAxiom.getIndividuals(), Keyword.SAME_INDIVIDUAL,
                Keyword.SAME_INDIVIDUALS);
    }

    public void visit(OWLSubClassOfAxiom theAxiom) {
        writeKeywordInfix(Keyword.SUB_CLASS_OF, theAxiom.getSubClass(), theAxiom.getSuperClass());
    }

    public void visit(OWLSymmetricObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.SYMMETRIC);
    }

    public void visit(OWLTransitiveObjectPropertyAxiom theAxiom) {
        writeUnaryPropertyAxiom(theAxiom, Keyword.TRANSITIVE);
    }

    public void visit(OWLLiteral node) {
        if (node.isRDFPlainLiteral()) {
            write("\"");
            write(node.getLiteral());
            write("\"");
            if (node.getLang() != null && !node.getLang().equals("")) {
                write("@");
                write(node.getLang());
            }
        } else if (node.getDatatype().getIRI().equals(XSDVocabulary.INTEGER.getIRI())
                || node.getDatatype().getIRI().equals(XSDVocabulary.DECIMAL.getIRI())) {
            write(node.getLiteral());
        } else if (node.getDatatype().getIRI().equals(XSDVocabulary.FLOAT.getIRI())) {
            write(node.getLiteral());
            write("f");
        } else {
            write("\"");
            write(node.getLiteral());
            write("\"");
            write("^^");
            write(node.getDatatype());
        }
    }

    public void visit(SWRLLiteralArgument node) {
        write(node.getLiteral());
    }

    public void visit(SWRLIndividualArgument node) {
        write(node.getIndividual());
    }

    public void visit(SWRLVariable node) {
        write("?");
        write(node.getIRI());
    }

    public void visit(SWRLBuiltInAtom node) {
        write(node.getPredicate());
        write("(");
        for (SWRLArgument arg : node.getArguments()) {
            write(arg);
            write(" ");
        }
        write(")");
    }

    public void visit(SWRLClassAtom node) {
        write(node.getPredicate());
        write("(");
        write(node.getArgument());
        write(")");
    }

    /*
     * this is all the SWRL rendering stuff that we'll provide some defaults for
     * using evren's concise format stuff
     */
    public void visit(SWRLDataRangeAtom node) {
        write(node.getPredicate());
        write("(");
        write(node.getArgument());
        write(")");
    }

    public void visit(SWRLDataPropertyAtom node) {
        write(node.getPredicate());
        write("(");
        write(node.getFirstArgument());
        write(", ");
        write(node.getSecondArgument());
        write(")");
    }

    public void visit(SWRLDifferentIndividualsAtom node) {
        write("differentFrom");
        write("(");
        write(node.getFirstArgument());
        write(", ");
        write(node.getSecondArgument());
        write(")");
    }

    public void visit(SWRLObjectPropertyAtom node) {
        write(node.getPredicate());
        write("(");
        write(node.getFirstArgument());
        write(", ");
        write(node.getSecondArgument());
        write(")");
    }

    /**
     * @inheritDoc
     */
    public void visit(SWRLRule rule) {
        write("Rule");
        write("(");

//		if( !rule.isAnonymous() ) {
//			write( rule.getIRI() );
//			writeSpace();
//		}
        boolean first = true;
        for (SWRLAtom at : rule.getBody()) {
            if (first) {
                first = false;
            } else {
                write(", ");
            }
            write(at);

        }
        write(" -> ");

        first = true;
        for (SWRLAtom at : rule.getHead()) {
            if (first) {
                first = false;
            } else {
                write(", ");
            }
            write(at);
        }

        write(")");
    }

    public void visit(SWRLSameIndividualAtom node) {
        write("sameAs");
        write("(");
        write(node.getFirstArgument());
        write(", ");
        write(node.getSecondArgument());
        write(")");
    }

    protected void writeNaryKeyword(Keyword theKeyword, Set  objects) {

        //theObjects = DescriptionSorter.toSortedSet(theObjects);
        List theObjects = new ArrayList<>(objects);
        Collections.sort(theObjects);

        Iterator aIter = theObjects.iterator();

        write("(");

        startBlock(1);

        write(aIter.next());
        while (aIter.hasNext()) {
            writeNewLine();

            if (theKeyword != null) {
                write(theKeyword);
                writeSpace();
            }

            write(aIter.next());
        }

        endBlock();

        write(")");
    }

    protected void writeCardinalityRestriction(OWLCardinalityRestriction theRestriction,
            Keyword theKeyword) {
        if (theRestriction.isQualified()) {
            writeRestriction(theRestriction.getProperty(), theKeyword, theRestriction
                    .getCardinality(), theRestriction.getFiller());
        } else {
            writeRestriction(theRestriction.getProperty(), theKeyword, theRestriction
                    .getCardinality());
        }
    }

    /**
     * Render an n-ary axiom with special handling for the binary case.
     *
     * @param set objects to be rendered
     * @param binary keyword used for binary case
     * @param nary keyword used for n-ary case
     */
    protected void writeNaryAxiom(Set set, Keyword binary, Keyword nary) {
        //Set sortedSet = DescriptionSorter.toSortedSet(set);
        List sortedSet = new ArrayList<>(set);
        Collections.sort(sortedSet);
        Iterator aIter = sortedSet.iterator();

        if (set.size() == 2) {
            writeKeywordInfix(binary, aIter.next(), aIter.next());
        } else {
            write(nary);
            write("(");

            writeNaryKeyword(null, set);

            write(")");
        }
    }

    protected void writeQuantifiedRestriction(OWLQuantifiedRestriction theRestriction,
            Keyword theKeyword) {
        writeRestriction(theRestriction.getProperty(), theKeyword, theRestriction.getFiller());
    }

    protected void writeRestriction(OWLPropertyExpression theProperty, Keyword theKeyword,
            Object... theArgs) {
        // write( "(" );

        write(theProperty);
        writeSpace();
        write(theKeyword);
        for (Object aObject : theArgs) {
            writeSpace();
            if (aObject instanceof OWLObject) {
                write((OWLObject) aObject);
            } else {
                write(aObject.toString());
            }
        }

		// write( ")" );
    }

    protected void writeUnaryPropertyAxiom(OWLUnaryPropertyAxiom theAxiom, Keyword keyword) {
        writeKeywordPrefix(keyword, theAxiom.getProperty());
    }

    protected void writeKeywordPrefix(Keyword keyword, OWLObject arg) {
        write(keyword);
        writeSpace();
//		write( "(" );
        write(arg);
//		write( ")" );
    }

    protected void writeKeywordPrefix(Keyword keyword, OWLObject arg1, OWLObject arg2) {
        write(keyword);
        writeSpace();
        write("(");
        write(arg1);
        writeSpace();
        write(arg2);
        write(")");
//                flush();
    }

    protected void writeKeywordInfix(Keyword keyword, OWLObject arg1, OWLObject arg2) {
        write(arg1);
        writeSpace();
        write(keyword);
        writeSpace();
        write(arg2);
//                flush();
    }

    protected void writeEnumeration(Set objects) {
        write(Keyword.OPEN_BRACE);
        writeCollection(objects, ",", true);
        write(Keyword.CLOSE_BRACE);
//                flush();
    }

    protected void writeCollection(Collection objectsIn, String separator, boolean sort) {
        List objects = new ArrayList<>(objectsIn);
        if (sort) {
            //objects = DescriptionSorter.toSortedSet(objects);
            Collections.sort(objects);
        }
        boolean first = true;
        for (OWLObject ind : objects) {
            if (first) {
                first = false;
            } else {
                write(separator);
                writeSpace();
            }
            write(ind);
        }
//                flush();
    }

    protected void write(OWLObject object) {
        object.accept(this);
    }

    protected void write(Keyword keyword) {
        write(keyword.getLabel());
    }

    protected void write(String s) {

        if (to_write.length() + s.length() > this.MAX_LENGTH) {
            flush(3);
        }
        to_write += s;
    }

    protected void writeln(String s) {
        to_write += s;
        flush();
    }

    protected void write(IRI iri) {
        write(shortForm(iri));
    }

    protected void writeNewLine() {
        flush();
    }

    protected void writeSpace() {
        write(" ");
    }

    protected void writeIndent() {
        for (int i = 0; i < totalIndentation; i++) {
            write(" ");
        }
    }

    protected void writeIndent(int ind, boolean add) {
        if (add) {
            ind += totalIndentation;
        }
        for (int i = 0; i < ind; i++) {
            write(" ");
        }
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLHasKeyAxiom theAxiom) {
        write(theAxiom.getClassExpression());
        writeSpace();
        write(Keyword.HAS_KEY);
        if (!theAxiom.getObjectPropertyExpressions().isEmpty()) {
            writeCollection(theAxiom.getObjectPropertyExpressions(), "", true);
            writeSpace();
        }
        writeCollection(theAxiom.getDataPropertyExpressions(), "", true);
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLDatatypeDefinitionAxiom theAxiom) {
        writeKeywordInfix(Keyword.EQUIVALENT_TO, theAxiom.getDatatype(), theAxiom.getDataRange());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLAnnotationAssertionAxiom theAxiom) {
        write(theAxiom.getSubject());
        writeSpace();
        write(theAxiom.getProperty());
        writeSpace();
        write(theAxiom.getValue());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLSubAnnotationPropertyOfAxiom theAxiom) {
        writeKeywordInfix(Keyword.SUB_PROPERTY_OF, theAxiom.getSubProperty(), theAxiom.getSuperProperty());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLAnnotationPropertyDomainAxiom theAxiom) {
        writeKeywordInfix(Keyword.DOMAIN, theAxiom.getProperty(), theAxiom.getDomain());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLAnnotationPropertyRangeAxiom theAxiom) {
        writeKeywordInfix(Keyword.RANGE, theAxiom.getProperty(), theAxiom.getRange());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLDataIntersectionOf node) {
        writeNaryKeyword(Keyword.AND, node.getOperands());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLDataUnionOf node) {
        writeNaryKeyword(Keyword.OR, node.getOperands());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(OWLAnnotationProperty property) {
        write(property.getIRI());
    }

    /**
     * {@inheritDoc}
     */
    public void visit(IRI iri) {
        write(iri);
    }

    void startBlock(int ind) {
        if (to_write.length() > totalIndentation) {
            int diff = to_write.length() - totalIndentation;
            for (int i = 0; i < ind; i++) {
                write(" ");
            }
            ind += diff;
        }
        indentations.add(ind);
        indentationIndex++;
        totalIndentation += ind;
        //flush();
    }

    void endBlock() {
        totalIndentation -= indentations.get(indentationIndex - 1);
        indentations.remove(indentationIndex - 1);
        indentationIndex--;
        flush();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy