Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from scala-rules/feature/fix-field-test-issues
Browse files Browse the repository at this point in the history
Fix a type conversion bug and the inclusion of sub-derivations in group `all`
  • Loading branch information
NRBPerdijk committed Dec 21, 2016
2 parents 9bc9966 + 76d79bc commit 8c42395
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/RestController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class RestController @Inject() (derivationsService: DerivationsService, glossari

private def processConvertedContext(initialContextFragments: List[JsSuccess[Context]], jsonResponse: ResponseJsObject): JsObject = {
val initialContext: Context = initialContextFragments.foldLeft(Map.empty[Fact[Any], Any])((acc, jsSuccess) => acc ++ jsSuccess.get)
val resultContext: Context = RulesRunner.run(initialContext, derivationsService.derivations)
val resultContext: Context = RulesRunner.run(initialContext, derivationsService.topLevelDerivations)

jsonResponse.toJson(initialContext = initialContext, resultContext = resultContext, jsonConversionMap)
}
Expand Down
23 changes: 21 additions & 2 deletions app/controllers/conversion/Conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import org.scalarules.finance.nl._
import play.api.data.validation.ValidationError
import play.api.libs.json._

import scala.reflect.runtime.universe._

trait JsonConversionsProvider {
def contextToJsonConversions: Map[Class[_], (Fact[Any], Any) => JsObject]
def jsonToFactConversions: Map[String, ConvertToFunc]
Expand All @@ -20,7 +22,9 @@ object DefaultJsonConversion extends JsonConversionsProvider {
classOf[String] -> { contextStringToJsObject(_, _) },
classOf[Bedrag] -> { contextBedragToJsObject(_, _) },
classOf[Percentage] -> { contextPercentageToJsObject(_, _) },
classOf[BigDecimal] -> { contextBigDecimalToJsObject(_, _) }
classOf[BigDecimal] -> { contextBigDecimalToJsObject(_, _) },
classOf[Boolean] -> { contextBooleanToJsObject(_, _) },
classOf[java.lang.Boolean] -> { contextBooleanToJsObject(_, _) }
)

private def contextStringToJsObject(fact: Fact[Any], factValue: Any): JsObject = factValue match {
Expand All @@ -43,14 +47,24 @@ object DefaultJsonConversion extends JsonConversionsProvider {
case _ => throw new IllegalArgumentException
}

private def contextBooleanToJsObject(fact: Fact[Any], factValue: Any): JsObject = factValue match {
case bool: Boolean => JsObject(Map(fact.name -> JsBoolean(bool)))
case bool: java.lang.Boolean => JsObject(Map(fact.name -> JsBoolean(bool)))
case _ => throw new IllegalArgumentException
}

}

object JsonToFactConversionMap {
val jsonToFactConversionMap: Map[String, ConvertToFunc] = Map[String, ConvertToFunc](
classOf[String].getTypeName -> { stringFunct(_, _) },
weakTypeOf[String].toString -> { stringFunct(_, _) },
classOf[Bedrag].getTypeName -> { bedragFunct(_, _) },
classOf[Percentage].getTypeName -> { percentageFunct(_, _) },
classOf[BigDecimal].getTypeName -> { bigDecimalFunct(_, _) }
classOf[BigDecimal].getTypeName -> { bigDecimalFunct(_, _) },
weakTypeOf[BigDecimal].toString -> { bigDecimalFunct(_, _) },
classOf[Boolean].getTypeName -> { booleanFunct(_, _) },
weakTypeOf[Boolean].toString -> { booleanFunct(_, _) }
)

private def stringFunct(fact: Fact[Any], factValue: JsValue): JsResult[String] = factValue match {
Expand All @@ -73,6 +87,11 @@ object DefaultJsonConversion extends JsonConversionsProvider {
case _ => JsError(ValidationError(s"Conversion for Percentage fact ${fact.name} failed, corresponding value was not of expected type JsNumber"))
}

private def booleanFunct(fact: Fact[Any], factValue: JsValue): JsResult[Boolean] = factValue match {
case jsBoolean: JsBoolean => JsSuccess(jsBoolean.value)
case _ => JsError(ValidationError(s"Conversion for String fact ${fact.name} failed, corresponding value was not of expected type JsBoolean"))
}

}

}
Expand Down
17 changes: 13 additions & 4 deletions app/services/DerivationsService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package services
import javax.inject.{Inject, Singleton}

import org.scalarules.derivations.Derivation
import play.api.Configuration
import org.scalarules.dsl.nl.grammar.{Berekening, ElementBerekening}
import play.api.{Configuration, Logger}

@Singleton
class DerivationsService @Inject() (configuration: Configuration, jarLoaderService: JarLoaderService) {

val derivations: List[Derivation] = jarLoaderService.jars.flatMap(jarEntry => {
jarEntry._2.derivations.map( d => d.berekeningen)
}).toList.flatten
private val elementBerekeningClass = classOf[ElementBerekening[Any, Any]]

val topLevelDerivations: List[Derivation] = {
val topLevelDerivationsWithMeta = jarLoaderService.jars.flatMap(
jarEntry => { jarEntry._2.derivations.filterNot( d => elementBerekeningClass.isAssignableFrom( d.getClass ))}
).toList

Logger.info(s"Detected the following top-level derivations: [${topLevelDerivationsWithMeta.map( _.getClass.getName ).mkString(", ")}]")

topLevelDerivationsWithMeta.flatMap( _.berekeningen )
}

}
4 changes: 3 additions & 1 deletion app/services/GlossariesService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import javax.inject.{Inject, Singleton}

import org.scalarules.facts.Fact
import org.scalarules.utils.Glossary
import play.api.Configuration
import play.api.{Configuration, Logger}

@Singleton
class GlossariesService @Inject()(configuration: Configuration, jarLoaderService: JarLoaderService) {
Expand All @@ -13,6 +13,8 @@ class GlossariesService @Inject()(configuration: Configuration, jarLoaderService
jarEntry._2.glossaries.map( g => (g.getClass.getName, g) )
})

Logger.info(s"Detected the following Glossaries: [${glossaries.keys.mkString(", ")}]")

val mergedGlossaries : Map[String, Fact[Any]] = glossaries.values.foldLeft(Map.empty[String, Fact[Any]])((acc, glossary) => acc ++ glossary.facts)

def findById(id: String): Option[Glossary] = glossaries.get(id)
Expand Down
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lazy val commonSettings = Seq(
organization := "org.scala-rules",
organizationHomepage := Some(url("https://github.com/scala-rules")),
homepage := Some(url("https://github.com/scala-rules/rule-rest")),
version := "0.0.4-SNAPSHOT",
version := "0.0.5-SNAPSHOT",
scalaVersion := "2.11.8",
scalacOptions ++= Seq("-deprecation", "-unchecked", "-feature", "-Xlint", "-Xfatal-warnings")
) ++ staticAnalysisSettings ++ publishSettings
Expand Down Expand Up @@ -62,6 +62,7 @@ lazy val staticAnalysisSettings = {
}

addCommandAlias("verify", ";compileScalastyle;testScalastyle;coverage;test;coverageReport;coverageAggregate")
addCommandAlias("release", ";clean;compile;publishLocal;publishSigned")

// *** Publishing ***

Expand Down

0 comments on commit 8c42395

Please sign in to comment.