public class ChangeClassifier {

    private final Map<OWLEntity, Collection<AxiomChangeData>> changesByEntity;

    public ChangeClassifier(final Collection<AxiomChangeData> changes) {
        changesByEntity = new HashMap<OWLEntity, Collection<AxiomChangeData>>();
        for (final AxiomChangeData c : changes)
            for (final OWLEntity e : c.getAxiom().getSignature()) {
                Collection<AxiomChangeData> affectingChanges = changesByEntity.get(e);
                if (affectingChanges == null) {
                    affectingChanges = new ArrayList<AxiomChangeData>();
                    changesByEntity.put(e, affectingChanges);
                }
                affectingChanges.add(c);
            }
    }

    public Collection<AxiomChangeData> getChangesByEntity(final OWLEntity e) {
        return changesByEntity.get(e);
    }

}
 public class EntityClassifier {

    private final Collection<OWLEntity> newEntities;
    private final Collection<OWLEntity> removedEntities;
    private final Collection<OWLEntity> modifiedEntities;

    public Collection<OWLEntity> getNewEntities() {
        return newEntities;
    }

    public Collection<OWLEntity> getRemovedEntities() {
        return removedEntities;
    }

    public Collection<OWLEntity> getModifiedEntities() {
        return modifiedEntities;
    }

    public Iterable<OWLEntity> getAllEntities() {
        return Iterables.concat(removedEntities, newEntities, modifiedEntities);
    }

    /**
     * Classify all entities into removed, added and modified ones.
     * 
     * @param parentEntities
     * @param childEntities
     * @param affectedEntities
     */
    public EntityClassifier(final OWLOntology parent, final OWLOntology child,
            final Collection<OWLEntity> affectedEntities,
            final Set<OWLEntity> newEntities,
            final Set<OWLEntity> removedEntities,
            final Set<OWLEntity> modifiedEntities) {
        this.newEntities = newEntities;
        this.removedEntities = removedEntities;
        this.modifiedEntities = modifiedEntities;
        for (final OWLEntity e : affectedEntities)
            if (parent.containsEntityInSignature(e)) {
                if (child.containsEntityInSignature(e))
                    modifiedEntities.add(e);
                else
                    removedEntities.add(e);
            } else
                // assert child.containsEntityInSignature(e);
                newEntities.add(e);
    }
} public class EntityCollector extends OWLEntityCollector implements
        CustomOntologyChangeDataVisitor<Object, EntityCollectorException> {

    public EntityCollector(final Set<OWLEntity> toReturn,
            final Collection<OWLAnonymousIndividual> anonsToReturn) {
        super(toReturn, anonsToReturn);
    }

    @Override
    public Object visit(AddAxiomData data) {
        data.getAxiom().accept(this);
        return null;
    }

    @Override
    public Object visit(RemoveAxiomData data) {
        data.getAxiom().accept(this);
        return null;
    }

    @Override
    public Object visit(AddOntologyAnnotationData data) {
        // We don't need to count annotation properties and datatypes as entities
        return null;
    }

    @Override
    public Object visit(RemoveOntologyAnnotationData data) {
        // We don't need to count annotation properties and datatypes as entities
        return null;
    }

    @Override
    public Object visit(SetOntologyIDData data) {
        return null;
    }

    @Override
    public Object visit(AddImportData data) {
        return null;
    }

    @Override
    public Object visit(RemoveImportData data) {
        return null;
    }

    @Override
    public Object visit(SetOntologyFormatData data) {
        return null;
    }

    @Override
    public Object visit(AddPrefixData data) {
        return null;
    }

    @Override
    public Object visit(RemovePrefixData data) {
        return null;
    }

    @Override
    public Object visit(ModifyPrefixData data) {
        return null;
    }

    @Override
    public Object visit(RenamePrefixData data) {
        return null;
    }
}
 public class EntityCollectorException extends Exception {

    private static final long serialVersionUID = -3348031579335250998L;

}
 public abstract class PrefixChangeData extends CustomOntologyChangeData {

    private static final long serialVersionUID = -1862023752748180868L;

    private final String prefixName;
    private final String prefix;

    public PrefixChangeData(String prefixName, String prefix) {
        if (prefixName == null) {
            throw new NullPointerException("prefixName must not be null");
        }
        if (prefix == null) {
            throw new NullPointerException("prefix must not be null");
        }
        this.prefixName = prefixName;
        this.prefix = prefix;
    }

    /**
     * @return the prefixName
     */
    public String getPrefixName() {
        return prefixName;
    }

    /**
     * @return the prefix
     */
    public String getPrefix() {
        return prefix;
    }

    public Collection<String> getPrefixSignature() {
        final Collection<String> sig = new ArrayList<String>();
        sig.add(getPrefixName());
        sig.add(getPrefix());
        return sig;
    }

}
 public class SetOntologyFormatData extends CustomOntologyChangeData {

    private static final long serialVersionUID = 4000984588491273026L;

    private final OWLOntologyFormat newFormat;

    public SetOntologyFormatData(OWLOntologyFormat newFormat) {
        if (newFormat == null) {
            throw new NullPointerException("newFormat must not be null");
        }
        this.newFormat = newFormat;
    }

    /**
     * Creates a set ontology format change, which will set the format of the
     * ontology to the specified new format specified as a string.
     *
     * @param ont
     *            The ontology whose format is to be changed
     * @param newOntologyFormat
     *            A string representing the new ontology format
     * @throws UnknownOntologyFormatException
     */
    public SetOntologyFormatData(final String newFormatString)
            throws UnknownOntologyFormatException {
        super();
        OWLOntologyFormat newOntologyFormat;
        if (newFormatString.equals("RDF/XML"))
            newOntologyFormat = new RDFXMLOntologyFormat();
        else if (newFormatString.equals("OWL/XML"))
            newOntologyFormat = new OWLXMLOntologyFormat();
        else if (newFormatString.equals("Turtle"))
            newOntologyFormat = new TurtleOntologyFormat();
        else if (newFormatString.equals("OWL Functional Syntax"))
            newOntologyFormat = new OWLFunctionalSyntaxOntologyFormat();
        else if (newFormatString.equals("Manchester OWL Syntax"))
            newOntologyFormat = new ManchesterOWLSyntaxOntologyFormat();
        else
            throw new UnknownOntologyFormatException("Unknown format: " + newFormatString);
        this.newFormat = newOntologyFormat;
    }

    @Override
    public SetOntologyFormat createOntologyChange(OWLOntology ontology) {
        if (ontology == null) {
            throw new NullPointerException("ontology must not be null");
        }
        return new SetOntologyFormat(ontology, newFormat);
    }

    public OWLOntologyFormat getNewFormat() {
        return newFormat;
    }

    @Override
    public int hashCode() {
        return "SetOntologyFormatData".hashCode() + newFormat.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof SetOntologyFormatData)) {
            return false;
        }
        SetOntologyFormatData other = (SetOntologyFormatData) obj;
        return newFormat.equals(other.newFormat);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("SetOntologyFormatData");
        sb.append("(OntologyFormat(");
        sb.append(newFormat);
        sb.append("))");
        return sb.toString();
    }

    @Override
    public <R, E extends Exception> R accept(
            CustomOntologyChangeDataVisitor<R, E> visitor) throws E {
        return visitor.visit(this);
    }

}
 public class FullChangeSet extends ChangeSet {

    private final OWLOntology parent;

    public OWLOntology getParent() {
        return parent;
    }

    private final OWLOntology child;

    public OWLOntology getChild() {
        return child;
    }

    /**
     * Calculate changeset between the two ontologies.
     *
     * @param parent
     *            The original ontology
     * @param child
     *            The updated ontology
     * @throws OWLOntologyCreationException
     */
    public FullChangeSet(final OWLOntology parent, final OWLOntology child) throws OWLOntologyCreationException {
        this(parent, child, true);
    }

    /**
     * Calculate changeset between parent and child.
     *
     * @param parent
     *            The original ontology
     * @param child
     *            The updated ontology
     * @param cosiderPrefixModifications
     *            If true, changes of axioms that are caused just by prefix
     *            modifications are filtered out.
     *
     *            Note: this will change parent.
     * @throws OWLOntologyCreationException
     */
    public FullChangeSet(final OWLOntology parent, final OWLOntology child,
            final Boolean cosiderPrefixModifications) throws OWLOntologyCreationException {
        this.parent = parent;
        this.child = child;
        // Ontology format
        final OWLOntologyFormat parentFormat = parent.getOWLOntologyManager().getOntologyFormat(
                parent);
        final OWLOntologyFormat childFormat = child.getOWLOntologyManager()
                .getOntologyFormat(child);
        if (!parentFormat.equals(childFormat))
            setFormatChange(new SetOntologyFormatData(childFormat));
        // Prefixes
        setPrefixChanges(findPrefixChanges(parent, child));
        // Ontology ID
        if (!(parent.getOntologyID().isAnonymous() && child.getOntologyID().isAnonymous()))
            if (!parent.getOntologyID().equals(child.getOntologyID()))
                setOntologyIdChange(new SetOntologyIDData(child.getOntologyID()));
        // Imports
        setImportChanges(findImportChanges(parent, child));

        if (cosiderPrefixModifications) {
            // apply prefix modifications to a copy
            HashSet<OWLOntology> ontologies = new HashSet<OWLOntology>();
            OWLOntology modifiedParent = OntologyUtils.copyOntology(parent);
            ontologies.add(modifiedParent);
            OWLOntologyManager manager = modifiedParent.getOWLOntologyManager();
            OWLEntityRenamer renamer = new OWLEntityRenamer(manager,
                    ontologies);
            for (PrefixChangeData c : getPrefixChanges()) {
                if (c instanceof ModifyPrefixData) {
                    ModifyPrefixData mc = (ModifyPrefixData) c;
                    String oldPrefix = mc.getOldPrefix();
                    String newPrefix = mc.getPrefix();
                    for (OWLEntity e : modifiedParent.getSignature()) {
                        if (e.getIRI().getStart().equals(oldPrefix)) {
                            IRI newIRI = IRI.create(newPrefix, e.getIRI().getFragment());
                            // System.out.println("Renaming " + e.getIRI() + " to " + newIRI);
                            manager.applyChanges(renamer.changeIRI(e, newIRI));
                        }
                    }
                }
            }
            // Annotations
            setAnnotationChanges(findAnnotationChanges(modifiedParent, child));
            // Axioms
            setAxiomChanges(findAxiomChanges(modifiedParent, child));
        } else {
            // Annotations
            setAnnotationChanges(findAnnotationChanges(parent, child));
            // Axioms
            setAxiomChanges(findAxiomChanges(parent, child));
        }
    }

    public static Collection<ImportChangeData> findImportChanges(final OWLOntology parent,
            final OWLOntology child) {
        final Collection<ImportChangeData> importChanges = new ArrayList<ImportChangeData>();
        final Collection<OWLImportsDeclaration> parentImports = parent.getImportsDeclarations();
        final Collection<OWLImportsDeclaration> childImports = child.getImportsDeclarations();
        for (final OWLImportsDeclaration decl : parentImports)
            if (!childImports.contains(decl))
                importChanges.add(new RemoveImportData(decl));
        for (final OWLImportsDeclaration decl : childImports)
            if (!parentImports.contains(decl))
                importChanges.add(new AddImportData(decl));
        return importChanges;
    }

    public static Collection<OWLOntologyChangeData> findAnnotationChanges(final OWLOntology parent,
            final OWLOntology child) {
        final Collection<OWLOntologyChangeData> annotationChanges = new ArrayList<OWLOntologyChangeData>();
        final Collection<OWLAnnotation> parentAnnotations = parent.getAnnotations();
        final Collection<OWLAnnotation> childAnnotations = child.getAnnotations();
        for (final OWLAnnotation annotation : parentAnnotations)
            if (!childAnnotations.contains(annotation))
                annotationChanges.add(new RemoveOntologyAnnotationData(annotation));
        for (final OWLAnnotation annotation : childAnnotations)
            if (!parentAnnotations.contains(annotation))
                annotationChanges.add(new AddOntologyAnnotationData(annotation));
        return annotationChanges;
    }

    public static Collection<AxiomChangeData> findAxiomChanges(final OWLOntology parent,
            final OWLOntology child) {
        final Collection<AxiomChangeData> axiomChanges = new ArrayList<AxiomChangeData>();
        OWLOntologyFormat parentFormat = parent.getOWLOntologyManager().getOntologyFormat(parent);
        if (parentFormat instanceof ManchesterOWLSyntaxOntologyFormat) {
            // collect annotation properties
            final Set<OWLAnnotationProperty> parentAnnotationProperties = new HashSet<OWLAnnotationProperty>();
            for (final OWLAnnotation a : parent.getAnnotations())
                parentAnnotationProperties.add(a.getProperty());
            for (final OWLAxiom axiom : parent.getAxioms()) {
                if (!child.containsAxiom(axiom)) {
                    // ignore declarations of used entities
                    if (axiom.isOfType(AxiomType.DECLARATION)) {
                        final OWLEntity e = ((OWLDeclarationAxiom) axiom).getEntity();
                        if (parentAnnotationProperties.contains(e)
                                || (parent.getReferencingAxioms(e).size() > 1))
                            // skip this declaration axiom
                            continue;
                    }
                    axiomChanges.add(new RemoveAxiomData(axiom));
                }
            }
        } else
            for (final OWLAxiom axiom : parent.getAxioms())
                if (!child.containsAxiom(axiom))
                    axiomChanges.add(new RemoveAxiomData(axiom));
        OWLOntologyFormat childFormat = child.getOWLOntologyManager().getOntologyFormat(child);
        if (childFormat instanceof ManchesterOWLSyntaxOntologyFormat) {
            // collect annotation properties
            final Set<OWLAnnotationProperty> childAnnotationProperties = new HashSet<OWLAnnotationProperty>();
            for (final OWLAnnotation a : parent.getAnnotations())
                childAnnotationProperties.add(a.getProperty());
            for (final OWLAxiom axiom : child.getAxioms()) {
                if (!parent.containsAxiom(axiom)) {
                    if (axiom.isOfType(AxiomType.DECLARATION)) {
                        // ignore declarations of used entities
                        final OWLEntity e = ((OWLDeclarationAxiom) axiom).getEntity();
                        if (childAnnotationProperties.contains(e)
                                || (child.getReferencingAxioms(e).size() > 1))
                            // skip this declaration axiom
                            continue;
                    }
                    axiomChanges.add(new AddAxiomData(axiom));
                }
            }
        } else
            for (final OWLAxiom axiom : child.getAxioms())
                if (!parent.containsAxiom(axiom))
                    axiomChanges.add(new AddAxiomData(axiom));
        return axiomChanges;
    }

    public static Collection<PrefixChangeData> findPrefixChanges(final OWLOntology parent,
            final OWLOntology child) {
        final OWLOntologyFormat parentFormat = parent.getOWLOntologyManager().getOntologyFormat(
                parent);
        final OWLOntologyFormat childFormat = child.getOWLOntologyManager()
                .getOntologyFormat(child);
        final Collection<PrefixChangeData> prefixChanges = new ArrayList<PrefixChangeData>();
        if (parentFormat instanceof PrefixOWLOntologyFormat) {
            final PrefixOWLOntologyFormat parentPrefixFormat = (PrefixOWLOntologyFormat) parentFormat;
            if (childFormat instanceof PrefixOWLOntologyFormat) {
                final PrefixOWLOntologyFormat childPrefixFormat = (PrefixOWLOntologyFormat) childFormat;
                for (final String parentPrefixName : parentPrefixFormat.getPrefixNames()) {
                    final String parentPrefixValue = parentPrefixFormat.getPrefix(parentPrefixName);
                    final String childPrefixValue = childPrefixFormat.getPrefix(parentPrefixName);
                    if (parentPrefixValue.equals(childPrefixValue))
                        continue;
                    String childPrefixName = null;
                    for (final Entry<String, String> e : childPrefixFormat
                            .getPrefixName2PrefixMap().entrySet())
                        if (e.getValue().equals(parentPrefixValue))
                            childPrefixName = e.getKey();
                    if ((childPrefixValue != null) && (!childPrefixValue.equals(parentPrefixValue)))
                        prefixChanges.add(new ModifyPrefixData(parentPrefixName, parentPrefixValue,
                                childPrefixValue));
                    if ((childPrefixName != null) && (!childPrefixName.equals(parentPrefixName)))
                        prefixChanges.add(new RenamePrefixData(parentPrefixName, parentPrefixValue,
                                childPrefixName));
                    if ((childPrefixValue == null) && (childPrefixName == null))
                        prefixChanges
                                .add(new RemovePrefixData(parentPrefixName, parentPrefixValue));
                }
                for (final String childPrefixName : childPrefixFormat.getPrefixNames()) {
                    final String parentPrefixValue = parentPrefixFormat.getPrefix(childPrefixName);
                    final String childPrefixValue = childPrefixFormat.getPrefix(childPrefixName);
                    if (childPrefixValue.equals(parentPrefixValue))
                        continue;
                    String parentPrefixName = null;
                    for (final Entry<String, String> e : parentPrefixFormat
                            .getPrefixName2PrefixMap().entrySet())
                        if (e.getValue().equals(parentPrefixValue))
                            parentPrefixName = e.getKey();
                    if ((parentPrefixValue == null) && (parentPrefixName == null))
                        prefixChanges.add(new AddPrefixData(childPrefixName, childPrefixValue));
                }
            } else
                // Child has not-prefix format, remove all prefixes
                for (final String prefixName : parentPrefixFormat.getPrefixNames()) {
                    final String prefix = parentPrefixFormat.getPrefix(prefixName);
                    prefixChanges.add(new RemovePrefixData(prefixName, prefix));
                }
        } else if (childFormat instanceof PrefixOWLOntologyFormat) {
            // Parent has not-prefix format, add all child prefixes
            final PrefixOWLOntologyFormat childPrefixFormat = (PrefixOWLOntologyFormat) childFormat;
            for (final String prefixName : childPrefixFormat.getPrefixNames()) {
                final String prefix = childPrefixFormat.getPrefix(prefixName);
                prefixChanges.add(new AddPrefixData(prefixName, prefix));
            }
        }
        return prefixChanges;
    }

    /**
     * @return Prefix name to prefix map
     */
    public Map<String, String> getAllPrefixes() {
        final Map<String, String> prefixName2PrefixMap = new HashMap<String, String>();
        final Map<String, String> prefixName2PrefixMap1 = parent.getOWLOntologyManager()
                .getOntologyFormat(parent).asPrefixOWLOntologyFormat().getPrefixName2PrefixMap();
        final Map<String, String> prefixName2PrefixMap2 = child.getOWLOntologyManager()
                .getOntologyFormat(child).asPrefixOWLOntologyFormat().getPrefixName2PrefixMap();
        prefixName2PrefixMap.putAll(prefixName2PrefixMap1);
        prefixName2PrefixMap.putAll(prefixName2PrefixMap2);
        return prefixName2PrefixMap;
    }
}
 public enum ChangeFormat {
    COMPACT,
    INDENTED
}; public interface ChangeRenderer {

    String render(OWLOntologyChangeData change);
    Character getSymbol(OWLOntologyChangeData change);
}
 public class FunctionalChangeRenderer implements ChangeRenderer {

    private OWLObjectRenderer objectRenderer;
    private ChangeFormat changeFormat;

    public FunctionalChangeRenderer(final ShortFormProvider provider,
            final ChangeFormat changeFormat) {
        objectRenderer = new SimplerRenderer();
        objectRenderer.setShortFormProvider(provider);
        this.changeFormat = changeFormat;
    }

    /**
     * Render ontology changes.
     */
    @Override
    public final String render(final OWLOntologyChangeData change) {
        if (change == null)
            return null;
        // Custom changes
        if (change instanceof SetOntologyFormatData)
            return render((SetOntologyFormatData) change);
        else if (change instanceof AddPrefixData)
            return render((AddPrefixData) change);
        else if (change instanceof RemovePrefixData)
            return render((RemovePrefixData) change);
        else if (change instanceof ModifyPrefixData)
            return render((ModifyPrefixData) change);
        else if (change instanceof RenamePrefixData)
            return render((RenamePrefixData) change);
        // Standard changes
        else if (change instanceof SetOntologyIDData)
            return render((SetOntologyIDData) change);
        else if (change instanceof AddImportData)
            return render((AddImportData) change);
        else if (change instanceof RemoveImportData)
            return render((RemoveImportData) change);
        else if (change instanceof AddOntologyAnnotationData)
            return render((AddOntologyAnnotationData) change);
        else if (change instanceof RemoveOntologyAnnotationData)
            return render((RemoveOntologyAnnotationData) change);
        else if (change instanceof AddAxiomData)
            return render((AddAxiomData) change);
        else if (change instanceof RemoveAxiomData)
            return render((RemoveAxiomData) change);
        else
            return "UnknownChangeType: " + change.getClass().getName();
    }

    public final String render(final SetOntologyFormatData change) {
        return indentString("*", "OntologyFormat(\""
                + change.getNewFormat().toString() + "\")");
    }

    public final String render(final AddPrefixData change) {
        return indentString("+", "Prefix(" + change.getPrefixName() + "=<"
                + change.getPrefix() + ">)");
    }

    public final String render(final RemovePrefixData change) {
        return indentString("-", "Prefix(" + change.getPrefixName() + "=<"
                + change.getPrefix() + ">)");
    }

    public final String render(final ModifyPrefixData change) {
        return indentString("*", "Prefix(" + change.getPrefixName() + "=<"
                + change.getOldPrefix() + "> <" + change.getPrefix() + ">)");
    }

    public final String render(final RenamePrefixData change) {
        return indentString("#", "Prefix(" + change.getOldPrefixName() + " "
                + change.getPrefixName() + "=<"
                + change.getPrefix() + ">)");
    }

    private String renderOntologyId(final OWLOntologyID id) {
        if (id.isAnonymous())
            return "";
        else {
            final StringBuilder sb = new StringBuilder();
            sb.append(id.getOntologyIRI().toQuotedString());
            if (id.getVersionIRI() != null) {
                sb.append(" ");
                sb.append(id.getVersionIRI().toQuotedString());
            }
            return sb.toString();
        }
    }

    public final String render(final SetOntologyIDData change) {
        final OWLOntologyID newid = change.getNewId();
        return indentString("*", "OntologyID(" + renderOntologyId(newid) + ")");
    }

    public final String render(final AddImportData change) {
        return indentString("+", "Import(<"
                + change.getDeclaration().getIRI().toString() + ">)");
    }

    public final String render(final RemoveImportData change) {
        return indentString("-", "Import(<"
                + change.getDeclaration().getIRI().toString() + ">)");
    }

    public final String render(final AddOntologyAnnotationData change) {
        return indentString("+", objectRenderer.render(change.getAnnotation()));
    }

    public final String render(final RemoveOntologyAnnotationData change) {
        return indentString("-", objectRenderer.render(change.getAnnotation()));
    }

    public final String render(final AddAxiomData change) {
        return indentString("+", objectRenderer.render(change.getAxiom()));
    }

    public final String render(final RemoveAxiomData change) {
        return indentString("-", objectRenderer.render(change.getAxiom()));
    }

    private String writeIndent(final StringBuilder sb, final int indent) {
        sb.append("\n");
        for (int i = 0; i < indent; i++)
            sb.append(" ");
        return sb.toString();
    }

    protected final String indentString(final String prefix,
            final String compactString) {
        String indentedString = compactString;
        if (changeFormat == ChangeFormat.INDENTED) {
            final int indentSize = 2;
            final StringBuilder sb = new StringBuilder();
            int indent;
            if (prefix != null)
                indent = prefix.length() + 1;
            else
                indent = 0;
            boolean quote = false;
            for (final char c : compactString.toCharArray())
                switch (c) {
                case '(':
                    indent += indentSize;
                    // sb.append(':');
                    writeIndent(sb, indent);
                    break;
                case ')':
                    indent -= indentSize;
                    writeIndent(sb, indent);
                    break;
                case ' ':
                    if (quote)
                        sb.append(c);
                    else
                        writeIndent(sb, indent);
                    break;
                case '"':
                    quote = !quote;
                default:
                    sb.append(c);
                }
            indentedString = sb.toString().trim();
        }
        if (prefix == null)
            return indentedString;
        else
            return prefix + " " + indentedString;
    }

    @Override
    public Character getSymbol(OWLOntologyChangeData change) {
        if (change == null)
            return '?';
        // Standard changes
        if (change instanceof SetOntologyFormatData)
            return '*';
        else if (change instanceof AddPrefixData)
            return '+';
        else if (change instanceof RemovePrefixData)
            return '-';
        else if (change instanceof ModifyPrefixData)
            return '*';
        else if (change instanceof RenamePrefixData)
            return '#';
        // Custom changes
        else if (change instanceof SetOntologyIDData)
            return '*';
        else if (change instanceof AddImportData)
            return '+';
        else if (change instanceof RemoveImportData)
            return '-';
        else if (change instanceof AddOntologyAnnotationData)
            return '+';
        else if (change instanceof RemoveOntologyAnnotationData)
            return '-';
        else if (change instanceof AddAxiomData)
            return '+';
        else if (change instanceof RemoveAxiomData)
            return '-';
        else
            return '?';
    }
}
 public enum IriFormat {
    SIMPLE,
    QNAME,
    FULL,
    LABEL
}; public class CommonUtils {

    protected CommonUtils() {
    }

    public static String stackTrackAsString(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }

    public static void messageBox(String message, String title) {
        JOptionPane.showMessageDialog(null, message, title,
                JOptionPane.INFORMATION_MESSAGE);
    }

    public static void messageBox(String message) {
        messageBox(message, "Information");
    }

    public static void errorBox(String message) {
        JOptionPane.showMessageDialog(null, message, "Error",
                JOptionPane.ERROR_MESSAGE);
    }

}
 public class OntologyUtils {
    protected OntologyUtils() {
    };

    public static OWLOntology loadOntology(final FileDocumentSource source)
            throws OWLOntologyCreationException {

        final OWLOntologyLoaderConfiguration config = new OWLOntologyLoaderConfiguration();
        config.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
        final OWLOntologyManager manager = OntologyManagerThatDoesNotLoadImports.create();
        final OWLOntology ontology = manager.loadOntologyFromOntologyDocument(source, config);
        return ontology;
    }

    public static OWLOntology loadOntology(final String name, Class<?> klass)
            throws OWLOntologyCreationException {
        final FileDocumentSource source = new FileDocumentSource(new File(
                klass.getResource(name).getFile()));
        return OntologyUtils.loadOntology(source);
    }

    public static OWLOntology copyOntology(OWLOntology original)
            throws OWLOntologyCreationException {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology(original.getOntologyID().getOntologyIRI());
        manager.addAxioms(ontology, original.getAxioms());
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(
                original.getAnnotations().size() + original.getImportsDeclarations().size() + 2);
        // annotations
        for (OWLAnnotation a : original.getAnnotations()) {
            changes.add(new AddOntologyAnnotation(ontology, a));
        }
        // imports
        for (OWLImportsDeclaration decl : original.getImportsDeclarations()) {
            changes.add(new AddImport(ontology, decl));
        }
        manager.applyChanges(changes);
        OWLOntologyManager originalManager = original.getOWLOntologyManager();
        manager.setOntologyFormat(ontology, originalManager.getOntologyFormat(original));
        return ontology;
    }
}

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.semanticweb.owlapi.change.AxiomChangeData;
import org.semanticweb.owlapi.change.ImportChangeData;
import org.semanticweb.owlapi.change.OWLOntologyChangeData;
import org.semanticweb.owlapi.change.SetOntologyIDData;
import org.semanticweb.owlapi.io.FileDocumentSource;
import org.semanticweb.owlapi.io.OWLOntologyInputSourceException;
import org.semanticweb.owlapi.io.UnparsableOntologyException;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyFormat;
import org.semanticweb.owlapi.model.OWLOntologyID;
import org.semanticweb.owlapi.util.ShortFormProvider;
import owl2vcs.analysis.ChangeClassifier;
import owl2vcs.analysis.EntityClassifier;
import owl2vcs.analysis.EntityCollector;
import owl2vcs.analysis.EntityCollectorException;
import owl2vcs.changes.PrefixChangeData;
import owl2vcs.changes.SetOntologyFormatData;
import owl2vcs.changeset.FullChangeSet;
import owl2vcs.render.ChangeFormat;
import owl2vcs.render.ChangeRenderer;
import owl2vcs.render.FunctionalChangeRenderer;
import owl2vcs.render.IriFormat;
import owl2vcs.utils.CommonUtils;
import owl2vcs.utils.OntologyUtils;
import sun.swing.DefaultLookup;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.ScrollPaneConstants;

        } catch (final OWLOntologyCreationException e) {
            CommonUtils.errorBox(CommonUtils.stackTrackAsString(e));
        } catch (EntityCollectorException e) {
            // impossible
            CommonUtils.errorBox(CommonUtils.stackTrackAsString(e));
        }
    }
}

class TextAreaRenderer extends JTextArea implements TableCellRenderer {

    private static final long serialVersionUID = 7356544505909915181L;
    private Color unselectedForeground;
    private Color unselectedBackground;
    private int cellPadding;

    private static final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1,
            1);
    private static final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1,
            1, 1);
    protected static Border noFocusBorder = DEFAULT_NO_FOCUS_BORDER;

    public TextAreaRenderer(int cellPadding) {
        setLineWrap(true);
        setWrapStyleWord(true);
        this.cellPadding = cellPadding;
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        if (table == null) {
            return this;
        }

        Color fg = null;
        Color bg = null;

        if (isSelected) {
            super.setForeground(fg == null ? table.getSelectionForeground()
                    : fg);
            super.setBackground(bg == null ? table.getSelectionBackground()
                    : bg);
        } else {
            Color background = unselectedBackground != null ? unselectedBackground
                    : table.getBackground();
            if (background == null
                    || background instanceof javax.swing.plaf.UIResource) {
                Color alternateColor = DefaultLookup.getColor(this, ui,
                        "Table.alternateRowColor");
                if (alternateColor != null && row % 2 != 0) {
                    background = alternateColor;
                }
            }
            super.setForeground(unselectedForeground != null ? unselectedForeground
                    : table.getForeground());
            super.setBackground(background);
        }

        setFont(table.getFont());
        setLineWrap(false);

        setValue(value);

        int height_wanted = (int) getPreferredSize().getHeight();
        if (height_wanted != table.getRowHeight(row))
            table.setRowHeight(row, height_wanted);
        return this;
    }

    protected void setValue(Object value) {
        setText((value == null) ? "" : value.toString());
    }

    public void setForeground(Color c) {
        super.setForeground(c);
        unselectedForeground = c;
    }

    public void setBackground(Color c) {
        super.setBackground(c);
        unselectedBackground = c;
    }

}

public class VisualDiff {

    protected VisualDiffData data;
    protected ChangeRenderer renderer;

    private JFrame frmVisualdiff;
    private JPanel panel_Prefixes;
    private JPanel panel_PrefixChanges;
    private JPanel panel_ImportChanges;
    private JPanel panel_AxiomChanges;
    private JPanel panel_AnnotationChanges;
    private JTable table_PrefixChanges;
    private JTable table_ImportChanges;
    private JTable table_AnnotationChanges;
    private JTable table_AxiomChanges;
    private JTable table_Prefixes;
    private JTextField textField_Format;
    private JTextField textField_OntologyIRI;
    private JTextField textField_VersionIRI;
    private JTabbedPane tabbedPane;

    private String IriToString(IRI iri) {
        if (iri != null)
            return iri.toString();
        else
            return "";
    }

    private void update() {
        Color addedColor = Color.GREEN;
        Color removedColor = Color.RED;
        Color neutralColor = new Color(238, 238, 238);

        // Ontology format

        SetOntologyFormatData formatChange = data.changeset.getFormatChange();
