KDE 4.6 PyKDE API Reference

- KDE's Python API

# InferenceModel Class Reference

`from PyKDE4.soprano import *`

Inherits: Soprano.FilterModel → Soprano.Model → QObject
Namespace: Soprano.Inference

## Detailed Description

\class InferenceModel inferencemodel.h Soprano/Inference/InferenceModel

The Soprano Inference Model provides a simple forward chaining inference engine which uses the underlying parent model itself to store status information.

The InferenceModel does perfect inference which means that removing of statements is supported and results in a perfect update of the infered statements. There is only one exception: If a model contains two statements in different named graphs that both have the same subject, predicate, and object and trigger one rule then if one of these statements is removed the infered statements are removed, too, although the second statement would still make the infered one valid. This situation gets resolved once the same rule is triggered again by some other added statement or performInference gets called.

The inference is performed based on rules which are stored in Rule instances. Rules can be created manually or parsed using a RuleParser.

The inference engine works roughly as follows:

Whenever a new statement is added it is compared to each rule to check if it could trigger this rule. Then if it could trigger a rule this rule is applied to the whole model.

If a rule produces a new infered statement the following data is created:

- named graph A containing the infered statements
- the statements that triggered the rule are stored in named graph sil:InferenceMetadata as
source statements of A (sil:sourceStatement). The inference model supports two ways of storing source statements: plain and compressed (see setCompressedSourceStatements).

Thus, when removing a statement it can easily be checked if this statement had been used to infer another one by querying all named graphs that have this statement as a source statement.

Author:
  Sebastian Trueg <trueg@kde.org>

## Methods

| __init__ (self, Soprano.Model parent)
| addRule (self, Soprano.Inference.Rule a0)
Soprano.Error.ErrorCode | addStatement (self, Soprano.Statement a0)
| clearInference (self)
| performInference (self)
Soprano.Error.ErrorCode | removeAllStatements (self, Soprano.Statement a0)
Soprano.Error.ErrorCode | removeStatement (self, Soprano.Statement a0)
| setCompressedSourceStatements (self, bool b)
| setOptimizedQueriesEnabled (self, bool b)
| setRules (self, [Soprano.Inference.Rule] rules)

## Method Documentation

__init__ | ( | self, |
|  | Soprano.Model | parent
| ) |  |  |

addRule | ( | self, |
|  | Soprano.Inference.Rule | a0
| ) |  |  |

Add an inference rule to the set of rules. This method will not trigger any inference action. If inference is necessary call performInference() after adding the new rules.

Soprano.Error.ErrorCode addStatement | ( | self, |
|  | Soprano.Statement | a0
| ) |  |  |

Add a new statement to the model. Inferencing will be done directly. Inferenced statements are stored in additional named graphs.

clearInference | ( |  | self ) |

Removes all statements infered by this model. This can be useful if the parent model has been changed without informing the inference model and statements have been removed.

performInference | ( |  | self ) |

Normally inferencing is done once new statements are added to the model or statements are removed. This method performs inferencing on the whole model. It is useful for initializing a model that already contains statements or update the model if it has been modified bypassing this filter model.

Tha latter can easily be done by connecting the Model.statementsAdded and Model.statementsRemoved signals to this slot.

Soprano.Error.ErrorCode removeAllStatements | ( | self, |
|  | Soprano.Statement | a0
| ) |  |  |

Remove statements from the model.

Soprano.Error.ErrorCode removeStatement | ( | self, |
|  | Soprano.Statement | a0
| ) |  |  |

Remove one statement from the model.

setCompressedSourceStatements | ( | self, |
|  | bool | b
| ) |  |  |

If compressed statements are enabled source statements are stored compressed in one literal value. Otherwise source statements are stored using rdf:subject, rdf:predicate, rdf:object, and sil:context. Non-compressed statements are much cleaner from an ontology design point of view while compressed statements take much less space.

By default comressed source statements are enabled.

This method exists mainly for historical reasons and there normally is no need to call it. Compressed statements should work well for most users.

Parameters:

  | b | If true compressed source statements are enabled (the default).

setOptimizedQueriesEnabled | ( | self, |
|  | bool | b
| ) |  |  |

If the storage backend supports joined SPARQL queries via UNION it makes sense to enable this.

Parameters:

  | b | If true InferenceModel will use optimized queries for the inference during addStatement. This will speed up the process quite a lot as matching rules are only applied to the new statement. This flag has no influcence on performInference() though.

The default is to disable the optimized queries since the default soprano redland backend does not support UNION.

setRules | ( | self, |
|  | [Soprano.Inference.Rule] | rules
| ) |  |  |

Set the inference rules to be used. This method will not trigger any inference action. If inference is necessary call performInference() after adding the new rules.