1. 1
Mark
Proctor
Project Lead
●
The SkyNet funding bill is passed.
●
The system goes online on August 4th, 1997.
●
Human decisions are removed from strategic defense.
●
SkyNet begins to learn at a geometric rate.
●
It becomes self-aware at 2:14am Eastern time, August
29th
●
In a panic, they try to pull the plug.
●
And, Skynet fights back
3. 3
Sample Industries and Users
Investment
● Millennium Investment Group (MIG)
Logistics
● Fedex
Airline
● Sabre
Mortgage
● Franklin American
Healthcare
● OSDE
4. 4
Boot Camps
San Francisco 2009 (40+ attendees)
● Sponsored by Third Pillar
● Sun, FAMC, OSDE, Kaseya, Fedex, TU Group, Intermountain
Healthcare, Gap, Sony Pictures, Lockheed Martin, Kaiser, HP,
Wells Fargo, US Navy Research, FOLIOfn, Boeing .....
San Diego 2010 (80+ attendess)
● Sponsored by US Navy
● 5 day event, with 2 days focus on the healthcare industry
● OSDE, AT&T, SAIC, US Navy Research, Kaiser, Clinica,
Intermountain Healthcare, GE Healthcare, VA, Boeing,
Nationwide ....
7. 7
Rules and processes
generic
?
Decision
Services
SCOPE
Process
specific
Rules
tightly coupled COUPLING loosely coupled
8. 8
Some Areas of Artificial Intelligence
Vision
Robotics Natural
Some Areas of Language
Artificial
Speech Intelligence Understanding
Neural Nets Rule Base Systems/
Expert System Tools
9. 9
Knowledge Representation and Reasoning
The study of Knowledge is Epistemology
● Nature Structure and Origins of Knowledge
● Core or AI
Rule based systems
● Expert System tools
● Knowledge Representation and Reasoning
● Facilitate the codification of knowledge into a
knowledge base which can be used for reasoning
10. 10
Knowledege Representation and Reasoning
Production Rule Systems PRD (forward chaining)
● Reactive
● when Alarm( status == “alert” )
then send( “warning” )
Logic Programming LP (backward chaining)
● Query
● descendant( “mary”, “jane”)
Functional Programming FP
● Map,Fold, Filter
● avg([12, 16, 4, 6])
● Returns single value 9.5
● round([10.3, 4.7, 7.8] )
● Returns List [10, 5, 8]
Description Logic
11. 11
Knowledege Representation and Reasoning
Description Logic
● Man
● Father
● is a Man
● has at least one child
● All Fathers have sunday off
● Individual == instance
● Adhoc
● Classes and properties
added and inferred over time
● Does a Man with two children have sundays off?
12. 12
It All Started Here
Birth of CDSS
Dendral
1970s
Mycin
WM
Teiresias Emycin
Baobab
Puff
Guidon
Sacon
Centaur
Gravida
Wheeze Clot
1980s Neomycin Oncocin
17. 18
What is a Rule
salience <int>
Quotes on Rule names are
agenda-group <string>
optional if the rule name has
no-loop <boolean>
no spaces.
auto-focus <boolean>
duration <long>
....
• rule “<name>”
<attribute> <value>
when
<LHS>
then
<RHS> RHS can be any valid java.
Or MVEL. Other languages
end could be added.
18. 19
What is a Rule
Methods that must specific passing of
be called directly instances
• public void helloMark(Person person) {
Rules can never if ( person.getName().equals( “mark” ) {
be called directly System.out.println( “Hello Mark” );
} Specific instances
} cannot be passed.
• rule “Hello Mark” LHS
when
Person( name == “mark” )
then
System.out.println( “Hello Mark” );
end
RHS
19. 20
What is a Pattern
P a tte rn
O b je c t T y p e F ie ld C o n s t r a in t
F ie ld N a m e R e s t r ic t io n
E v a lu a t o r V a lu e
S h o w e r ( te m p e r a tu re = = “ h o t” )
20. 21
Classes
C a s h f lo w
A cco un t
D a te d a te
lo n g a c c o u n t N o
d o u b le a m o u n t
d o u b le b a la n c e
in t t y p e
lo n g a c c o u n t N o
A c c o u n t in g P e r io d
D a te s ta r t
D a te e n d
21. 22
Credit Cashflow Rule
select * from Account acc,
Cashflow cf, AccountPeriod ap
where acc.accountNo == cf.accountNo and
cf.type == CREDIT
cf.date >= ap.start and
cf.date <= ap.end
trigger : acc.balance += cf.amount
rule “increase balance for AccountPeriod Credits”
when
ap : AccountPeriod()
acc : Account( $accountNo : accountNo )
CashFlow( type == CREDIT,
accountNo == $accountNo,
date >= ap.start && <= ap.end,
$ammount : ammount )
then
acc.balance += $amount;
end
22. 23
Rules as a “view”
CashFlow AccountingPeriod
date amount type accountNo start end
12-Jan-07 100 CREDIT 1 01-Jan-07 31-Mar-07
2-Feb-07 200 DEBIT 1
18-May-07 50 CREDIT 1 Account
9-Mar-07 75 CREDIT 1 accountNo balance
1 0
rule “increase balance for AccountPeriod rule “decrease balance for AccountPeriod
Credits” Debits”
when when
ap : AccountPeriod() ap : AccountPeriod()
acc : Account( $accountNo : accountNo ) acc : Account( $accountNo : accountNo )
CashFlow( type == CREDIT, CashFlow( type == DEBIT,
accountNo == $accountNo, accountNo == $accountNo,
date >= ap.start && <= ap.end, date >= ap.start && <= ap.end,
$ammount : ammount ) $ammount : ammount )
then then
acc.balance += $amount; acc.balance -= $amount;
end CashFlow end CashFlow
date amount type date amount type
12-Jan-07 100 CREDIT 2-Feb-07 200 DEBIT
9-Mar-07 75 CREDIT
Account
accountNo balance
1 -25
23. 24
Production Rule System
Approximated by SQL and Views
T a b le s A cco unt A c c o u n t in g P e r io d C a s h f lo w
O b je c t T y p e s A cco unt A c c o u n t in g P e r io d C a s h f lo w
V ie w s v ie w 1 v ie w 2
V ie w
m a in v ie w R u le s r u le 1 r u le 2
agenda
agenda
24. 25
Two Phase System
Determine
possible rules to
fire
Agenda Evaluation
Working Memory Action
Rule
insert modify
Found Select
Fire Rule
Rule to Fire
retract
No Rule
Found
exit
25. 26
Conflict Resolution with Salience
Salience
rule “Print blance for AccountPeriod”
salience -50
when
ap : AccountPeriod()
acc : Account( )
then
System.out.println( acc.accountNo + “ : “ acc.balance );
end
Agenda
1 increase balance
2 decrease balance arbitrary
3 increase balance
4 print balance
26. 27
RuleFlow
rule “increase balance for AccountPeriod Credits”
ruleflow-group “calculation”
when
ap : AccountPeriod()
acc : Account( $accountNo : accountNo )
CashFlow( type == CREDIT,
accountNo == $accountNo,
date >= ap.start && <= ap.end,
$ammount : ammount )
then
acc.balance += $amount;
end
ruleflow-group
rule “Print blance for AccountPeriod”
ruleflow-group “report”
when
ap : AccountPeriod()
acc : Account( )
then
System.out.println( acc.accountNo + “ : “ acc.balance );
end
27. 28
Definitions
public class Applicant {
private String name;
private int age;
private boolean valid;
// getter and setter methods here
}
rule "Is of valid age" when
$a : Applicant( age < 18 )
then
modify( $a ) { valid = false };
ends
29. 30
Executing
rule "Is of valid age" when
$a : Applicant( age < 18 )
then
modify( $a ) { valid = false };
ends
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
Applicant applicant = new Applicant( "Mr John Smith", 16 );
assertTrue( applicant.isValid() );
ksession.execute( applicant );
assertFalse( applicant.isValid() );
30. 31
Executing
rule "Is of valid age" when
$a : Applicant( age < 18 )
then
modify( $a ) { valid = false };
ends
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();
Applicant applicant = new Applicant( "Mr John Smith", 16 );
assertTrue( applicant.isValid() );
ksession.execute( applicant );
assertFalse( applicant.isValid() );
31. 32
Definitions
public class Room {
private String name
// getter and setter methods here
}
public class Sprinkler {
private Room room;
private boolean on;
// getter and setter methods here
}
public class Fire {
private Room room;
// getter and setter methods here
}
public class Alarm {
}
32. 33
Definitions
rule "When there is a fire turn on the sprinkler" when
Fire($room : room)
$sprinkler : Sprinkler( room == $room, on == false )
then
modify( $sprinkler ) { on = true };
println( "Turn on the sprinkler for room " + $room.name );
end
rule "When the fire is gone turn off the sprinkler" when
$room : Room( )
$sprinkler : Sprinkler( room == $room, on == true )
not Fire( room == $room )
then
modify( $sprinkler ) { on = false };
println( "Turn off the sprinkler for room " + $room.name );
end
33. 34
Definitions
rule "Raise the alarm when we have one or more fires" when
exists Fire()
then
insert( new Alarm() );
println( "Raise the alarm" );
end
rule "Cancel the alarm when all the fires have gone" when
not Fire()
$alarm : Alarm()
then
retract( $alarm );
println( "Cancel the alarm" );
end
34. 35
Definitions
rule "Status output when things are ok" when
not Alarm()
not Sprinkler( on == true )
then
println( "Everything is ok" );
end
35. 36
Executing
String[] names = new String[]{"kitchen", "bedroom", "office", "livingroom"};
Map<String,Room> name2room = new HashMap<String,Room>();
for( String name: names ){
Room room = new Room( name );
name2room.put( name, room );
ksession.insert( room );
Sprinkler sprinkler = new Sprinkler( room );
ksession.insert( sprinkler );
}
ksession.fireAllRules()
> Everything is ok
36. 37
Executing
Fire kitchenFire = new Fire( name2room.get( "kitchen" ) );
Fire officeFire = new Fire( name2room.get( "office" ) );
FactHandle kitchenFireHandle = ksession.insert( kitchenFire );
FactHandle officeFireHandle = ksession.insert( officeFire );
ksession.fireAllRules();
> Raise the alarm
> Turn on the sprinkler for room kitchen
> Turn on the sprinkler for room office
37. 38
Executing
ksession.retract( kitchenFireHandle );
ksession.retract( officeFireHandle );
ksession.fireAllRules()
> Turn off the sprinkler for room office
> Turn off the sprinkler for room kitchen
> Cancel the alarm
> Everything is ok
rule "Status output when things are ok" when
not Alarm()
not Sprinkler( on == true )
then
println( "Everything is ok" );
end
38. 39
Conditional Elements
not Bus( color = “red” )
exists Bus( color = “red” )
forall ( $bus : Bus( color == “red” ) )
forall ( $bus : Bus( floors == 2 )
Bus( this == $bus, color == “red” ) )
39. 40
Accumulate CE
rule "accumulate"
when
accumulate( Bus( color == "red", $t : takings );
$sum : sum( $t ) )
then
print "sum is “ + $sum;
end
accumulate( Bus( color == "red", $t : takings );
$sum : sum( $t ),
$min : min( $t )
$max : max( $t );
$min > 100 && $max < 200 )
40. 41
Timers
Field Name Mandatory? Allowed Values Allowed Special Characters
Seconds YES 0-59 ,-*/
Minutes YES 0-59 ,-*/
Hours YES 0-23 ,-*/
Day of month YES 1-31 ,-*?/LW
Month YES 1-12 or JAN-DEC ,-*/
Day of week YES 1-7 or SUN-SAT ,-*?/L#
Year NO empty, 1970-2099 ,-*/
Send alert every quarter of an hour
rule “name”
timer ( cron: 0 0/15 * * * * )
when
Alarm( )
then
sendEmail( ”Alert Alert Alert!!!” )
41. 42
Calendars
rule "weekdays are high priority"
calendars "weekday"
timer (int:0 1h) Execute now and after
1 hour duration
when
Alarm()
then
send( "priority high - we have an alarm” );
end
rule "weekend are low priority"
calendars "weekend"
timer (int:0 4h) Execute now and after
4 hour duration
when
Alarm()
then
send( "priority low - we have an alarm” );
end
42. 43
TMS and Inference
rule "Issue Child Bus Pass" Couples the logic
when
$p : Person( age < 16 )
then
insert(new ChildBusPass( $p ) );
end
rule "Issue Adult Bus Pass"
What happens when the Child
when stops being 16?
$p : Person( age >= 16 )
then
insert(new AdultBusPass( $p ) );
end
43. 44
TMS and Inference
Bad
● Monolithic
● Leaky
● Brittle integrity - manual maintenance
44. 45
TMS and Inference
A rule “logically” inserts an object
When the rule is no longer true, the object is retracted.
when
$p : Person( age < 16 ) de-couples the logic
then
logicalInsert( new IsChild( $p ) )
end
when
Maintains the truth by
$p : Person( age >= 16 ) automatically retracting
then
logicalInsert( new IsAdult( $p ) )
end
45. 46
TMS and Inference
rule "Issue Child Bus Pass"
when
$p : Person( )
IsChild( person =$p )
then
logicalInsert(new ChildBusPass( $p ) );
end
rule "Issue Adult Bus Pass"
The truth maintenance cascades
when
$p : Person()
IsAdult( person =$p )
then
logicalInsert(new AdultBusPass( $p ) );
end
46. 47
TMS and Inference
rule "Issue Child Bus Pass"
when
$p : Person( )
not( ChildBusPass( person == $p ) )
then
requestChildBusPass( $p );
end The truth maintenance cascades
47. 48
TMS and Inference
Good
● De-couple knowledge responsibilities
● Encapsulate knowledge
● Provide semantic abstractions for those encapsulation
● Integrity robustness – truth maintenance
48. 49
Backward Chaining
query isChild( Person p )
$p := Person( age <= 16 )
end
rule "Issue Child Bus Pass"
when
$p : Person( )
isChild( $p; )
then
logicalInsert(new ChildBusPass( $p ) );
end
52. 53
Questions?
• Dave Bowman: All right, HAL; I'll go in
through the emergency airlock.
• HAL: Without your space helmet, Dave,
you're going to find that rather difficult.
• Dave Bowman: HAL, I won't argue with
you anymore! Open the doors!
• HAL: Dave, this conversation can serve
no purpose anymore. Goodbye.
Joshua: Greetings, Professor Falken.
Stephen Falken: Hello, Joshua.
Joshua: A strange game. The only
winning move is not to play. How
about a nice game of chess?