Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 2.6.4
version = 2.7.5
maxColumn = 140
continuationIndent.callSite = 2
continuationIndent.defnSite = 2
Expand Down
19 changes: 8 additions & 11 deletions src/main/scala/org/renci/relationgraph/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ final case class Config(ontologyFile: String,
propertiesFile: Option[String],
outputSubclasses: BoolValue = FalseValue,
reflexiveSubclasses: BoolValue = TrueValue,
equivalenceAsSubclass: BoolValue = TrueValue) {

}
equivalenceAsSubclass: BoolValue = TrueValue) {}

object Config {

Expand All @@ -30,15 +28,14 @@ object Config {
arg.toLowerCase match {
case "rdf" => Right(RDFMode)
case "owl" => Right(OWLMode)
case _ => Left(MalformedValue("output mode", arg))
case _ => Left(MalformedValue("output mode", arg))
}
}

}

/**
* This works around some confusing behavior in case-app boolean parsing
*/
/** This works around some confusing behavior in case-app boolean parsing
*/
sealed trait BoolValue {

def bool: Boolean
Expand All @@ -59,11 +56,11 @@ object Config {

implicit val argParser: ArgParser[BoolValue] = SimpleArgParser.from[BoolValue]("boolean value") { arg =>
arg.toLowerCase match {
case "true" => Right(TrueValue)
case "true" => Right(TrueValue)
case "false" => Right(FalseValue)
case "1" => Right(TrueValue)
case "0" => Right(FalseValue)
case _ => Left(MalformedValue("boolean value", arg))
case "1" => Right(TrueValue)
case "0" => Right(FalseValue)
case _ => Left(MalformedValue("boolean value", arg))
}
}

Expand Down
87 changes: 44 additions & 43 deletions src/main/scala/org/renci/relationgraph/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,35 @@ object Main extends ZCaseApp[Config] {
nonredundantRDFWriter <- createStreamRDF(nonredundantOutputStream)
redundantRDFWriter <- createStreamRDF(redundantOutputStream)
} yield (nonredundantRDFWriter, redundantRDFWriter)
val program = streamsManaged.use {
case (nonredundantRDFWriter, redundantRDFWriter) =>
for {
fileProperties <- config.propertiesFile.map(readPropertiesFile).getOrElse(ZIO.succeed(Set.empty[OWLObjectProperty]))
specifiedProperties = fileProperties ++ config.property.map(prop => df.getOWLObjectProperty(IRI.create(prop))).to(Set)
manager <- ZIO.effect(OWLManager.createOWLOntologyManager())
ontology <- ZIO.effect(manager.loadOntologyFromOntologyDocument(ontologyFile))
whelkOntology = Bridge.ontologyToAxioms(ontology)
_ <- ZIO.effectTotal(scribe.info("Running reasoner"))
whelk = Reasoner.assert(whelkOntology)
_ <- ZIO.effectTotal(scribe.info("Done running reasoner"))
_ <- (effectBlockingIO(
nonredundantRDFWriter.triple(Triple.create(NodeFactory.createBlankNode("nonredundant"), RDFType, OWLOntology))) *>
effectBlockingIO(redundantRDFWriter.triple(Triple.create(NodeFactory.createBlankNode("redundant"), RDFType, OWLOntology))))
.when(config.mode == OWLMode)
start <- ZIO.effectTotal(System.currentTimeMillis())
classes = allClasses(ontology)
classesTasks = classes.map(c => Task(processSuperclasses(c, whelk, config)))
restrictions = extractAllRestrictions(ontology, specifiedProperties)
restrictionsTasks = restrictions.map(r => Task(processRestriction(r, whelk, config.mode)))
allTasks = classesTasks ++ restrictionsTasks
processed = allTasks.mapParallelUnordered(JRuntime.getRuntime.availableProcessors)(identity)
monixTask = processed.foreachL {
case TriplesGroup(nonredundant, redundant) =>
nonredundant.foreach(nonredundantRDFWriter.triple)
redundant.foreach(redundantRDFWriter.triple)
}
_ <- IO.fromTask(monixTask)
stop <- ZIO.effectTotal(System.currentTimeMillis())
_ <- ZIO.effectTotal(scribe.info(s"Computed relations in ${(stop - start) / 1000.0}s"))
} yield ()
val program = streamsManaged.use { case (nonredundantRDFWriter, redundantRDFWriter) =>
for {
fileProperties <- config.propertiesFile.map(readPropertiesFile).getOrElse(ZIO.succeed(Set.empty[OWLObjectProperty]))
specifiedProperties = fileProperties ++ config.property.map(prop => df.getOWLObjectProperty(IRI.create(prop))).to(Set)
manager <- ZIO.effect(OWLManager.createOWLOntologyManager())
ontology <- ZIO.effect(manager.loadOntologyFromOntologyDocument(ontologyFile))
whelkOntology = Bridge.ontologyToAxioms(ontology)
_ <- ZIO.effectTotal(scribe.info("Running reasoner"))
whelk = Reasoner.assert(whelkOntology)
_ <- ZIO.effectTotal(scribe.info("Done running reasoner"))
_ <- (effectBlockingIO(
nonredundantRDFWriter.triple(Triple.create(NodeFactory.createBlankNode("nonredundant"), RDFType, OWLOntology))) *>
effectBlockingIO(redundantRDFWriter.triple(Triple.create(NodeFactory.createBlankNode("redundant"), RDFType, OWLOntology))))
.when(config.mode == OWLMode)
start <- ZIO.effectTotal(System.currentTimeMillis())
classes = allClasses(ontology)
classesTasks = classes.map(c => Task(processSuperclasses(c, whelk, config)))
restrictions = extractAllRestrictions(ontology, specifiedProperties)
restrictionsTasks = restrictions.map(r => Task(processRestriction(r, whelk, config.mode)))
allTasks = classesTasks ++ restrictionsTasks
processed = allTasks.mapParallelUnordered(JRuntime.getRuntime.availableProcessors)(identity)
monixTask = processed.foreachL { case TriplesGroup(nonredundant, redundant) =>
nonredundant.foreach(nonredundantRDFWriter.triple)
redundant.foreach(redundantRDFWriter.triple)
}
_ <- IO.fromTask(monixTask)
stop <- ZIO.effectTotal(System.currentTimeMillis())
_ <- ZIO.effectTotal(scribe.info(s"Computed relations in ${(stop - start) / 1000.0}s"))
} yield ()
}
program.exitCode
}
Expand All @@ -98,7 +96,8 @@ object Main extends ZCaseApp[Config] {
}
}(stream => ZIO.effectTotal(stream.finish()))

def allClasses(ont: OWLOntology): Observable[OWLClass] = Observable.fromIterable(ont.getClassesInSignature(Imports.INCLUDED).asScala.to(Set) - OWLThing - OWLNothing)
def allClasses(ont: OWLOntology): Observable[OWLClass] =
Observable.fromIterable(ont.getClassesInSignature(Imports.INCLUDED).asScala.to(Set) - OWLThing - OWLNothing)

def processSuperclasses(cls: OWLClass, whelk: ReasonerState, config: Config): TriplesGroup = {
val subject = NodeFactory.createURI(cls.getIRI.toString)
Expand All @@ -110,19 +109,21 @@ object Main extends ZCaseApp[Config] {
val (equivs, directSuperclasses) = whelk.directlySubsumedBy(concept)
val adjustedEquivs = if (config.reflexiveSubclasses.bool) equivs + concept else equivs - concept
val directSuperclassTriples = directSuperclasses.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id)))
val equivalentClassTriples = if (config.equivalenceAsSubclass.bool)
adjustedEquivs.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id)))
else
adjustedEquivs.map(c => Triple.create(subject, OWLEquivalentClass, NodeFactory.createURI(c.id)))
val equivalentClassTriples =
if (config.equivalenceAsSubclass.bool)
adjustedEquivs.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id)))
else
adjustedEquivs.map(c => Triple.create(subject, OWLEquivalentClass, NodeFactory.createURI(c.id)))
val nonredundantTriples = directSuperclassTriples ++ equivalentClassTriples
val adjustedSuperclasses = if (config.reflexiveSubclasses.bool) allSuperclasses + concept else allSuperclasses - concept
val redundantTriples = if (config.equivalenceAsSubclass.bool)
adjustedSuperclasses.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id)))
else {
val superclassesMinusEquiv = adjustedSuperclasses -- adjustedEquivs
superclassesMinusEquiv.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id))) ++
equivalentClassTriples
}
val redundantTriples =
if (config.equivalenceAsSubclass.bool)
adjustedSuperclasses.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id)))
else {
val superclassesMinusEquiv = adjustedSuperclasses -- adjustedEquivs
superclassesMinusEquiv.map(c => Triple.create(subject, RDFSSubClassOf, NodeFactory.createURI(c.id))) ++
equivalentClassTriples
}
TriplesGroup(nonredundantTriples, redundantTriples)
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/scala/org/renci/relationgraph/ZCaseApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import caseapp.core.{Error, RemainingArgs}
import zio._
import zio.console.{putStrLn, Console}

/**
* Adapted from caseapp.cats.IOCaseApp
/** Adapted from caseapp.cats.IOCaseApp
*/
abstract class ZCaseApp[T](implicit val parser0: Parser[T], val messages: Help[T]) extends App {

Expand All @@ -32,8 +31,7 @@ abstract class ZCaseApp[T](implicit val parser0: Parser[T], val messages: Help[T
private[this] def usageAsked: ZIO[Console, Nothing, ExitCode] =
putStrLn(messages.withHelp.usage).as(ExitCode.success)

/**
* Arguments are expanded then parsed. By default, argument expansion is the identity function.
/** Arguments are expanded then parsed. By default, argument expansion is the identity function.
* Overriding this method allows plugging in an arbitrary argument expansion logic.
*
* One such expansion logic involves replacing each argument of the form '@<file>' with the
Expand All @@ -51,8 +49,7 @@ abstract class ZCaseApp[T](implicit val parser0: Parser[T], val messages: Help[T
*/
private[this] def expandArgs(args: List[String]): List[String] = args

/**
* Whether to stop parsing at the first unrecognized argument.
/** Whether to stop parsing at the first unrecognized argument.
*
* That is, stop parsing at the first non option (not starting with "-"), or
* the first unrecognized option. The unparsed arguments are put in the `args`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ object TestRelationGraph extends DefaultRunnableSpec {
restrictions
.map(Main.processRestriction(_, whelk, Config.RDFMode))
.reduce((left, right) => TriplesGroup(left.nonredundant ++ right.nonredundant, left.redundant ++ right.redundant))
.headL)
.headL
)
TriplesGroup(nonredundant, redundant) = triples
} yield assert(nonredundant)(contains(Triple.create(n(s"$Prefix#A"), P, n(s"$Prefix#D")))) &&
assert(redundant)(contains(Triple.create(n(s"$Prefix#A"), P, n(s"$Prefix#D")))) &&
Expand Down