Documentation: Programming guide

Rules and facts

Rules are logical expressions that have a prescribed probability or a probability interval. They represent beliefs on the variables of the knowledge base. Rules are defined in the SPIRIT Syntax of rules. They might consist of a premise and a conclusion. Facts are rules without any premise, or rather said the premise delivering true.

This chapter explains how to declare and use rules in SPIRIT.

Rule Indexing

Each `KBase` can manage a list of rules, each of which is accessible through a unique index starting at zero. You can find out the number of rules using the following call:

````int ruleCount = kb.getRuleCount();`
```

The first rule inserted to the list is assigned to index zero, whereas each additional rule will receive a corresponding index.Therefore deleting one rule will decrease the indices of higher value by one.

Declaring Rules

Like variables, rules can be imported from a file and declared directly. To declare a new rule or fact you have to supply a string of the SPIRIT Syntax of rules and an instance of either `SinglePreProb` or `IntervalPreProb` to prescribe a probabilty (interval). All variables and values mentioned in the rule string must be valid at the time of declaration. Here are some sample rule declarations:

````kb.addRule("A & (B=1 | C<7) => D", new SinglePreProb(0.7), -1);`
`kb.addRule("SENSE=42", new IntervalPreProb(0,1), 0);`
```

In case you are not sure, if a rule string is correct, you can check it using the `KBase.ruleStringCheck` method. This is particularly useful in interactive systems with a rule editor.

Deleting Rules

To delete a rule from the `KBase`, just call the `KBase.deleteRule` method with the index of the rule you want to get rid of. Remember that this operation might change some other rules' indices.

Editing Rules

After its declaration, a rule cannot be edited. Only the prescribed probability can be changed. Use the method `KBase.setRulePreProb` to do this.

The only way of "editing" an existing rule is to delete it and add it anew. Therefore it is possible to supply the `KBase.addRule` method with an additional `ruleIndex` argument. To "edit" a rule, delete it and insert a new one with the old index of it.

Assigning User Infos to a Rule

You can assign arbitrary user infos to each rule (compareable to those infos that might be assigned to knowlegde bases and variables). Examples for such information are comments on the rule or buffering of old alpha values. Use the twin methods `KBase.getRuleUserInfo` and `KBase.setRuleUserInfo` to get and set those infos.