Skip to content

Commit

Permalink
Stable sorting for Markdown and HTML output. Add a minimal test.
Browse files Browse the repository at this point in the history
  • Loading branch information
balhoff committed Nov 18, 2024
1 parent 68930d2 commit 1c650f3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
10 changes: 6 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ homepage := Some(url("https://github.com/balhoff/owl-diff"))

scalaVersion := "2.13.15"

crossScalaVersions := Seq("2.11.12", "2.12.20", "2.13.15")
crossScalaVersions := Seq("2.12.20", "2.13.15")

scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8")

scalacOptions in Test ++= Seq("-Yrangepos")
Test / scalacOptions ++= Seq("-Yrangepos")

fork in Test := true
testFrameworks += new TestFramework("utest.runner.Framework")

libraryDependencies ++= {
Seq(
"net.sourceforge.owlapi" % "owlapi-distribution" % "4.5.29",
"org.apache.commons" % "commons-text" % "1.12.0"
"org.apache.commons" % "commons-text" % "1.12.0",
"com.lihaoyi" %% "utest" % "0.8.3" % Test,
"com.outr" %% "scribe-slf4j2" % "3.15.2" % Test
)
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/org/geneontology/owl/differ/Differ.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Differ {

}

final case class ModifiedAnnotation(item: OWLAnnotation, added: Boolean) extends ModifiedOntologyContent[OWLAnnotation] {
final case class ModifiedOntologyAnnotation(item: OWLAnnotation, added: Boolean) extends ModifiedOntologyContent[OWLAnnotation] {

def owlObject: OWLObject = item

Expand Down Expand Up @@ -73,18 +73,18 @@ object Differ {
def groupedDiff(diff: BasicDiff): GroupedDiff = {
val allChangedAxioms: Set[ModifiedOntologyContent[_]] = diff.left.axioms.map(ModifiedAxiom(_, false)) ++ diff.right.axioms.map(ModifiedAxiom(_, true))
val allChangedImports: Set[ModifiedOntologyContent[_]] = diff.left.imports.map(ModifiedImport(_, false)) ++ diff.right.imports.map(ModifiedImport(_, true))
val allChangedAnnotations: Set[ModifiedOntologyContent[_]] = diff.left.annotations.map(ModifiedAnnotation(_, false)) ++ diff.right.annotations.map(ModifiedAnnotation(_, true))
val allChangedAnnotations: Set[ModifiedOntologyContent[_]] = diff.left.annotations.map(ModifiedOntologyAnnotation(_, false)) ++ diff.right.annotations.map(ModifiedOntologyAnnotation(_, true))
val groupedAxioms = allChangedAxioms.groupBy {
case ModifiedAxiom(ax, _) =>
case ModifiedAxiom(ax, _) =>
AxiomSubjectProviderInst.getSubject(ax) match {
case named: OWLNamedObject => IRIGrouping(named.getIRI)
case iri: IRI => IRIGrouping(iri)
case _: OWLClassExpression => GCIGrouping
case _: SWRLObject => RuleGrouping
case subj => NonIRIGrouping(subj)
}
case ModifiedAnnotation(_, _) => OntologyAnnotationGrouping //shouldn't be matched
case ModifiedImport(_, _) => OntologyImportGrouping //shouldn't be matched
case ModifiedOntologyAnnotation(_, _) => OntologyAnnotationGrouping //shouldn't be matched
case ModifiedImport(_, _) => OntologyImportGrouping //shouldn't be matched
}
val allGrouped = groupedAxioms + (OntologyImportGrouping -> allChangedImports) + (OntologyAnnotationGrouping -> allChangedAnnotations)
GroupedDiff(diff.left.id, diff.left.source, diff.right.id, diff.right.source, allGrouped)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ object HTMLDiffRenderer {
group <- sortedGroups
} yield {
def sortKey(modifiedItem: ModifiedOntologyContent[_]): String = modifiedItem match {
case ModifiedAnnotation(item, _) => item.toString
case ModifiedImport(item, _) => item.toString
case ModifiedAxiom(item, _) => item.getAxiomType.getName
case ModifiedOntologyAnnotation(item, _) => item.toString
case ModifiedImport(item, _) => item.toString
case ModifiedAxiom(item, _) => item.getAxiomType match {
case AxiomType.DECLARATION => s"1-${item.toString}"
case _ => s"2-${item.toString}"
}
}

val removed = diff.groups(group).filterNot(_.added).toSeq.sortBy(sortKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ object MarkdownGroupedDiffRenderer {
val content = (for {
group <- sortedGroups
} yield {
def sortKey(modifiedItem: ModifiedOntologyContent[_]): String = modifiedItem match {
case ModifiedAnnotation(item, _) => item.toString
case ModifiedImport(item, _) => item.toString
case ModifiedAxiom(item, _) => item.getAxiomType.getName
def sortKey(modifiedItem: ModifiedOntologyContent[_]): String = {
modifiedItem match {
case ModifiedOntologyAnnotation(item, _) => item.toString
case ModifiedImport(item, _) => item.toString
case ModifiedAxiom(item, _) => item.getAxiomType match {
case AxiomType.DECLARATION => s"1-${item.toString}"
case _ => s"2-${item.toString}"
}
}
}

val removed = diff.groups(group).filterNot(_.added).toSeq.sortBy(sortKey)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.geneontology.owl.differ

import org.geneontology.owl.differ.render.MarkdownGroupedDiffRenderer
import org.semanticweb.owlapi.apibinding.OWLManager
import utest._

import java.io.File

object TestMarkdownSorting extends TestSuite {

val tests: Tests = Tests {
val left = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(new File("src/test/resources/left-1.ofn"))
val right = OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(new File("src/test/resources/right-1.ofn"))
test("Things are ordered") {
val diff = Differ.diff(left, right)
val grouped = Differ.groupedDiff(diff)
val output = MarkdownGroupedDiffRenderer.render(grouped, right.getOWLOntologyManager)
assert(output.indexOf("Class: [class five](http://example.org/E)") <
output.indexOf("[class five](http://example.org/E) [label](http://www.w3.org/2000/01/rdf-schema#label) \"class five\""))
assert(output.indexOf("[class five-one](http://example.org/A) [label](http://www.w3.org/2000/01/rdf-schema#label) \"class five-one\"") >
output.indexOf("[class five-one](http://example.org/A) [definition](http://example.org/1) \"Definition one-five.\""))
assert(output.indexOf("[class five-one](http://example.org/A) EquivalentTo [class five](http://example.org/E)") >
output.indexOf("[class five-one](http://example.org/A) [label](http://www.w3.org/2000/01/rdf-schema#label) \"class five-one\""))
assert(output.indexOf("[class five-one](http://example.org/A) SubClassOf [class three](http://example.org/C)") >
output.indexOf("[class five-one](http://example.org/A) EquivalentTo [class five](http://example.org/E)"))
}
}

}

0 comments on commit 1c650f3

Please sign in to comment.