Package com.semedy.reasoner.api.core
Class DeductiveDatabase
java.lang.Object
com.semedy.reasoner.api.core.DeductiveDatabase
- Direct Known Subclasses:
QueryRunner,StreamReasoningDB
High level interface for a whole deductive database.
Encapsulates symbol handler, edb, idb, builtins, rewriters.
Provides adding/removing facts, adding/removing rules,
querying and transaction handling of fact and rule modifications.
DDB is thread local, so every thread has its own instance of DDB.
Core which is a member of DDB is shared for different DDBs.
Encapsulates symbol handler, edb, idb, builtins, rewriters.
Provides adding/removing facts, adding/removing rules,
querying and transaction handling of fact and rule modifications.
DDB is thread local, so every thread has its own instance of DDB.
Core which is a member of DDB is shared for different DDBs.
- Author:
- angele
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a fact to the deductive database.booleanaddFacts(Collection<Literal> facts) Add facts to the deductive database.booleanadd a rule to the deductive database.booleanadd a rule to the deductive database.booleanaddRules(Collection<Rule> rules, boolean parsed) Add rules to the deductive database.voidaddRulesAndFacts(String rulesAndFacts) add rules and facts from textvoidapplyChangeSet(List<String> paths) Applies changes to the extensional database.
During this operation the to be modified predicates
are read locked.voidcreate a backup for the databasevoidexport(InterruptFlag interrupt, List<String> predicates, String resultdirectory) bulk export of facts for predicates to raw files in result directoryvoidexportNQuads(Writer writer, String iri) export of facts as N-Quads.voidexportNQuads(String resultpath, String iri) export of facts as N-QuadsvoidexportOOLogic(InterruptFlag interrupt, Writer writer, String module) export of facts and rules for predicates to file in OO-logic into a module.voidexportOOLogic(InterruptFlag interrupt, String resultpath, String module) export of facts and rules for predicates to file in OO-logic into a module.voidexportOOLogic(InterruptFlag interrupt, List<Filter> filters, Writer writer, String module) export of facts and rules for predicates to file in OO-logic into a module.voidexportOOLogic(InterruptFlag interrupt, List<Filter> filter, String resultpath, String module) export of facts and rules for predicates to file in OO-logic into a module.voidexportRules(Writer writer) store all rules in file given by file pathvoidexportRules(String filepath, boolean append) store all rules in file given by file pathgetCore()Return the reasoner coregetRuleById(String ruleIdentifier) get a rule by its rule identifiergetRules()get all rulesget the extensional database for snapshotsget reasoning symbol tablevoidload files from file path
path can be a directory or a single file
files are distinguished by their endings:
.raw, .raw.zip, .rls, .fctvoidload files from file paths
files are distinguished by their endings:
.raw, .raw.zip, .rls, .fctvoidloadByStream(InputStream in, InputStream companionstream, String format) load facts by stream. format is either the file format like raw,json,nq etc.voidloadPredicates(InputStream input) Load facts and/or rules in predicate format from an input stream.booleanstart a long transaction
If a long transaction is already running false is returnedbooleancommit a long transactionvoiddiscard all changesvoidmaterializeQueries(InterruptFlag interruptFlag, String squeries, ReasoningMonitor monitor) Materialize queries.voidmaterializeQueries(InterruptFlag interruptFlag, String squeries, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) Materialize queries.voidmaterializeQueries(InterruptFlag interruptFlag, List<Rule> queries, ReasoningMonitor monitor) Materialize queries.voidmaterializeQueries(InterruptFlag interruptFlag, List<Rule> queries, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) Materialize queries.query(InterruptFlag interruptFlag, Rule query, int maxNumberOfResults, ReasoningMonitor monitor) Pose a query.query(InterruptFlag interruptFlag, Rule query, int maxNumberOfResults, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) Pose a query.query(InterruptFlag interruptFlag, String query, int maxNumberOfResults, Configuration config, Properties builtinConfig) Pose a query.query(InterruptFlag interruptFlag, String query, int maxNumberOfResults, ReasoningMonitor monitor) Pose a query.query(InterruptFlag interruptFlag, String query, int maxNumberOfResults, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) Pose a query.query(InterruptFlag interruptFlag, String query, Properties builtinConfig) Pose a query.query(InterruptFlag interruptFlag, List<Rule> queries, int maxNumberOfResults, ReasoningMonitor monitor) Pose a query and several rulesquery(InterruptFlag interruptFlag, List<Rule> queries, int maxNumberOfResults, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) Pose a query and several rulesquery(InterruptFlag interruptFlag, List<Rule> queries, int maxNumberOfResults, ReasoningMonitor monitor, ExtensionalDB[] additionalEDBs, IntensionalDB[] additionalIDBs, boolean skipAnswers, Configuration config, Properties builtinConfig) Pose a query and several rules.booleanRemove a fact from the deductive database.booleanRemove a fact from the deductive database.booleanremoveFacts(Collection<Literal> facts) Remove facts from the deductive database.booleanremoveFactsEncoded(Collection<Literal> facts) Remove facts from the deductive database.booleanremoveRuleById(String id) remove a rule identified by the unique id.booleanremoveRules(Collection<Rule> rules) Remove rules from the deductive database.voidremoveRulesAndFacts(String rulesAndFacts) remove rules and facts from textvoidstart a transactionvoidcommit all modificationsvoiddiscard all changes
-
Constructor Details
-
DeductiveDatabase
- Throws:
IOExceptionSemReasonerException
-
-
Method Details
-
getRules
get all rules- Returns:
- Throws:
SemReasonerException
-
getRuleById
get a rule by its rule identifier- Parameters:
ruleIdentifier-- Returns:
- Throws:
SemReasonerException
-
getSymbolTable
get reasoning symbol table- Returns:
-
getSnapshotEDB
get the extensional database for snapshots- Returns:
-
getCore
Return the reasoner core -
addFact
Add a fact to the deductive database.
This facts becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
L- , literal to be added- Returns:
- Throws:
IOExceptionEDBExceptionInterruptedException
-
addRulesAndFacts
public void addRulesAndFacts(String rulesAndFacts) throws SemReasonerException, InterruptedException add rules and facts from text- Parameters:
rulesAndFacts- , rules and facts as text- Throws:
SemReasonerExceptionInterruptedException
-
removeRulesAndFacts
public void removeRulesAndFacts(String rulesAndFacts) throws IOException, SemReasonerException, InterruptedException remove rules and facts from text- Parameters:
rulesAndFacts-- Throws:
IOExceptionSemReasonerExceptionInterruptedException
-
addRule
public boolean addRule(Rule R, boolean parsed) throws IOException, SemReasonerException, InterruptedException add a rule to the deductive database.- Parameters:
R- , rule to be added @return, true if rule has been addedparsed- , rule has been parse before- Throws:
IOExceptionSemReasonerExceptionInterruptedException
-
addRule
add a rule to the deductive database.- Parameters:
R- , rule to be added @return, true if rule has been added- Throws:
SemReasonerExceptionInterruptedException
-
removeRuleById
public boolean removeRuleById(String id) throws IOException, SemReasonerException, InterruptedException remove a rule identified by the unique id.- Parameters:
id-- Returns:
- true if rule has been removed
- Throws:
SemReasonerExceptionIOExceptionInterruptedException
-
removeFact
Remove a fact from the deductive database.
This change becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
L- , literal to be removed- Returns:
- true, if fact has been removed
- Throws:
IOExceptionEDBExceptionInterruptedException
-
removeFactEncoded
public boolean removeFactEncoded(Literal L) throws IOException, SemReasonerException, InterruptedException Remove a fact from the deductive database.
Fact is already encoded.
This change becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
L- , literal to be removed- Returns:
- true, if fact has been removed
- Throws:
IOExceptionSemReasonerExceptionInterruptedException
-
addFacts
public boolean addFacts(Collection<Literal> facts) throws EDBException, IOException, InterruptedException Add facts to the deductive database.
These facts becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
facts-- Returns:
- true, if all facts have been added
- Throws:
IOExceptionEDBExceptionInterruptedException
-
removeFacts
public boolean removeFacts(Collection<Literal> facts) throws EDBException, IOException, InterruptedException Remove facts from the deductive database.
This change becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
facts- to be added- Returns:
- true, if all facts have been added
- Throws:
IOExceptionEDBExceptionInterruptedException
-
removeFactsEncoded
public boolean removeFactsEncoded(Collection<Literal> facts) throws IOException, SemReasonerException, InterruptedException Remove facts from the deductive database.
Facts are already encoded.
This change becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
facts- to be added- Returns:
- true, if all facts have been added
- Throws:
IOExceptionSemReasonerExceptionInterruptedException
-
addRules
public boolean addRules(Collection<Rule> rules, boolean parsed) throws IOException, SemReasonerException, InterruptedException Add rules to the deductive database.
This change becomes visible to the current thread only.
These rules must not be parsed by rule parser before.
After commit it becomes visible to others as well.- Parameters:
rules- to be addedparsed- rules have been parsed before- Returns:
- true, if all rules have been added
- Throws:
IOExceptionSemReasonerExceptionInterruptedException
-
removeRules
public boolean removeRules(Collection<Rule> rules) throws IOException, SemReasonerException, InterruptedException Remove rules from the deductive database.
This change becomes visible to the current thread only.
After commit it becomes visible to others as well.- Parameters:
rules- to be removed.- Returns:
- true if all rules have been removed
- Throws:
SemReasonerExceptionIOExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, String query, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Pose a query. Get the results as an enumerator for tuples of objects- Parameters:
interruptFlag- for interrupting the evaluationquery- , the querybuiltinConfig- , configurations for built-ins e.g. access tokens- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionSimpleParseExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, String query, int maxNumberOfResults, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Pose a query. Get the results as an enumerator for tuples of objects- Parameters:
interruptFlag- for interrupting the evaluationquery- , the querymaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsbuiltinConfig- , configurations for built-ins e.g. access tokens- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionSimpleParseExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, String query, int maxNumberOfResults, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Pose a query. Get the results as an enumerator for tuples of objects- Parameters:
interruptFlag- for interrupting the evaluationquery- , the querymaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , monitoring reasoning, may be nullbuiltinConfig- , configurations for built-ins e.g. access tokens- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionSimpleParseExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, String query, int maxNumberOfResults, ReasoningMonitor monitor) throws SemReasonerException, InterruptedException, IOException Pose a query. Get the results as an enumerator for tuples of objects- Parameters:
interruptFlag- for interrupting the evaluationquery- , the querymaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , monitoring reasoning, may be null- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionSimpleParseExceptionIOExceptionSemReasonerExceptionInterruptedException
-
transactionBegin
start a transaction- Throws:
SemReasonerExceptionIOException
-
longTransactionBegin
start a long transaction
If a long transaction is already running false is returned- Throws:
SemReasonerExceptionIOException
-
longTransactionCommit
public boolean longTransactionCommit() throws SemReasonerException, EDBException, IOException, InterruptedExceptioncommit a long transaction -
longTransactionRollback
discard all changes -
transactionRollback
discard all changes -
transactionCommit
commit all modifications -
load
public void load(String path) throws EDBException, IOException, SimpleParseException, SemReasonerException, InterruptedException, SymboltableException load files from file path
path can be a directory or a single file
files are distinguished by their endings:
.raw, .raw.zip, .rls, .fct- Parameters:
path-- Throws:
EDBExceptionIOExceptionSimpleParseExceptionSemReasonerExceptionInterruptedExceptionSymboltableException
-
loadByStream
public void loadByStream(InputStream in, InputStream companionstream, String format) throws EDBException, IOException, SimpleParseException, SemReasonerException, SymboltableException, InterruptedException load facts by stream. format is either the file format like raw,json,nq etc. In case of raw it is the predicate like value|3 A companionstream contains additional information, e.g. schema information for csv- Parameters:
in- , stream for the csv datacompanionstream- , additional stream describing the data types and new header namesformat-- Throws:
UnsupportedEncodingExceptionIOExceptionSemReasonerExceptionInterruptedExceptionEDBExceptionSimpleParseExceptionSymboltableException
-
load
public void load(List<String> paths) throws EDBException, IOException, SimpleParseException, SemReasonerException, InterruptedException, SymboltableException load files from file paths
files are distinguished by their endings:
.raw, .raw.zip, .rls, .fct- Parameters:
paths-- Throws:
EDBExceptionIOExceptionSimpleParseExceptionSemReasonerExceptionInterruptedExceptionSymboltableException
-
loadPredicates
public void loadPredicates(InputStream input) throws SemReasonerException, IOException, SimpleParseException, InterruptedException, EDBException Load facts and/or rules in predicate format from an input stream. Loading facts in this way is much slower than raw files or fct files.- Parameters:
input- , the input stream- Throws:
IOExceptionSimpleParseExceptionSemReasonerExceptionInterruptedExceptionEDBException
-
backup
create a backup for the database- Parameters:
backupfile-- Throws:
IOExceptionEDBExceptionSemReasonerException
-
export
public void export(InterruptFlag interrupt, List<String> predicates, String resultdirectory) throws EDBException, SemReasonerException, IOException, InterruptedException bulk export of facts for predicates to raw files in result directory- Parameters:
interrupt- , flag to interrupt exportpredicates-resultdirectory-- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportOOLogic
public void exportOOLogic(InterruptFlag interrupt, String resultpath, String module) throws EDBException, SemReasonerException, IOException, InterruptedException export of facts and rules for predicates to file in OO-logic into a module.
If there is already a module, this one is used.
If module is null, it is exported but not filled into a module.- Parameters:
interrupt- , flag to interrupt exportresultpath-module- , module name- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportOOLogic
public void exportOOLogic(InterruptFlag interrupt, List<Filter> filter, String resultpath, String module) throws EDBException, SemReasonerException, IOException, InterruptedException export of facts and rules for predicates to file in OO-logic into a module.
If there is already a module, this one is used.
If module is null, it is exported but not filled into a module.- Parameters:
interrupt- , flag to interrupt exportfilter- , a list of filters for predicates to be exportedresultpath-module- , module name- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportOOLogic
public void exportOOLogic(InterruptFlag interrupt, Writer writer, String module) throws EDBException, SemReasonerException, IOException, InterruptedException export of facts and rules for predicates to file in OO-logic into a module.
If there is already a module, this one is used.
If module is null, it is exported but not filled into a module.- Parameters:
interrupt- , flag to interrupt exportwriter-- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportOOLogic
public void exportOOLogic(InterruptFlag interrupt, List<Filter> filters, Writer writer, String module) throws EDBException, SemReasonerException, IOException, InterruptedException export of facts and rules for predicates to file in OO-logic into a module.
If there is already a module, this one is used.
If module is null, it is exported but not filled into a module.- Parameters:
interrupt- , flag to interrupt exportfilters- , predicates together with filters to export.writer-- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportNQuads
public void exportNQuads(Writer writer, String iri) throws EDBException, SemReasonerException, IOException, InterruptedException export of facts as N-Quads.- Parameters:
writer-iri-- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportNQuads
public void exportNQuads(String resultpath, String iri) throws EDBException, SemReasonerException, IOException, InterruptedException export of facts as N-Quads- Parameters:
resultpath- , path of fileiri- , iri used for the n-quads- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
exportRules
store all rules in file given by file path- Parameters:
filepath-append- , rules are appended to the file- Throws:
SemReasonerExceptionIOException
-
exportRules
store all rules in file given by file path- Parameters:
writer- , writer to write rules to- Throws:
SemReasonerExceptionIOException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, Rule query, int maxNumberOfResults, ReasoningMonitor monitor) throws SemReasonerException, InterruptedException, IOException Pose a query. Get the results as an enumerator for tuples of objects- Parameters:
interruptFlag- for interrupting the evaluationquery- , the querymonitor- , monitoring reasoning, may be nullmaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , reasoning monitor- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, Rule query, int maxNumberOfResults, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Pose a query. Get the results as an enumerator for tuples of objects- Parameters:
interruptFlag- for interrupting the evaluationquery- , the querymonitor- , monitoring reasoning, may be nullmaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , reasoning monitorconfig- , configurationbuiltinConfig- , configurations for built-ins e.g. access tokens- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionIOExceptionSemReasonerExceptionInterruptedException
-
applyChangeSet
public void applyChangeSet(List<String> paths) throws EDBException, SemReasonerException, IOException, InterruptedException Applies changes to the extensional database.
During this operation the to be modified predicates
are read locked. A write lock is used for a short time only at the end of the operation
The input files are raw files
For every predicate p two files may be given:
_add_p contains tuples to be added
_delete_p contains tuples to be deleted
if T(p) is the set of tuples for predicate p,
add(p) is the set of tuples to be added,
del(p) the set of tuples to be deleted
(T(p)-del(p)) + add(p) is computed- Parameters:
paths- , paths of the input files- Throws:
EDBExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, List<Rule> queries, int maxNumberOfResults, ReasoningMonitor monitor) throws SemReasonerException, InterruptedException, IOException Pose a query and several rules- Parameters:
interruptFlag- for interrupting the evaluationqueries- , a query and rulesmaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , monitoring reasoning, may be null- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, List<Rule> queries, int maxNumberOfResults, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Pose a query and several rules- Parameters:
interruptFlag- for interrupting the evaluationqueries- , a query and rulesmaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , monitoring reasoning, may be nullconfig- , configurationbuiltinConfig- , built-in configuration like access tokens etc.- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionIOExceptionSemReasonerExceptionInterruptedException
-
query
public ResultEnumerator query(InterruptFlag interruptFlag, List<Rule> queries, int maxNumberOfResults, ReasoningMonitor monitor, ExtensionalDB[] additionalEDBs, IntensionalDB[] additionalIDBs, boolean skipAnswers, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Pose a query and several rules.- Parameters:
interruptFlag- for interrupting the evaluationqueries- , list of queriesmaxNumberOfResults- , the maximum number of result tuples, -1 for all resultsmonitor- , monitoring reasoning, may be nulladditionalEDBs- , additional edbs to be used during queryingadditionalIDBs- , additional idbs to be used during queryingmonitor- , reasoning monitorbuiltinConfig- , configurations for built-ins e.g. access tokens- Returns:
- an enumerator for results
- Throws:
ReasoningExceptionIOExceptionSemReasonerExceptionInterruptedException
-
materializeQueries
public void materializeQueries(InterruptFlag interruptFlag, List<Rule> queries, ReasoningMonitor monitor) throws SemReasonerException, InterruptedException, IOException Materialize queries.
This means that the queries are evaluated and the results are stored in the database.- Parameters:
interruptFlag-queries- , the queries to be materializedmonitor- , reasoning monitor- Throws:
ReasoningExceptionInterruptedExceptionIOExceptionSemReasonerException
-
materializeQueries
public void materializeQueries(InterruptFlag interruptFlag, List<Rule> queries, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Materialize queries.
This means that the queries are evaluated and the results are stored in the database.- Parameters:
interruptFlag-queries- , the queries to be materializedmonitor- , reasoning monitorconfig- , configurationbuiltinConfig- , configurations for built-ins e.g. access tokens- Throws:
ReasoningExceptionInterruptedExceptionIOExceptionSemReasonerException
-
materializeQueries
public void materializeQueries(InterruptFlag interruptFlag, String squeries, ReasoningMonitor monitor) throws ReasoningException, InterruptedException, IOException, SemReasonerException Materialize queries.
This means that the queries are evaluated and the results are stored in the database.- Parameters:
interruptFlag-squeries- , the queries to be materializedmonitor- , reasoning monitor- Throws:
ReasoningExceptionInterruptedExceptionIOExceptionSemReasonerException
-
materializeQueries
public void materializeQueries(InterruptFlag interruptFlag, String squeries, ReasoningMonitor monitor, Configuration config, Properties builtinConfig) throws SemReasonerException, InterruptedException, IOException Materialize queries.
This means that the queries are evaluated and the results are stored in the database.- Parameters:
interruptFlag-squeries- , the queries to be materializedmonitor- , reasoning monitorconfig- , configurationbuiltinConfig- , built-in configuration like access tokens etc.- Throws:
ReasoningExceptionInterruptedExceptionSemReasonerExceptionIOException
-