Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* This file is part of the OWL API.
* The contents of this file are subject to the LGPL License, Version 3.0.
* Copyright 2014, The University of Manchester
*
* 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.manchestersyntax.renderer;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.ANNOTATIONS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.ANNOTATION_PROPERTY;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.ASYMMETRIC;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.CHARACTERISTICS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.CLASS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DATATYPE;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DATA_PROPERTY;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DIFFERENT_FROM;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DIFFERENT_INDIVIDUALS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DISJOINT_CLASSES;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DISJOINT_PROPERTIES;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DISJOINT_UNION_OF;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DISJOINT_WITH;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.DOMAIN;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.EQUIVALENT_CLASSES;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.EQUIVALENT_PROPERTIES;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.EQUIVALENT_TO;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.FACTS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.FUNCTIONAL;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.HAS_KEY;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.IMPORT;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.INDIVIDUAL;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.INDIVIDUALS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.INVERSE_FUNCTIONAL;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.INVERSE_OF;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.IRREFLEXIVE;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.NOT;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.OBJECT_PROPERTY;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.ONTOLOGY;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.PREFIX;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.RANGE;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.REFLEXIVE;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.RULE;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SAME_AS;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SAME_INDIVIDUAL;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SUBCLASS_OF;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SUB_PROPERTY_CHAIN;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SUB_PROPERTY_OF;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SUPERCLASS_OF;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SUPER_PROPERTY_OF;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.SYMMETRIC;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.TRANSITIVE;
import static org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax.TYPES;
import static org.semanticweb.owlapi.model.parameters.Imports.EXCLUDED;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.semanticweb.owlapi.io.OWLRendererException;
import org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntax;
import org.semanticweb.owlapi.model.AxiomType;
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.OWLAnnotationSubject;
import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
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.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLDataRange;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDatatypeDefinitionAxiom;
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.OWLEntity;
import org.semanticweb.owlapi.model.OWLEntityVisitor;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLFunctionalObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLHasKeyAxiom;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLInverseObjectPropertiesAxiom;
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.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyDomainAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLObjectPropertyRangeAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLRuntimeException;
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.SWRLAtom;
import org.semanticweb.owlapi.model.SWRLRule;
import org.semanticweb.owlapi.util.CollectionFactory;
import org.semanticweb.owlapi.util.OWLAxiomFilter;
import org.semanticweb.owlapi.util.OWLObjectComparator;
import org.semanticweb.owlapi.util.OntologyIRIShortFormProvider;
import org.semanticweb.owlapi.util.ShortFormProvider;
/**
* The Class ManchesterOWLSyntaxFrameRenderer.
*
* @author Matthew Horridge, The University Of Manchester, Bio-Health Informatics Group
* @since 2.0.0
*/
public class ManchesterOWLSyntaxFrameRenderer extends ManchesterOWLSyntaxObjectRenderer
implements OWLEntityVisitor {
class SectionMap {
@Nonnull
private final Map> object2Axioms = new LinkedHashMap<>();
/**
* @return true if empty
*/
public boolean isNotEmpty() {
return !object2Axioms.isEmpty();
}
/**
* @param o key
* @param forAxiom axiom to add
*/
public void put(O o, V forAxiom) {
Collection axioms = object2Axioms.get(o);
if (axioms == null) {
axioms = sortedCollection();
object2Axioms.put(o, axioms);
}
axioms.add(forAxiom);
}
/**
* @param o object to remove
*/
public void remove(O o) {
object2Axioms.remove(o);
}
/**
* @return sections
*/
@Nonnull
public Collection getSectionObjects() {
return object2Axioms.keySet();
}
/**
* @param sectionObject sectionObject
* @return annotations for objects
*/
@Nonnull
public Collection> getAnnotationsForSectionObject(
Object sectionObject) {
Collection axioms = object2Axioms.get(sectionObject);
if (axioms == null) {
return sortedSet();
}
Collection> annos = new ArrayList<>();
for (OWLAxiom ax : axioms) {
annos.add(sortedCollection(ax.getAnnotations()));
}
return annos;
}
}
/** The ontologies. */
private final Set ontologies;
/** The short form provider. */
private OntologyIRIShortFormProvider shortFormProvider = new OntologyIRIShortFormProvider();
/** The filtered axiom types. */
@Nonnull
private final Set filteredAxiomTypes = filtered();
private static Set filtered() {
return Collections.singleton((AxiomType) AxiomType.SWRL_RULE);
}
/** The render extensions. */
private boolean renderExtensions = false;
private boolean explicitXsdStrings;
/** The listeners. */
@Nonnull
private final List listeners = new ArrayList<>();
/** The axiom filter. */
private OWLAxiomFilter axiomFilter = axiom -> true;
/** The rendering director. */
private RenderingDirector renderingDirector = new DefaultRenderingDirector();
@Nonnull
private final OWLObjectComparator owlObjectComparator;
/**
* Instantiates a new manchester owl syntax frame renderer.
*
* @param ontology the ontology
* @param writer the writer
* @param entityShortFormProvider the entity short form provider
*/
public ManchesterOWLSyntaxFrameRenderer(@Nonnull OWLOntology ontology, @Nonnull Writer writer,
@Nonnull ShortFormProvider entityShortFormProvider) {
this(ontology, false, writer, entityShortFormProvider);
}
/**
* Instantiates a new manchester owl syntax frame renderer.
*
* @param ontology the ontology
* @param explicitXsdString true if {@code xsd:string} datatype should be explicit in the output
* @param writer the writer
* @param entityShortFormProvider the entity short form provider
*/
public ManchesterOWLSyntaxFrameRenderer(@Nonnull OWLOntology ontology,
boolean explicitXsdString, @Nonnull Writer writer,
@Nonnull ShortFormProvider entityShortFormProvider) {
this(CollectionFactory.createSet(ontology), explicitXsdString, writer,
entityShortFormProvider);
}
/**
* Instantiates a new manchester owl syntax frame renderer.
*
* @param ontologies the ontologies
* @param writer the writer
* @param entityShortFormProvider the entity short form provider
*/
public ManchesterOWLSyntaxFrameRenderer(@Nonnull Set ontologies, Writer writer,
@Nonnull ShortFormProvider entityShortFormProvider) {
this(ontologies, false, writer, entityShortFormProvider);
}
/**
* Instantiates a new manchester owl syntax frame renderer.
*
* @param ontologies the ontologies
* @param writer the writer
* @param explicitXsdString true if {@code xsd:string} datatype should be explicit in the output
* @param entityShortFormProvider the entity short form provider
*/
public ManchesterOWLSyntaxFrameRenderer(@Nonnull Set ontologies,
boolean explicitXsdString, Writer writer,
@Nonnull ShortFormProvider entityShortFormProvider) {
super(writer, explicitXsdString, entityShortFormProvider);
this.ontologies = new LinkedHashSet<>(ontologies);
owlObjectComparator = new OWLObjectComparator(entityShortFormProvider);
}
/**
* Sets the rendering director.
*
* @param renderingDirector the new rendering director
*/
public void setRenderingDirector(RenderingDirector renderingDirector) {
this.renderingDirector = renderingDirector;
}
/**
* @param shortFormProvider short form provider to be used
*/
public void setOntologyIRIShortFormProvider(OntologyIRIShortFormProvider shortFormProvider) {
this.shortFormProvider = shortFormProvider;
}
/**
* Adds the renderer listener.
*
* @param listener the listener
*/
public void addRendererListener(RendererListener listener) {
listeners.add(listener);
}
/**
* Removes the renderer listener.
*
* @param listener the listener
*/
public void removeRendererListener(RendererListener listener) {
listeners.remove(listener);
}
/**
* Sets the axiom filter.
*
* @param axiomFilter the new axiom filter
*/
public void setAxiomFilter(OWLAxiomFilter axiomFilter) {
this.axiomFilter = axiomFilter;
}
/** Clear filtered axiom types. */
public void clearFilteredAxiomTypes() {
filteredAxiomTypes.clear();
}
/**
* Adds the filtered axiom type.
*
* @param axiomType the axiom type
*/
public void addFilteredAxiomType(AxiomType> axiomType) {
filteredAxiomTypes.add(axiomType);
}
/**
* Sets the render extensions.
*
* @param renderExtensions the new render extensions
*/
public void setRenderExtensions(boolean renderExtensions) {
this.renderExtensions = renderExtensions;
}
/**
* Write ontology.
*
* @throws OWLRendererException the OWL renderer exception
*/
public void writeOntology() throws OWLRendererException {
if (ontologies.size() != 1) {
throw new OWLRuntimeException("Can only render one ontology");
}
OWLOntology ontology = ontologies.iterator().next();
assert ontology != null;
writePrefixMap();
writeNewLine();
writeOntologyHeader(ontology);
for (OWLAnnotationProperty prop : sortedCollection(
ontology.getAnnotationPropertiesInSignature())) {
assert prop != null;
write(prop);
}
for (OWLDatatype datatype : sortedCollection(ontology.getDatatypesInSignature())) {
assert datatype != null;
write(datatype);
}
for (OWLObjectProperty prop : sortedCollection(ontology.getObjectPropertiesInSignature())) {
assert prop != null;
write(prop);
OWLObjectPropertyExpression invProp = prop.getInverseProperty();
if (!ontology.getAxioms(invProp, EXCLUDED).isEmpty()) {
write(invProp);
}
}
for (OWLDataProperty prop : sortedCollection(ontology.getDataPropertiesInSignature())) {
assert prop != null;
write(prop);
}
for (OWLClass cls : sortedCollection(ontology.getClassesInSignature())) {
assert cls != null;
write(cls);
}
for (OWLNamedIndividual ind : sortedCollection(ontology.getIndividualsInSignature())) {
assert ind != null;
write(ind);
}
for (OWLAnonymousIndividual ind : sortedCollection(
ontology.getReferencedAnonymousIndividuals(EXCLUDED))) {
assert ind != null;
write(ind);
}
// Nary disjoint classes axioms
event = new RendererEvent(this, ontology);
for (OWLDisjointClassesAxiom ax : sortedCollection(
ontology.getAxioms(AxiomType.DISJOINT_CLASSES))) {
if (ax.getClassExpressions().size() > 2) {
SectionMap