Search in sources :

Example 1 with RuleException

use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.

the class RulePluginRepositoryImpl method initialize.

@Override
public void initialize() {
    for (JqassistantPlugin plugin : plugins) {
        IdClassListType ruleParsers = plugin.getRuleParser();
        if (ruleParsers != null) {
            for (IdClassType pluginType : ruleParsers.getClazz()) {
                RuleParserPlugin ruleParserPlugin = createInstance(pluginType.getValue());
                try {
                    ruleParserPlugin.initialize();
                } catch (RuleException e) {
                    throw new PluginRepositoryException("Cannot initialize plugin " + ruleParserPlugin, e);
                }
                ruleParserPlugins.add(ruleParserPlugin);
            }
        }
    }
}
Also used : IdClassType(org.jqassistant.schema.plugin.v1.IdClassType) PluginRepositoryException(com.buschmais.jqassistant.core.plugin.api.PluginRepositoryException) RuleParserPlugin(com.buschmais.jqassistant.core.rule.api.reader.RuleParserPlugin) JqassistantPlugin(org.jqassistant.schema.plugin.v1.JqassistantPlugin) IdClassListType(org.jqassistant.schema.plugin.v1.IdClassListType) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException)

Example 2 with RuleException

use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.

the class AnalyzerRuleVisitor method getRuleParameters.

private Map<String, Object> getRuleParameters(ExecutableRule executableRule) throws RuleException {
    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 RuleException("Cannot determine value for parameter " + parameterName + "' of rule '" + executableRule + "'.");
            }
        } else {
            parameterValue = parameter.getDefaultValue();
        }
        if (parameterValue == null) {
            throw new RuleException("No value or default value defined for required parameter '" + parameterName + "' of rule '" + executableRule + "'.");
        }
        ruleParameters.put(parameterName, parameterValue);
    }
    return ruleParameters;
}
Also used : HashMap(java.util.HashMap) Parameter(com.buschmais.jqassistant.core.rule.api.model.Parameter) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with RuleException

use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.

the class AnalyzerRuleVisitor method execute.

private <T extends ExecutableRule> Result<T> execute(T executableRule, Severity severity) throws RuleException {
    Map<String, Object> ruleParameters = getRuleParameters(executableRule);
    Executable<?> executable = executableRule.getExecutable();
    Collection<RuleInterpreterPlugin> languagePlugins = ruleInterpreterPlugins.get(executable.getLanguage());
    if (languagePlugins == null) {
        throw new RuleException("Could not determine plugin to execute " + executableRule);
    }
    for (RuleInterpreterPlugin languagePlugin : languagePlugins) {
        if (languagePlugin.accepts(executableRule)) {
            Result<T> result = execute(executableRule, severity, ruleParameters, languagePlugin);
            if (result != null) {
                return result;
            }
        }
    }
    throw new RuleException("No plugin for language '" + executable.getLanguage() + "' returned a result for " + executableRule);
}
Also used : RuleInterpreterPlugin(com.buschmais.jqassistant.core.analysis.api.RuleInterpreterPlugin) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException)

Example 4 with RuleException

use of com.buschmais.jqassistant.core.rule.api.model.RuleException in project jqa-core-framework by buschmais.

the class AbstractCypherRuleInterpreterPlugin method execute.

protected <T extends ExecutableRule<?>> Result<T> execute(String cypher, T executableRule, Map<String, Object> parameters, Severity severity, AnalyzerContext context) throws RuleException {
    List<Map<String, Object>> rows = new LinkedList<>();
    context.getLogger().debug("Executing query '" + cypher + "' with parameters [" + parameters + "]");
    String primaryColumn = null;
    List<String> columnNames = null;
    try (Query.Result<Query.Result.CompositeRowObject> compositeRowObjects = context.getStore().executeQuery(cypher, parameters)) {
        for (Query.Result.CompositeRowObject rowObject : compositeRowObjects) {
            if (columnNames == null) {
                columnNames = unmodifiableList(rowObject.getColumns());
                primaryColumn = executableRule.getReport().getPrimaryColumn();
                if (primaryColumn == null) {
                    primaryColumn = columnNames.get(0);
                }
            }
            Map<String, Object> row = new LinkedHashMap<>();
            for (String columnName : columnNames) {
                row.put(columnName, rowObject.get(columnName, Object.class));
            }
            if (!isSuppressedRow(executableRule.getId(), row, primaryColumn)) {
                rows.add(row);
            }
        }
    } catch (Exception e) {
        throw new RuleException("Cannot execute query for rule '" + executableRule + "'.", e);
    }
    Status status = getStatus(executableRule, columnNames, rows, context);
    return Result.<T>builder().rule(executableRule).status(status).severity(severity).columnNames(columnNames).rows(rows).build();
}
Also used : Status(com.buschmais.jqassistant.core.report.api.model.Result.Status) Query(com.buschmais.xo.api.Query) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) LinkedList(java.util.LinkedList) RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Result(com.buschmais.jqassistant.core.report.api.model.Result) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 5 with RuleException

use of com.buschmais.jqassistant.core.rule.api.model.RuleException 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 RuleException {
    LOGGER.debug("Verifying result of " + executable);
    if (rows.isEmpty()) {
        return getStatus(executable, 0, verification.getMin(), verification.getMax());
    }
    if (columnNames.isEmpty()) {
        throw new RuleException("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 RuleException("The result does not contain a column '" + column);
        } else if (!Number.class.isAssignableFrom(value.getClass())) {
            throw new RuleException("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 : RuleException(com.buschmais.jqassistant.core.rule.api.model.RuleException) Result(com.buschmais.jqassistant.core.report.api.model.Result)

Aggregations

RuleException (com.buschmais.jqassistant.core.rule.api.model.RuleException)8 Result (com.buschmais.jqassistant.core.report.api.model.Result)3 Map (java.util.Map)3 Constraint (com.buschmais.jqassistant.core.rule.api.model.Constraint)2 Test (org.junit.jupiter.api.Test)2 RuleInterpreterPlugin (com.buschmais.jqassistant.core.analysis.api.RuleInterpreterPlugin)1 PluginRepositoryException (com.buschmais.jqassistant.core.plugin.api.PluginRepositoryException)1 Status (com.buschmais.jqassistant.core.report.api.model.Result.Status)1 Parameter (com.buschmais.jqassistant.core.rule.api.model.Parameter)1 RuleParserPlugin (com.buschmais.jqassistant.core.rule.api.reader.RuleParserPlugin)1 Query (com.buschmais.xo.api.Query)1 XOException (com.buschmais.xo.api.XOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ScriptEngine (javax.script.ScriptEngine)1 ScriptEngineFactory (javax.script.ScriptEngineFactory)1 ScriptException (javax.script.ScriptException)1 IdClassListType (org.jqassistant.schema.plugin.v1.IdClassListType)1