All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geneontology.owl.differ.render.HTMLDiffRenderer.scala Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package org.geneontology.owl.differ.render

import com.google.common.base.Optional
import org.apache.commons.io.output.ByteArrayOutputStream
import org.apache.commons.text.StringEscapeUtils
import org.geneontology.owl.differ.Differ._
import org.geneontology.owl.differ.ManchesterSyntaxOWLObjectRenderer
import org.geneontology.owl.differ.Util.OptionalOption
import org.geneontology.owl.differ.shortform.{HTMLLinkShortFormProvider, HTMLSafeIRIShortFormProvider, HTMLSafeShortFormProvider}
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.model._
import org.semanticweb.owlapi.util.{AnnotationValueShortFormProvider, SimpleIRIShortFormProvider, SimpleShortFormProvider}

import java.io.{OutputStream, PrintWriter, Writer}
import java.nio.charset.StandardCharsets
import scala.jdk.CollectionConverters._
import scala.util.Using

object HTMLDiffRenderer {

  private val factory = OWLManager.getOWLDataFactory

  private val rdfsLabel = factory.getRDFSLabel

  def renderWriter(diff: GroupedDiff, renderingOntologyProvider: OWLOntologySetProvider, givenWriter: Writer): Unit =
    Using.resource(new PrintWriter(givenWriter)) { writer =>
      val shortFormProvider = new HTMLSafeShortFormProvider(new AnnotationValueShortFormProvider(renderingOntologyProvider, new SimpleShortFormProvider(), new HTMLSafeIRIShortFormProvider(new SimpleIRIShortFormProvider()), List(rdfsLabel).asJava, Map.empty[OWLAnnotationProperty, java.util.List[String]].asJava))
      val htmlLinkShortFormProvider = new HTMLLinkShortFormProvider(shortFormProvider)
      val labelRenderer = new ManchesterSyntaxOWLObjectRenderer()
      labelRenderer.setShortFormProvider(shortFormProvider)
      val htmlRenderer = new ManchesterSyntaxOWLObjectRenderer()
      htmlRenderer.setShortFormProvider(htmlLinkShortFormProvider)
      val groupsToLabels: Map[Grouping, String] = diff.groups.keys.map {
        case group @ IRIGrouping(iri)           => group -> labelRenderer.render(factory.getOWLClass(iri)) // send as OWLClass because labels aren't rendered for IRIs
        case group @ GCIGrouping                => group -> "GCIs"
        case group @ RuleGrouping               => group -> "Rules"
        case group @ NonIRIGrouping(obj)        => group -> labelRenderer.render(obj)
        case group @ OntologyImportGrouping     => group -> "Ontology imports"
        case group @ OntologyAnnotationGrouping => group -> "Ontology annotations"
      }.toMap
      val sortedGroups = OntologyImportGrouping :: OntologyAnnotationGrouping :: (diff.groups - OntologyImportGrouping - OntologyAnnotationGrouping).keys.toList.sortBy(groupsToLabels)
      val header =
        s"""
  
  
  
    
    
    
    OWL diff
  
  

  

Ontology comparison

Left

  • Ontology IRI: ${optionalIRI(diff.left.getOntologyIRI)}
  • Version IRI: ${optionalIRI(diff.left.getVersionIRI)}
  • Loaded from: ${StringEscapeUtils.escapeHtml4(diff.leftSource.toQuotedString)}

Right

  • Ontology IRI: ${optionalIRI(diff.right.getOntologyIRI)}
  • Version IRI: ${optionalIRI(diff.right.getVersionIRI)}
  • Loaded from: ${StringEscapeUtils.escapeHtml4(diff.rightSource.toQuotedString)}
""" writer.println(header) def htmlForObject(obj: OWLObject): String = { val (annotations, objToRender) = obj match { case hasAnnotations: HasAnnotations => val inner = hasAnnotations.getAnnotations.asScala.map(htmlForObject(_)).mkString("\n") val objWithoutAnnotations = obj match { case ax: OWLAxiom => ax.getAxiomWithoutAnnotations case ann: OWLAnnotation => factory.getOWLAnnotation(ann.getProperty, ann.getValue) case _ => obj } s"
    $inner
" -> objWithoutAnnotations case _ => "" -> obj } s"""
  • ${htmlRenderer.render(objToRender)} $annotations
  • """ } def changeList(header: String, axioms: Seq[OWLObject]) = s"""

    $header

      ${axioms.map(htmlForObject).mkString("\n")}
    """ def frameElement(header: String, headerIRI: String, removedList: String, addedList: String) = s"""

    $header $headerIRI

    $removedList $addedList
    """ for { group <- sortedGroups } yield { 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}" } } writer.println() val removed = diff.groups(group).filterNot(_.added).toSeq.sortBy(sortKey) val added = diff.groups(group).filter(_.added).toSeq.sortBy(sortKey) val removedList = if (removed.nonEmpty) changeList("Removed", removed.map(_.owlObject)) else "" val addedList = if (added.nonEmpty) changeList("Added", added.map(_.owlObject)) else "" val header = groupsToLabels(group) val headerIRI = group match { case IRIGrouping(iri) => s"- $iri" case _ => "" } val frame = frameElement(header, headerIRI, removedList, addedList) writer.println(frame) } writer.println("") } def renderStream(diff: GroupedDiff, renderingOntologyProvider: OWLOntologySetProvider, stream: OutputStream): Unit = Using.resource(new PrintWriter(stream)) { writer => renderWriter(diff, renderingOntologyProvider, writer) } def render(diff: GroupedDiff, renderingOntologyProvider: OWLOntologySetProvider): String = Using.resource(new ByteArrayOutputStream()) { stream => renderStream(diff, renderingOntologyProvider, stream) stream.toString(StandardCharsets.UTF_8) } private def optionalIRI(iriOpt: Optional[IRI]): String = (for { iri <- iriOpt.toOption } yield { StringEscapeUtils.escapeHtml4(iri.toQuotedString) }).getOrElse("None") }




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy