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 java.util.stream.Collectors.toList;
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.util.OWLAPIStreamUtils.add;
import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.asList;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
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.HasAnnotations;
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.OWLAnnotationSubject;
import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLDataRange;
import org.semanticweb.owlapi.model.OWLDatatype;
import org.semanticweb.owlapi.model.OWLDifferentIndividualsAxiom;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLEntityVisitor;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNaryPropertyAxiom;
import org.semanticweb.owlapi.model.OWLNegativeDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLNegativeObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLRuntimeException;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
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 {
protected final OWLObjectComparator ooc;
private final OWLOntology o;
private final Set> filteredAxiomTypes = Collections.singleton(AxiomType.SWRL_RULE);
private final List listeners = new ArrayList<>();
private final Predicate props =
ax -> ((OWLNaryPropertyAxiom>) ax).properties().count() == 2;
private OntologyIRIShortFormProvider shortFormProvider = new OntologyIRIShortFormProvider();
private boolean renderExtensions = false;
private boolean explicitXsdStrings;
private OWLAxiomFilter axiomFilter = axiom -> true;
private RenderingDirector renderingDirector = new DefaultRenderingDirector();
/**
* The event.
*/
@Nullable
private RendererEvent event;
/**
* 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(OWLOntology ontology, Writer writer,
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(OWLOntology ontology, boolean explicitXsdString,
Writer writer, ShortFormProvider entityShortFormProvider) {
super(writer, explicitXsdString, entityShortFormProvider);
o = ontology;
ooc = new OWLObjectComparator(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(Collection ontologies, Writer writer,
ShortFormProvider entityShortFormProvider) {
super(writer, entityShortFormProvider);
if (ontologies.size() != 1) {
throw new OWLRuntimeException("Can only render one ontology");
}
o = ontologies.iterator().next();
ooc = new OWLObjectComparator(entityShortFormProvider);
}
static Collection sortedSet() {
return new TreeSet<>();
}
/**
* 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 renderer exception
*/
public void writeOntology() throws OWLRendererException {
writePrefixMap();
writeNewLine();
writeOntologyHeader();
o.annotationPropertiesInSignature().sorted(ooc).forEach(this::write);
o.datatypesInSignature().sorted(ooc).forEach(this::write);
o.objectPropertiesInSignature().sorted(ooc).forEach(prop -> {
write(prop);
OWLObjectPropertyExpression invProp = prop.getInverseProperty();
if (o.axioms(invProp).count() > 0) {
write(invProp);
}
});
o.dataPropertiesInSignature().sorted(ooc).forEach(this::write);
o.classesInSignature().sorted(ooc).forEach(this::write);
o.individualsInSignature().sorted(ooc).forEach(this::write);
o.referencedAnonymousIndividuals().sorted(ooc).forEach(this::write);
// Nary disjoint classes axioms
event = new RendererEvent(this, o);
o.axioms(AxiomType.DISJOINT_CLASSES).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.classExpressions(), DISJOINT_CLASSES));
// Nary equivalent classes axioms
o.axioms(AxiomType.EQUIVALENT_CLASSES).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.classExpressions(), EQUIVALENT_CLASSES));
// Nary disjoint properties
o.axioms(AxiomType.DISJOINT_OBJECT_PROPERTIES).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.properties(), DISJOINT_PROPERTIES));
// Nary equivalent properties
o.axioms(AxiomType.EQUIVALENT_OBJECT_PROPERTIES).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.properties(), EQUIVALENT_PROPERTIES));
// Nary disjoint properties
o.axioms(AxiomType.DISJOINT_DATA_PROPERTIES).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.properties(), DISJOINT_PROPERTIES));
// Nary equivalent properties
o.axioms(AxiomType.EQUIVALENT_DATA_PROPERTIES).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.properties(), EQUIVALENT_PROPERTIES));
// Nary different individuals
o.axioms(AxiomType.DIFFERENT_INDIVIDUALS).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.individuals(), DIFFERENT_INDIVIDUALS, true));
o.axioms(AxiomType.SAME_INDIVIDUAL).sorted(ooc)
.forEach(ax -> writeMoreThanTwo(ax, ax.individuals(), SAME_INDIVIDUAL, true));
o.axioms(AxiomType.SWRL_RULE).sorted(ooc).forEach(
rule -> writeSection(RULE, Collections.singleton(rule).iterator(), ", ", false));
filtersort(o.axioms(AxiomType.SUBCLASS_OF), a -> ((OWLSubClassOfAxiom) a).isGCI())
.collect(Collectors.groupingBy(OWLSubClassOfAxiom::getSubClass)).forEach(this::write);
flush();
}
protected void writeMoreThanTwo(OWLAxiom ax, Stream stream, ManchesterOWLSyntax section,
boolean writeTwoIfAnnotated) {
List individuals = asList(stream);
if (individuals.size() > 2 || writeTwoIfAnnotated && individuals.size() == 2
&& !ax.annotationsAsList().isEmpty()) {
SectionMap