From: <lor...@us...> - 2010-03-07 10:26:06
|
Revision: 2094 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2094&view=rev Author: lorenz_b Date: 2010-03-07 10:25:59 +0000 (Sun, 07 Mar 2010) Log Message: ----------- Continued axiom editor component. Modified Paths: -------------- trunk/src/dl-learner/org/dllearner/tools/ore/OREManager.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/ExplanationTable.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/ExpressionEditor.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/OWLClassAxiomEditor.java Added Paths: ----------- trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/InputVerificationStatusChangedListener.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifiedInputEditor.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifyingOptionPane.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/DescriptionComparator.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxOWLObjectRendererImpl.java trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxObjectRenderer.java Modified: trunk/src/dl-learner/org/dllearner/tools/ore/OREManager.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/OREManager.java 2010-03-06 18:44:55 UTC (rev 2093) +++ trunk/src/dl-learner/org/dllearner/tools/ore/OREManager.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -37,6 +37,7 @@ import org.dllearner.tools.ore.cache.OWLObjectRenderingCache; import org.dllearner.tools.ore.ui.DescriptionLabel; import org.dllearner.tools.ore.ui.editor.OWLEntityFinder; +import org.dllearner.tools.ore.ui.rendering.ManchesterOWLSyntaxOWLObjectRendererImpl; import org.dllearner.tools.ore.ui.rendering.OWLEntityRenderer; import org.dllearner.utilities.owl.OWLAPIConverter; import org.mindswap.pellet.exceptions.InconsistentOntologyException; @@ -55,7 +56,6 @@ import org.semanticweb.owl.model.UnknownOWLOntologyException; import org.semanticweb.owl.util.SimpleShortFormProvider; -import uk.ac.manchester.cs.owl.mansyntaxrenderer.ManchesterOWLSyntaxOWLObjectRendererImpl; public class OREManager { Modified: trunk/src/dl-learner/org/dllearner/tools/ore/ui/ExplanationTable.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/ExplanationTable.java 2010-03-06 18:44:55 UTC (rev 2093) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/ExplanationTable.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -2,8 +2,11 @@ import java.awt.Color; import java.awt.Component; +import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.ComponentAdapter; +import java.awt.event.ComponentEvent; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.MouseAdapter; @@ -14,9 +17,12 @@ import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JButton; +import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JLabel; +import javax.swing.JOptionPane; import javax.swing.JTable; +import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @@ -30,13 +36,19 @@ import org.dllearner.tools.ore.RepairManager; import org.dllearner.tools.ore.RepairManagerListener; import org.dllearner.tools.ore.explanation.Explanation; +import org.dllearner.tools.ore.ui.editor.InputVerificationStatusChangedListener; import org.dllearner.tools.ore.ui.editor.OWLClassAxiomEditor; +import org.dllearner.tools.ore.ui.editor.VerifiedInputEditor; +import org.dllearner.tools.ore.ui.editor.VerifyingOptionPane; import org.dllearner.tools.ore.ui.rendering.TextAreaRenderer; import org.jdesktop.swingx.JXTable; import org.jdesktop.swingx.decorator.ColorHighlighter; import org.jdesktop.swingx.decorator.HighlightPredicate; import org.protege.editor.core.Disposable; +import org.semanticweb.owl.model.OWLAxiom; import org.semanticweb.owl.model.OWLClass; +import org.semanticweb.owl.model.OWLClassAxiom; +import org.semanticweb.owl.model.OWLObject; import org.semanticweb.owl.model.OWLOntologyChange; import uk.ac.manchester.cs.owl.dlsyntax.DLSyntaxObjectRenderer; @@ -256,10 +268,12 @@ @Override public void actionPerformed(ActionEvent e) { fireEditingStopped(); - JDialog dialog = new JDialog(); - dialog.add(new OWLClassAxiomEditor(OREManager.getInstance()).getEditorComponent()); - dialog.pack(); - dialog.setVisible(true); + OWLClassAxiomEditor editor = new OWLClassAxiomEditor(OREManager.getInstance()); + OWLAxiom ax = ((ExplanationTableModel)getModel()).getOWLAxiomAtRow(2); + if(ax instanceof OWLClassAxiom){ + editor.setEditedObject((OWLClassAxiom) ax); + } + showEditorDialog(editor, ax); } } @@ -296,9 +310,65 @@ repMan.removeListener(this); } + private void showEditorDialog(final OWLClassAxiomEditor editor, OWLObject value) { + if (editor == null) { + return; + } + // Create the editing component dialog - we use an option pane + // so that the buttons and keyboard actions are what are expected + // by the user. + final JComponent editorComponent = editor.getEditorComponent(); + final VerifyingOptionPane optionPane = new VerifyingOptionPane( + editorComponent) { - + public void selectInitialValue() { + // This is overriden so that the option pane dialog default + // button + // doesn't get the focus. + } + }; + final InputVerificationStatusChangedListener verificationListener = new InputVerificationStatusChangedListener() { + public void verifiedStatusChanged(boolean verified) { + optionPane.setOKEnabled(verified); + } + }; + // if the editor is verifying, will need to prevent the OK button from + // being available + if (editor instanceof VerifiedInputEditor) { + ((VerifiedInputEditor) editor) + .addStatusChangedListener(verificationListener); + } + final Component parent = SwingUtilities.getAncestorOfClass(Frame.class, getParent()); + final JDialog dlg = optionPane.createDialog(parent, null); + // The editor shouldn't be modal (or should it?) + dlg.setModal(false); + dlg.setResizable(true); + dlg.pack(); + dlg.setLocationRelativeTo(parent); + dlg.addComponentListener(new ComponentAdapter() { + public void componentHidden(ComponentEvent e) { + Object retVal = optionPane.getValue(); + editorComponent.setPreferredSize(editorComponent.getSize()); + if (retVal != null && retVal.equals(JOptionPane.OK_OPTION)) { + handleEditFinished(editor); + } +// setSelectedValue(frameObject, true); + if (editor instanceof VerifiedInputEditor) { + ((VerifiedInputEditor) editor) + .removeStatusChangedListener(verificationListener); + } +// editor.dispose(); + } + }); + + dlg.setTitle(OREManager.getInstance().getRendering(value)); + dlg.setVisible(true); + } + void handleEditFinished(OWLClassAxiomEditor editor){ + System.out.println(editor.getEditedObject()); + } + } Modified: trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/ExpressionEditor.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/ExpressionEditor.java 2010-03-06 18:44:55 UTC (rev 2093) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/ExpressionEditor.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -9,7 +9,9 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Collection; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.StringTokenizer; import javax.swing.BorderFactory; @@ -44,7 +46,7 @@ * that the text is well formed and provides feedback if the * text is not well formed. */ -public class ExpressionEditor<O> extends JTextPane{ +public class ExpressionEditor<O> extends JTextPane implements VerifiedInputEditor{ private static Logger logger = Logger.getLogger(ExpressionEditor.class); @@ -223,12 +225,13 @@ timer.restart(); clearError(); performHighlighting(); + notifyValidationChanged(false); } private void setError(OWLExpressionParserException e) { logger.debug("Set error " + e); - + notifyValidationChanged(e == null); if (e != null) { ToolTipManager.sharedInstance().setInitialDelay(ERROR_TOOL_TIP_INITIAL_DELAY); ToolTipManager.sharedInstance().setDismissDelay(ERROR_TOOL_TIP_DISMISS_DELAY); @@ -335,6 +338,30 @@ } StyleConstants.setForeground(getStyledDocument().addStyle(Color.BLACK.toString(), null), Color.BLACK); } + +///////////////////////// content verification + private Set<InputVerificationStatusChangedListener> listeners = new HashSet<InputVerificationStatusChangedListener>(); + + private boolean previousValue = true; + + public void addStatusChangedListener(InputVerificationStatusChangedListener listener) { + listeners.add(listener); + } + + public void removeStatusChangedListener(InputVerificationStatusChangedListener listener) { + listeners.remove(listener); + } + + private void notifyValidationChanged(boolean valid){ + if (valid != previousValue){ // only report changes + previousValue = valid; + for (InputVerificationStatusChangedListener l : listeners){ + l.verifiedStatusChanged(valid); + } + } + } + + } Added: trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/InputVerificationStatusChangedListener.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/InputVerificationStatusChangedListener.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/InputVerificationStatusChangedListener.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -0,0 +1,5 @@ +package org.dllearner.tools.ore.ui.editor; + +public interface InputVerificationStatusChangedListener { + void verifiedStatusChanged(boolean newState); +} Modified: trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/OWLClassAxiomEditor.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/OWLClassAxiomEditor.java 2010-03-06 18:44:55 UTC (rev 2093) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/OWLClassAxiomEditor.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -10,7 +10,7 @@ import org.semanticweb.owl.model.OWLClassAxiom; import org.semanticweb.owl.model.OWLException; -public class OWLClassAxiomEditor { +public class OWLClassAxiomEditor implements VerifiedInputEditor{ private ExpressionEditor<OWLClassAxiom> editor; @@ -92,13 +92,13 @@ setEditedObject(null); } -// -// public void addStatusChangedListener(InputVerificationStatusChangedListener listener) { -// editor.addStatusChangedListener(listener); -// } -// -// -// public void removeStatusChangedListener(InputVerificationStatusChangedListener listener) { -// editor.removeStatusChangedListener(listener); -// } + + public void addStatusChangedListener(InputVerificationStatusChangedListener listener) { + editor.addStatusChangedListener(listener); + } + + + public void removeStatusChangedListener(InputVerificationStatusChangedListener listener) { + editor.removeStatusChangedListener(listener); + } } Added: trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifiedInputEditor.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifiedInputEditor.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifiedInputEditor.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -0,0 +1,10 @@ +package org.dllearner.tools.ore.ui.editor; + +public interface VerifiedInputEditor { + + void addStatusChangedListener( + InputVerificationStatusChangedListener listener); + + void removeStatusChangedListener( + InputVerificationStatusChangedListener listener); +} Added: trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifyingOptionPane.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifyingOptionPane.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/editor/VerifyingOptionPane.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -0,0 +1,62 @@ +package org.dllearner.tools.ore.ui.editor; + +import java.awt.Component; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JOptionPane; +import javax.swing.UIManager; + +import org.apache.log4j.Logger; + +public class VerifyingOptionPane extends JOptionPane { + + /** + * + */ + private static final long serialVersionUID = -6308201481924625979L; + + private static final Logger logger = Logger.getLogger(VerifyingOptionPane.class); + + private JButton okButton; + + + public VerifyingOptionPane(JComponent c) { + super(c, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); + } + + public VerifyingOptionPane(JComponent c, int messageType, int optionType) { + super(c, messageType, optionType); + } + + public void setOKEnabled(boolean enabled){ + if (okButton == null){ + okButton = getButtonComponent(this, JButton.class, (String)UIManager.get("OptionPane.okButtonText")); + } + if (okButton != null){ + okButton.setEnabled(enabled); + } + else{ + logger.warn("Cannot find OK button for this system. Please report this with details of your OS and language."); + } + } + + private <T extends JComponent> T getButtonComponent(JComponent parent, Class<T> type, String name) { + if (type.isAssignableFrom(parent.getClass())){ + if (parent instanceof JButton){ + if (name.equals(((JButton)parent).getText())){ + return (T)parent; + } + } + } + for (Component c : parent.getComponents()){ + if (c instanceof JComponent){ + T target = getButtonComponent((JComponent)c, type, name); + if (target != null){ + return target; + } + } + } + return null; + } +} Added: trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/DescriptionComparator.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/DescriptionComparator.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/DescriptionComparator.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -0,0 +1,109 @@ +package org.dllearner.tools.ore.ui.rendering; + +import org.semanticweb.owl.model.*; + +import java.util.Comparator; + +/** + * Author: Simon Jupp<br> + * Date: Jan 11, 2007<br> + * The University of Manchester<br> + * Bio-Health Informatics Group<br> + */ +public class DescriptionComparator implements OWLDescriptionVisitor, Comparator<OWLObject> { + + private int last = 0; + + public int compare(OWLObject o, OWLObject o1) { + + int first = 0; + if(o instanceof OWLDescription) { + ((OWLDescription) o).accept(this); + } + first = last; + if (o1 instanceof OWLDescription) { + ((OWLDescription) o1).accept(this); + } + + + return signum (first - last); + } + + public int signum (int diff) { + if (diff > 0) return 1; + if (diff <0) return -1; + else return 0; + } + + public void visit(OWLClass desc) { + last = 1; + } + + public void visit(OWLObjectSomeRestriction desc) { + last = 2; + } + + public void visit(OWLObjectAllRestriction desc) { + last = 3; + } + + public void visit(OWLDataSomeRestriction desc) { + last = 4; + } + + public void visit(OWLDataAllRestriction desc) { + last = 5; + } + + public void visit(OWLObjectValueRestriction desc) { + last = 6; + } + + public void visit(OWLDataValueRestriction desc) { + last = 7; + } + + public void visit(OWLObjectMinCardinalityRestriction desc) { + last = 8; + } + + public void visit(OWLDataMinCardinalityRestriction desc) { + last = 9; + } + + public void visit(OWLObjectMaxCardinalityRestriction desc) { + last = 10; + } + + public void visit(OWLDataMaxCardinalityRestriction desc) { + last = 11; + } + + public void visit(OWLObjectExactCardinalityRestriction desc) { + last = 12; + } + + public void visit(OWLDataExactCardinalityRestriction desc) { + last = 13; + } + + public void visit(OWLObjectComplementOf desc) { + last = 14; + } + + public void visit(OWLObjectUnionOf desc) { + last = 15; + } + + public void visit(OWLObjectIntersectionOf desc) { + last = 16; + } + + public void visit(OWLObjectSelfRestriction desc) { + last = 17; + } + + public void visit(OWLObjectOneOf desc) { + last = 18; + } +} Added: trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxOWLObjectRendererImpl.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxOWLObjectRendererImpl.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxOWLObjectRendererImpl.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -0,0 +1,94 @@ +package org.dllearner.tools.ore.ui.rendering; + +import org.semanticweb.owl.io.OWLObjectRenderer; +import org.semanticweb.owl.model.OWLObject; +import org.semanticweb.owl.util.ShortFormProvider; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +/* + * Copyright (C) 2007, University of Manchester + * + * Modifications to the initial code base are copyright of their + * respective authors, or their employers as appropriate. Authorship + * of the modifications may be determined from the ChangeLog placed at + * the end of this file. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library 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 + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +/** + * Author: Matthew Horridge<br> + * The University Of Manchester<br> + * Bio-Health Informatics Group<br> + * Date: 25-Nov-2007<br><br> + * + * An implementation of the OWLObjectRenderer interface. (Renders + * standalone class descriptions and axioms in the manchester syntax). + */ +public class ManchesterOWLSyntaxOWLObjectRendererImpl implements OWLObjectRenderer { + + private ManchesterOWLSyntaxObjectRenderer ren; + + private WriterDelegate writerDelegate; + + public ManchesterOWLSyntaxOWLObjectRendererImpl() { + writerDelegate = new WriterDelegate(); + ren = new ManchesterOWLSyntaxObjectRenderer(writerDelegate); + } + + + public String render(OWLObject object) { + writerDelegate.reset(); + object.accept(ren); + return writerDelegate.toString(); + } + + + public void setShortFormProvider(ShortFormProvider shortFormProvider) { + ren.setShortFormProvider(shortFormProvider); + } + + private class WriterDelegate extends Writer { + + private StringWriter delegate; + + private void reset() { + delegate = new StringWriter(); + } + + + public String toString() { + return delegate.getBuffer().toString(); + } + + + public void close() throws IOException { + delegate.close(); + } + + + public void flush() throws IOException { + delegate.flush(); + } + + + public void write(char cbuf[], int off, int len) throws IOException { + delegate.write(cbuf, off, len); + } + } +} Added: trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxObjectRenderer.java =================================================================== --- trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxObjectRenderer.java (rev 0) +++ trunk/src/dl-learner/org/dllearner/tools/ore/ui/rendering/ManchesterOWLSyntaxObjectRenderer.java 2010-03-07 10:25:59 UTC (rev 2094) @@ -0,0 +1,859 @@ +package org.dllearner.tools.ore.ui.rendering; + +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.AND; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.ANTI_SYMMETRIC; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.DIFFERENT_FROM; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.DISJOINT_UNION_OF; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.DISJOINT_WITH; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.DOMAIN; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.EQUIVALENT_TO; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.EXACTLY; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.FACET_RESTRICTION_SEPARATOR; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.FUNCTIONAL; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.INVERSE; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.INVERSE_FUNCTIONAL; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.IRREFLEXIVE; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.MAX; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.MIN; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.NOT; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.ONE_OF_DELIMETER; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.ONLY; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.OR; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.RANGE; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.REFLEXIVE; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.SAME_AS; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.SELF; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.SOME; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.SUBCLASS_OF; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.SUB_PROPERTY_OF; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.SYMMETRIC; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.THAT; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.TRANSITIVE; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.TYPES; +import static org.coode.manchesterowlsyntax.ManchesterOWLSyntax.VALUE; + +import java.io.Writer; +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.coode.manchesterowlsyntax.ManchesterOWLSyntax; +import org.semanticweb.owl.model.OWLAnonymousDescription; +import org.semanticweb.owl.model.OWLAntiSymmetricObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLAxiomAnnotationAxiom; +import org.semanticweb.owl.model.OWLCardinalityRestriction; +import org.semanticweb.owl.model.OWLClass; +import org.semanticweb.owl.model.OWLClassAssertionAxiom; +import org.semanticweb.owl.model.OWLConstantAnnotation; +import org.semanticweb.owl.model.OWLDataAllRestriction; +import org.semanticweb.owl.model.OWLDataComplementOf; +import org.semanticweb.owl.model.OWLDataExactCardinalityRestriction; +import org.semanticweb.owl.model.OWLDataMaxCardinalityRestriction; +import org.semanticweb.owl.model.OWLDataMinCardinalityRestriction; +import org.semanticweb.owl.model.OWLDataOneOf; +import org.semanticweb.owl.model.OWLDataProperty; +import org.semanticweb.owl.model.OWLDataPropertyAssertionAxiom; +import org.semanticweb.owl.model.OWLDataPropertyDomainAxiom; +import org.semanticweb.owl.model.OWLDataPropertyRangeAxiom; +import org.semanticweb.owl.model.OWLDataRangeFacetRestriction; +import org.semanticweb.owl.model.OWLDataRangeRestriction; +import org.semanticweb.owl.model.OWLDataSomeRestriction; +import org.semanticweb.owl.model.OWLDataSubPropertyAxiom; +import org.semanticweb.owl.model.OWLDataType; +import org.semanticweb.owl.model.OWLDataValueRestriction; +import org.semanticweb.owl.model.OWLDeclarationAxiom; +import org.semanticweb.owl.model.OWLDescription; +import org.semanticweb.owl.model.OWLDifferentIndividualsAxiom; +import org.semanticweb.owl.model.OWLDisjointClassesAxiom; +import org.semanticweb.owl.model.OWLDisjointDataPropertiesAxiom; +import org.semanticweb.owl.model.OWLDisjointObjectPropertiesAxiom; +import org.semanticweb.owl.model.OWLDisjointUnionAxiom; +import org.semanticweb.owl.model.OWLEntityAnnotationAxiom; +import org.semanticweb.owl.model.OWLEquivalentClassesAxiom; +import org.semanticweb.owl.model.OWLEquivalentDataPropertiesAxiom; +import org.semanticweb.owl.model.OWLEquivalentObjectPropertiesAxiom; +import org.semanticweb.owl.model.OWLFunctionalDataPropertyAxiom; +import org.semanticweb.owl.model.OWLFunctionalObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLImportsDeclaration; +import org.semanticweb.owl.model.OWLIndividual; +import org.semanticweb.owl.model.OWLInverseFunctionalObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLInverseObjectPropertiesAxiom; +import org.semanticweb.owl.model.OWLIrreflexiveObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLNegativeDataPropertyAssertionAxiom; +import org.semanticweb.owl.model.OWLNegativeObjectPropertyAssertionAxiom; +import org.semanticweb.owl.model.OWLObject; +import org.semanticweb.owl.model.OWLObjectAllRestriction; +import org.semanticweb.owl.model.OWLObjectAnnotation; +import org.semanticweb.owl.model.OWLObjectComplementOf; +import org.semanticweb.owl.model.OWLObjectExactCardinalityRestriction; +import org.semanticweb.owl.model.OWLObjectIntersectionOf; +import org.semanticweb.owl.model.OWLObjectMaxCardinalityRestriction; +import org.semanticweb.owl.model.OWLObjectMinCardinalityRestriction; +import org.semanticweb.owl.model.OWLObjectOneOf; +import org.semanticweb.owl.model.OWLObjectProperty; +import org.semanticweb.owl.model.OWLObjectPropertyAssertionAxiom; +import org.semanticweb.owl.model.OWLObjectPropertyChainSubPropertyAxiom; +import org.semanticweb.owl.model.OWLObjectPropertyDomainAxiom; +import org.semanticweb.owl.model.OWLObjectPropertyExpression; +import org.semanticweb.owl.model.OWLObjectPropertyInverse; +import org.semanticweb.owl.model.OWLObjectPropertyRangeAxiom; +import org.semanticweb.owl.model.OWLObjectSelfRestriction; +import org.semanticweb.owl.model.OWLObjectSomeRestriction; +import org.semanticweb.owl.model.OWLObjectSubPropertyAxiom; +import org.semanticweb.owl.model.OWLObjectUnionOf; +import org.semanticweb.owl.model.OWLObjectValueRestriction; +import org.semanticweb.owl.model.OWLObjectVisitor; +import org.semanticweb.owl.model.OWLOntology; +import org.semanticweb.owl.model.OWLOntologyAnnotationAxiom; +import org.semanticweb.owl.model.OWLPropertyExpression; +import org.semanticweb.owl.model.OWLQuantifiedRestriction; +import org.semanticweb.owl.model.OWLReflexiveObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLRestriction; +import org.semanticweb.owl.model.OWLSameIndividualsAxiom; +import org.semanticweb.owl.model.OWLSubClassAxiom; +import org.semanticweb.owl.model.OWLSymmetricObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLTransitiveObjectPropertyAxiom; +import org.semanticweb.owl.model.OWLTypedConstant; +import org.semanticweb.owl.model.OWLUntypedConstant; +import org.semanticweb.owl.model.OWLValueRestriction; +import org.semanticweb.owl.model.SWRLAtom; +import org.semanticweb.owl.model.SWRLAtomConstantObject; +import org.semanticweb.owl.model.SWRLAtomDObject; +import org.semanticweb.owl.model.SWRLAtomDVariable; +import org.semanticweb.owl.model.SWRLAtomIVariable; +import org.semanticweb.owl.model.SWRLAtomIndividualObject; +import org.semanticweb.owl.model.SWRLBuiltInAtom; +import org.semanticweb.owl.model.SWRLClassAtom; +import org.semanticweb.owl.model.SWRLDataRangeAtom; +import org.semanticweb.owl.model.SWRLDataValuedPropertyAtom; +import org.semanticweb.owl.model.SWRLDifferentFromAtom; +import org.semanticweb.owl.model.SWRLObjectPropertyAtom; +import org.semanticweb.owl.model.SWRLRule; +import org.semanticweb.owl.model.SWRLSameAsAtom; +import org.semanticweb.owl.vocab.XSDVocabulary; + +import uk.ac.manchester.cs.owl.mansyntaxrenderer.AbstractRenderer; + + + +/** + * Author: Matthew Horridge<br> + * The University Of Manchester<br> + * Bio-Health Informatics Group<br> + * Date: 25-Apr-2007<br><br> + */ +public class ManchesterOWLSyntaxObjectRenderer extends AbstractRenderer implements OWLObjectVisitor { + + public static final int LINE_LENGTH = 70; + + private boolean wrap = true; + + private DescriptionComparator descriptionComparator; + + + public ManchesterOWLSyntaxObjectRenderer(Writer writer) { + super(writer); + descriptionComparator = new DescriptionComparator(); + } + + + public void setWrap(boolean wrap) { + this.wrap = wrap; + } + + protected List<? extends OWLObject> sort(Collection<? extends OWLObject> objects) { + List<? extends OWLObject> sortedDescriptions = new ArrayList<OWLObject>(objects); + Collections.sort(sortedDescriptions, descriptionComparator); + return sortedDescriptions; + } + + + protected void write(Set<? extends OWLObject> objects, ManchesterOWLSyntax delimeter, boolean newline) { + int tab = getIndent(); + pushTab(tab); + for (Iterator<? extends OWLObject> it = sort(objects).iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + if (newline && wrap) { + writeNewLine(); + } + write(delimeter); + } + } + popTab(); + } + + + protected void write(Set<? extends OWLDescription> objects, boolean newline) { + boolean lastWasNamed = false; + boolean first = true; + + for (Iterator<? extends OWLObject> it = sort(objects).iterator(); it.hasNext();) { + OWLObject desc = it.next(); + if (!first) { + if (newline) { + writeNewLine(); + } + if (lastWasNamed && desc instanceof OWLRestriction) { + write("", THAT, " "); + } + else { + write("", AND, " "); + } + } + + first = false; + desc.accept(this); + + lastWasNamed = desc instanceof OWLClass; + } + } + + + private void writeRestriction(OWLQuantifiedRestriction restriction, ManchesterOWLSyntax keyword) { + restriction.getProperty().accept(this); + write(keyword); + if (restriction.getFiller() instanceof OWLAnonymousDescription) { + write("("); + } + restriction.getFiller().accept(this); + if (restriction.getFiller() instanceof OWLAnonymousDescription) { + write(")"); + } + } + + + private void writeRestriction(OWLValueRestriction restriction) { + restriction.getProperty().accept(this); + write(VALUE); + restriction.getValue().accept(this); + } + + + private void writeRestriction(OWLCardinalityRestriction restriction, ManchesterOWLSyntax keyword) { + restriction.getProperty().accept(this); + write(keyword); + write(Integer.toString(restriction.getCardinality())); +// if(restriction.isQualified()) { + writeSpace(); + restriction.getFiller().accept(this); +// } + + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Class descriptions + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLClass desc) { + write(getShortFormProvider().getShortForm(desc)); + } + + + public void visit(OWLObjectIntersectionOf desc) { + write(desc.getOperands(), true); + } + + + public void visit(OWLObjectUnionOf desc) { + write(desc.getOperands(), OR, true); + } + + + public void visit(OWLObjectComplementOf desc) { + write("", NOT, desc.isAnonymous() ? " " : ""); + if (desc.isAnonymous()) { + write("("); + } + desc.getOperand().accept(this); + if (desc.isAnonymous()) { + write(")"); + } + } + + + public void visit(OWLObjectSomeRestriction desc) { + writeRestriction(desc, SOME); + } + + + public void visit(OWLObjectAllRestriction desc) { + writeRestriction(desc, ONLY); + } + + + public void visit(OWLObjectValueRestriction desc) { + writeRestriction(desc); + } + + + public void visit(OWLObjectMinCardinalityRestriction desc) { + writeRestriction(desc, MIN); + } + + + public void visit(OWLObjectExactCardinalityRestriction desc) { + writeRestriction(desc, EXACTLY); + } + + + public void visit(OWLObjectMaxCardinalityRestriction desc) { + writeRestriction(desc, MAX); + } + + + public void visit(OWLObjectSelfRestriction desc) { + desc.getProperty().accept(this); + write(SOME); + write(SELF); + } + + + public void visit(OWLObjectOneOf desc) { + write("{"); + write(desc.getIndividuals(), ONE_OF_DELIMETER, false); + write("}"); + } + + + public void visit(OWLDataSomeRestriction desc) { + writeRestriction(desc, SOME); + } + + + public void visit(OWLDataAllRestriction desc) { + writeRestriction(desc, ONLY); + } + + + public void visit(OWLDataValueRestriction desc) { + writeRestriction(desc); + } + + + public void visit(OWLDataMinCardinalityRestriction desc) { + writeRestriction(desc, MIN); + } + + + public void visit(OWLDataExactCardinalityRestriction desc) { + writeRestriction(desc, EXACTLY); + } + + + public void visit(OWLDataMaxCardinalityRestriction desc) { + writeRestriction(desc, MAX); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Entities stuff + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLObjectProperty property) { + write(getShortFormProvider().getShortForm(property)); + } + + + public void visit(OWLDataProperty property) { + write(getShortFormProvider().getShortForm(property)); + } + + + public void visit(OWLIndividual individual) { + write(getShortFormProvider().getShortForm(individual)); + } + + + public void visit(OWLDataType dataType) { + write(dataType.getURI().getFragment()); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Data stuff + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLDataComplementOf node) { + write(NOT); + node.getDataRange().accept(this); + } + + + public void visit(OWLDataOneOf node) { + write("{"); + write(node.getValues(), ONE_OF_DELIMETER, false); + write("}"); + } + + + public void visit(OWLDataRangeRestriction node) { + node.getDataRange().accept(this); + write("["); + write(node.getFacetRestrictions(), FACET_RESTRICTION_SEPARATOR, false); + write("]"); + } + + + public void visit(OWLTypedConstant node) { + if (node.getDataType().getURI().equals(XSDVocabulary.DOUBLE.getURI())) { + write(node.getLiteral()); + } + else if (node.getDataType().getURI().equals(XSDVocabulary.STRING.getURI())) { + write("\""); + write(node.getLiteral()); + write("\""); + } + else if (node.getDataType().getURI().equals(XSDVocabulary.FLOAT.getURI())) { + write(node.getLiteral()); + write("f"); + } + else if (node.getDataType().getURI().equals(XSDVocabulary.INT.getURI())) { + write(node.getLiteral()); + } + else if (node.getDataType().getURI().equals(XSDVocabulary.INTEGER.getURI())) { + write(node.getLiteral()); + } + else { + write("\""); + pushTab(getIndent()); + write(node.getLiteral(), wrap ? LINE_LENGTH : Integer.MAX_VALUE); + popTab(); + write("\"^^"); + write(node.getDataType().getURI()); + } + } + + + public void visit(OWLUntypedConstant node) { + write("\""); + pushTab(getIndent()); + write(node.getLiteral(), wrap ? LINE_LENGTH : Integer.MAX_VALUE); + popTab(); + write("\""); + if (node.hasLang()) { + write("@"); + write(node.getLang()); + } + } + + + public void visit(OWLDataRangeFacetRestriction node) { + write(node.getFacet().getSymbolicForm()); + writeSpace(); + node.getFacetValue().accept(this); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Property expression stuff + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLObjectPropertyInverse property) { + write(INVERSE); + write("("); + property.getInverse().accept(this); + write(")"); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Annotation stuff + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLObjectAnnotation annotation) { + write(annotation.getAnnotationURI()); + writeSpace(); + annotation.getAnnotationValue().accept(this); + } + + + public void visit(OWLConstantAnnotation annotation) { + write(annotation.getAnnotationURI()); + writeSpace(); + annotation.getAnnotationValue().accept(this); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Stand alone axiom representation + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLSubClassAxiom axiom) { + axiom.getSubClass().accept(this); + write(SUBCLASS_OF); + axiom.getSuperClass().accept(this); + } + + + public void visit(OWLNegativeObjectPropertyAssertionAxiom axiom) { + write(NOT); + write("("); + axiom.getSubject().accept(this); + write(" "); + axiom.getProperty().accept(this); + write(" "); + axiom.getObject().accept(this); + write(")"); + } + + + private void writePropertyCharacteristic(ManchesterOWLSyntax characteristic, OWLPropertyExpression prop) { + write(characteristic); + write("("); + prop.accept(this); + write(")"); + } + + + public void visit(OWLAntiSymmetricObjectPropertyAxiom axiom) { + writePropertyCharacteristic(ANTI_SYMMETRIC, axiom.getProperty()); + } + + + public void visit(OWLReflexiveObjectPropertyAxiom axiom) { + writePropertyCharacteristic(REFLEXIVE, axiom.getProperty()); + } + + + public void visit(OWLDisjointClassesAxiom axiom) { + write(axiom.getDescriptions(), DISJOINT_WITH, wrap); + } + + + public void visit(OWLDataPropertyDomainAxiom axiom) { + axiom.getProperty().accept(this); + write(DOMAIN); + axiom.getDomain().accept(this); + } + + + public void visit(OWLImportsDeclaration axiom) { + } + + + public void visit(OWLAxiomAnnotationAxiom axiom) { + } + + + public void visit(OWLObjectPropertyDomainAxiom axiom) { + axiom.getProperty().accept(this); + write(DOMAIN); + axiom.getDomain().accept(this); + } + + + public void visit(OWLEquivalentObjectPropertiesAxiom axiom) { + write(axiom.getProperties(), EQUIVALENT_TO, wrap); + } + + + public void visit(OWLNegativeDataPropertyAssertionAxiom axiom) { + write(NOT); + write("("); + axiom.getSubject().accept(this); + write(" "); + axiom.getProperty().accept(this); + write(" "); + axiom.getObject().accept(this); + write(")"); + } + + + public void visit(OWLDifferentIndividualsAxiom axiom) { + write(axiom.getIndividuals(), DIFFERENT_FROM, wrap); + } + + + public void visit(OWLDisjointDataPropertiesAxiom axiom) { + write(axiom.getProperties(), DISJOINT_WITH, wrap); + } + + + public void visit(OWLDisjointObjectPropertiesAxiom axiom) { + write(axiom.getProperties(), DISJOINT_WITH, wrap); + } + + + public void visit(OWLObjectPropertyRangeAxiom axiom) { + axiom.getProperty().accept(this); + write(RANGE); + axiom.getRange().accept(this); + } + + + public void visit(OWLObjectPropertyAssertionAxiom axiom) { + axiom.getSubject().accept(this); + write(" "); + axiom.getProperty().accept(this); + write(" "); + axiom.getObject().accept(this); + } + + + public void visit(OWLFunctionalObjectPropertyAxiom axiom) { + writePropertyCharacteristic(FUNCTIONAL, axiom.getProperty()); + } + + + public void visit(OWLObjectSubPropertyAxiom axiom) { + axiom.getSubProperty().accept(this); + write(SUB_PROPERTY_OF); + axiom.getSuperProperty().accept(this); + } + + + public void visit(OWLDisjointUnionAxiom axiom) { + axiom.getOWLClass().accept(this); + write(DISJOINT_UNION_OF); + for(Iterator<OWLDescription> it = axiom.getDescriptions().iterator(); it.hasNext(); ) { + it.next().accept(this); + if(it.hasNext()) { + write(", "); + } + } + } + + + public void visit(OWLDeclarationAxiom axiom) { + + } + + + public void visit(OWLEntityAnnotationAxiom axiom) { + } + + + public void visit(OWLOntologyAnnotationAxiom axiom) { + } + + + public void visit(OWLSymmetricObjectPropertyAxiom axiom) { + writePropertyCharacteristic(SYMMETRIC, axiom.getProperty()); + } + + + public void visit(OWLDataPropertyRangeAxiom axiom) { + axiom.getProperty().accept(this); + write(RANGE); + axiom.getRange().accept(this); + } + + + public void visit(OWLFunctionalDataPropertyAxiom axiom) { + writePropertyCharacteristic(FUNCTIONAL, axiom.getProperty()); + } + + + public void visit(OWLEquivalentDataPropertiesAxiom axiom) { + write(axiom.getProperties(), EQUIVALENT_TO, wrap); + } + + + public void visit(OWLClassAssertionAxiom axiom) { + axiom.getIndividual().accept(this); + write(TYPES); + axiom.getDescription().accept(this); + } + + + public void visit(OWLEquivalentClassesAxiom axiom) { + write(axiom.getDescriptions(), EQUIVALENT_TO, wrap); + } + + + public void visit(OWLDataPropertyAssertionAxiom axiom) { + axiom.getSubject().accept(this); + write(" "); + axiom.getProperty().accept(this); + write(" "); + axiom.getObject().accept(this); + } + + + public void visit(OWLTransitiveObjectPropertyAxiom axiom) { + writePropertyCharacteristic(TRANSITIVE, axiom.getProperty()); + } + + + public void visit(OWLIrreflexiveObjectPropertyAxiom axiom) { + writePropertyCharacteristic(IRREFLEXIVE, axiom.getProperty()); + } + + + public void visit(OWLDataSubPropertyAxiom axiom) { + axiom.getSubProperty().accept(this); + write(SUB_PROPERTY_OF); + axiom.getSuperProperty().accept(this); + } + + + public void visit(OWLInverseFunctionalObjectPropertyAxiom axiom) { + writePropertyCharacteristic(INVERSE_FUNCTIONAL, axiom.getProperty()); + } + + + public void visit(OWLSameIndividualsAxiom axiom) { + write(axiom.getIndividuals(), SAME_AS, wrap); + } + + + public void visit(OWLObjectPropertyChainSubPropertyAxiom axiom) { + for (Iterator<OWLObjectPropertyExpression> it = axiom.getPropertyChain().iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + write(" o "); + } + } + write(SUB_PROPERTY_OF); + axiom.getSuperProperty().accept(this); + } + + + public void visit(OWLInverseObjectPropertiesAxiom axiom) { + axiom.getFirstProperty().accept(this); + write(INVERSE); + axiom.getSecondProperty().accept(this); + } + + + public void visit(SWRLRule rule) { + for (Iterator<SWRLAtom> it = rule.getBody().iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + write(", "); + } + } + write(" -> "); + for (Iterator<SWRLAtom> it = rule.getHead().iterator(); it.hasNext();) { + it.next().accept(this); + if (it.hasNext()) { + write(", "); + } + } + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // SWRL + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(SWRLClassAtom node) { + node.getPredicate().accept(this); + write("("); + node.getArgument().accept(this); + write(")"); + } + + + public void visit(SWRLDataRangeAtom node) { + node.getPredicate().accept(this); + write("("); + node.getArgument().accept(this); + write(")"); + } + + + public void visit(SWRLObjectPropertyAtom node) { + node.getPredicate().accept(this); + write("("); + node.getFirstArgument().accept(this); + write(", "); + node.getSecondArgument().accept(this); + write(")"); + } + + + public void visit(SWRLDataValuedPropertyAtom node) { + node.getPredicate().accept(this); + write("("); + node.getFirstArgument().accept(this); + write(", "); + node.getSecondArgument().accept(this); + write(")"); + } + + + public void visit(SWRLBuiltInAtom node) { + write(node.getPredicate().getShortName()); + write("("); + for(Iterator<SWRLAtomDObject> it = node.getArguments().iterator(); it.hasNext(); ) { + it.next().accept(this); + if(it.hasNext()) { + write(", "); + } + } + write(")"); + } + + + public void visit(SWRLAtomDVariable node) { + write("?"); + write(node.getURI().getFragment()); + } + + + public void visit(SWRLAtomIVariable node) { + write("?"); + write(node.getURI().getFragment()); + } + + + public void visit(SWRLAtomIndividualObject node) { + node.getIndividual().accept(this); + } + + + public void visit(SWRLAtomConstantObject node) { + node.getConstant().accept(this); + } + + + public void visit(SWRLSameAsAtom node) { + write(SAME_AS); + write("("); + node.getFirstArgument().accept(this); + write(", "); + node.getSecondArgument().accept(this); + write(")"); + } + + + public void visit(SWRLDifferentFromAtom node) { + write(DIFFERENT_FROM); + write("("); + node.getFirstArgument().accept(this); + write(", "); + node.getSecondArgument().accept(this); + write(")"); + } + + /////////////////////////////////////////////////////////////////////////////////////////////// + // + // Ontology + // + /////////////////////////////////////////////////////////////////////////////////////////////// + + + public void visit(OWLOntology ontology) { + + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |