Skip to content

Commit

Permalink
Merge pull request #717 from clulab/kwalcock/dynamicRules
Browse files Browse the repository at this point in the history
Add CustomRuleReader
  • Loading branch information
kwalcock committed Feb 28, 2023
2 parents 0160148 + 1d51057 commit f14ada8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
46 changes: 46 additions & 0 deletions main/src/main/scala/org/clulab/odin/impl/CustomRuleReader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.clulab.odin.impl

import org.clulab.odin.Actions
import org.clulab.odin.impl.RuleReader.Rule
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor

import java.nio.charset.Charset
import java.util.{Collection, Map => JMap}

/** This class addresses [[https://github.com/clulab/processors/issues/309]]
*
* Note: nothing is synchronized here, so don't manipulate the configs in a multi-
* threaded environment.
*/
class CustomRuleReader(actions: Actions, charset: Charset) extends RuleReader(actions, charset) {
/** whether the circumstances are right to capture the config in [[readRules]] */
protected var captureConfig: Boolean = false
/** most-recent config generated in [[rulesFromMasterFile]] and then captured */
protected var config: OdinConfig = OdinConfig(resources = OdinResourceManager(Map.empty))

/** Override that reuses the captured config */
override protected def rulesFromSimpleFile(input: String): Seq[Rule] = {
val yaml = new Yaml(new Constructor(classOf[Collection[JMap[String, Any]]]))
val jRules = yaml.load(input).asInstanceOf[Collection[JMap[String, Any]]]

readRules(jRules, this.config)
}

/** Override that enables the config to be captured */
override protected def rulesFromMasterFile(input: String): Seq[Rule] = {
// The superclass's version calls readRules and when this happens, we want the config
// to be captured. This saves us from reimplementation of the superclass's method.
captureConfig = true
super.rulesFromMasterFile(input)
}

/** Override that *captures* the [[OdinConfig]] as a side-effect */
override protected def readRules(rules: Collection[JMap[String, Any]], config: OdinConfig): Seq[Rule] = {
if (captureConfig) {
this.config = config
captureConfig = false
}
super.readRules(rules, config)
}
}
6 changes: 3 additions & 3 deletions main/src/main/scala/org/clulab/odin/impl/RuleReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RuleReader(val actions: Actions, val charset: Charset, val ruleDir: Option
}
}

private def rulesFromSimpleFile(input: String): Seq[Rule] = {
protected def rulesFromSimpleFile(input: String): Seq[Rule] = {
val yaml = new Yaml(new Constructor(classOf[Collection[JMap[String, Any]]]))
val jRules = yaml.load(input).asInstanceOf[Collection[JMap[String, Any]]]
// no resources are specified
Expand All @@ -50,7 +50,7 @@ class RuleReader(val actions: Actions, val charset: Charset, val ruleDir: Option
readRules(jRules, config)
}

private def rulesFromMasterFile(input: String): Seq[Rule] = {
protected def rulesFromMasterFile(input: String): Seq[Rule] = {
val yaml = new Yaml(new Constructor(classOf[JMap[String, Any]]))
val master = yaml.load(input).asInstanceOf[JMap[String, Any]].asScala.toMap
val taxonomy = master.get("taxonomy").map(readTaxonomy)
Expand Down Expand Up @@ -194,7 +194,7 @@ class RuleReader(val actions: Actions, val charset: Charset, val ruleDir: Option
}
}

private def readRules(
protected def readRules(
rules: Collection[JMap[String, Any]],
config: OdinConfig
): Seq[Rule] = {
Expand Down

0 comments on commit f14ada8

Please sign in to comment.