Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Easy Rules with Aviator integration. Aviator is wid… #424

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions easy-rules-aviator/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules</artifactId>
<version>4.1.1-SNAPSHOT</version>
</parent>

<artifactId>easy-rules-aviator</artifactId>
<packaging>jar</packaging>
<name>Easy Rules Aviator module</name>
<description>Aviator integration module</description>

<properties>
<aviator-version>5.4.1</aviator-version>
</properties>

<scm>
<url>[email protected]:j-easy/easy-rules.git</url>
<connection>scm:git:[email protected]:j-easy/easy-rules.git</connection>
<developerConnection>scm:git:[email protected]:j-easy/easy-rules.git</developerConnection>
<tag>HEAD</tag>
</scm>

<issueManagement>
<system>GitHub</system>
<url>https://github.com/j-easy/easy-rules/issues</url>
</issueManagement>

<ciManagement>
<system>Github Actions</system>
<url>https://github.com/j-easy/easy-rules/actions</url>
</ciManagement>

<developers>
<developer>
<id>benas</id>
<name>Mahmoud Ben Hassine</name>
<url>http://benas.github.io</url>
<email>[email protected]</email>
<roles>
<role>Lead developer</role>
</roles>
</developer>
</developers>

<licenses>
<license>
<name>MIT License</name>
<url>http://opensource.org/licenses/mit-license.php</url>
</license>
</licenses>

<dependencies>
<!-- production dependencies -->
<dependency>
<groupId>org.jeasy</groupId>
<artifactId>easy-rules-support</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.aviator</groupId>
<artifactId>aviator</artifactId>
<version>${aviator-version}</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<configuration>
<header>${project.parent.basedir}/licence-header-template.txt</header>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License
*
* Copyright (c) 2021, Mahmoud Ben Hassine ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jeasy.rules.aviator;

import com.googlecode.aviator.Expression;
import org.jeasy.rules.api.Action;
import org.jeasy.rules.api.Facts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.jeasy.rules.aviator.AviatorRule.DEFAULT_AVIATOR;

/**
* author peterxiemin([email protected])
*/
public class AviatorAction implements Action {

public static final Logger LOGGER = LoggerFactory.getLogger(AviatorAction.class.getName());
private final String expression;

private final Expression compileExpression;

/**
* Create a new {@link AviatorAction}.
*
* @param expression the action written in expression language
*/
public AviatorAction(String expression) {
this.expression = expression;
this.compileExpression = DEFAULT_AVIATOR.compile(expression);
}

@Override
public void execute(Facts facts) throws Exception {
try {
compileExpression.execute(facts.asMap());
} catch (Exception e) {
LOGGER.error("Unable to evaluate expression: '" + expression + "' on facts: " + facts);
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License
*
* Copyright (c) 2021, Mahmoud Ben Hassine ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jeasy.rules.aviator;

import com.googlecode.aviator.Expression;
import org.jeasy.rules.api.Condition;
import org.jeasy.rules.api.Facts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.jeasy.rules.aviator.AviatorRule.DEFAULT_AVIATOR;

/**
* author peterxiemin([email protected])
*/
public class AviatorCondition implements Condition {
private static final Logger LOGGER = LoggerFactory.getLogger(AviatorCondition.class.getName());

private final String expression;

private final Expression compileExpression;


/**
* Create a new {@link AviatorCondition}.
* @param expression
* the condition written in expression language
*/
public AviatorCondition(String expression) {
this.expression = expression;
this.compileExpression = DEFAULT_AVIATOR.compile(expression);
}

/**
* Evaluate the rule.
* @param facts known when evaluating the rule.
* @return true if the rule should be applied, false otherwise.
*/
@Override
public boolean evaluate(Facts facts) {
return (Boolean) compileExpression.execute(facts.asMap());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* The MIT License
*
* Copyright (c) 2021, Mahmoud Ben Hassine ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jeasy.rules.aviator;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.AviatorEvaluatorInstance;
import com.googlecode.aviator.runtime.JavaMethodReflectionFunctionMissing;
import org.jeasy.rules.api.Action;
import org.jeasy.rules.api.Condition;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.core.BasicRule;

import java.util.ArrayList;
import java.util.List;

/**
* author peterxiemin([email protected])
*/
public class AviatorRule extends BasicRule {
public static final AviatorEvaluatorInstance DEFAULT_AVIATOR;

static {
DEFAULT_AVIATOR = AviatorEvaluator.getInstance();
DEFAULT_AVIATOR.setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
}

private Condition condition = Condition.FALSE;

private final List<Action> actions = new ArrayList<>();

/**
* Create a new Aviator rule.
*/
public AviatorRule() {
super(Rule.DEFAULT_NAME, Rule.DEFAULT_DESCRIPTION, Rule.DEFAULT_PRIORITY);
}

/**
* set rule name
* @param name
* @return this rule
*/
public AviatorRule name(String name) {
this.name = name;
return this;
}

/**
* set rule description
* @param description
* @return this rule
*/
public AviatorRule description(String description) {
this.description = description;
return this;
}

/**
* set rule priority
* @param priority
* @return this rule
*/
public AviatorRule priority(int priority) {
this.priority = priority;
return this;
}

/**
* set rule condition
* @param condition
* @return this rule
*/
public AviatorRule when(String condition) {
this.condition = new AviatorCondition(condition);
return this;
}

/**
* set rule action
* @param action
* @return this rule
*/
public AviatorRule then(String action) {
actions.add(new AviatorAction(action));
return this;
}

/**
* evaluate the rule
* @param facts
* @return
*/
@Override
public boolean evaluate(Facts facts) {
return condition.evaluate(facts);
}

/**
* execute the rule
* @param facts
* @throws Exception
*/
@Override
public void execute(Facts facts) throws Exception {
for (Action action : actions) {
action.execute(facts);
}
}
}
Loading