Skip to content

Commit

Permalink
Merge pull request #59 from Adrian-Bielefeldt/26-exception-using-clos…
Browse files Browse the repository at this point in the history
…ed-reasoner

26 exception using closed reasoner
  • Loading branch information
irina-dragoste authored Nov 29, 2018
2 parents abb1adf + f8dee46 commit 1e53fd2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ public static Reasoner getInstance() {
*
* @param logLevel
* the logging level to be set for VLog C++ resource.
* @throws ReasonerStateException if the method is called on a closed reasoner.
*/
void setLogLevel(@NonNull LogLevel logLevel);
void setLogLevel(@NonNull LogLevel logLevel) throws ReasonerStateException;

/**
* Returns the logging level of the internal VLog C++ resource. If no value has
Expand All @@ -194,8 +195,9 @@ public static Reasoner getInstance() {
* the file for the internal VLog C++ resource to log to. If
* {@code null} or an invalid file path, the reasoner will log to the
* default system output.
* @throws ReasonerStateException if the method is called on a closed reasoner.
*/
void setLogFile(@Nullable String filePath);
void setLogFile(@Nullable String filePath) throws ReasonerStateException;

/**
* Adds rules to the reasoner <b>knowledge base</b> in the given order. After
Expand Down Expand Up @@ -322,10 +324,11 @@ void addFactsFromDataSource(@NonNull Predicate predicate, @NonNull DataSource da
* source ({@link #addFactsFromDataSource(Predicate, DataSource)})
* does nor match the arity of the facts in the corresponding data
* source.
* @throws ReasonerStateException if the method is called on a closed reasoner.
*/
// FIXME should EdbIdbSeparationException be thrown when users try to add
// facts/rules?
void load() throws IOException, EdbIdbSeparationException, IncompatiblePredicateArityException;
void load() throws IOException, EdbIdbSeparationException, IncompatiblePredicateArityException, ReasonerStateException;

/**
* Performs reasoning on the loaded <b>knowledge base</b>, depending on the set
Expand Down Expand Up @@ -358,7 +361,7 @@ void addFactsFromDataSource(@NonNull Predicate predicate, @NonNull DataSource da
* @throws IOException
* if I/O exceptions occur during reasoning.
* @throws ReasonerStateException
* if this method is called before loading ({@link Reasoner#load()}.
* if this method is called before loading ({@link Reasoner#load()} or after closing ({@link Reasoner#close()}).
*/
boolean reason() throws IOException, ReasonerStateException;

Expand Down Expand Up @@ -396,7 +399,7 @@ void addFactsFromDataSource(@NonNull Predicate predicate, @NonNull DataSource da
* @return an {@link AutoCloseable} iterator for {@link QueryResult}s,
* representing distinct answers to the query.
* @throws ReasonerStateException if this method is called before loading
* ({@link Reasoner#load()}.
* ({@link Reasoner#load()} or after closing ({@link Reasoner#close()}).
* @throws IllegalArgumentException if the given {@code queryAtom} contains
* terms ({@link Atom#getTerms()}) which are
* not of type {@link TermType#CONSTANT} or
Expand Down Expand Up @@ -441,7 +444,7 @@ void addFactsFromDataSource(@NonNull Predicate predicate, @NonNull DataSource da
* (representing named individuals).
*
* @throws ReasonerStateException if this method is called before loading
* ({@link Reasoner#load()}.
* ({@link Reasoner#load()} or after closing ({@link Reasoner#close()}).
* @throws IOException if an I/O error occurs regarding given file
* ({@code csvFilePath)}.
* @throws IllegalArgumentException
Expand All @@ -463,8 +466,9 @@ void exportQueryAnswersToCsv(@NonNull Atom queryAtom, @NonNull String csvFilePat
* {@link #load()} method). All facts inferred by reasoning are discarded. Rules
* and facts added to the reasoner need to be loaded again, to be able to
* perform querying and reasoning.
* @throws ReasonerStateException if the method is called on a closed reasoner.
*/
void resetReasoner();
void resetReasoner() throws ReasonerStateException;

// TODO Map<Predicate,DataSource> exportDBToDir(File location);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ public enum ReasonerState {
* adding rules, fact and fact data sources and setting the rule re-writing
* strategy are not allowed in this state.
*/
AFTER_REASONING("completed reasoning");
AFTER_REASONING("completed reasoning"),
/**
* State a Reasoner is in after method {@link Reasoner#close()} has been called.
* The Reasoner cannot reason again, once it reached this state.
* Loading and setting the reasoning algorithm in this state are ineffective.
* Reasoning, adding rules, fact and fact data sources and setting the rule re-writing
* strategy are not allowed in this state.
*/
AFTER_CLOSING("closed");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class VLogReasoner implements Reasoner {
public void setAlgorithm(final Algorithm algorithm) {
Validate.notNull(algorithm, "Algorithm cannot be null!");
this.algorithm = algorithm;
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
LOGGER.warn("Setting algorithm on a closed reasoner.");
}

@Override
Expand All @@ -92,6 +94,8 @@ public void setReasoningTimeout(Integer seconds) {
Validate.isTrue(seconds > 0, "Only strictly positive timeout period alowed!", seconds);
}
this.timeoutAfterSeconds = seconds;
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
LOGGER.warn("Setting timeout on a closed reasoner.");
}

@Override
Expand All @@ -112,6 +116,8 @@ public void addRules(final List<Rule> rules) throws ReasonerStateException {
}
Validate.noNullElements(rules, "Null rules are not alowed! The list contains a null at position [%d].");
this.rules.addAll(new ArrayList<>(rules));
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
LOGGER.warn("Adding rules to a closed reasoner.");
}

@Override
Expand All @@ -122,6 +128,7 @@ public void setRuleRewriteStrategy(RuleRewriteStrategy ruleRewritingStrategy) th
"Rules cannot be re-writen after the reasoner has been loaded! Call reset() to undo loading and reasoning.");
}
this.ruleRewriteStrategy = ruleRewritingStrategy;
LOGGER.warn("Setting rule rewrite strategy on a closed reasoner.");
}

@Override
Expand Down Expand Up @@ -150,6 +157,8 @@ public void addFacts(final Collection<Atom> facts) throws ReasonerStateException
this.factsForPredicate.putIfAbsent(predicate, new HashSet<>());
this.factsForPredicate.get(predicate).add(fact);
}
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
LOGGER.warn("Adding facts to a closed reasoner.");
}

@Override
Expand All @@ -167,6 +176,8 @@ public void addFactsFromDataSource(final Predicate predicate, final DataSource d
predicate, this.factsForPredicate.get(predicate));

this.dataSourceForPredicate.put(predicate, dataSource);
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
LOGGER.warn("Adding facts to a closed reasoner.");
}

private void validateFactTermsAreConstant(Atom fact) {
Expand All @@ -185,7 +196,9 @@ private void validateNoDataSourceForPredicate(final Predicate predicate) {
}

@Override
public void load() throws EdbIdbSeparationException, IOException, IncompatiblePredicateArityException {
public void load() throws EdbIdbSeparationException, IOException, IncompatiblePredicateArityException, ReasonerStateException {
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
throw new ReasonerStateException(reasonerState, "Loading is not allowed after closing.");
if (this.reasonerState != ReasonerState.BEFORE_LOADING) {
LOGGER.warn("This method call is ineffective: the Reasoner has already been loaded.");
} else {
Expand Down Expand Up @@ -242,6 +255,8 @@ private void validateDataSourcePredicateArities() throws IncompatiblePredicateAr
public boolean reason() throws IOException, ReasonerStateException {
if (this.reasonerState == ReasonerState.BEFORE_LOADING) {
throw new ReasonerStateException(this.reasonerState, "Reasoning is not allowed before loading!");
} else if (reasonerState.equals(ReasonerState.AFTER_CLOSING)) {
throw new ReasonerStateException(reasonerState, "Reasoning is not allowed after closing.");
} else if (this.reasonerState == ReasonerState.AFTER_REASONING) {
LOGGER.warn(
"This method call is ineffective: this Reasoner has already reasoned. Successive reason() calls are not supported. Call reset() to undo loading and reasoning and reload to be able to reason again");
Expand All @@ -268,6 +283,8 @@ public QueryResultIterator answerQuery(Atom queryAtom, boolean includeBlanks) th
final boolean filterBlanks = !includeBlanks;
if (this.reasonerState == ReasonerState.BEFORE_LOADING) {
throw new ReasonerStateException(this.reasonerState, "Querying is not alowed before reasoner is loaded!");
} else if (reasonerState.equals(ReasonerState.AFTER_CLOSING)) {
throw new ReasonerStateException(reasonerState, "Querying is not allowed after closing.");
}
Validate.notNull(queryAtom, "Query atom must not be null!");

Expand All @@ -287,6 +304,8 @@ public void exportQueryAnswersToCsv(final Atom queryAtom, final String csvFilePa
final boolean filterBlanks = !includeBlanks;
if (this.reasonerState == ReasonerState.BEFORE_LOADING) {
throw new ReasonerStateException(this.reasonerState, "Querying is not alowed before reasoner is loaded!");
} else if (reasonerState.equals(ReasonerState.AFTER_CLOSING)) {
throw new ReasonerStateException(reasonerState, "Querying is not allowed after closing.");
}
Validate.notNull(queryAtom, "Query atom must not be null!");
Validate.notNull(csvFilePath, "File to export query answer to must not be null!");
Expand All @@ -302,7 +321,9 @@ public void exportQueryAnswersToCsv(final Atom queryAtom, final String csvFilePa
}

@Override
public void resetReasoner() {
public void resetReasoner() throws ReasonerStateException {
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
throw new ReasonerStateException(reasonerState, "Resetting is not allowed after closing.");
this.reasonerState = ReasonerState.BEFORE_LOADING;
this.vLog.stop();
LOGGER.warn(
Expand All @@ -311,6 +332,7 @@ public void resetReasoner() {

@Override
public void close() {
reasonerState = ReasonerState.AFTER_CLOSING;
this.vLog.stop();
}

Expand Down Expand Up @@ -382,7 +404,9 @@ private void loadRules() {
}

@Override
public void setLogLevel(LogLevel logLevel) {
public void setLogLevel(LogLevel logLevel) throws ReasonerStateException {
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
throw new ReasonerStateException(reasonerState, "Setting log level is not allowed after closing.");
Validate.notNull(logLevel, "Log level cannot be null!");
this.internalLogLevel = logLevel;
this.vLog.setLogLevel(ModelToVLogConverter.toVLogLogLevel(this.internalLogLevel));
Expand All @@ -394,7 +418,9 @@ public LogLevel getLogLevel() {
}

@Override
public void setLogFile(String filePath) {
public void setLogFile(String filePath) throws ReasonerStateException {
if (reasonerState.equals(ReasonerState.AFTER_CLOSING))
throw new ReasonerStateException(reasonerState, "Setting log file is not allowed after closing.");
this.vLog.setLogFile(filePath);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testSetLogFileInexistent() throws ReasonerStateException, IOExceptio
}

@Test(expected = NullPointerException.class)
public void testSetLogLevelNull() {
public void testSetLogLevelNull() throws ReasonerStateException {
try (final Reasoner instance = Reasoner.getInstance()) {
instance.setLogLevel(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void testAddFacts2() throws EdbIdbSeparationException, IOException, Reaso
}

@Test
public void testResetBeforeLoad() {
public void testResetBeforeLoad() throws ReasonerStateException {
try (final Reasoner reasoner = Reasoner.getInstance()) {
reasoner.resetReasoner();
}
Expand Down Expand Up @@ -286,7 +286,7 @@ public void testFailExportQueryAnswerToCsvBeforeLoad() throws ReasonerStateExcep
}

@Test
public void testSuccessiveCloseAfterLoad() throws EdbIdbSeparationException, IOException, IncompatiblePredicateArityException {
public void testSuccessiveCloseAfterLoad() throws EdbIdbSeparationException, IOException, IncompatiblePredicateArityException, ReasonerStateException {
try (final Reasoner reasoner = Reasoner.getInstance()) {
reasoner.load();
reasoner.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
public class ReasonerTest {

@Test
public void testCloseRepeatedly() throws EdbIdbSeparationException, IOException, IncompatiblePredicateArityException {
public void testCloseRepeatedly() throws EdbIdbSeparationException, IOException, IncompatiblePredicateArityException, ReasonerStateException {
try (final VLogReasoner reasoner = new VLogReasoner()) {
reasoner.close();
}
Expand Down

0 comments on commit 1e53fd2

Please sign in to comment.