# Documentation: Programming guide

## Making queries

SPIRIT mainly provides two ways of making queries, both of which are described in this chapter.

- Assigning evidence to certain variables and observing the impact on other probabilities.
- Learning additional rules that serve as premises for the conditional probability of other rules and facts
- Getting a Variable's Probabilities
- Getting a Rule's Probabilities
- Assigning Evidence
- Complex Queries

### Getting a Variable's Probabilities

The simplest way of querying the actual knowlegde is to compute the actual probability
of a certain variable value. This can be done using the
`KBase.valueProb`

method.
The following sequence computes these values for variable 0:

`int arity = kb.getVarArity(0);`

`double[] p = new double[arity];`

`double rest = 1;`

`for(int i = 0; i < arity - 1; i++) {`

`p[i] = kb.valueProb(0,i);`

`rest -= p[i];`

`}`

`p[arity - 1] = rest; »The last value is 1 - sum of the others«`

Of similar interest is the expected value of a variable. This is defined as the
weightened sum of the values' probabilities and can be computed using
`KBase.expectedValue`

.

### Getting a Rule's Probabilities

The actual conditional probability of a rule or fact can be computed with the
`KBase.ruleProb`

method. After a
successful iteration process, this value should be the rule's prescribed probability
plus a tolerance given as the threshold for the iteration. For non-facts it is also
possible to compute the actual premise probability with the method
`KBase.rulePremProb`

.

### Assigning Evidence

So far about the ways to get the information the user might be interested in. Assigning
evidence to a variable is a very simple way of querying a `KBase`

. SPIRIT
provides two methods for that purpose. First, call
`KBase.assignEvidence`

to assign evidence to certain values. After measuring the impact on the probabilities of
other variables, call `KBase.assignEvidenceReset`

to return to the original distribution. Remember that many methods are unavailable when
evidence is assigned.

### Complex Queries

Making complex queries means to learn additional rules (the premises) into the
`KBase`

and compute the probabilities of other rules, that were not learned
before (the conclusions). Here is a general technique to make such complex queries:

- Add the set of premise rules to the KBase.
- To be able to learn those into the KBase, be sure that each of them is active. Otherwise rebuild the structure and reinit it.
- Then add the set of conclusions to the KBase.
- Then make all rules but the premises activable or passive, for that they would not be used in the following iteration process.
- Make a full iteration on the premises.
- After a successful ending, query the actual probabilities of the conclusions (they don't need to be active or activable).
- Then you could remove the newly added rules again and restore the old knowlegde state with a reinit. Make sure that the original rules are active.