Giter Site home page Giter Site logo

java-rule-engine's Introduction

java-rule-engine: Rule Engine Libraries for Java

Java-rule-engine is a simple java based rule engine libraries that supports creating rule with conditions and actions, also comes with a native rule engine to execute rules.

Dependencies

Example

Define conditions

    Condition male = new SimpleCondition<>("gender", "male");
    Condition female = new SimpleCondition<>("gender", "female");

    Condition adult = new RangeCondition<Integer>("age", 18, 100);
    Condition notAdult = NotCondition.reverse(adult);

    Condition maleAdult = ConditionGroup.all(male, adult);
    Condition femaleAdult = ConditionGroup.all(female, adult);
    Condition maleNotAdult = ConditionGroup.all(male, notAdult);
    Condition adultMaleOrFemale = ConditionGroup.all(ConditionGroup.any(male, female), adult);

Define actions

    Action<Fact> print = f -> System.out.println("rule fired");
    Action<Fact> printprint = ActionChain.all(Arrays.asList(print, print));

Define rules

    Rule<Fact> printWhenMaleNotAdult = new BaseRule<>()
            .setName("print when male not adult")
            .setPriority(1)
            .setExclusive(false)
            .given(maleNotAdult)
            .then(printprint);

    Rule<Fact> printForFemaleAdult = new BaseRule<>()
            .setName("print for female adult")
            .setPriority(2)
            .setExclusive(false)
            .given(femaleAdult)
            .then(print);

Define fact and run rules

    RuleEngine ruleEngine = new NativeRuleEngineImpl();

    List<KVFactPair> facts = new ArrayList<>();
    facts.add(new KVFactPair("gender", "male"));
    facts.add(new KVFactPair("age", 20));

    Fact fact = new DefaultKVFact(new KVFactPairs(facts));
    ruleEngine.runRules(Arrays.asList(printWhenMaleNotAdult, printForFemaleAdult), fact);

java-rule-engine's People

Contributors

jefffang 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.