Skip to content

Commit

Permalink
Merge branch 'protegeproject:master' into removeLibs
Browse files Browse the repository at this point in the history
  • Loading branch information
mosfet80 authored Oct 8, 2024
2 parents b5f4a23 + 80c2ad7 commit 898f51b
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.protege.editor.owl.ui;

import org.protege.editor.owl.ui.frame.OWLFrameSectionRow;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLPropertyAssertionAxiom;

import java.util.Comparator;

/**
* Compares rows of type {@link OWLPropertyAssertionAxiom}.
* Comparison priority is as follows:
* inference > property > object
* <p>
* Author: Michael Opitz <br>
* https://github.com/mmopitz<br>
* Date: 30-Sep-2024<br><br>
*/
public class OWLPropertyAssertionRowComparator<E extends OWLPropertyAssertionAxiom<?, ?>, PAIR> implements
Comparator<OWLFrameSectionRow<OWLIndividual, E, PAIR>> {
private final Comparator<OWLObject> delegate;

public OWLPropertyAssertionRowComparator(Comparator<OWLObject> delegate) {
this.delegate = delegate;
}


@Override
public int compare(OWLFrameSectionRow<OWLIndividual, E, PAIR> o1, OWLFrameSectionRow<OWLIndividual, E, PAIR> o2) {
if (o1.isInferred() && !o2.isInferred()) {
return 1;
}
else if (o2.isInferred() && !o1.isInferred()) {
return -1;
}
int comparison = delegate.compare(o1.getAxiom().getProperty(), o2.getAxiom().getProperty());
if (comparison != 0) {
return comparison;
}
return delegate.compare(o1.getAxiom().getObject(), o2.getAxiom().getObject());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.model.inference.ReasonerPreferences.OptionalInferenceTask;
import org.protege.editor.owl.ui.OWLPropertyAssertionRowComparator;
import org.protege.editor.owl.ui.editor.OWLDataPropertyRelationshipEditor;
import org.protege.editor.owl.ui.editor.OWLObjectEditor;
import org.protege.editor.owl.ui.frame.AbstractOWLFrameSection;
Expand Down Expand Up @@ -106,7 +107,7 @@ public OWLObjectEditor<OWLDataPropertyConstantPair> getObjectEditor() {
* or <code>null</code> if the rows shouldn't be sorted.
*/
public Comparator<OWLFrameSectionRow<OWLIndividual, OWLDataPropertyAssertionAxiom, OWLDataPropertyConstantPair>> getRowComparator() {
return null;
return new OWLPropertyAssertionRowComparator<>(getOWLModelManager().getOWLObjectComparator());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.protege.editor.owl.ui.frame.individual;

import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.ui.OWLPropertyAssertionRowComparator;
import org.protege.editor.owl.ui.editor.OWLDataPropertyRelationshipEditor;
import org.protege.editor.owl.ui.editor.OWLObjectEditor;
import org.protege.editor.owl.ui.frame.AbstractOWLFrameSection;
Expand Down Expand Up @@ -73,7 +74,7 @@ public OWLObjectEditor<OWLDataPropertyConstantPair> getObjectEditor() {
* or <code>null</code> if the rows shouldn't be sorted.
*/
public Comparator<OWLFrameSectionRow<OWLIndividual, OWLNegativeDataPropertyAssertionAxiom, OWLDataPropertyConstantPair>> getRowComparator() {
return null;
return new OWLPropertyAssertionRowComparator<>(getOWLModelManager().getOWLObjectComparator());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.protege.editor.owl.ui.frame.individual;

import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.ui.OWLPropertyAssertionRowComparator;
import org.protege.editor.owl.ui.editor.OWLObjectEditor;
import org.protege.editor.owl.ui.editor.OWLObjectPropertyIndividualPairEditor2;
import org.protege.editor.owl.ui.frame.AbstractOWLFrameSection;
Expand Down Expand Up @@ -70,7 +71,7 @@ public OWLObjectEditor<OWLObjectPropertyIndividualPair> getObjectEditor() {
* or <code>null</code> if the rows shouldn't be sorted.
*/
public Comparator<OWLFrameSectionRow<OWLIndividual, OWLNegativeObjectPropertyAssertionAxiom, OWLObjectPropertyIndividualPair>> getRowComparator() {
return null;
return new OWLPropertyAssertionRowComparator<>(getOWLModelManager().getOWLObjectComparator());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.protege.editor.owl.OWLEditorKit;
import org.protege.editor.owl.model.inference.ReasonerPreferences.OptionalInferenceTask;
import org.protege.editor.owl.ui.OWLPropertyAssertionRowComparator;
import org.protege.editor.owl.ui.editor.OWLObjectEditor;
import org.protege.editor.owl.ui.editor.OWLObjectPropertyIndividualPairEditor2;
import org.protege.editor.owl.ui.frame.AbstractOWLFrameSection;
Expand Down Expand Up @@ -106,7 +107,7 @@ public OWLObjectEditor<OWLObjectPropertyIndividualPair> getObjectEditor() {
* or <code>null</code> if the rows shouldn't be sorted.
*/
public Comparator<OWLFrameSectionRow<OWLIndividual, OWLObjectPropertyAssertionAxiom, OWLObjectPropertyIndividualPair>> getRowComparator() {
return null;
return new OWLPropertyAssertionRowComparator<>(getOWLModelManager().getOWLObjectComparator());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.protege.editor.owl.ui;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.protege.editor.owl.ui.frame.OWLFrameSectionRow;
import org.semanticweb.owlapi.model.*;

import java.util.Comparator;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class OWLPropertyAssertionRowComparator_TestCase {

@Mock
Comparator<OWLObject> delegate;
@Mock
OWLFrameSectionRow<OWLIndividual, OWLObjectPropertyAssertionAxiom, String> o1;
@Mock
OWLFrameSectionRow<OWLIndividual, OWLObjectPropertyAssertionAxiom, String> o2;
@Mock
OWLObjectPropertyAssertionAxiom opa1;
@Mock
OWLObjectPropertyAssertionAxiom opa2;
@Mock
OWLObjectProperty op1;
@Mock
OWLObjectProperty op2;
@Mock
OWLIndividual i1;
@Mock
OWLIndividual i2;

OWLPropertyAssertionRowComparator<OWLObjectPropertyAssertionAxiom, String> comparator;

@Before
public void setUp() {
comparator = new OWLPropertyAssertionRowComparator<>(delegate);
}

@Test
public void testInferred1() {
when(o1.isInferred()).thenReturn(true);
when(o2.isInferred()).thenReturn(false);
assertEquals(1, comparator.compare(o1, o2));
}

@Test
public void testInferred2() {
when(o1.isInferred()).thenReturn(false);
when(o2.isInferred()).thenReturn(true);
assertEquals(-1, comparator.compare(o1, o2));
}

@Test
public void testDifferentProperty() {
when(o1.getAxiom()).thenReturn(opa1);
when(o2.getAxiom()).thenReturn(opa2);
when(opa1.getProperty()).thenReturn(op1);
when(opa2.getProperty()).thenReturn(op2);
when(delegate.compare(op1, op2)).thenReturn(-2);
assertEquals(-2, comparator.compare(o1, o2));
}

@Test
public void testDifferentIndividuals() {
when(o1.getAxiom()).thenReturn(opa1);
when(o2.getAxiom()).thenReturn(opa2);
when(opa1.getProperty()).thenReturn(op1);
when(opa2.getProperty()).thenReturn(op2);
when(opa1.getObject()).thenReturn(i1);
when(opa2.getObject()).thenReturn(i2);
when(delegate.compare(op1, op2)).thenReturn(0);
when(delegate.compare(i1, i2)).thenReturn(-2);
assertEquals(-2, comparator.compare(o1, o2));
}

@Test
public void testSame() {
when(o1.getAxiom()).thenReturn(opa1);
when(o2.getAxiom()).thenReturn(opa2);
when(opa1.getProperty()).thenReturn(op1);
when(opa2.getProperty()).thenReturn(op2);
when(opa1.getObject()).thenReturn(i1);
when(opa2.getObject()).thenReturn(i2);
when(delegate.compare(op1, op2)).thenReturn(0);
when(delegate.compare(i1, i2)).thenReturn(0);
assertEquals(0, comparator.compare(o1, o2));
}

}

0 comments on commit 898f51b

Please sign in to comment.