Expert System Shell

Expert system shell implemented in Java

License

License

MIT
Categories

Categories

Java Languages
GroupId

GroupId

com.github.cschen1205
ArtifactId

ArtifactId

java-expert-system-shell
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Expert System Shell
Expert system shell implemented in Java
Project URL

Project URL

https://github.com/cschen1205/java-expert-system-shell
Source Code Management

Source Code Management

https://github.com/cschen1205/java-expert-system-shell

Download java-expert-system-shell

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.cschen1205/java-expert-system-shell/ -->
<dependency>
    <groupId>com.github.cschen1205</groupId>
    <artifactId>java-expert-system-shell</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.cschen1205/java-expert-system-shell/
implementation 'com.github.cschen1205:java-expert-system-shell:1.0.1'
// https://jarcasting.com/artifacts/com.github.cschen1205/java-expert-system-shell/
implementation ("com.github.cschen1205:java-expert-system-shell:1.0.1")
'com.github.cschen1205:java-expert-system-shell:jar:1.0.1'
<dependency org="com.github.cschen1205" name="java-expert-system-shell" rev="1.0.1">
  <artifact name="java-expert-system-shell" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.cschen1205', module='java-expert-system-shell', version='1.0.1')
)
libraryDependencies += "com.github.cschen1205" % "java-expert-system-shell" % "1.0.1"
[com.github.cschen1205/java-expert-system-shell "1.0.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.20
org.slf4j : slf4j-log4j12 jar 1.7.20

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.6

test (10)

Group / Artifact Type Version
org.testng : testng jar 6.9.10
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3
org.assertj : assertj-core jar 3.5.2
org.powermock : powermock-core jar 1.6.5
org.powermock : powermock-api-mockito jar 1.6.5
org.powermock : powermock-module-junit4 jar 1.6.5
org.powermock : powermock-module-testng jar 1.6.5
org.mockito : mockito-core jar 2.0.2-beta
org.mockito : mockito-all jar 2.0.2-beta

Project Modules

There are no modules declared in this project.

Expert System Shell (Java)

A simple and user-friendly expert system shell implemented in Java. The rule engine also support rule files written in Javascript.

Note that this expert system shell do not require external dependencies for its logics

Build Status Coverage Status

Features

  • Forward Rule Chaining
  • Backward Rule Chaining
  • Backward Rule Chaining with Prompt
  • Support rules file written in Javascript

Install

Add the following dependency into your POM file:

<dependency>
  <groupId>com.github.cschen1205</groupId>
  <artifactId>java-expert-system-shell</artifactId>
  <version>1.0.1</version>
</dependency>

Usage

Add rules and initialize the rule engine

Below is an example to create a rule engine from scratch with a set of rules in java

private RuleInferenceEngine getInferenceEngine()
{
    RuleInferenceEngine rie=new KieRuleInferenceEngine();

    Rule rule=new Rule("Bicycle");
    rule.addAntecedent(new EqualsClause("vehicleType", "cycle"));
    rule.addAntecedent(new EqualsClause("num_wheels", "2"));
    rule.addAntecedent(new EqualsClause("motor", "no"));
    rule.setConsequent(new EqualsClause("vehicle", "Bicycle"));
    rie.addRule(rule);

    rule=new Rule("Tricycle");
    rule.addAntecedent(new EqualsClause("vehicleType", "cycle"));
    rule.addAntecedent(new EqualsClause("num_wheels", "3"));
    rule.addAntecedent(new EqualsClause("motor", "no"));
    rule.setConsequent(new EqualsClause("vehicle", "Tricycle"));
    rie.addRule(rule);

    rule=new Rule("Motorcycle");
    rule.addAntecedent(new EqualsClause("vehicleType", "cycle"));
    rule.addAntecedent(new EqualsClause("num_wheels", "2"));
    rule.addAntecedent(new EqualsClause("motor", "yes"));
    rule.setConsequent(new EqualsClause("vehicle", "Motorcycle"));
    rie.addRule(rule);

    rule=new Rule("SportsCar");
    rule.addAntecedent(new EqualsClause("vehicleType", "automobile"));
    rule.addAntecedent(new EqualsClause("size", "medium"));
    rule.addAntecedent(new EqualsClause("num_doors", "2"));
    rule.setConsequent(new EqualsClause("vehicle", "Sports_Car"));
    rie.addRule(rule);

    rule=new Rule("Sedan");
    rule.addAntecedent(new EqualsClause("vehicleType", "automobile"));
    rule.addAntecedent(new EqualsClause("size", "medium"));
    rule.addAntecedent(new EqualsClause("num_doors", "4"));
    rule.setConsequent(new EqualsClause("vehicle", "Sedan"));
    rie.addRule(rule);

    rule=new Rule("MiniVan");
    rule.addAntecedent(new EqualsClause("vehicleType", "automobile"));
    rule.addAntecedent(new EqualsClause("size", "medium"));
    rule.addAntecedent(new EqualsClause("num_doors", "3"));
    rule.setConsequent(new EqualsClause("vehicle", "MiniVan"));
    rie.addRule(rule);

    rule=new Rule("SUV");
    rule.addAntecedent(new EqualsClause("vehicleType", "automobile"));
    rule.addAntecedent(new EqualsClause("size", "large"));
    rule.addAntecedent(new EqualsClause("num_doors", "4"));
    rule.setConsequent(new EqualsClause("vehicle", "SUV"));
    rie.addRule(rule);

    rule=new Rule("Cycle");
    rule.addAntecedent(new LessClause("num_wheels", "4"));
    rule.setConsequent(new EqualsClause("vehicleType", "cycle"));
    rie.addRule(rule);

    rule=new Rule("Automobile");
    rule.addAntecedent(new EqualsClause("num_wheels", "4"));
    rule.addAntecedent(new EqualsClause("motor", "yes"));
    rule.setConsequent(new EqualsClause("vehicleType", "automobile"));
    rie.addRule(rule);

    return rie;
}

Infer more facts using forward chaining

public void testForwardChain()
{
    RuleInferenceEngine rie=getInferenceEngine();
    rie.addFact(new EqualsClause("num_wheels", "4"));
    rie.addFact(new EqualsClause("motor", "yes"));
    rie.addFact(new EqualsClause("num_doors", "3"));
    rie.addFact(new EqualsClause("size", "medium"));

    System.out.println("before inference");
    System.out.println(rie.getFacts());
    System.out.println();

    rie.infer(); //forward chain

    System.out.println("after inference");
    System.out.println(rie.getFacts());
    System.out.println();
}

Search for answer to a question using backward chaining

public void testBackwardChain()
{
    RuleInferenceEngine rie=getInferenceEngine();
    rie.addFact(new EqualsClause("num_wheels", "4"));
    rie.addFact(new EqualsClause("motor", "yes"));
    rie.addFact(new EqualsClause("num_doors", "3"));
    rie.addFact(new EqualsClause("size", "medium"));

    System.out.println("Infer: vehicle");

    Vector<Clause> unproved_conditions= new Vector<>();

    Clause conclusion=rie.infer("vehicle", unproved_conditions);

    System.out.println("Conclusion: "+conclusion);
}

Ask more questions when no sufficient facts are present

public void demoBackwardChainWithNullMemory()
{
    RuleInferenceEngine rie=getInferenceEngine();

    System.out.println("Infer with All Facts Cleared:");
    rie.clearFacts();

    Vector<Clause> unproved_conditions= new Vector<>();

    Clause conclusion=null;
    while(conclusion==null)
    {
        conclusion=rie.infer("vehicle", unproved_conditions);
        if(conclusion==null)
        {
            if(unproved_conditions.size()==0)
            {
                break;
            }
            Clause c=unproved_conditions.get(0);
            System.out.println("ask: "+c+"?");
            unproved_conditions.clear();
            String value=showInputDialog("What is "+c.getVariable()+"?");
            rie.addFact(new EqualsClause(c.getVariable(), value));
        }
    }

    System.out.println("Conclusion: "+conclusion);
    System.out.println("Memory: ");
    System.out.println(rie.getFacts());
}

private String showInputDialog(String question) {
    Scanner scanner = new Scanner(System.in);
    System.out.print(question + " ");
    return scanner.next();
}

Running rule engine using rules defined in a Javascript

Below is an example of a rules file written in Javascript (vehicle-rules.js)

expert.newRule("Bicycle")
    .ifEquals("vehicleType", "cycle")
    .andEquals("num_wheels", 2)
    .andEquals("motor", "no")
    .thenEquals("vehicle", "Bicycle")
    .build();

expert.newRule("Tricycle")
    .ifEquals("vehicleType", "cycle")
    .andEquals("num_wheels", 3)
    .andEquals("motor", "no")
    .thenEquals("vehicle", "Tricycle")
    .build();

expert.newRule("Motorcycle")
    .ifEquals("vehicleType", "cycle")
    .andEquals("num_wheels", 2)
    .andEquals("motor", "yes")
    .thenEquals("vehicle", "Motorcycle")
    .build();

expert.newRule("SportsCar")
    .ifEquals("vehicleType", "automobile")
    .andEquals("size", "medium")
    .andEquals("num_doors", 2)
    .thenEquals("vehicle", "Sports_Car")
    .build();

expert.newRule("Sedan")
    .ifEquals("vehicleType", "automobile")
    .andEquals("size", "medium")
    .andEquals("num_doors", 4)
    .thenEquals("vehicle", "Sedan")
    .build();

expert.newRule("MiniVan")
    .ifEquals("vehicleType", "automobile")
    .andEquals("size", "medium")
    .andEquals("num_doors", 3)
    .thenEquals("vehicle", "MiniVan")
    .build();

expert.newRule("SUV")
    .ifEquals("vehicleType", "automobile")
    .andEquals("size", "large")
    .andEquals("num_doors", 4)
    .thenEquals("vehicle", "SUV")
    .build();

expert.newRule("Cycle")
    .ifLess("num_wheels", 4)
    .thenEquals("vehicleType", "cycle")
    .build();

expert.newRule("Automobile")
    .ifEquals("num_wheels", 4)
    .andEquals("motor", "yes")
    .thenEquals("vehicleType", "automobile")
    .build();

The rule engine can then load these rules into its shell and run:

JSRuleInferenceEngine engine = new JSRuleInferenceEngine();
String jsContent = readToEnd("/vehicle-rules.js");
engine.loadString(jsContent);
engine.buildRules();

engine.clearFacts();

engine.addFact("num_wheels", "4");
engine.addFact("motor", "yes");
engine.addFact("num_doors", "3");
engine.addFact("size", "medium");



System.out.println("before inference");
System.out.println(engine.getKnowledgeBase());
System.out.println();



engine.infer(); //forward chain

System.out.println("after inference");
System.out.println(engine.getKnowledgeBase());
System.out.println();

Versions

Version
1.0.1