Show Table of Contents
6.4. Truth Maintenance
The inference engine is responsible for logical decisions on assertions and retraction of facts. After regular insertions, facts are generally retracted explicitly. However, in case of logical assertions, the fact that was asserted are automatically retracted when the conditions that asserted it in the first place are no longer true. In other words, the facts are retracted when there is no single condition that supports the logical assertion.
The inference engine uses a the mechanism of truth maintenance to efficiently handle the inferred information from rules. A Truth Maintenance System (TMS) refers to an inference engine's ability to enforce truthfulness when applying rules. It provides justified reasoning for each and every action taken by the inference engine. It validates the conclusions of an inference engine. If the inference engine asserts some data as a result of firing a rule, it uses the truth maintenance to justify the assertion.
A Truth Maintenance System also helps identify inconsistencies and handle contradictions. For example, if there are two rules to be fired, each resulting in a contradictory action, the Truth Maintenance System enables the inference engine to decide its actions based on assumptions and derivations of previously calculated conclusions. Truth maintenance plays an important role in enabling the inference engine to logically insert or retract facts. With logical assertions, the fact that was asserted are automatically retracted when the conditions that asserted it in the first place are no longer true.
The normal insertion of facts, referred to as stated insertions, are straight forward and do not need a reasoning. However, the logical assertions need to be justified. If the inference engine tries to logically insert an object when there is an equal stated object, it fails as it can not justify a stated fact. If the inference engine tries for a stated insertion of an existing equal object that is justified, then it overrides the justified insertion, and removes the justifications.
The following flowcharts illustrate the lifecycle of stated and logical insertions:

Figure 6.1. Stated Assertion

Figure 6.2. Logical Assertion
Important
For the Truth Maintenance System and logical assertions to work, your fact objects (POJOs) must override
equals and hashCode methods from java.lang.Object as per the Java standard.Two objects are equal if and only if their equals methods return true for each other and if their hashCode methods return the same values. For more information, refer the Java API documentation.
6.4.1. Example Illustrating Truth Maintenance
This example illustrates how the Truth Maintenance System helps in the inference mechanism. The following rules provides information on basic policies on issuing child and adult bus passes.
rule "Issue Child Bus Pass" when $p : Person( age < 16 ) then insert(new ChildBusPass( $p ) ); end rule "Issue Adult Bus Pass" when $p : Person( age >= 16 ) then insert(new AdultBusPass( $p ) ); end
These rules are monolithic and provide poor separation of concerns. The truth maintenance mechanism in an inference engine makes the system become more robust and have a clear separation of concerns. For example, the following rule uses logical insertion of facts, which makes the fact dependent on the truth of the when clause:
rule "Infer Child" when $p : Person( age < 16 ) then insertLogical( new IsChild( $p ) ) end rule "Infer Adult" when $p : Person( age >= 16 ) then insertLogical( new IsAdult( $p ) ) end
When the condition in the rule is false, the fact is automatically retracted. This works particularly well as the two rules are mutually exclusive. So in the above rules, if the person is under 16 years, it inserts an
IsChild fact. Once the person is 16 years or above, the IsChild fact is automatically retracted and the IsAdult fact inserted.
Now the two rules for issuing child and adult bus pass can logically insert the
ChildBusPass and AdultBusPass facts, as the Truth Maintenance System supports chaining of logical insertions for a cascading set of retracts. Here is how the logical insertion is done:
rule "Issue Child Bus Pass"
when
$p : Person( )
IsChild( person == $p )
then
insertLogical(new ChildBusPass( $p ) );
end
rule "Issue Adult Bus Pass"
when
$p : Person( age >= 16 )
IsAdult( person =$p )
then
insertLogical(new AdultBusPass( $p ) );
end
When a person turns 16 years old, the
IsChild fact as well as the person's ChildBusPass fact is retracted. To these set of conditions, you can relate another rule which states that a person must return the child pass after turning 16 years old. So when the Truth Maintenance System automatically retracts the ChildBusPass object, this rule triggers and sends a request to the person:
rule "Return ChildBusPass Request"
when
$p : Person( )
not( ChildBusPass( person == $p ) )
then
requestChildBusPass( $p );
end

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.