Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
1
Distributed Inference Systems
Federico Cabitza
Spring, 2006
Blackboard models, Generative Communication, Rule-base Inference Systems, DJess
Main Rationales
Why building a distributed inference system?Space decoupling
Support for mobility
Support for heterogeneity
Better access to contextTime decoupling:
Parallel execution vs Sequentiality
Non-overlapping lifetimes autonomy
2
Inference
What is inference?The act or process of deriving logical conclusions from premises known or assumed to be true. The act of reasoning from factual knowledge or evidence.
Human inference (i.e. how we draw conclusions) is studied within the field of cognitive psychology but rules and inference processes are some of philosophy's oldest subject matters.
Logic studies the laws of valid inference. Artificial Intelligence researchers develop automated inference systems.
Inference Systems
in AI, inference agents (something that perceives and acts)
are agents that: form (explicit) representations of the environment,
use a process of inference to derive new representations of the environment and
use these new representations to deduce what to do.(Russel and Norvig, 1995)
3
Agencies
“If Two Minds Are Better Than One, Then How About Two Thousand”?
An agency is a “set of heterogeneous agents that interact and collaborate towards a common goal”(even non-deliberately).
The agency is an AI metaphor to support the conception of a “Society of Mind” and to address the question: “how can intelligence emerge from nonintelligence?”
Minsky’s theory (1986): minds as collections of enormous numbers of semi-autonomous, intricately connected agents that are themselves mindless.
Interaction and collaboration
Loose Interaction between agentsbroadcast communication
Tight Interactionpoin-to-point communication: messaging
artifact-based communication: blackboard
Coordination and Cooperation
4
Cooperative Problem Solving
Speaking of approaches to problem solving the term BLACKBOARDwas coined by Newell (with Simon) in 1962 but the seminal idea in Cognitive Science and AI was the Pandemonium by Selfridge (1958)
“a set of demons, each independently looking at the total situation and shrieking in proportion to what they see that fits their natures.”
*
* Oliver Selfridge, founder with Minsky in 1953 of AI field; his early papers were on pattern recognition, machine learning (1955) and neural nets (1948)
The Blackboard Model
Blackboard-based problem solving metaphor:Imagine a group of human specialists seated next to a large blackboard. The specialists are working cooperatively to solve a problem, using the blackboard as the workplace for developing the solution. Problem solving begins when the problem and initial data are written onto the blackboard. The specialists watch the blackboard, looking for an opportunity to apply their expertise to the developing solution. When a specialist finds sufficient information to make a contribution, she records the contribution on the blackboard, hopefully enabling other specialists to apply their expertise. This process of adding/deleting contributions to the blackboard continues until the problem has been solved.
Detail of La scuola di Atene (by Raffaello Sanzio)1511
5
Blackboard-based Applications: Background
Its success from a failureHearSay-II (1975-1980) lost the race against Harpy in fulfilling DARPA requirements for speech recognition but opened a research field…
Other pioneering applications:AGE (1977-1982 Stanford)
BB1 system (1986, Stanford)
UMass GBB (1986)
Implications for Design 1/2
Independence of expertiseFlexibility, modularity, domain-independent reuse of single Knowledge Sources – KS
Diversity in problem-solving techniquescomplementarity, KS as black boxes, each of their diverse approaches can make its contributions, no matter which computational paradigm they abide by
Divide et imperaThe blackboard can be divided into regions, each corresponding to a particular kind of information (or sub-problem).
Incremental solution generationBlackboard systems are particularly effective when there are many steps toward the solution and many potential paths involving those steps.
6
Implications for Design 2/2
Flexible representation of blackboard informationfocus is not on full interoperability but on solution achieving.
Common interaction languagetrade off between the representational expressiveness of a specialized representation shared by only a few KSs and a fully general representation understood by all KSs. Finding the proper balance is important
Event-based interactionThe blackboard mediates collaboration: each specialist watches the blackboard, looking for an opportunity to contribute to the solution.
Need for control?Not necessarily, but some is needed and fine.
Blackboard Systems
A blackboard system consists of three components:Knowledge Sources (KSs) are independent modules that contain the knowledge needed to solve the problem. KSs can be widely diverse in representation and inference techniques.
The Blackboard is a global data repository containing input data, partial solutions, and other data that are in various problem-solving states.
A Control component makes runtime decisions about the course of problem solving: an opportunistic strategy (event/data-driven).
Blackboard
KS1 KS2 KSk
Control Modulei/o
7
Cooperative Efforts
Beyond the problem solving effort:The blackboard is also used to allow inter-knowledge source communication and acts as a shared memory visible to all of the knowledge sources.
Information is independent of source and destination:
messages
context
Blackboard Approach Pros
B. can be a powerful problem-solving architecture when:
Many diverse, specialized knowledge representations are needed. KSs can be developed in the most appropriate representation for the data they are to handle (e.g., rule based, neural-net, fuzzy-logic routines)
Uncertain knowledge or limited data inhibits absolute determination of a solution.
incremental approachevent driven approach : unanticipated control flow
An integration framework is neededto achieve collaboration in a highly distributed and heterogeneous settings of agents
The development of an application involves numerous developers.modular approach
8
Blackboard Approach Cons
Little scalabilityAdvantages of blackboard systems do not scale down to simple problems
No hypelack of commercial software designed specifically for building blackboard applications
No communitya shortage of application developers with experience
Application Domains
sensory interpretation (Hearsay-II, ’80)
design and layoutprocess control (HASP/SIAP, ’82; Barbara ‘83)
planning and schedulingcomputer vision (HMAX, ’99)
case-based reasoningknowledge-based simulationknowledge-based instructiondecision –making support (Paladin, ‘89)
symbolic learningAmbient Intelligence and Ubicom (see next…)
9
Blackboards Now
An important contribution to the blackboard paradigm is due to the Generative Communication Model (Gelernter, ‘85), a model for inter-process communication based on a Distributed Shared Memory (DSM)
Best represented by the Linda coordination language for parallel computing (and all its dialects, among which important commercial systems as JavaSpaces and TSpaces by IBM).
Programming distributed applications using a shared memory abstraction is less complex than explicitly using message passing(abstraction)
In addition, the DSM model allows communicating processes to be uncoupled
logically (Independent, heterogeneous, autonomous agents…)temporally (…with their own lifespans and asynchronous reasoning…)spatially (… distributed in space and mobile.)
The Generative Model
Main features: Processes (aka KSs) share a virtual memory space (in Linda called a tuplespace)
The fundamental data units (Linda: tuples), are ordered vectors (or lists) of typed values.
Until it is withdrawn a generated tuple is indipendent from the generator, is accessible to all KSs but bound to none.
Processes communicate by reading (read) and writing (out), and consuming/retracting (in) these tuples.
Associative matching: tuples are accessed associatively, that is, by their contents rather than by their addresses.
Some applications in Mobile ComputingLiME (1999), MARS (2000), TuCSoN (1999)
10
Expert System
An ES is a program that is able to “emulate”human reasoning and decision taking in some way and in some domain.
A very general definition:
Expert SystemsThey are quite common, embedded in everyday softwares and
their use ranges from easy tasks to very difficult ones.
Some use domains:
Spam filteringFault diagnosisMedical diagnosisFactory Process ControlPrediction/classification…
11
Expert System
“a program that uses knowledge and inference procedures to solve problems that are difficult enough to require significant human expertise for their solution.”
Feigenbaum 1982
A historical citation:
An Expert System has…
some inferencing(reasoning on factual knowledge)
some interface to world (user and enviroment)
some expertise(expert knowledge in a particular area)
12
KNOWLEDGE BASE
INFERENTIAL ENGINE
API’s, Shell, Etc….
A very general architecture:
Expert System: inside
Expert System: outside
expertise
solutions
users
expertsprogrammers
KNOWLEDGE BASE
INFERENTIAL ENGINE
API’s, Shell, Etc….
KNOWLEDGE BASE
INFERENTIAL ENGINE
API’s, Shell, Etc….
models,algorithms
facts
World representation
13
A conceptual guide
In the following we’ll treat the three keywords above outlined
Expertise: we’ll speak of Knowledge and Rules
Inferencing: we’ll speak of reasoning and the way Rule
Based ES’s do it
Interface: we’ll speak of a Java Interface to RBES’s: JESS !
KnowledgeWhat is it? I mean, in this context…Three, ever present, concepts:
Mind: in Expert Systems jargon they are used quite idiosyncratically between the classical (if any) AI acceptation and the classical ICT one.Misunderstanding is no surprise…
DataInformation
Knowledge
14
KnowledgeIn the Expert System domain:
A proceduralmodel of the world
“Factual” knowledge
Data
Information
Knowledge
inference
Facts about the world
Recommendations, solutions, etc.
Knowledge
Data
Information
Knowledge
processing
infering
A DIKW (??) view of the same concept:
Usual Programs
Expert Systems
15
The DIKW Pyramid
Zeleny, M. "Management Support Systems: Towards Integrated Knowledge Management," Human Systems Management, 7(1987)1, pp. 59-70.
Ackoff, R.L. "From Data to Wisdom", Journal of Applied Systems Analysis, Volume 16, 1989 p 3-9.
In 1987 Milan Zeleny and then Russell Ackoff posited the hierarchy of Data, Information, Knowledge and… Wisdom
Types Of Expert Systems
- Neural Networks
- Blackboard Systems
- Belief (Bayesian) Networks
- Case-Based Reasoning
- Rule-Based Systems
16
- represent knowledge as a bunch of rules that tell you what you should do or what you could conclude (suggestions) in different situationsrepresented as a bunch of facts
- originated from AI research in the 70s and 80s.
MYCIN 1974 written in LISP by Shortliffe
Rule-Based Programming
Alan Newell and Herbert Simon, “General Problem Solver” (1972) – Post “Production Systems” (1943)
Rule-based Programming
Rule-based systems
We need to consider:Declarative Knowledge (Rules!)
Control strategy : The Match-Resolve-Execute Cycle
“Direction” of the MRE Cycle
Matching Algorithms (M-phase)
Conflict Resolution Strategies (R-phase)
17
The Rule as KnowledgeMuch of our knowledge comes in a rule-form
If-then statementwhenever-then statement
Two main use for rules, Production System Rules
In the production system, the rule is a mapping from one state to another state where the mapping contains a condition and an action
Predicate Calculus RulesIn predicate calculus, the rule acts as a way to convert a clause from one predicate (or collection) to another
In problem solving, rules are often used to denote if a course of action or conclusion is applicable in the current situation
Rules are computational units that are computed only when they are applicable. A rule is composed of an IF-portion (LHS) and a THEN-portion (RHS).
some patternssome actions
to execute
IF THEN
Rules in a Nutshell
18
IF THEN
A RBES compares facts of the world with patterns of rules
…and whenever the patterns of the rule match with some facts of the environment, then the rule is activated and its actions executed.
Rules in a Nutshell
More on the rule
Rules seem to be procedural knowledge, but they can also be declarative knowledge
If the patient has a high fever, then conclude that the patient has a virus
Rules tend to blur the line of what kind of knowledge they are, but also, how to apply them – it is a convenient form of knowledge though
19
Problem Domainnarrowwell-understood
Knowledge Representation facts rules
Output recommendations to humans, support to decisions
Learning not essential
often!
When to use RBIS
A rule-based inference system:a Working Memory (WM) containing facts
a Rule Set (RS) containing if-then constructs
a Rule Engine (E) matching and executing rules
(defrule air-conditioning(room (temperature ?t))(test (> ?t 28))
=>(switch-on-AC) )
(room(name “BLT1”)(temperature 29) )
RBIS Architecture
20
Working Memory
Working MemoryWhat we know is true
Working memory changes as time goes on as new things are concluded
Rules examine working memory to see if the left-hand side is true and execute actions in their right-hand side that manipulates working memory
INFERENCEENGINEWORKING
MEMORY
RULE BASE
EXECUTIONENGINE
PATTERNMATCHER
CONFLICT RESOLVER
RBIS Inside
21
Pattern Matcherdecides what rules to fire and when
(conflict set).Conflict Resolver
schedules the order in which activated rules will fire (agenda).
Execution Engineresponsible for firing rules and executing other code.
Rule Engine Components
1) Match the facts against the rules.
2) Select which rules to fire.
3) Execute the actions associated to rules.
The RB Inference Process
22
Deduction:
Induction:
The act or process of deriving logical conclusions from premises known or assumed to be true (modus ponens)
The process of deriving general principles from particular facts
Rule-Based Expert Systems
Abduction: The process of deriving causes from the effects(explanations from facts)
In emulating human reasoning RBES can reason (“infer”) in two ways:
Rule-Based Expert Systems
Both the “directions” are valid: it depends on the situation and on the required outcome which is the most suitable.
Inference Start Purpose- Deduction byForward chaining: Facts: Conclusions that must follow
-Abduction byBackward chaining: Uncertain conclusion Facts to support the conclusions
23
Rule-Based Expert Systems
Forward Chaining (basically repetition of modus ponens)
aka “production systems” :
Known facts “New” factsrules
OPS5 (1977) in LISP
1) start from initial facts 2) keep using the rules to draw new conclusions
(or take certain actions)
Backward Chaining (useful for generating explanations (abductive reasoning)
PROLOG (1972) in ALGOL
1) start from some hypothesis (or goal) to prove2) keep looking for rules that would allow you to conclude that hypothesis
(also by setting new subgoals to prove as you go).
Right rules
New facts facts (goals)
Rule-Based Expert Systems
24
Forward ChainingSelect a rule by finding one whose left-hand side conditions are true, execute (fire) the right hand side
Example would be medical diagnosisIf A, B, C then conclude DIf B, D, E then conclude FIf G, H then conclude I
Right-hand side might be actions such as perform test A or change value of patient.fever to “yes”, or come to some conclusionForward chaining is mostly used in data-driven problems like diagnosis, data interpretation, perception and so forth
Backward ChainingSome problems are goal-driven like planning
For these problems, we might use backward chainingGoal is X
Find a rule that has X in its right-hand side such as If A, B, C X
The conditions A, B, C are new subgoals to accomplish, find a new rule that has A in its right-hand side, B in its right-hand side, C in its right-hand side and continue
Similar to means-ends analysis because we focus on a goal and try to figure out how to accomplish it
25
Matching
Consider a set of thousands of rules and hundreds of pieces of working knowledge
Do you search each rule to see if it is relevant?This could be very time consumingIt may not be obvious that the conditions of a rule are true because the conditions are in the forms of parameterized procedure calls or parameterized predicates
To make matching more efficient, we could use some form of indexing (e.g. hashing)
Matching conditionals
Use a RETE network for matching
Group rules with similar conditionals together
Remember rules that have almost matched
Use approximate matching if necessary
26
Conflict Resolution
What if two or more rules match at a given situation? Which rule gets fired?We could randomly pick oneWe could use brute-force (breadth-first or depth-first) search and backtrackingWe could base our choice on preferences which come in the form of a conflict resolution strategy
Preferences based on Rules
Give priority by arranging rules in preferential order
Have “special case” rules which have priority over all other rules
Have a general rule that indicates rules with more conditionals match before rules with less conditions or rules with more actions match before rules with less actions
27
Preferences based on Objects
Rate the conditions themselves in terms of priorityRules with more important conditions have higher priority than rules with less important conditionsFor instance, in medical diagnosis, more peculiar or rarer symptoms are more important than typical symptoms
1) Initial facts put in a working memory (WM)
2) Rules and facts compared using pattern matching.
3) Matched rules activated put into a conflict set.
4) Conflict set resolved into agenda (ordered c.s.)(process called conflict resolution).
5) Rule engine fires on agenda (->new facts in WM).
Engine cycles until all rules in agenda are fired.
details
Halt
The RB Inference Algorithm
28
Jess matches facts in the fact base to rules in the rule base.
The rules contain function calls that manipulate the fact base and/or other Java code.
Jess uses the Rete (ree-tee) algorithm to match patterns.
Rete network = an interconnected collection of nodes = working memory.
JESS in short
Syntax similar to LISP(Lots of Irrititating, Superflous Parentheses)
Basic data structure is the list.
Inspired to CLIPS (C Language Integrated Production System)
all structured data are lists:
facts, rules, also function calls are lists.
JESS as scripting environment
29
DJess: Distributed JESS
A Jess-based communication middleware for distributed inference systems
Transparent sharing of facts and rules (blackboard)
DJess: Features
Transparent sharing of facts (blackboard)
Rule propagation mechanism (high level logic)
Same syntax of Jess (backward compatible)
Complete scripting environment (Jess)
Embeddable in a Java program (Jess)
30
DJess: WoIS
The Web of Inference Sites (WoIS)One shared memory
DJess: WoIS
The Web of Inference Sites (WoIS)One shared memory (SWM like a common Fact Space)
31
WoIS Manager
Supervision of joining and leaving of members
Additional housekeeping
DJess: Rule Propagation
Two-step mechanism:Sharing (1): embedding a rule in a shared fact
Loading (2): fetching the rule from the SWM into the local Rule Base.
32
SWM: Shadow Facts
A simple fact template associated to a Java class
An instance of that class can be part of the SWM: a shadow fact
SWM: Ghost Facts
Shared facts are replicated in every IS
33
SWM: Ghost Facts
Shared facts are replicated in every IS
Coherence is maintained by ghost facts
DJESS: Interferences
The problem: 2 rules accessing the same factRead-write dependency
Write-write dependency
The solution: rule firings are like transactionsTwo-phase lock scheme
Deadlock preventionRule firings are broken in three steps
1. Lock acquisition for every fact involved
2. Rule execution
3. Lock release
34
Numbers: INTEGER, LONG, FLOAT
Data Types
Atoms: ATOM
foobarFoobarnumber#3 3#number
Nil TRUE FALSE
0.357 42 6.0E4
Data Types
Strings: STRING
“0.357” “foobar” “ \”between commas\” ”
( foobar) (a foobar 4) (deftamplate foo (slot bar))
Lists: LIST
head
Comments: ;*
35
Obligatory Tradition
Your very first Jess program!
(printout t “Hello World!” crlf)
It is NOT a “good” program…
Functions in Jess
Function call: a list whose head is the name of an existing function list
Prefix notations
Nesting: the outer function evaluates the inner one(s)
(+ 2 3) 5
(* (+ 2 3) (+ (* 2 3) 2))
5 8 640
(printout t “Hello World!” crlf)
36
Atoms that begin with a ? mark.
Untyped: types can change during lifetime (like in dynamically typed languages)
Can be local or global (aka defglobals)
Variables (?x) are declared by first assigning(using bind function), defglobals (?*x*) are assigned in declaration
Variables in Jess
(bind ?x 2)
(defglobal ?*y* = 3)
(bind ?result (+ ?x ?y))
Variables in Jess
assignes value 2 to variable x
assignes value 3 to global variable y
assignes to variable resultthe result of the expression(3 + 2)
An example
37
Variables in Jess
Differences between variables and defglobals??
Easy:Global variables (defined through the defglobalconstruct, hence defglobals) are variables thatare not wiped out by issuing the resetcommand.
Functions in Jess
You can create a function by creating a particular list whose head is the deffunction atom
(deffunction max (?a ?b)(if (> ?a ?b) then
(return ?a)else
(return ?b))
)
38
Functions in Jess
In a function you can create any variableand use it with input parameters. Example:
(deffunction area-circle (?radius)(bind ?area (* (pi) (* ?radius
?radius)))(return ?area))
Functions in Jess
Putting things togetherafter having created a new function, touse it is quite trivial. Example:
(printout t "The area of a 2 meter radius circle is " + (area-circle 2) + " m^2")
39
Common Functions
(reset): resets the Working Memory *(clear): clears Jess (more deeply than reset)(facts): shows facts currently in WM(rules): shows rules currently in WM(batch <filename>): sends the Shell some file content(run): starts the engine(apply) calls a function on a given set of arguments.(build) parses and executes a function from a string.(eval) ”(open <filename> id w): opens a file in write mode(read t): reads from Standard Input(read id): reads the file content(printout t “…” crlf): prints to StdOutput(printout id “…” crlf): prints to file(exit): exits the Shell
Let’s talk facts
Jess (and any other RBES) representsthe world and simple knowledge of it bymeans of facts.
Facts are lists with a specific structure:
( atom [slot]*)headlike a label, it expresses the category of the fact
slotsData containers (simple or lists…)
40
Jess Facts Types
- Ordered, not structured
Where just position counts.
- Unordered, structured
Where types and default values are allowed.
- Shadow
Where Jess and Java meet.
Jess Facts Examples
(person (surname “Smith”)(first-names Robert Jerome) (age 35)
)
Slots: like fields of a form, they hold (un)typed data.(first-names is a multislot: see below)
(error-occurred)
(shopping-list eggs milk bread)
(person “Robert J. Smith" Male 35)
Just a head, no slot: this fact indicates a state in the world
41
Jess Facts assertion
Facts “exist” if they are asserted.Asserting a fact means to put it in the “fact base”.Likewise, retracting a fact means to remove it from the Working Memory.
Jess> (assert (shopping-list eggs bread))<Fact-1>
Jess> (facts)f-0 (MAIN::initial-fact)f-1 (MAIN::shopping-list eggs bread)For a total of 2 facts.
Jess> (retract (fact-id 1))
Deftemplate
To work with unordered facts they must be defined first by calling the deftemplateconstruct.
(deftemplate <deftemplate-name>(slot | multislot <slot-name>)* )
A multifield slot (multislot) can have any number (including zero) of values.
42
Deftemplate Ex. 1/2Jess> (deftemplate automobile
"A specific car."(slot make)(slot model)(slot year (type INTEGER))(slot color (default white))
)
Jess> (bind ?id (assert (automobile (make Volvo) (model 460)(year 1997))))<Fact-0>
Jess> (facts)f-0
(MAIN::automobile (make Volvo) (model 460)(year 1997) (color white))
Deftemplate Ex. 2/2
Jess> (facts)f-0
(MAIN::automobile (make Volvo) (model 460)(year 1997) (color black))
Jess> (modify ?id (color black))
Modify will… just modify a value in a slot!
43
Shadow Facts
- Defclass
creates a deftemplate that represents a category of Java bean.
- Definstance
adds a representation of one specific bean to working memory.
Shadow facts are unordered facts whose slots correspond to the properties of a JavaBean.
Sort of class import
Sort of object instance
What are patterns?How actions
are expressed?
IF THEN
Jess Rules…
Now we can look inside these boxes…
44
This is the rule syntax:
(defrule <rule-name> {;optional comment}<condition-element-1><condition-element-2>…..<condition-element-k>
=><action-1>…..<action-m>
)
Jess Rules Syntax
What is a Condition Element? (CE)
<condition-element> =<pattern> or <NOT-CE> or <AND-CE>
or <OR-CE> or <test-CE>
Condition Elements
Yes, this is a recursive definition……at last CE’s are bunches of patterns and some of them can be evaluated in logical expressions.A CE can be either True or False.
Eg.: an OR-CE is true if any CE is True, False otherwise.
45
When is a CE true?
CE and Patterns
It depends on whether the patterns that constitute it find a match in the WM.
Patterns are partial descriptions of data.In Jess they can be expressed by facts that include some variables looking for a… value!
(Exp (Name ?X) (Arg1 5) (Op ?))
(Exp (Name E21) (Arg1 5) (Op *) (Arg2 X))
… will match with this fact:
1) This pattern…
3 Patterns Examples
field constraintliteral constraint
wildcard constraint
46
2) ? is a monofield wildcard; $? is a multifield one.Ex.: $?items can match any number of values
Patterns Examples
(defrule cart-containing-milk(shopping-cart (contents $?anything_before milk $?anything_after))
=>(printout t “The cart contains milk.” crlf))
3) You can use Connective Constraints (&, |, ~, :)
?x&:(> 5 ?x) match with any x that is less than 5
(person (city ~Milano& ~Roma)) match with anyone who lives neither in Rome nor in Milan
Patterns and rules…
If all the LHS CE’s of a rule are True thenthe rule instance is entered into the conflict set and becomes an Activation.Then attention moves to the RHS, where actions wait to be fired. Actions are expressed quite easily…they are function calls!!
47
;; This is a trivial error handler
(defrule report-error
(error-is-present)
=>
(printout t “There is an error” crlf))
Let’s see again the JESS rule syntax in a simple example:
definition
L H S
then R H S
What a Rule can do…
A rule can change the Fact base
Adding…(assert ()) to assert a fact(deffacts ()) to assert more unstructured facts
Removing…(retract (fact-id ?fact-id)) to delete a fact
Modifying…(modify ?f (slotname slotvalue))
to modify a fact
48
( defrule fratelli_mamma(madre ?mamma ?figlio1)(madre ?mamma ?figlio2)(test (neq ?figlio1 ?figlio2))=>(assert (fratelli ?figlio1 ?figlio2))
)
IF a mother has got two children…
… I mean, TWO children
THEN
These two guys are brothers!!
Jess Rule Example
Not a big deal, uh?! ;-)
A More Complex Rule
;; A better error handler
(defrule report-err?err <- (is-error (msg ?msg))=>(printout t "Error was: " ?msg crlf)(retract ?err))
A more useful rule…
49
Inference Process details
1) Initial facts put in a working memory (WM)
2) Rules and facts compared using pattern matching.
3) Matched rules activated put into a conflict set.
4) Conflict set resolved into agenda(process called conflict resolution).
5) Rule engine fires on agenda (new facts in WM).
Engine cycles until all rules in agenda are fired.
Halt
Conflict Resolution
The conflict set is the set of all the rules that are eligible to be fired (executed).
Conflict resolution is the process of putting the rules in firing order.
Different strategies can be applied according to the domain (and how this is formalized into rules).
Usually strategies are accomplished according to some rationale (trade-off).
50
Conflict Resolution
- Rule-based rationale- Recency (activation order) - Specificity (number of conditions, or lenght of conditions)- Anti-refraction (not the same rule under the same conditions) - Anti-repetition (not the same effect twice)- …
1. - Object-based rationale- Classification and ranking of object referenced in LHSs
2. - Effect-based rationale- Assessment of effects beforehand
3. - Metarules
Conflict Resolution
The Jess built-in strategies are depth and breadth and can be called with the (set-strategy (depth | breadth)) command.
- Depth (default):The most recently activated rules fire first. Last in, first out.
Caveat: troubles arise if every fired rule activates another rule, since old activations can never get a chance to fire.
51
Conflict Resolution
- Breadth: The most recently activated rules fire last (i.e., firing order is activation order): First in, first out.
Not enough? You can write your own strategy in Java by implementing the jess.Strategyinterface (‘compare’ method):
(set-strategy your-alluring-strategy)
You lazy? You can use salience…
Salience- Each rule has a property called salience.It’s sort of rule priority.
- Activated rules of the highest salience will fire first, followed by rules of lower salience.
- Rules can include a salience declaration
(defrule last-rule(declare (salience -100))(command exit-when-idle)=>(printout t "exiting..." crlf))
52
Java ← Jess- Jess comes with other 200 built-in functions
and you can also define any function you want. This makes Jess a good scripting language.
- More importantly from JESS you can access all Java APIs (link to databases, GUI forms, etc.), create and manipulate Java objects.
(foreach <variable> <multifield-expression> <action>*)
(if <expression> then <action>* [else <action>*])
(try <expression>* [catch <expression>*])
(while <expression> [do] <action>*)
Jess> (bind ?ht (new java.util.Hashtable))<External-Address:java.util.Hashtable>
Jess> (call ?ht put "key1" "element1")Jess> (call ?ht put "key2" "element2")Jess> (call ?ht get "key1")
"element1"
Examples
Name of an existing class
Methods of the class
53
Examples
public class java_class{public int fibonacci(int n){
if(n < 3) return 1;else return fibonacci(n-1) +
fibonacci(n-2);}
}
Jess> (defglobal ?*external-class-obj* = 0)Jess> (bind ?*external-class-obj* (new java_class))) Jess> (printout t "The 6th fibonacci number is: “
(?*external-class-obj* fibonacci 6) crlf)
This class must be compiled
Methods of the class
Java → JessFrom Java you can call JESS (Jess API).
import jess.*;
public class ExSquare { public static void main(String[] str) { try {
Rete r = new Rete(); r.executeCommand(“(deffunction square (?n)
(return (* ?n ?n)))"); Value v = r.executeCommand("(square 3)"); System.out.println(v.intValue(r.getGlobalContext())); } catch (JessException ex) {
System.err.println(ex); }
} }
54
try {
Rete engine = new Rete();
engine.executeCommand(
(batch foobar.clp));
engine.executeCommand(“(run)”);
}
catch (JessException je) {
System.err.println(je);
}
Java → Jess
Two good methods to exchange data between Java and Jess and viceversa.
Java → Jess
- Using Fetch / Store commands
- Using Shadow facts
55
Shadow Facts
- Defclass
creates a deftemplate that represents a category of Java bean.
- Definstance
adds a representation of one specific bean (which has been already instanced) to the working memory.
Shadow facts are unordered facts whose slots correspond to the properties of a JavaBean.
Java → Jess
Shadow factsPublic class JavaBean{public String getPropertyOne() …public void setPropertyOne(String)…public int String getPropertyTwo() …public void setPropertyTwo(int)…}
(deftemplate Javabean(slot propertyOne)(slot propertyTtwo))
Templates (static) come from the Introspector classInstances Updates come from the PropertyChangeSupport class
56
Java → Jess
public class DimmerSwitch { private int brightness = 0; public int getBrightness() { return brightness; } public void setBrightness(int b) {
brightness = b; adjustTriac(b);
} private void adjustTriac(int brightness) {
// Code not shown, maybe JNI }
}
An example
Java → Jess
Jess> (defclass dimmer DimmerSwitch)
This generate a deftemplate that represent the class
Jess> (bind ?ds (new DimmerSwitch))
This creates an instance (object) of DimmerSwitch (it’s a Java Object). Alternatively this object could have been created outside Jess
Jess> (definstance dimmer ?ds)
This adds the object into the working memory
57
Java → Jess
Jess> (ppdeftemplate dimmer)(deftemplate MAIN::dimmer extends MAIN::__fact\"$JAVA-OBJECT$ DimmerSwitch\"(slot brightness
(default <External-Address:jess.SerializablePD>))(slot class
(default <Externaladdress:jess.SerializablePD>))(slot OBJECT (type 2048)))"
The slot brightness derives from the brightness property of the JavaBean.
The slot class comes from themethod getClass inherited from java.lang.Object.
The slot OBJECT is added by Jess and contains a reference to the JavaBean.
Java → Jess
If the definstance is static only the object changes, not the fact (unless you run a reset command). In fact the reset updates the shadow fact to match the JavaBean, like (update ?ds).
In the “dynamic” fact Jess puts a PropertyChangeListener but to have a sf change when a bean does, you need also to define a PropertyChangeSupport in your Java class.
Jess> (definstance dimmer ?ds [static])
58
Java → Jess
A propertyChangeSupport
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);public voidaddPropertyChangeListener(PropertyChangeListener p) {
pcs.addPropertyChangeListener(p);}public void
removePropertyChangeListener(PropertyChangeListener p) {pcs.removePropertyChangeListener(p);
}
1) Add these code in the class (JavaBean) definition
2) Change the Set Method of the JavaBean
In the setBrightness method of the example: pcs.firePropertyChange(“brightness",new Integer(old),(new Integer(b)));
Java → Jess
Jess> (call ?ds setBrightness 10)
This is a call to a method of Dimmerswitch classthat changes the attribute of the object referenced by ?ds.
Jess> (call ?ds getBrightness)
This is a call to a method of Dimmerswitch classthat outputs the attribute of the object referenced by ?ds.
Jess> (modify ?f-id (brightness 10)))
This is an equivalent command that changes the attribute of the object referenced by ?ds.
59
Java → Jess
An example from Friedman-Hill (p.93-94)
Java → Jess
You CANNOT use assert on shadow facts (sf).Use definstance instead.Neither you can assert a sf with deffactsconstruct.
You CAN use both retract and modify on sf.If you modify a sf, Jess automatically updates the corresponding JavaBean property.
(call ?sf setProperty x) == (modify ?fact-id (Property x))
60
Java → Jess
Jess> (facts)f-0 (MAIN: :dimmer
(brightness 0)(class <External-Address…>)(OBJECT <External-Address…>)
)
(defrule example(dimmer (OBJECT ?o))(test (eq (call ?o getBrightness) 10))
=>(assert (Brightness 10))
)
How to use a shadow fact in a rule…
References 1/2S. Russell and P. Norvig. Artificial Intelligence: a modern approach. Prentice-Hall, 1995.
M. Minsky. The Society of Mind. New York: Simon and Schuster, 1986.
D. Corkill. Blackboard Systems. AI Expert 6(9):40-47, September 1991.
R. Engelmore and A. Morgan, editors. Blackboard Systems. Addison-Wesley, 1988.
V. Jagannathan, R. Dodhiawala, and L. Baum, editors. Blackboard Architectures and Applications. Academic Press, 1989.
L. Erman, F. Hayes-Roth, V. Lesser, and D. Reddy. The Hearsay-II speech-understanding system: Integrating knowledge to resolve uncertainty. Computing Surveys, 12(2):213–253, June 1980.
H. Penny Nii and Nelleke Aiello. AGE (Attempt to GEneralize): A knowledge-based program for building knowledge-based programs. In Proceedings of the Sixth International Joint Conference on Artificial Intelligence, pages 645–655, Tokyo, Japan, August 1979.
A. Garvey, M. Hewett, M. Vaughan Johnson, R. Schulman, and B. Hayes-Roth. BB1 User Manual. Knowledge Systems Laboratory, Departments of Medical and Computer Science, Stanford, California 94305, Common Lisp edition, October 1986.
D. Corkill, K. Gallagher, and K. Murray. GBB: A generic blackboard development system. In Proceedings of the National Conference on Artificial Intelligence, pages 1008–1014, Philadelphia, Pennsylvania, August 1986
R. Engelmore and A. Morgan, editors, pages 503–518, Addison-Wesley, 1988.
E.H. Durfee. Coordination of Distributed Problem Solvers, Kluwer Academic Publshers, Boston 1988
A. Newell. Some problems of the basic organization in problem-solving programs. In:Proceedings of the Second Conference on Self-Organizing Systems. Yovits, M.C., Jocobi, G.T.and Goldstien, G.D. (eds), pp. 393-423, Spartan Books. 1962.
Allen Newell and Herbert A. Simon. Human Problem Solving. Prentice Hall, New Jersey, 1972.
Clearwater, S.H., Huberman, B.A.,Hogg,T.,Cooperative problem solving, in: Huberman (ed.), Computation: The Micro and Macro View, Singapore: World Scientific, pp. , 33-70, 1992
61
References 2/2O. Selfridge. Pandemonium: A paradigm for learning. In Symposium on the mechanization of thought processes. London: HM Stationary Office. 1959.
D. Gelernter. Generative communication in linda. ACM Transactions on Programming, 2(1):80--112, January 1985.
G. Picco, A. Murphy, and G. Roman. Lime: Linda meets mobility. In Proceedings of the 21st International Conference on Software Engineering (ICSE99), pages 368–377. ACM press, 1999.
G. Cabri, L. Leonardi, and F. Zambonelli. Mars: a programmable coordination architecture for mobile agents. IEEE Internet Computing, 4(4):26–35, 2000.
A. Omicini and F. Zambonelli. Coordination for Internet application development. Autonomous Agents and Multi-Agent Systems, 2(3):251–269, September 1999. Special Issue: Coordination Mechanisms for Web Agents.
E. J. Friedman-Hill, Jess In Action, Manning Press, 2003
P. Jackson, Introduction to Expert Systems – 3rd Ed., Addison-Wesley, 1999
J. Giarratano, Expert Systems: Principals and Programming, PSW-Kent, 1989
Lindsay P., Norman D. A.: L' uomo elaboratore di informazioni. Giunti Editore, 1968