Jess Information

Jess Home
Jess 7 Features
Download Now!
Online Demo

Documentation
FAQ
Manual
Mailing List
Jess Wiki

More information Related Web Sites
User Contributions
JSR94 Info
Developer's Log
About This Site

JESS ®, the Rule Engine for the JavaTM Platform

Jess Wiki: Facts Vs Objects

This has been stated in the Jess manual, JIA, and every expert system book youíll ever pick up, but, for new Jess developers, it bears repeating. Facts are fundamentally different than objects in Java. They are not merely units of data. They are statements of truth. They inform your expert system about what is true about the world. If you donít respect this, it will come back to bite you.

For example, you may have a Java class that represents a coordinate in a two-dimensional cartesian plane with members called "X" and "Y":


public class Coordinate {
    public Coordinate(double x, double y) {
        setX(x);
        setY(y);
    }

    private double x;
    public double getX();
    public void setX(double x) { this.= x; }

    private double y;
    public double getY();
    public void setY(double y) { this.= y; }

    public String toString() {
        return "(" + getX() + "," + getY() + ")";
    }
}

In Java, you can create two Coordinate objects with the same values of X and Y:

    Coordinate coord1 = new Coordinate(2.0, 5.0);
    Coordinate coord2 = new Coordinate(2.0, 5.0);

This will produce two distinct objects in memory that may be changed and managed independently of each other:

    coord1.setX(10.0);
    coord2.setY(3.0);
    System.out.println(coord1);
    System.out.println(coord2);

Output:

   (10.0, 5.0)
   (2.0, 3.0)

Now, consider doing the same thing in Jess. First, we define the template:

    (deftemplate coordinate
        (slot X)
        (slot Y))
Then we try to assert two facts with the same X and Y values:
    Jess> (assert (coordinate (X 2.0) (Y 5.0)))
    <Fact-1>
    Jess> (assert (coordinate (X 2.0) (Y 5.0)))
    FALSE
Why did the second assertion return FALSE? Letís see whatís in working memory:
    Jess> (facts)
    f-0   (MAIN::initial-fact)
    f-1   (MAIN::coordinate (X 2.0) (Y 5.0))
    For a total of 2 facts.
Only one of our coordinate facts where asserted. Why?

Like I said above, facts are statements of truth, not merely data. In the Java example, when we created the two coordinate objects we were making two independent statements:

  • Coordinate 1 has values X = 2.0 and Y = 5.0
  • Coordinate 2 has values X = 2.0 and Y = 5.0

In the Jess example, we were saying something quite different. The first assertion says:

  • There exists a coordinate at X = 2.0 and Y = 5.0

The second assertion is saying exactly the same thing. The Jess system recognizes this and fails replying with FALSE, which is Jessí way of saying, "I already knew that".

If you're using shadow facts, you'll also want to read the FactsVsShadowFacts Wiki.

Submitted by:
GeorgeWilliamson
Union Pacific Railroad
gawillia@up.com



FrontPage, JavaPitfalls


Front Page | Sandbox | Recent Changes | Powered by Friki | Last Edited: 22 September 2008