Search in sources :

Example 6 with RuleExecutorException

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

the class AnalyzerVisitor method visitConcept.

@Override
public boolean visitConcept(Concept concept, Severity effectiveSeverity) throws RuleExecutorException {
    try {
        store.beginTransaction();
        ConceptDescriptor conceptDescriptor = store.find(ConceptDescriptor.class, concept.getId());
        Result.Status status;
        if (conceptDescriptor == null || configuration.isExecuteAppliedConcepts()) {
            logger.info("Applying concept '" + concept.getId() + "' with severity: '" + concept.getSeverity().getInfo(effectiveSeverity) + "'.");
            reportPlugin.beginConcept(concept);
            Result<Concept> result = execute(concept, effectiveSeverity);
            reportPlugin.setResult(result);
            status = result.getStatus();
            if (conceptDescriptor == null) {
                conceptDescriptor = store.create(ConceptDescriptor.class);
                conceptDescriptor.setId(concept.getId());
                conceptDescriptor.setStatus(result.getStatus());
            }
            reportPlugin.endConcept();
        } else {
            status = conceptDescriptor.getStatus();
        }
        store.commitTransaction();
        return Result.Status.SUCCESS.equals(status);
    } catch (XOException e) {
        store.rollbackTransaction();
        throw new RuleExecutorException("Cannot apply concept " + concept.getId(), e);
    }
}
Also used : XOException(com.buschmais.xo.api.XOException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ConceptDescriptor(com.buschmais.jqassistant.core.analysis.api.model.ConceptDescriptor) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Example 7 with RuleExecutorException

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

the class AnalyzerVisitor method visitConstraint.

@Override
public void visitConstraint(Constraint constraint, Severity effectiveSeverity) throws RuleExecutorException {
    logger.info("Validating constraint '" + constraint.getId() + "' with severity: '" + constraint.getSeverity().getInfo(effectiveSeverity) + "'.");
    try {
        store.beginTransaction();
        reportPlugin.beginConstraint(constraint);
        reportPlugin.setResult(execute(constraint, effectiveSeverity));
        reportPlugin.endConstraint();
        store.commitTransaction();
    } catch (XOException e) {
        store.rollbackTransaction();
        throw new RuleExecutorException("Cannot validate constraint " + constraint.getId(), e);
    }
}
Also used : XOException(com.buschmais.xo.api.XOException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)

Example 8 with RuleExecutorException

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

the class AnalyzerVisitor method getRuleParameters.

private Map<String, Object> getRuleParameters(ExecutableRule executableRule) throws RuleExecutorException {
    Map<String, Object> ruleParameters = new HashMap<>();
    Map<String, Parameter> parameters = executableRule.getParameters();
    for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
        String parameterName = entry.getKey();
        Parameter parameter = entry.getValue();
        Object parameterValue;
        String parameterValueAsString = this.ruleParameters.get(parameterName);
        if (parameterValueAsString != null) {
            try {
                parameterValue = parameter.getType().parse(parameterValueAsString);
            } catch (RuleException e) {
                throw new RuleExecutorException("Cannot determine value for parameter " + parameterName + "' of rule '" + executableRule + "'.");
            }
        } else {
            parameterValue = parameter.getDefaultValue();
        }
        if (parameterValue == null) {
            throw new RuleExecutorException("No value or default value defined for required parameter '" + parameterName + "' of rule '" + executableRule + "'.");
        }
        ruleParameters.put(parameterName, parameterValue);
    }
    return ruleParameters;
}
Also used : RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException)

Example 9 with RuleExecutorException

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

the class AnalyzerVisitor method executeCypher.

/**
     * Execute the cypher query of a rule.
     * 
     * @param executableRule
     *            The executable.
     * @param executable
     *            The executable.
     * @param parameters
     *            The parameters.
     * @param severity
     *            The severity.
     * @param <T>
     *            The rule type.
     * @return The result.
     * @throws RuleExecutorException
     *             If execution fails.
     */
private <T extends ExecutableRule> Result<T> executeCypher(T executableRule, CypherExecutable executable, Map<String, Object> parameters, Severity severity) throws RuleExecutorException {
    String cypher = executable.getStatement();
    List<Map<String, Object>> rows = new ArrayList<>();
    try (Query.Result<Query.Result.CompositeRowObject> compositeRowObjects = executeQuery(cypher, parameters)) {
        List<String> columnNames = null;
        for (Query.Result.CompositeRowObject rowObject : compositeRowObjects) {
            if (columnNames == null) {
                columnNames = new ArrayList<>(rowObject.getColumns());
            }
            Map<String, Object> row = new LinkedHashMap<>();
            for (String columnName : columnNames) {
                row.put(columnName, rowObject.get(columnName, Object.class));
            }
            rows.add(row);
        }
        Result.Status status = verify(executableRule, columnNames, rows);
        return new Result<>(executableRule, status, severity, columnNames, rows);
    } catch (Exception e) {
        throw new RuleExecutorException("Cannot execute query for rule '" + executableRule + "'.", e);
    }
}
Also used : Query(com.buschmais.xo.api.Query) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) XOException(com.buschmais.xo.api.XOException) RuleExecutorException(com.buschmais.jqassistant.core.rule.api.executor.RuleExecutorException) ScriptException(javax.script.ScriptException) Result(com.buschmais.jqassistant.core.analysis.api.Result)

Example 10 with RuleExecutorException

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

the class AnalyzerVisitorTest method missingParameter.

@Test
public void missingParameter() throws RuleException {
    String statement = "match (n) return n";
    Concept concept = createConcept(statement);
    ReportPlugin reportWriter = mock(ReportPlugin.class);
    try {
        AnalyzerVisitor analyzerVisitor = new AnalyzerVisitor(configuration, Collections.<String, String>emptyMap(), store, reportWriter, console);
        analyzerVisitor.visitConcept(concept, Severity.MINOR);
        fail("Expecting an " + RuleExecutorException.class.getName());
    } catch (RuleExecutorException e) {
        String message = e.getMessage();
        assertThat(message, containsString(concept.getId()));
        assertThat(message, containsString(PARAMETER_WITHOUT_DEFAULT));
    }
}
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)

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