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.*;
import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.*;
import java.io.Writer;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;
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.*;
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;
import com.google.common.collect.Sets;
/**
* 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 {
private class SectionMap {
@Nonnull private final Map> object2Axioms = new LinkedHashMap<>();
SectionMap() {}
boolean isNotEmpty() {
return !object2Axioms.isEmpty();
}
void put(O obj, V forAxiom) {
Collection axioms = object2Axioms.get(obj);
if (axioms == null) {
axioms = sortedCollection();
object2Axioms.put(obj, axioms);
}
axioms.add(forAxiom);
}
void remove(O obj) {
object2Axioms.remove(obj);
}
Collection getSectionObjects() {
return object2Axioms.keySet();
}
Collection> getAnnotationsForSectionObject(Object sectionObject) {
Collection axioms = object2Axioms.get(sectionObject);
if (axioms == null) {
return sortedSet();
}
Collection> annos = new ArrayList<>();
axioms.forEach(ax -> annos.add(asList(ax.annotations().sorted(ooc))));
return annos;
}
}
private final OWLOntology o;
private OntologyIRIShortFormProvider shortFormProvider = new OntologyIRIShortFormProvider();
@Nonnull private final Set> filteredAxiomTypes = Sets.newHashSet(AxiomType.SWRL_RULE);
private boolean renderExtensions = false;
@Nonnull private final List listeners = new ArrayList<>();
private OWLAxiomFilter axiomFilter = axiom -> true;
private RenderingDirector renderingDirector = new DefaultRenderingDirector();
@Nonnull protected final OWLObjectComparator ooc;
private final Predicate props = ax -> ((OWLNaryPropertyAxiom>) ax).properties().count() == 2;
/** The event. */
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) {
super(writer, 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);
}
/**
* 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 {
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));
o.axioms(AxiomType.SWRL_RULE).sorted(ooc).forEach(rule -> writeSection(RULE, Collections.singleton(rule)
.iterator(), ", ", false));
flush();
}
protected void writeMoreThanTwo(OWLAxiom ax, Stream stream, ManchesterOWLSyntax section) {
List individuals = asList(stream);
if (individuals.size() > 2) {
SectionMap