Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

3.3. Advanced Concepts and Theory


3.3.1. Logical Assertions

After a standard object insertion, you have to retract facts explicitly. With logical assertions, the fact that was asserted will be automatically retracted when the conditions that asserted it in the first place are no longer true. It will be retracted only if there isn't any single condition that supports the logical assertion.

3.3.2. Stated Insertions

Normal insertions are said to be stated, as in "stating a fact". Using a HashMap and a counter, you can track how many times a particular equality is stated. This means you can count how many different instances are equal.

3.3.3. Justified Insertions

When an object is logically inserted, it is said to be justified. It is considered to be justified by the firing rule. For each logical insertion there can only be one equal object, and each subsequent equal logical insertion increases the justification counter for this logical assertion. A justification is removed when the creating rule's LHS becomes untrue, and the counter is decreased accordingly. As soon as there are no more justifications, the logical object is automatically retracted.

3.3.4. The WM_BEHAVIOR_PRESERVE Setting

If you try to logically insert an object when there is an equal stated object, this will fail and return null. If you state an object that has an existing equal object that is justified, you will override the Fact. How this override works depends on the configuration setting WM_BEHAVIOR_PRESERVE. When the property is set to discard, you can use the existing handle and replace the existing instance with the new Object, which is the default behavior. Otherwise you should override it to stated but create an new FactHandle.

3.3.5. Stated Insertion Flowchart

3.3.6. Logical Insertion Flowchart

3.3.7. The Truth Maintenance System

The JBoss Rules Truth Maintenance System (TMS) is a method of representing beliefs and their related dependencies/justifications in a knowledge base.

Important

For Truth Maintenance (and logical assertions) to work, the Fact objects must override equals and hashCode methods correctly. As the truth maintenance system needs to know when two different physical objects are equal in value, both equals and hashCode must be overridden correctly, as per the Java standard.
Two objects are equal only if their equals methods return true for each other and if their hashCode methods return the same values. You must override both equals and hashCode.

3.3.8. The insertLogical Fact

The insertLogical fact is part of the JBoss Rules TMS. It "inserts logic" so that rules behave and are modified according to the situation. For example, the insertLogical fact can be added to a set of rules so that when a rule becomes false, the fact is automatically retracted.

3.3.9. Using Inference and TMS

Procedure 3.7. Task

  1. In this example we will use a bus pass issuing system. See the code snippet below:
    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
    Copy to Clipboard Toggle word wrap
  2. Insert the insertLogical property to provide inference:
    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
    Copy to Clipboard Toggle word wrap
  3. Re-enter the code to issue the passes. These two configurations can also be logically inserted, as the TMS supports chaining of logical insertions for a cascading set of retracts:
    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
    Copy to Clipboard Toggle word wrap
    Now when the person changes from being 15 to 16, both the IsChild fact and the person's ChildBusPass fact are automatically retracted.
  4. Optionally, insert the not conditional element to handle notifications. (In this example, a request for the returning of the pass.) When the TMS 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
    Copy to Clipboard Toggle word wrap
Nach oben
Red Hat logoGithubredditYoutubeTwitter

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2025 Red Hat