This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Other Java Products and Servers and the fly likes JESS: running in a thread and more... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "JESS: running in a thread and more..." Watch "JESS: running in a thread and more..." New topic
Author

JESS: running in a thread and more...

Jerome Butcher-Green
Greenhorn

Joined: Jul 19, 2005
Posts: 5
Hi,
I am an intern trying to use jess to create a rule engine that will control a persons profiile (answers to questions that corralate to services that will/wont be run. I am new to both java and jess and REALLY struggling to make progress in this. ive read your book and 2-3 on java in the last month ...
My plan is to have a central class that can be run alone or with a gui ...or through another application
-In this class will be a static member, the engine.

I have also made a gui that will call the various methods in the class ...for demonstartion and to help me visualize everything

Finally there is the engine in which i have a bunch of dummy services with a nil answer and a single valid service (accessFile) that has the answer yes
-The settings look like this: (setting (slot service) (slot question) (slot answer))

Now all of this will become ever increasingly complicated ...I fully intend to incoparate backward-chaining in order to recognize pattern and infer answers to upcomming questions -but i need to get this off the ground! for now heres how its suppose to work:
1. Im (i refering to my main class) told to run a service (havent implemented this part and dont plan on it for a while)
2. I look to my profile for the answer to rather or not i should run the service (answer can be yes/no/nil) (yes/no run/dont run the service nil =ask user)
3. Run/don't run service

-Ive come to the conclusion that i need the engine in a continuous state to have this function properly (after 2weeks of head banging and failed attempts)

My problem is that i cant find a simple example with the jess engine in its own thread
...Sooo yes this is a question for my lack of knowledge in java and in jess and since you, Mr. Hill moderate this and the threading -thread :-> im hopin ull help me
I beleive my problem is that the way ive read threads should be done is:


Also in the gui i make a static method which is my main class
Then when the action is performed (ie button pressed) i simply call the method i wanna use with its parameters ...is this ok? as in will that still cause the gui to freeze even though RE will be in its own thread

Finally ive read through every post i could find here at the ranch and you say that store and fetch are the best ways to transport info between java n jess ...now in my case will that hold true
-I want my rules to fire when i query for a service :


I think ONE of my major problems is knowing how to know when my varibles are ready for retrivable!
Id use this to get it out:



ANY help at all would be VERY useful im at my limits here because i cant seem to be able to make progress ...which wuold simply get this whole thing working together!
Also i realize that i need more help than a question in a forum but im alone on this thing

thnx a bunch
-Jerome BG
[ July 24, 2005: Message edited by: Jerome BG ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Jerome BG:
Hi,
I am an intern trying to use jess to create a rule engine that will control a persons profiile (answers to questions that corralate to services that will/wont be run. I am new to both java and jess and REALLY struggling to make progress in this. ive read your book and 2-3 on java in the last month ...


Hi Jerome,

Welcome to JavaRanch!

Boy, sounds like somebody's thrown you into the deep end of the pool. This is an awful lot to learn all at once. I hope you won't feel too put upon if I give you another little task: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. Thanks for your cooperation!

Now, I should say at the outset that learning Jess and Java together and trying to get everything running at once is really too much to handle unless you're an alien super-genius. This is true not only of Java and Jess, but of Java together with any non-trival third-party library -- but especially of a library that implements another programming language!

If you rush into this too quickly, there's a very real danger that you'll end up with something that sort-of works, but not quite, and neither you, nor, anyone else who comes along afterwords, will be able to figure out what's wrong. I don't want that to happen, because you'll naturally blame Java or Jess.

My recommendation to you is to learn things step-by-step, in pieces. Make sure you can write a simple GUI app and understand what everything does, before you add Jess to the picture. Make sure you can write a simple Jess program, run it from the command line, and understand how it works before you integrate Jess into your Java program.

Having said all that, I'll answer some specific questions below, but keep my warning in mind, please.


-Ive come to the conclusion that i need the engine in a continuous state to have this function properly ... My problem is that i cant find a simple example with the jess engine in its own thread

The "runUntilHalt()" method is, indeed, what you want to use if you need the engine to just sit there and fire rules forever in its own thread. Note that that's what it will do: fire rules. Calling "runUntilHalt()" in its own thread won't magically make Jess methods you can on other threads "jump" onto this thread. I say this only because one of your questions below makes it sound like you're unsure of that point.

Anyway, to call this method (or any method) in its own thread, you just need to define a "Runnable" class containing the code you want to execute. One way would look like this:



[qb]

Also in the gui i make a static method which is my main class
Then when the action is performed (ie button pressed) i simply call the method i wanna use with its parameters ...is this ok? as in will that still cause the gui to freeze even though RE will be in its own thread


Any method you call from the GUI thread will execute on the GUI thread. If you call a Jess method like, for example assertFact(), and then that method will run on the GUI thread, but if that method activates some rules, those rules will fire in your runUntilHalt() thread, because rules are fired by that method.


Finally ive read through every post i could find here at the ranch and you say that store and fetch are the best ways to transport info between java n jess ...now in my case will that hold true


store and fetch are a simple way to do this. There's no "best" way.


I think ONE of my major problems is knowing how to know when my varibles are ready for retrivable!


Doing this with Jess is no different than doing it without. Communication between threads in a Java program involves using the wait() and notify() methods. Schematically, you'd probably want to do something like this:

  • In your GUI event handler, start a new thread "T".
  • On thread "T", assert your facts (or whatever you need to do to Jess), then call wait() on some object.
  • Meanwhile, on the Jess thread, the rules will fire. When a rule wants to report results, it stores them, then calls notify() on that same object.
  • Thread "T"'s call to wait() will return in response to calling notify(), and "T" can then confidently fetch the results.



  • Now, you have to ask yourself whether you actually need both threads. Maybe rather than having runUntilHalt() run on its own thread, you could simply call run() on "T" after asserting the facts, and then when run() returns, you could collect the results.

    Finally, as I said, store/fetch is just one way. The Jess rule can call any Java method to report results. So you can provide a method in your Java code, and then call it from your Jess code; that Java method can then store the data for further processing by your Java code -- or it can actually do the processing itself!


    [Jess in Action][AskingGoodQuestions]
    Jerome Butcher-Green
    Greenhorn

    Joined: Jul 19, 2005
    Posts: 5
    ha i guess i do have a little more on my plate than i can handle ...but none the less ill try

    thnx a bunch for your help and suggestions ...i had it set up so that id do just what you said (minus the hold thread thing):
    Now, you have to ask yourself whether you actually need both threads. Maybe rather than having runUntilHalt() run on its own thread, you could simply call run() on "T" after asserting the facts, and then when run() returns, you could collect the results.

    ...but it must have been something else causeing my error (witch unfortunatly i cant remember what it was -i abanduned that design last week) so ill go back to that!

    just FYI errr.. for my info could you explain what you mean by:
    Note that that's what it will do: fire rules. Calling "runUntilHalt()" in its own thread won't magically make Jess methods you can on other threads "jump" onto this thread. I say this only because one of your questions below makes it sound like you're unsure of that point.

    -i dont really understand what limits your saying that running it this way would have

    again thank you for the java/jess tutorial
    (and sorry about the name ...i rarly -aside from government papers- go through the trouble of typing/writing out my full name ...proly cause most databases/forms dont even have space for all the charaters)
    -Jerome BG
    [ July 25, 2005: Message edited by: Jerome Butcher-Green ]
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Originally posted by Jerome Butcher-Green:

    -i dont really understand what limits your saying that running it this way would have


    Just think of the rule engine "running" like a real engine running. It just sits there and runs. You can do other things asynchronously that affect it -- push the gas pedal for a real engine, add some facts for the rule engine -- but you still have to hold down the gas pedal yourself, just as the work of adding facts (and it does involve computational work!) is done on the thread where you call the methods to do it. The runUntilHalt() just keeps spinning away independently, waiting for rules to fire.

    Maybe you already understand this perfectly -- I just wasn't sure.


    (and sorry about the name ...i rarly -aside from government papers- go through the trouble of typing/writing out my full name ...proly cause most databases/forms dont even have space for all the charaters)


    As you might guess, I can totally sympathize.
    Jerome Butcher-Green
    Greenhorn

    Joined: Jul 19, 2005
    Posts: 5
    Mr Hill,
    I have another question but i could not post it here do to your HTML error ...and i couldnt figure out what part of the code was causeing it so i PM'd you

    thnx again
    -Jerome BG
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24187
        
      34

    Don't know what you mean about the HTML error, but I replied to your PM at 7:15 AM EST.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: JESS: running in a thread and more...