Skip to content

Commit

Permalink
feat: add multiple projects (#35)
Browse files Browse the repository at this point in the history
satvik-s authored Nov 4, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3dddb58 commit 7ab3962
Showing 36 changed files with 294 additions and 193 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-and-publish-kotlin.yml
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ jobs:
- name: Get release tag
id: get-release-tag
shell: bash
run: echo "::set-output name=release-tag::v$(grep "version =" build.gradle.kts | cut -d'"' -f2)"
run: echo "::set-output name=release-tag::v$(grep "packageVersionId=" gradle.properties | cut -d '=' -f 2)"
- name: Create GitHub release
shell: bash
run: gh release create $RELEASE_TAG-kotlin
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -37,3 +37,6 @@ You can pass in the token during CI like the following:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
For more information about using GitHub Packages registry, go
[here](https://docs.github.com/en/packages/working-with-a-github-packages-registry)
6 changes: 6 additions & 0 deletions kotlin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
*.bat text eol=crlf

13 changes: 11 additions & 2 deletions kotlin/README.md
Original file line number Diff line number Diff line change
@@ -37,8 +37,17 @@ If using Gradle (kotlin DSL), add the following to the `repositories`:
}
```

For more information about using GitHub Packages registry, go
[here](https://docs.github.com/en/packages/working-with-a-github-packages-registry)
Then, add the following to `dependencies`:

```kotlin
implementation("com.warnermedia.rulesengine:rulesengine-core:<version>")
```

To allow for JSON persistence capability, add the following in addition to the above:

```kotlin
implementation("com.warnermedia.rulesengine:rulesengine-jsonadapter:<version>")
```

## Initial Contributors

45 changes: 0 additions & 45 deletions kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,45 +0,0 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.20"
`maven-publish`
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.4")
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}

publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/warnermedia/rules-engine")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
publications {
create<MavenPublication>("maven") {
groupId = "com.warnermedia"
artifactId = "rulesengine"
version = "1.8.1"

from(components["java"])
}
}
}
18 changes: 18 additions & 0 deletions kotlin/buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.20"
`kotlin-dsl`
}

repositories {
gradlePluginPortal()
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin")
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
`maven-publish`
`java-library`
}

repositories {
mavenCentral()
}

dependencies {
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "17"
}

publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/warnermedia/rules-engine")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
}
}
18 changes: 18 additions & 0 deletions kotlin/core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id("rulesengine.kotlin-library-conventions")
}

val packageGroupId: String by project
val packageVersionId: String by project

publishing {
publications {
create<MavenPublication>("maven") {
groupId = packageGroupId
artifactId = "rulesengine-core"
version = packageVersionId

from(components["java"])
}
}
}
1 change: 1 addition & 0 deletions kotlin/core/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "core"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining results from boolean operations like equals, greater than, etc.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining options passed during evaluation of a condition
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Enum defining possible ways of joining multiple conditions within a rule
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining the possible outputs of a condition evaluation
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
package com.warnermedia.rulesengine

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.io.File
package core

/**
* Class defining a rules engine instance with an input rule set and other options
@@ -21,14 +18,6 @@ class Engine(val id: String, rules: ArrayList<Rule>, val options: EngineOptions
)
}

fun saveToFile(path: String) {
File(path).writeText(this.toString(), Charsets.UTF_8)
}

override fun toString(): String {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this)
}

private fun Iterable<Rule>.evaluateEngineRulesLatestInclusive(
facts: HashMap<String, Any?>, engineOptions: EngineOptions
): Pair<ArrayList<RuleResult>, Boolean> {
@@ -52,17 +41,4 @@ class Engine(val id: String, rules: ArrayList<Rule>, val options: EngineOptions
}
return Pair(list, false)
}

companion object {
val mapper = jacksonObjectMapper()

fun readFromFile(path: String): Engine {
val engineStringRepresentation = File(path).readText(Charsets.UTF_8)
return readFromString(engineStringRepresentation)
}

fun readFromString(engineStringRepresentation: String): Engine {
return mapper.readValue(engineStringRepresentation, Engine::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Enum defining different ways an engine will evaluate the rule set
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining properties for a rules engine instance.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining result of an engine evaluation of the rule set
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining possible exit criteria for an engine evaluation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining an operation type along with the value to perform it against
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Enum defining possible operators supported to use when creating a condition
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

import java.time.Instant

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining options passed to a Rule during runtime evaluation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining set of options to change behavior of a rule
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Class defining the possible outputs of a rule evaluation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Enum class defining possible reasons to skip runtime evaluation
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

/**
* Enum defining how to evaluate a runtime fact that is not defined
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.warnermedia.rulesengine
package core

import kotlin.test.Test
import kotlin.test.assertEquals
@@ -35,16 +35,4 @@ internal class EngineTest {
result.ruleEvaluations,
)
}

@Test
fun testPersistence() {
engine.saveToFile("engine.json")

val engineFromFile = Engine.readFromFile("engine.json")
val result = engineFromFile.evaluate(hashMapOf("temperature" to 75, "rainfall" to 0))
assertEquals<ArrayList<RuleResult>>(
arrayListOf(RuleResult.Success("good-weather", "good-weather-day")),
result.ruleEvaluations,
)
}
}
2 changes: 2 additions & 0 deletions kotlin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
kotlin.code.style=official
packageGroupId=com.warnermedia.rulesengine
packageVersionId=0.0.1
Binary file modified kotlin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion kotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStorePath=wrapper/dists
178 changes: 89 additions & 89 deletions kotlin/gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar


@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
27 changes: 27 additions & 0 deletions kotlin/json-adapter/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id("rulesengine.kotlin-library-conventions")
}

repositories {
mavenCentral()
}

dependencies {
implementation(project(":core"))
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.4")
}

val packageGroupId: String by project
val packageVersionId: String by project

publishing {
publications {
create<MavenPublication>("maven") {
groupId = packageGroupId
artifactId = "rulesengine-jsonadapter"
version = packageVersionId

from(components["java"])
}
}
}
1 change: 1 addition & 0 deletions kotlin/json-adapter/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "json-adapter"
28 changes: 28 additions & 0 deletions kotlin/json-adapter/src/main/kotlin/jsonadapter/Adapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package jsonadapter

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import core.Engine
import java.io.File

class Adapter {
companion object {
private val mapper = jacksonObjectMapper()

fun readFromFile(path: String): Engine {
val engineStringRepresentation = File(path).readText(Charsets.UTF_8)
return readFromString(engineStringRepresentation)
}

fun saveToFile(engine: Engine, path: String) {
File(path).writeText(engineToString(engine), Charsets.UTF_8)
}

private fun engineToString(engine: Engine): String {
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(engine)
}

private fun readFromString(engineStringRepresentation: String): Engine {
return mapper.readValue(engineStringRepresentation, Engine::class.java)
}
}
}
32 changes: 32 additions & 0 deletions kotlin/json-adapter/src/test/kotlin/jsonadapter/AdapterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package jsonadapter

import core.*
import kotlin.test.Test
import kotlin.test.assertEquals

internal class AdapterTest {
private val engine = Engine(
"weather-type-engine",
arrayListOf(
Rule(
"good-weather",
arrayListOf(
Condition("temperature", Operator(OperatorType.GREATER_THAN, 70)),
Condition("rainfall", Operator(OperatorType.EQUALS, 0)),
),
Pair("good-weather-day", "work-from-home-day"),
),
),
)

@Test
fun testPersistence() {
Adapter.saveToFile(engine, "engine.json")
val engineFromFile = Adapter.readFromFile("engine.json")
val result = engineFromFile.evaluate(hashMapOf("temperature" to 75, "rainfall" to 0))
assertEquals<ArrayList<RuleResult>>(
arrayListOf(RuleResult.Success("good-weather", "good-weather-day")),
result.ruleEvaluations,
)
}
}
3 changes: 2 additions & 1 deletion kotlin/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rootProject.name = "rules-engine"
rootProject.name = "rulesengine"
include("core", "json-adapter")

0 comments on commit 7ab3962

Please sign in to comment.