Search in sources :

Example 1 with RuleExecutorException

use of com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException in project jqa-core-framework by buschmais.

the class AggregationVerificationStrategy method verify.

@Override
public <T extends ExecutableRule> Result.Status verify(T executable, AggregationVerification verification, List<String> columnNames, List<Map<String, Object>> rows) throws RuleExecutorException {
    LOGGER.debug("Verifying result of " + executable);
    if (rows.isEmpty()) {
        return getStatus(executable, 0, verification.getMin(), verification.getMax());
    }
    if (columnNames.isEmpty()) {
        throw new RuleExecutorException("Result contains no columns, at least one with a numeric value is expected.");
    }
    String column = verification.getColumn();
    if (column == null) {
        column = columnNames.get(0);
        LOGGER.debug("No aggregation column specified, using " + column);
    }
    for (Map<String, Object> row : rows) {
        Object value = row.get(column);
        if (value == null) {
            throw new RuleExecutorException("The result does not contain a column '" + column);
        } else if (!Number.class.isAssignableFrom(value.getClass())) {
            throw new RuleExecutorException("The value in column '" + column + "' must be a numeric value but was '" + value + "'");
        }
        int aggregationValue = ((Number) value).intValue();
        Result.Status status = getStatus(executable, aggregationValue, verification.getMin(), verification.getMax());
        if (Result.Status.FAILURE.equals(status)) {
            return Result.Status.FAILURE;
        }
    }
    return Result.Status.SUCCESS;
}
Also used : RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Example 2 with RuleExecutorException

use of com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException in project jqa-core-framework by buschmais.

the class AnalyzerVisitorTest method ruleSourceInErrorMessage.

@Test
public void ruleSourceInErrorMessage() throws RuleException {
    String statement = "match (n) return n";
    Concept concept = createConcept(statement);
    when(store.executeQuery(Mockito.eq(statement), anyMap())).thenThrow(new IllegalStateException("An error"));
    ReportPlugin reportWriter = mock(ReportPlugin.class);
    try {
        AnalyzerVisitor analyzerVisitor = new AnalyzerVisitor(configuration, ruleParameters, store, reportWriter, console);
        analyzerVisitor.visitConcept(concept, Severity.MINOR);
        fail("Expecting an " + RuleExecutorException.class.getName());
    } catch (RuleExecutorException e) {
        String message = e.getMessage();
        assertThat(message, containsString(RULESOURCE));
    }
}
Also used : ReportPlugin(com.buschmais.jqassistant.core.report.api.ReportPlugin) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 3 with RuleExecutorException

use of com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method execute.

private <T extends ExecutableRule> Result<T> execute(T executableRule, Severity severity) throws RuleExecutorException {
    Map<String, Object> ruleParameters = getRuleParameters(executableRule);
    Executable executable = executableRule.getExecutable();
    // TODO extract to strategies
    if (executable instanceof CypherExecutable) {
        return executeCypher(executableRule, (CypherExecutable) executable, ruleParameters, severity);
    } else if (executable instanceof ScriptExecutable) {
        return executeScript(executableRule, (ScriptExecutable) executable, ruleParameters, severity);
    } else {
        throw new RuleExecutorException("Unsupported executable type " + executable);
    }
}
Also used : RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)

Example 4 with RuleExecutorException

use of com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException in project jqa-core-framework by buschmais.

the class RuleSetWriterImpl method write.

@Override
public void write(RuleSet ruleSet, Writer writer) throws RuleException {
    CollectRulesVisitor visitor = new CollectRulesVisitor();
    RuleSelection ruleSelection = RuleSelection.Builder.newInstance().addGroupIds(ruleSet.getGroupsBucket().getIds()).addConstraintIds(ruleSet.getConstraintBucket().getIds()).addConceptIds(ruleSet.getConceptBucket().getIds()).get();
    try {
        new RuleExecutor(visitor, configuration).execute(ruleSet, ruleSelection);
    } catch (RuleExecutorException e) {
        throw new RuleException("Cannot create rule set", e);
    }
    JqassistantRules rules = new JqassistantRules();
    writeGroups(visitor.getGroups(), rules);
    writeConcepts(visitor.getConcepts().keySet(), rules);
    writeConstraints(visitor.getConstraints().keySet(), rules);
    marshal(writer, rules);
}
Also used : RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) RuleExecutor(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutor) CollectRulesVisitor(com.buschmais.jqassistant.core.rule.api.executor.CollectRulesVisitor)

Example 5 with RuleExecutorException

use of com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException in project jqa-core-framework by buschmais.

the class AnalyzerVisitor method executeScript.

/**
     * Execute an analysis script
     * 
     * @param <T>
     *            The result type.
     * @param scriptExecutable
     *            The script.
     * @param ruleParameters
     * @param severity
     *            The severity. @return The result.
     * @throws RuleExecutorException
     *             If script execution fails.
     */
private <T extends ExecutableRule> Result<T> executeScript(T executable, ScriptExecutable scriptExecutable, Map<String, Object> ruleParameters, Severity severity) throws RuleExecutorException {
    String language = scriptExecutable.getLanguage();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
    if (scriptEngine == null) {
        List<String> availableLanguages = new ArrayList<>();
        for (ScriptEngineFactory factory : scriptEngineManager.getEngineFactories()) {
            availableLanguages.addAll(factory.getNames());
        }
        throw new RuleExecutorException("Cannot resolve scripting engine for '" + language + "', available languages are " + availableLanguages);
    }
    // Set default variables
    scriptEngine.put(ScriptVariable.STORE.getVariableName(), store);
    scriptEngine.put(ScriptVariable.RULE.getVariableName(), executable);
    scriptEngine.put(ScriptVariable.SEVERITY.getVariableName(), severity);
    // Set rule parameters
    for (Map.Entry<String, Object> entry : ruleParameters.entrySet()) {
        scriptEngine.put(entry.getKey(), entry.getValue());
    }
    Object scriptResult;
    try {
        scriptResult = scriptEngine.eval(scriptExecutable.getSource());
    } catch (ScriptException e) {
        throw new RuleExecutorException("Cannot execute script.", e);
    }
    if (!(scriptResult instanceof Result)) {
        throw new RuleExecutorException("Script returned an invalid result type, expected " + Result.class.getName() + " but got " + scriptResult);
    }
    return Result.class.cast(scriptResult);
}
Also used : ScriptException(javax.script.ScriptException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ScriptEngineFactory(javax.script.ScriptEngineFactory) ScriptEngine(javax.script.ScriptEngine) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Aggregations

RuleExecutorException (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)10 Result (com.buschmais.jqassistant.core.analysis.api.Result)4 XOException (com.buschmais.xo.api.XOException)3 ReportPlugin (com.buschmais.jqassistant.core.report.api.ReportPlugin)2 ScriptException (javax.script.ScriptException)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.Test)2 ConceptDescriptor (com.buschmais.jqassistant.core.analysis.api.model.ConceptDescriptor)1 CollectRulesVisitor (com.buschmais.jqassistant.core.rule.api.executor.CollectRulesVisitor)1 RuleExecutor (com.buschmais.jqassistant.core.rule.api.executor.RuleExecutor)1 Query (com.buschmais.xo.api.Query)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1