Skip to content

Commit

Permalink
feat: modularize project (#22)
Browse files Browse the repository at this point in the history
* create module to allow using kotlin evaluator without importing rhino
libraries
* create module to contain jackson serialization
  • Loading branch information
rapatao authored Jun 5, 2024
1 parent 2301a46 commit 9f10714
Show file tree
Hide file tree
Showing 51 changed files with 324 additions and 285 deletions.
18 changes: 17 additions & 1 deletion JSON.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This file is automatically generated during the test phase.

To see more details, check its source: [here](src/test/kotlin/com/rapatao/projects/ruleset/SerializationExamplesBuilder.kt).
To see more details, check its source: [here](src/test/kotlin/com/rapatao/projects/ruleset/jackson/SerializationExamplesBuilder.kt).

* equalsTo:

Expand Down Expand Up @@ -1795,6 +1795,22 @@ To see more details, check its source: [here](src/test/kotlin/com/rapatao/projec
}
```

```json
{
"left" : "item.arrTags",
"operator" : "CONTAINS",
"right" : "\"in_array\""
}
```

```json
{
"left" : "item.arrTags",
"operator" : "CONTAINS",
"right" : "\"not_in_array\""
}
```

```json
{
"left" : "item.name",
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ Below are the available engines that can be used to evaluate expressions:

### Mozilla Rhino (JavaScript) engine implementation

[Rhino](https://github.com/mozilla/rhino) is an open-source, embeddable JavaScript interpreter from Mozilla.
This engine implementation supports using JavaScript expressions inside the rule operands.
[Mozilla Rhino](https://github.com/mozilla/rhino) is an open-source, embeddable JavaScript interpreter from Mozilla.
This engine implementation supports using JavaScript expressions inside the rule operands and is particularly useful
when rules contain complex logic or when you want to leverage JavaScript's extensive library of functions.

### Kotlin (internal) engine implementation

This engine only uses Kotlin code to support all Operator functions., supporting all Operator functions.
Although it provides an expressive performance, it doesn't support Kotlin expression into the expression operands.
This engine uses only Kotlin code to support all Operator functions, offering expressive performance. Although it
doesn't support Kotlin expressions inside the expression operands, it can be a suitable choice for simpler rule sets or
projects where you prefer using a statically-typed language like Kotlin.

Supported types:

Expand Down
154 changes: 63 additions & 91 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,84 +1,89 @@
import kotlinx.kover.gradle.plugin.dsl.MetricType

plugins {
id "java-library"
id "signing"
id 'maven-publish'
id "maven-publish"
id "org.jetbrains.kotlin.jvm" version "${kotlinVersion}"
id "io.gitlab.arturbosch.detekt" version "${detektVersion}"
id "org.jetbrains.kotlinx.kover" version "${koverVersion}"
id "io.github.gradle-nexus.publish-plugin" version "${publishPluginVersion}"
id "com.github.ben-manes.versions" version "${BenManerVersionsVersion}"
}

group "com.rapatao.ruleset"
allprojects {
apply plugin: "java-library"
apply plugin: "signing"
apply plugin: "maven-publish"
apply plugin: "org.jetbrains.kotlin.jvm"
apply plugin: "io.gitlab.arturbosch.detekt"
apply plugin: "org.jetbrains.kotlinx.kover"
// apply plugin: "io.github.gradle-nexus.publish-plugin"
apply plugin: "com.github.ben-manes.versions"

repositories {
mavenCentral()
}
group "com.rapatao.ruleset"

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
repositories {
mavenCentral()
}

implementation("org.mozilla:rhino:${rhinoVersion}")
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
}

compileOnly("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}")
compileOnly("com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonVersion}")
testImplementation("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}")
testImplementation("com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonVersion}")
test {
useJUnitPlatform()
}

testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation("org.junit.jupiter:junit-jupiter:${junitVersion}")
testImplementation("org.hamcrest:hamcrest:${hamcrestVersion}")
}
java {
withSourcesJar()
withJavadocJar()
toolchain {
languageVersion = JavaLanguageVersion.of(11)
}
}

test {
useJUnitPlatform()
}
detekt {
buildUponDefaultConfig = true
allRules = false
}

java {
withSourcesJar()
withJavadocJar()
toolchain {
languageVersion = JavaLanguageVersion.of(11)
signing {
useGpgCmd()
sign publishing.publications
}
}

detekt {
buildUponDefaultConfig = true
allRules = false
}
publishing {
publications {
maven(MavenPublication) {
from components.java

koverReport {
verify {
rule {
enabled = true
bound {
metric = MetricType.BRANCH
minValue = 80
}
bound {
metric = MetricType.LINE
minValue = 90
}
bound {
metric = MetricType.INSTRUCTION
minValue = 90
pom {
name = "RuleSet Engine"
description = "Simple yet powerful rules engine that offers the flexibility of using the built-in engine and creating a custom one."
url = "https://github.com/rapatao/ruleset-engine"
packaging = "jar"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "rapatao"
name = "Luiz Henrique Rapatao"
email = "rapatao@rapatao.com"
}
}
scm {
connection = "scm:git:git://github.com/rapatao/ruleset-engine.git"
developerConnection = "scm:git:ssh://github.com/rapatao/ruleset-engine.git"
url = "https://github.com/rapatao/ruleset-engine"
}
}
}
}
}

defaults {
html {
onCheck = true
}
}
}

signing {
useGpgCmd()
sign publishing.publications
}

nexusPublishing {
Expand All @@ -89,36 +94,3 @@ nexusPublishing {
}
}
}

publishing {
publications {
maven(MavenPublication) {
from components.java

pom {
name = "RuleSet Engine"
description = "A simple but fully customizable rule engine"
url = "https://github.com/rapatao/ruleset-engine"
packaging = "jar"
licenses {
license {
name = "The Apache License, Version 2.0"
url = "http://www.apache.org/licenses/LICENSE-2.0.txt"
}
}
developers {
developer {
id = "rapatao"
name = "Luiz Henrique Rapatao"
email = "rapatao@rapatao.com"
}
}
scm {
connection = "scm:git:git://github.com/rapatao/ruleset-engine.git"
developerConnection = "scm:git:ssh://github.com/rapatao/ruleset-engine.git"
url = "https://github.com/rapatao/ruleset-engine"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.rapatao.projects.ruleset.engine

import com.rapatao.projects.ruleset.engine.context.EvalContext
import com.rapatao.projects.ruleset.engine.context.EvalEngine
import com.rapatao.projects.ruleset.engine.evaluator.rhino.RhinoEvalEngine
import com.rapatao.projects.ruleset.engine.types.Expression
import com.rapatao.projects.ruleset.engine.types.OnFailure

Expand All @@ -12,7 +11,7 @@ import com.rapatao.projects.ruleset.engine.types.OnFailure
* @property engine The factory used to create a context for evaluating the expressions.
*/
class Evaluator(
private val engine: EvalEngine = RhinoEvalEngine(),
private val engine: EvalEngine,
) {

/**
Expand Down
9 changes: 9 additions & 0 deletions jackson/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {
implementation project(":core")

implementation("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonVersion}")

testImplementation project(":tests")
testImplementation project(":kotlin-evaluator")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rapatao.projects.ruleset
package com.rapatao.projects.ruleset.jackson

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
Expand All @@ -17,7 +17,6 @@ import com.rapatao.projects.ruleset.engine.types.builder.extensions.lessOrEqualT
import com.rapatao.projects.ruleset.engine.types.builder.extensions.lessThan
import com.rapatao.projects.ruleset.engine.types.builder.extensions.notEqualsTo
import com.rapatao.projects.ruleset.engine.types.builder.extensions.startsWith
import com.rapatao.projects.ruleset.jackson.ExpressionMixin
import org.junit.jupiter.api.Test
import java.nio.file.Path
import java.nio.file.Paths
Expand Down Expand Up @@ -138,7 +137,7 @@ internal class SerializationExamplesBuilder {
}

private fun createJsonMD(): Path {
val jsonMD = Paths.get("JSON.md")
val jsonMD = Paths.get("../JSON.md")

if (jsonMD.exists()) {
jsonMD.writeText("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.rapatao.projects.ruleset.engine
package com.rapatao.projects.ruleset.jackson

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.rapatao.projects.ruleset.engine.cases.TestData
import com.rapatao.projects.ruleset.engine.context.EvalEngine
import com.rapatao.projects.ruleset.engine.evaluator.kotlin.KotlinEvalEngine
import com.rapatao.projects.ruleset.engine.helper.Helper.compareMatcher
import com.rapatao.projects.ruleset.engine.helper.Helper.doEvaluationTest
import com.rapatao.projects.ruleset.engine.helper.Helper.mapper
import com.rapatao.projects.ruleset.engine.types.Expression
import com.rapatao.projects.ruleset.engine.types.OnFailure.THROW
import com.rapatao.projects.ruleset.engine.types.OnFailure.TRUE
Expand All @@ -17,21 +18,25 @@ import org.junit.jupiter.params.provider.MethodSource

class SerializationTest {

private val mapper = jacksonObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.addMixIn(Expression::class.java, ExpressionMixin::class.java)

companion object {
@JvmStatic
fun tests() = TestData.allCases()
}

@ParameterizedTest
@MethodSource("tests")
fun doSerializationTest(engine: EvalEngine, matcher: Expression, expected: Boolean) {
fun doSerializationTest(matcher: Expression, expected: Boolean) {
val json = mapper.writeValueAsString(matcher)

val matcherFromJson = mapper.readValue<Expression>(json)

compareMatcher(matcher, matcherFromJson)

doEvaluationTest(engine, matcherFromJson, expected)
doEvaluationTest(KotlinEvalEngine(), matcherFromJson, expected)
}

@Test
Expand Down
36 changes: 36 additions & 0 deletions kotlin-evaluator/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import kotlinx.kover.gradle.plugin.dsl.MetricType

dependencies {
api project(":core")

testImplementation project(":tests")
}

koverReport {
verify {
rule {
enabled = true
bound {
enabled = true
metric = MetricType.BRANCH
minValue = 70
}
bound {
enabled = true
metric = MetricType.LINE
minValue = 90
}
bound {
enabled = true
metric = MetricType.INSTRUCTION
minValue = 90
}
}
}

defaults {
html {
onCheck = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ open class KotlinEvalEngine : EvalEngine {
}

input is Array<*> -> {
parseKeys(node, params, input)
params[node] = input

input.forEachIndexed { index, value ->
parseKeys("${node}[${index}]", params, value)
}
}

input is Map<*, *> -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.rapatao.projects.ruleset.engine.evaluator.kotlin

import com.rapatao.projects.ruleset.engine.BaseEngineBenchmark

fun main(args: Array<String>) {
BaseEngineBenchmark(KotlinEvalEngine()).main(args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.rapatao.projects.ruleset.engine.evaluator.kotlin

import com.rapatao.projects.ruleset.engine.BaseEvaluatorTest

class KotlinEvalEngineTest : BaseEvaluatorTest(KotlinEvalEngine())
Loading

0 comments on commit 9f10714

Please sign in to comment.