The honest politician example demonstrates truth maintenance with logical assertions, the basic premise is that an object can only exist while a statement is true. A rule's consequence can logical insert an object with the insertLogical method, this means the object will only remain in the working memory as long as the rule that logically inserted it remains true, when the rule is no longer true the object is automatically retracted.
In this example there is Politician class with a name and a boolean value for honest state, four politicians with honest state set to true are inserted.
Example 8.62. Politician Class
public class Politician { private String name; private boolean honest; ... }
Example 8.63. Honest Politician Example Execution
Politician blair = new Politician("blair", true); Politician bush = new Politician("bush", true); Politician chirac = new Politician("chirac", true); Politician schroder = new Politician("schroder", true); ksession.insert( blair ); ksession.insert( bush ); ksession.insert( chirac ); ksession.insert( schroder ); ksession.fireAllRules();
The console out shows that while there is atleast one honest
polician democracy lives, however as each politician is in turn corrupted
by an evil corporation, when all politicians are dishonest democracy is
dead.
Example 8.64. Honest Politician Example Console Output
Hurrah!!! Democracy Lives I'm an evil corporation and I have corrupted schroder I'm an evil corporation and I have corrupted chirac I'm an evil corporation and I have corrupted bush I'm an evil corporation and I have corrupted blair We are all Doomed!!! Democracy is Dead
As soon as there is one more more honest politcians in the
working memory a new Hope object is logically asserted, this object will
only exist while there is at least one or more honest politicians, the
moment all politicians are dishonest then the Hope object will be
automatically retracted. This rule is given a salience of 10 to make sure
it fires before any other rules, as at this stage the "Hope is Dead" rule
is actually true.
Example 8.65. Honest Politician Example : Rule "We have an honest politician"
rule "We have an honest Politician" salience 10 when exists( Politician( honest == true ) ) then insertLogical( new Hope() ); end
As soon as a Hope object exists the "Hope Lives" rule matches, and fires, it has a salience of 10 so that it takes priority over "Corrupt the Honest".
Example 8.66. Honest Politician Example : Rule "Hope Lives"
rule "Hope Lives" salience 10 when exists( Hope() ) then System.out.println("Hurrah!!! Democracy Lives"); end
Now that hope exists and we have, at the start, four honest politicians we have 4 activations for this rule all in conflict. This rule iterates over those rules firing each one in turn, corrupting each politician so that they are no longer honest. When all four politicians have been corrupted we have no politicians with the property "honest == true" thus the rule "We have an honest Politician" is no longer true and the object it logical inserts "new Hope()" is automatically retracted.
Example 8.67. Honest Politician Example : Rule "Corrupt the Honest"
rule "Corrupt the Honest" when politician : Politician( honest == true ) exists( Hope() ) then System.out.println( "I'm an evil corporation and I have corrupted " + politician.getName() ); modify ( politician ) { honest = false }; end
With Hope being automatically retracted, via the truth maintenance system, then Hope no longer exists in the system and this rule will match and fire.
Example 8.68. Honest Politician Example : Rule "Hope is Dead"
rule "Hope is Dead" when not( Hope() ) then System.out.println( "We are all Doomed!!! Democracy is Dead" ); end
lets take a look the audit trail for this application:
The moment we insert the first politician we have two activations, the "We have an honest Politician" is activated only once for the first inserted politician because it uses an existential 'exists' conditional element which only matches. the rule "Hope is Dead" is also activated at this stage, because as of yet we have not inserted the Hope object. "We have an honest Politician" fires first, as it has a higher salience over "Hope is Dead" which inserts the Hope object, that action is highlighted green above. The insertion of the Hope object activates "Hope Lives" and de-activates "Hope is Dead", it also actives "Corrupt the Honest" for each inserted honested politician. "Rule Hope Lives" executes printing "Hurrah!!! Democracy Lives". Then for each politician the rule "Corrupt the Honest" fires printing "I'm an evil corporation and I have corrupted X", where X is the name of the politician, and modifies the politicians honest value to false. When the last honest polician is corrupted Hope is automatically retracted, by the truth maintenance system, as shown by the blue highlighted area. The green highlighted area shows the origin of the currently selected blue highlighted area. Once Hope is retracted "Hope is dead" activates and fires printing "We are all Doomed!!! Democracy is Dead".