Code Monkey home page Code Monkey logo

json-rules-engine's Introduction

json-rules-engine

Java 实现简单的 json-rules-engine,灵感来源于 json-rules-engine

基本用法

构建规则 JsonRule

JsonRule 包含两个部分,condition 以及 response。你可以通过如下两种方式构建 JsonRule:

  • 通过构造方法 new JsonRule(Condition condition, Object response)
  • 通过json string 构造 JsonRuleEngine.parseOne(String jsonString),推荐使用。

json string 示例如下:

{
    "condition":{
        "ANY":[
            {
                "ALL":[
                    {
                        "key":"gameDuration",
                        "operator":"EQ",
                        "value":40
                    },
                    {
                        "key":"personalFoulCount",
                        "operator":"EQ",
                        "value":5
                    }
                ]
            },
            {
                "ALL":[
                    {
                        "key":"gameDuration",
                        "operator":"EQ",
                        "value":5
                    },
                    {
                        "key":"personalFoulCount",
                        "operator":"EQ",
                        "value":40
                    }
                ]
            }
        ]
    },
    "response":"xx"
}

实际值 Fact

包含两个字段 key 和 value,与 condition 中 key/value 比较。

支持操作

  • ANY 或
  • ALL 与
  • EQ 相等,使用 equals 判断
  • ALL_IN 实际值全部包含在条件值内,两者必须都是 Collection
  • ANY_IN 实际值任意一个包含在条件值内,两者必须都是 Collection

简单示例

boolean match = JsonRuleEngine.parseOne("{\n" +
                "    \"condition\":{\n" +
                "        \"ANY\":[\n" +
                "            {\n" +
                "                \"ALL\":[\n" +
                "                    {\n" +
                "                        \"key\":\"gameDuration\",\n" +
                "                        \"operator\":\"EQ\",\n" +
                "                        \"value\":40\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"key\":\"personalFoulCount\",\n" +
                "                        \"operator\":\"EQ\",\n" +
                "                        \"value\":5\n" +
                "                    }\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"ALL\":[\n" +
                "                    {\n" +
                "                        \"key\":\"gameDuration\",\n" +
                "                        \"operator\":\"EQ\",\n" +
                "                        \"value\":5\n" +
                "                    },\n" +
                "                    {\n" +
                "                        \"key\":\"personalFoulCount\",\n" +
                "                        \"operator\":\"EQ\",\n" +
                "                        \"value\":40\n" +
                "                    }\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"response\":\"xx\"\n" +
                "}").match(Arrays.asList(new Fact("gameDuration", 5), new Fact("personalFoulCount", 40)));
        Assert.assertTrue(match);

json-rules-engine's People

Contributors

schhx avatar

Stargazers

 avatar  avatar  avatar Cathy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.