Skip to content

Commit

Permalink
Deprecate withFactsOfType
Browse files Browse the repository at this point in the history
- Convert all DSL methods and tests to use factsOfType
- Convert all methods from using deprecated NamedLens.atKey to .at
  • Loading branch information
jeffmay committed Apr 8, 2021
1 parent 0b3ddb0 commit d7ede34
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ trait ExprDsl extends TimeFunctions with WrapExprSyntax with WrapEachExprSyntax
captureResult: CaptureP[V, Seq[R], P],
): Expr.WrapOutputSeq[V, R, P] = sequence(expressions)

@deprecated("Use factsOfType() instead", "0.14.0")
def withFactsOfType[T, P](
factTypeSet: FactTypeSet[T],
)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ class CapturePSpec extends AnyWordSpec with TypeCheckedTripleEquals {
import com.rallyhealth.vapors.core.example.CaptureTimeRange._

"find a single fact from a query" in {
val q = withFactsOfType(FactTypes.WeightMeasurement).where {
_.exists {
_.get(_.select(_.value).select(_.value)) > 18
}
val q = factsOfType(FactTypes.WeightMeasurement).exists {
_.get(_.select(_.value).select(_.value)) > 18
}
val result = eval(JoeSchmoe.factTable)(q)
assert(result.param.value === TimeRange(JoeSchmoe.weight.value.timestamp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ class ExprBuilderSpec extends AnyWordSpec {
"ValExprBuilder" should {

"combine lenses from chained .get() methods" in {
val q = withFactsOfType(FactTypes.GenericMeasurement).where {
_.exists {
_.get(_.select(_.value)).get(_.select(_.value)) > 0.0
}
}
val q = factsOfType(FactTypes.GenericMeasurement).exists {
_.get(_.select(_.value)).get(_.select(_.value)) > 0.0
}.returnOutput
inside(q) {
case Expr.WithFactsOfType(_, Expr.ExistsInOutput(_, condExpr, _), _) =>
case Expr.ExistsInOutput(_, condExpr, _) =>
inside(condExpr) {
case Expr.OutputWithinWindow(Expr.SelectFromOutput(_, valueLens, _), _, _) =>
assertResult(DataPath.empty.atField("value").atField("value")) {
Expand All @@ -35,13 +33,11 @@ class ExprBuilderSpec extends AnyWordSpec {
}

"combine lenses from .get() and .getFoldable() methods" in {
val q = withFactsOfType(FactTypes.ProbabilityToUse).where {
_.exists {
_.get(_.select(_.value)).getFoldable(_.select(_.scores)).isEmpty
}
}
val q = factsOfType(FactTypes.ProbabilityToUse).exists {
_.get(_.select(_.value)).getFoldable(_.select(_.scores)).isEmpty
}.returnOutput
inside(q) {
case Expr.WithFactsOfType(_, Expr.ExistsInOutput(_, condExpr, _), _) =>
case Expr.ExistsInOutput(_, condExpr, _) =>
inside(condExpr) {
case Expr.OutputIsEmpty(Expr.SelectFromOutput(_, valueLens, _), _) =>
assertResult(DataPath.empty.atField("value").atField("scores")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ class Snippets(val clock: Clock) {
}

lazy val isUser: RootExpr[Boolean, Unit] = {
withFactsOfType(FactTypes.Role).where {
_.exists {
_.get(_.select(_.value)) >= Role.User
}
factsOfType(FactTypes.Role).exists {
_.get(_.select(_.value)) >= Role.User
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ class DivideOutputsSpec extends AnyFreeSpec {
)
}

lazy val humanAgeFromDogYears: Expr.WithFactsOfType[Int, Seq[Int], Unit] = {
withFactsOfType(FactTypes.Age).where { facts =>
facts.map { fact =>
fact.get(_.select(_.value)) / 7
}
lazy val humanAgeFromDogYears: Expr[FactTable, Seq[Int], Unit] = {
factsOfType(FactTypes.Age).map { fact =>
fact.get(_.select(_.value)) / 7
}
}

Expand Down Expand Up @@ -93,11 +91,9 @@ class DivideOutputsSpec extends AnyFreeSpec {
)
}

lazy val celciusFromFahrenheit: Expr.WithFactsOfType[Double, Seq[Double], Unit] = {
withFactsOfType(FactTypes.TempFahrenheit).where { facts =>
facts.map { fact =>
(fact.get(_.select(_.value)) - 32.0) / 1.8
}
lazy val celciusFromFahrenheit: Expr[FactTable, Seq[Double], Unit] = {
factsOfType(FactTypes.TempFahrenheit).map { fact =>
(fact.get(_.select(_.value)) - 32.0) / 1.8
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,22 @@ class EmbedExprSpec extends AnyWordSpec {
"embedding an expression inside a logical operator inside a 'withFactsOfType'" when {

def insideProbOfWeightloss(cond: ValCondExpr[Double, Unit]): RootExpr[Boolean, Unit] = {
withFactsOfType(FactTypes.ProbabilityToUse).where {
_.toList
.flatMap {
_.getFoldable {
_.select(_.value).select(_.scores).atKey("weightloss").to(List)
}
factsOfType(FactTypes.ProbabilityToUse)
.flatMap {
_.getFoldable {
_.select(_.value).select(_.scores).at("weightloss").to(Seq)
}
.exists { _ =>
cond
}
}
}
.exists { _ =>
cond
}
}

def weightMeasuredWithin(window: Window[Int]): RootExpr[Boolean, Unit] = {
import cats.syntax.invariant._
val doubleWindow = window.imap(_.toDouble)(_.toInt)
withFactsOfType(FactTypes.WeightMeasurement).where {
_.exists {
_.get(_.select(_.value).select(_.value)).within(doubleWindow)
}
factsOfType(FactTypes.WeightMeasurement).exists {
_.get(_.select(_.value).select(_.value)).within(doubleWindow)
}
}

Expand Down Expand Up @@ -107,10 +103,8 @@ class EmbedExprSpec extends AnyWordSpec {
}

"disallows embedding an invalid return type" in {
val listOfNumberExpr = withFactsOfType(FactTypes.ProbabilityToUse).where {
_.toList.flatMap {
_.getFoldable(_.select(_.value).select(_.scores).atKey("weightloss").to(List))
}
val listOfNumberExpr = factsOfType(FactTypes.ProbabilityToUse).flatMap {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss").to(Seq))
}
assertDoesNotCompile {
"""insideProbOfWeightloss(or(trueLiteral, listOfNumberExpr))"""
Expand Down Expand Up @@ -177,10 +171,8 @@ class EmbedExprSpec extends AnyWordSpec {
}

"disallows embedding an invalid return type" in {
val listOfNumberExpr = withFactsOfType(FactTypes.ProbabilityToUse).where {
_.toList.flatMap {
_.getFoldable(_.select(_.value).select(_.scores).atKey("weightloss").asIterable.to(List))
}
val listOfNumberExpr = factsOfType(FactTypes.ProbabilityToUse).flatMap {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss").asIterable.to(Seq))
}
assertDoesNotCompile {
"""insideProbOfWeightloss(and(trueLiteral, listOfNumberExpr))"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ class FilterOutputSpec extends AnyWordSpec {
"comparing facts" should {

"return all matching facts from a given subset" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.filter(_ in Set(Tags.asthma))
val q = factsOfType(FactTypes.Tag).filter {
_ in Set(Tags.asthma)
}
val res = eval(tagFacts)(q)
res.output.value should contain theSameElementsAs Seq(Tags.asthma)
assertResult(Evidence(Tags.asthma))(res.output.evidence)
}

"return all matching facts from a given superset" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.filter(_ in Set(Tags.asthma, Tags.obeseBmi))
val q = factsOfType(FactTypes.Tag).filter {
_ in Set(Tags.asthma, Tags.obeseBmi)
}
val res = eval(tagFacts)(q)
res.output.value should contain theSameElementsAs Seq(Tags.asthma)
assertResult(Evidence(Tags.asthma))(res.output.evidence)
}

"return an empty list of facts when given a set that contains no common elements" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.filter(_ in Set(Tags.obeseBmi))
val q = factsOfType(FactTypes.Tag).filter {
_ in Set(Tags.obeseBmi)
}
val res = eval(tagFacts)(q)
assert(res.output.value.isEmpty)
Expand All @@ -46,16 +46,16 @@ class FilterOutputSpec extends AnyWordSpec {
"comparing values" should {

"return all matching values from a given subset" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).filter(_ in Set(Tags.asthma).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).filter {
_ in Set(Tags.asthma).map(_.value)
}
val res = eval(tagFacts)(q)
res.output.value should contain theSameElementsAs Seq(Tags.asthma).map(_.value)
}

"return the correct evidence for the matching values from a given subset" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).filter(_ in Set(Tags.asthma).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).filter {
_ in Set(Tags.asthma).map(_.value)
}
val res = eval(tagFacts)(q)
pendingUntilFixed {
Expand All @@ -65,16 +65,16 @@ class FilterOutputSpec extends AnyWordSpec {
}

"return the matching values from a given superset" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).filter(_ in Set(Tags.asthma, Tags.obeseBmi).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).filter {
_ in Set(Tags.asthma, Tags.obeseBmi).map(_.value)
}
val res = eval(tagFacts)(q)
res.output.value should contain theSameElementsAs Seq(Tags.asthma).map(_.value)
}

"return the correct evidence for the matching values from a given superset" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).filter(_ in Set(Tags.asthma, Tags.obeseBmi).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).filter {
_ in Set(Tags.asthma, Tags.obeseBmi).map(_.value)
}
val res = eval(tagFacts)(q)
pendingUntilFixed {
Expand All @@ -84,8 +84,8 @@ class FilterOutputSpec extends AnyWordSpec {
}

"return an empty list of values when given a set that contains no common elements" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).filter(_ in Set(Tags.obeseBmi).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).filter {
_ in Set(Tags.obeseBmi).map(_.value)
}
val res = eval(tagFacts)(q)
assert(res.output.value.isEmpty)
Expand All @@ -96,16 +96,16 @@ class FilterOutputSpec extends AnyWordSpec {
"using the 'containsAny' op" should {

"return 'true' when the fact table contains a superset of the given set" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).containsAny(Set(Tags.asthma).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).containsAny {
Set(Tags.asthma).map(_.value)
}
val res = eval(tagFacts)(q)
assert(res.output.value)
}

"return the correct evidence for the facts that contain a superset of the given set" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).containsAny(Set(Tags.asthma).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).containsAny {
Set(Tags.asthma).map(_.value)
}
val res = eval(tagFacts)(q)
pendingUntilFixed {
Expand All @@ -115,16 +115,16 @@ class FilterOutputSpec extends AnyWordSpec {
}

"return 'true' when the fact table contains a subset of the given set" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).containsAny(Set(Tags.asthma, Tags.obeseBmi).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).containsAny {
Set(Tags.asthma, Tags.obeseBmi).map(_.value)
}
val res = eval(tagFacts)(q)
assert(res.output.value)
}

"return the correct evidence for the facts that contain a subset of the given set" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).containsAny(Set(Tags.asthma, Tags.obeseBmi).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).containsAny {
Set(Tags.asthma, Tags.obeseBmi).map(_.value)
}
val res = eval(tagFacts)(q)
pendingUntilFixed {
Expand All @@ -134,8 +134,8 @@ class FilterOutputSpec extends AnyWordSpec {
}

"return 'false' with no Evidence when the facts do not contain anything in the given set" in {
val q = withFactsOfType(FactTypes.Tag).where { facts =>
facts.map(_.value).containsAny(Set(Tags.obeseBmi).map(_.value))
val q = factsOfType(FactTypes.Tag).map(_.value).containsAny {
Set(Tags.obeseBmi).map(_.value)
}
val res = eval(tagFacts)(q)
assert(!res.output.value)
Expand All @@ -151,16 +151,16 @@ class FilterOutputSpec extends AnyWordSpec {
val numericFacts = FactTable(low, middle, high)

"return all values that match the condition" in {
val q = withFactsOfType(FactTypes.Age).where { facts =>
facts.map(_.value).filter(_ >= middle.value)
val q = factsOfType(FactTypes.Age).map(_.value).filter {
_ >= middle.value
}
val res = eval(numericFacts)(q)
res.output.value should contain theSameElementsAs Seq(middle, high).map(_.value)
}

"return the correct evidence for the matching values from a given subset" in {
val q = withFactsOfType(FactTypes.Age).where { facts =>
facts.map(_.value).filter(_ >= middle.value)
val q = factsOfType(FactTypes.Age).map(_.value).filter {
_ >= middle.value
}
val res = eval(numericFacts)(q)
pendingUntilFixed {
Expand All @@ -170,26 +170,26 @@ class FilterOutputSpec extends AnyWordSpec {
}

"return all facts that match the condition" in {
val q = withFactsOfType(FactTypes.Age).where { facts =>
facts.filter(_.value >= middle.value)
val q = factsOfType(FactTypes.Age).filter {
_.value >= middle.value
}
val res = eval(numericFacts)(q)
res.output.value should contain theSameElementsAs Seq(middle, high)
assertResult(Evidence(middle, high))(res.output.evidence)
}

"return an empty list of values when none of the elements meet the condition" in {
val q = withFactsOfType(FactTypes.Age).where { facts =>
facts.map(_.value).filter(_ > high.value)
val q = factsOfType(FactTypes.Age).map(_.value).filter {
_ > high.value
}
val res = eval(numericFacts)(q)
assert(res.output.value.isEmpty)
assert(res.output.evidence.isEmpty)
}

"return an empty list of facts when none meet the condition" in {
val q = withFactsOfType(FactTypes.Age).where { facts =>
facts.filter(_.value > high.value)
val q = factsOfType(FactTypes.Age).filter {
_.value > high.value
}
val res = eval(numericFacts)(q)
assert(res.output.value.isEmpty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class InterpretExprAsResultFnSpec extends AnyWordSpec {
"using no post processing" should {

"find a single fact from a query" in {
val q = withFactsOfType(FactTypes.Age).where {
_.exists {
_.get(_.select(_.value)) >= 18
}
val q = factsOfType(FactTypes.Age).exists {
_.get(_.select(_.value)) >= 18
}
val result = eval(JoeSchmoe.factTable)(q)
assert(result.param.value === ())
Expand All @@ -25,23 +23,19 @@ class InterpretExprAsResultFnSpec extends AnyWordSpec {
}

"find a complex fact from a query" in {
val q = withFactsOfType(FactTypes.ProbabilityToUse).where {
_.exists {
_.getFoldable(_.select(_.value).select(_.scores).atKey("weightloss")).exists {
_ > 0.5
}
val q = factsOfType(FactTypes.ProbabilityToUse).exists {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss")).exists {
_ > 0.5
}
}
val result = eval(JoeSchmoe.factTable)(q)
assertResult(Evidence(JoeSchmoe.probs))(result.output.evidence)
}

"define a fact expression" in {
val likelyToJoinWeightloss = withFactsOfType(FactTypes.ProbabilityToUse).where {
_.exists {
_.getFoldable(_.select(_.value).select(_.scores).atKey("weightloss")).exists {
_ > 0.5
}
val likelyToJoinWeightloss = factsOfType(FactTypes.ProbabilityToUse).exists {
_.getFoldable(_.select(_.value).select(_.scores).at("weightloss")).exists {
_ > 0.5
}
}
val result = eval(JoeSchmoe.factTable)(likelyToJoinWeightloss)
Expand Down
Loading

0 comments on commit d7ede34

Please sign in to comment.