-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #717 from clulab/kwalcock/dynamicRules
Add CustomRuleReader
- Loading branch information
Showing
2 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
main/src/main/scala/org/clulab/odin/impl/CustomRuleReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters