More Basic Setup

Custom JESS REPL

If you want to use a custom JESS REPL with Emacs then set up a shell script
jsjs-repl like this:

#!/bin/bash
mvn --file /home/gcr/git/jess-sample-java-setup/jsjs/pom.xml compile -Pjess-repl

And then make this call in EMACS before calling run-jess:

(setq inferior-jess-program "/home/gcr/git/jess-sample-java-setup/jsjs/jsjs-repl")

More Background

My background is custom software development in a consulting and stuff sup role. My work has been primarily in insurance, manufacturing, and the financial industries with clients ranging from large to small. Tech/training wise I studied mscs, computing, and computational sciences at MU; and have practiced software engineering at my day job for the past eleven years, and I mean practice, it is something I am constantly trying to get better at. Tech wise I am a pretty typical GNU/Linux & Windows person more or less, doing Java, .NET, Lisp, and recently Python.

A commonality among nearly every project I’ve worked on is the eventual occurrence of if statements 5-levels deep repeated across multiple call-sites (40+ typically) through no want/action/desire of my own. When I look at stuff like this, I feel like in addition to refactoring (regardless of the system type) there might be a better way. From what I’ve learned so far, rules engines seem like they might be a good fit.

Part of my goal here was to find a tool that not only provided a rules engine, but also helps the user to understand and debug what is happening, when, how, and why, with the intent that the developer implementing the rules can do it in a non-arcane manner and also share the behavior of the engine with business-related users to help them down the path of thinking in that manner.

jess-mode Cheat Sheet

Before tackling the CLIPS literature I felt like I ought to ramp up on the key-bindings for jess-mode in EMACS. Here are some good ones, you will find that they are basically what you would expect in the grouping that I view them in order to help the development process:

Comprehension:

  • \C-c,f ‘inf-jess-get-facts: Retrieve the fact list from the inferior Jess process.
  • \C-c,r ‘inf-jess-get-rules: Retrieve the rule list from the inferior Jess process.
  • \C-c,a ‘inf-jess-get-agenda: Retrieve the agenda from the inferior Jess process.
  • \C-c,v ‘inf-jess-get-view: The view command displays a live snapshot of the Rete network in a graphical window. I added this one myself.

Management:

  • \C-c,g ‘inf-jess-run-engine: Run the inference engine in the inferior Jess process.
  • \C-c,i ‘inf-jess-reset-engine: Reset the inference engine running in the inferior Jess process.

Evaluation:

  • \C-x\C-e ‘inf-jess-eval-last-sexp: Send the previous sexp to the inferior Jess process. Prefix argument means switch to the Jess buffer afterwards.
  • \C-c,e ‘inf-jess-eval-region: Send the current region to the inferior Jess process. Prefix argument forces switch to Jess buffer afterwards.
  • \C-c,b ‘inf-jess-eval-buffer: Send the entire buffer to the Inferior Jess process. Prefix argument means switch to the Jess buffer afterwards.
  • \C-c,t ‘inf-jess-eval-deftemplate: Send the current deftemplate to the inferior Jess process. Prefix argument means switch to the Jess buffer afterwards.
  • \M-\C-x ‘inf-jess-eval-deffunction: Send the current deffunction to the inferior Jess process. Prefix argument means switch to the Jess buffer afterwards.

I bound ‘inf-jess-val-buffer to , that is what I do for all lisp modes, but that is a personal preference thing.

To set the JESS interpreter command (make sure it is a shell scrip):

(setq inferior-jess-program "foo")

To start JESS in EMACS run:

run-jess

CLIPS Notes

For fun I worked through the CLIPS 6.30 beta literature specifically the user’s guide and basic programming guide. These are so, so helpful; they are aimed and programmers who have never worked in an expert system before. It goes in a lot more depth what you may do in CLIPS, and consequently JESS, than JESS does.

A Bit about CLIPS

JESS is a superset of CLIPS. On Linux it is a little less obvious how to get the install because CLIPS seems to come with a powerful IDE, but not on Linux. Oh well, on Ubuntu you can install it with:

sudo apt-get install clips clips-common clips-doc

Installing it provides

  • the interpreter
  • a nice man page
  • a help file accessible within the interpreter via ‘(help)
  • /usr/share/doc/clips-common/CLIPS-FAQ a compressed FAQ file readable with zcat or zless or zless to read it.
  • /usr/share/doc/clips-common/html/ Local copy of the main website with links out to stuff like documentation
  • /usr/share/doc/clips-common/examples/ “A number of examples of CLIPS program are available so you can test the interpreter and learn how it works. You can, for example, load one of them with (load “/usr/share/doc/clips-common/examples/wordgame.clp”) and run it using (reset) and (run).”

CLIPS seems like a great place to start with learning at least parts of JESS. I’m guessing that there is a lot of good material between the docs and examples.

Jess Sample Java Setup Project: Minimal Setup

This project will start out with a typical Java development setup:

Continue reading “Jess Sample Java Setup Project: Minimal Setup”

Jess Sample Java Setup Project: Mission Statement

For a couple of months I have thought about how fun it would be to set up a real simple Java+Jess project to help myself out with the basics of dependency management and logistics of interaction between Java and Jess. Recently it made sense to start using GitHub. With the hope that lowering the barriers to anyone interested in using Jess, I decided to throw the project out here. More so, because I will be working on it piecemeal, I decided to log progress in this diary and also on the blog I’m hoping it will serve as a resource for anyone interested in the project. My particular interest in Jess is for where a rules engine might fit in a 3 month to 2 year long Java project with a team of 3-12.
Here are some of my goals:

  • Provide a preconfigured project setup using Maven
  • Demonstrate some sample projects
  • Learn and cover interesting aspects of how Java and Jess work together
  • Learn or create nice ways of testing the system

Here are some of the options for sample projects to drive it:

  • Porting CLIPS samples
  • Logic puzzles
  • Video games
  • HVAC system
  • Form data validation
  • Data conversion

Here are some of ways to test it:

  • TestNG
  • Fitnesse

My preparation so far has been to read virtually everything available on Jess ( http://herzberg.ca.sandia.gov/), twice, and additionally read whatever I can find about CLIPS (http://clipsrules.sourceforge.net/), rules engines, and expert systems in books and on the Internet.
Getting started with Git meant reading the official docs (http://git-scm.com/documentation) and man pages, and GitHub itself has excellent documentation (https://help.github.com/). One helpful page was for the basic Markdown syntax which I had never learned officially (http://daringfireball.net/projects/markdown/basics) (except for using it in various wikis).
Admittedly, the Jess wiki cites a project like the above to be outside the sweet-spot of Jess. My worldview right now, though, is that the rules engine way-of-thinking has a lot to offer beyond mega-million enterprise-style projects, and that anyone who masters it will have a very valuable and powerful tool in his kit when he is done!

On Rules

Over the last few weeks nights and weekends I’ve played around with Jess studying up on rules engines and expert systems. The approach of focusing on the limited reality defined by the facts and rules in the universe of the engine along with letting go of control of sequencing are surprisingly freeing. Memories of event loops, OOAD, OOP, multimethods, state machines, horribly hand-written code, and event handlers all come to mind. It is fascinating to see how well some things can be simplified. It makes me wonder why we ever choose one approach over another. Supposedly we always use the best tool for the job; but in practice most of us learn one halfway decent approach (“tool”) once and end up using it forever. I suppose that is a human trait and not unique to problem solving with programming languages.

Installing Jess 71p2 in Eclipse 4.2

  1. Downloaded “Eclipse IDE for Java Developers”.
  2. The file name is “eclipse-java-juno-SR1-win32.zip”.
  3. This is release number 4.2 (Juno).
  4. Extracted the archive. Started Eclipse and stopped it.
  5. Extracted Jess plugins to the desired dir.
  6. Started Jess. Verified it’s presence.
  7. Installed GEF by using the built in “Juno” repository and searching for “GEF”. It showed up under “Modeling”. The installation took 10 minutes.
  8. Restarted Eclipse.

When you run this sample program:

(reset)
; define the counter
(deftemplate counter
  (slot count (type integer) (default 0)))
; define the rule
(defrule switchme
  ?p <- (counter {count < 10})
  =>
  (printout t ?p.count crlf)
  (bind ?p.count (+ ?p.count 1)))
; initialise counter
(deffunction init ()
  (assert (counter (count 1))))
;initialise program
(init)
;run program
(run)

Then you get a nice structure breakdown and syntax-highlighting:

You can set breakpoints and get debug information:

Here is the Rete network:

Good to know it is available, not that I know the value of it yet! I’m just investigating the plugins.
Next step is to do the studying!