Skip to content

Commit

Permalink
#488 resolve rules for effective rules API (#773)
Browse files Browse the repository at this point in the history
* #488 revolse rules for effective rules API

* #488 fix Checkstyle violation
  • Loading branch information
StephanPirnbaum authored Jan 15, 2025
1 parent 877aff6 commit 0cc9e37
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractTask implements Task {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTask.class);

protected File outputDirectory;
protected StoreFactory storeFactory;

Expand All @@ -29,7 +25,7 @@ public final void initialize(PluginRepository pluginRepository, StoreFactory sto
this.outputDirectory = new File(DEFAULT_OUTPUT_DIRECTORY);
this.pluginRepository = pluginRepository;
this.storeFactory = storeFactory;
this.ruleHelper = new RuleHelper(LOGGER);
this.ruleHelper = new RuleHelper();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,15 @@
import com.buschmais.jqassistant.core.rule.api.executor.RuleSetExecutor;
import com.buschmais.jqassistant.core.rule.api.model.*;

import org.slf4j.Logger;
import lombok.extern.slf4j.Slf4j;

/**
* Provides utility functionality for creating reports.
*/
@Slf4j
public class RuleHelper {

public static final String LOG_LINE_PREFIX = " \"";
private final Logger logger;

/**
* Constructor.
*
* @param log
* The logger to use for printing messages.
*/
public RuleHelper(Logger log) {
this.logger = log;
}

/**
* Logs the given rule set on level info.
Expand Down Expand Up @@ -63,22 +53,22 @@ public void printRuleSet(RuleSet ruleSet, RuleSelection ruleSelection, Rule conf
* The visitor.
*/
private void printValidRules(CollectRulesVisitor visitor) {
logger.info("Groups [" + visitor.getGroups()
log.info("Groups [" + visitor.getGroups()
.size() + "]");
for (Group group : visitor.getGroups()) {
logger.info(LOG_LINE_PREFIX + group.getId() + "\"");
log.info(LOG_LINE_PREFIX + group.getId() + "\"");
}
logger.info("Constraints [" + visitor.getConstraints()
log.info("Constraints [" + visitor.getConstraints()
.size() + "]");
for (Constraint constraint : visitor.getConstraints()
.keySet()) {
logger.info(LOG_LINE_PREFIX + constraint.getId() + "\" - " + constraint.getDescription());
log.info(LOG_LINE_PREFIX + constraint.getId() + "\" - " + constraint.getDescription());
}
logger.info("Concepts [" + visitor.getConcepts()
log.info("Concepts [" + visitor.getConcepts()
.size() + "]");
for (Concept concept : visitor.getConcepts()
.keySet()) {
logger.info(LOG_LINE_PREFIX + concept.getId() + "\" - " + concept.getDescription());
log.info(LOG_LINE_PREFIX + concept.getId() + "\" - " + concept.getDescription());
}
}

Expand All @@ -93,7 +83,7 @@ private void printValidRules(CollectRulesVisitor visitor) {
* @throws RuleException
* If the rules cannot be evaluated.
*/
private CollectRulesVisitor getAllRules(RuleSet ruleSet, RuleSelection ruleSelection, Rule configuration) throws RuleException {
public CollectRulesVisitor getAllRules(RuleSet ruleSet, RuleSelection ruleSelection, Rule configuration) throws RuleException {
CollectRulesVisitor visitor = new CollectRulesVisitor();
RuleSetExecutor<Boolean> executor = new RuleSetExecutor<>(visitor, configuration);
executor.execute(ruleSet, ruleSelection);
Expand All @@ -109,23 +99,23 @@ private CollectRulesVisitor getAllRules(RuleSet ruleSet, RuleSelection ruleSelec
private void printMissingRules(CollectRulesVisitor visitor) {
Set<String> missingConcepts = visitor.getMissingConcepts();
if (!missingConcepts.isEmpty()) {
logger.info("Missing concepts [{}]", missingConcepts.size());
log.info("Missing concepts [{}]", missingConcepts.size());
for (String missingConcept : missingConcepts) {
logger.warn(LOG_LINE_PREFIX + "{}", missingConcept);
log.warn(LOG_LINE_PREFIX + "{}", missingConcept);
}
}
Set<String> missingConstraints = visitor.getMissingConstraints();
if (!missingConstraints.isEmpty()) {
logger.info("Missing constraints [{}]", missingConstraints.size());
log.info("Missing constraints [{}]", missingConstraints.size());
for (String missingConstraint : missingConstraints) {
logger.warn(LOG_LINE_PREFIX + "{}", missingConstraint);
log.warn(LOG_LINE_PREFIX + "{}", missingConstraint);
}
}
Set<String> missingGroups = visitor.getMissingGroups();
if (!missingGroups.isEmpty()) {
logger.info("Missing groups [{}]", missingGroups.size());
log.info("Missing groups [{}]", missingGroups.size());
for (String missingGroup : missingGroups) {
logger.warn(LOG_LINE_PREFIX + "{}", missingGroup);
log.warn(LOG_LINE_PREFIX + "{}", missingGroup);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;

import com.buschmais.jqassistant.core.analysis.api.configuration.Analyze;
import com.buschmais.jqassistant.core.rule.api.RuleHelper;
import com.buschmais.jqassistant.core.rule.api.executor.CollectRulesVisitor;
import com.buschmais.jqassistant.core.rule.api.model.RuleException;
import com.buschmais.jqassistant.core.rule.api.model.RuleSelection;
import com.buschmais.jqassistant.core.rule.api.model.RuleSet;
Expand All @@ -32,7 +34,7 @@ public class RuleProvider {
private final RuleSet availableRules;

@Getter
private final RuleSelection effectiveRules;
private final CollectRulesVisitor effectiveRules;

public static RuleProvider create(Configuration configuration, String defaultRuleDirectory, PluginRepository pluginRepository) throws RuleException {
return new RuleProvider(configuration, pluginRepository, defaultRuleDirectory);
Expand Down Expand Up @@ -78,8 +80,15 @@ private RuleSet initAvailableRules() throws RuleException {
return ruleParser.parse(ruleSources);
}

private RuleSelection initEffectiveRules() {
private CollectRulesVisitor initEffectiveRules() throws RuleException {
Analyze analyze = this.configuration.analyze();
return RuleSelection.select(this.availableRules, analyze.groups(), analyze.constraints(), analyze.excludeConstraints(), analyze.concepts());
RuleSelection selection = RuleSelection.select(this.availableRules, analyze.groups(), analyze.constraints(), analyze.excludeConstraints(), analyze.concepts());
RuleHelper ruleHelper = new RuleHelper();
try {
return ruleHelper.getAllRules(this.availableRules, selection, configuration.analyze().rule());
} catch (RuleException e) {
throw new RuleException(e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ void bootstrap() throws RuleException {

log.info("Rule sources: {}.", ruleProvider.getRuleSources());
log.info("Available rules: {}.", ruleProvider.getAvailableRules());
log.info("Effective rules: {}.", ruleProvider.getEffectiveRules());
log.info("Effective rules (Groups): {}.", ruleProvider.getEffectiveRules().getGroups());
log.info("Effective rules (Concepts): {}.", ruleProvider.getEffectiveRules().getConcepts());
log.info("Effective rules (Constraints): {}.", ruleProvider.getEffectiveRules().getConstraints());
}

@ConfigMapping(prefix = Configuration.PREFIX)
Expand Down
9 changes: 9 additions & 0 deletions core/test/src/test/resources/.jqassistant.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
jqassistant:
plugins:
- group-id: com.buschmais.jqassistant.plugin
artifact-id: java
version: 2.6.0-M2
store:
embedded:
neo4j-plugins:
- group-id: org.neo4j.procedure
artifact-id: apoc-core
classifier: core
version: ${neo4j_5x.version}
analyze:
rule:
directory: ${project.basedir}/test/resources/rules
groups:
- bootstrap:Default
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Lists all available rules.
*/
@Mojo(name = "available-rules", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
public class AvailableRulesMojo extends AbstractRuleMojo {
private static final Logger LOGGER = LoggerFactory.getLogger(AvailableRulesMojo.class);

@Override
public void aggregate(MojoExecutionContext mojoExecutionContext) throws MojoExecutionException, MojoFailureException {
getLog().info("Available rules for '" + mojoExecutionContext.getRootModule()
.getName() + "'.");
RuleSet ruleSet = readRules(mojoExecutionContext);
RuleHelper ruleHelper = new RuleHelper(LOGGER);
RuleHelper ruleHelper = new RuleHelper();
try {
ruleHelper.printRuleSet(ruleSet, mojoExecutionContext.getConfiguration()
.analyze()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void aggregate(MojoExecutionContext mojoExecutionContext) throws MojoExec
.analyze();
RuleSet ruleSet = readRules(mojoExecutionContext);
RuleSelection ruleSelection = RuleSelection.select(ruleSet, analyze.groups(), analyze.constraints(), analyze.excludeConstraints(), analyze.concepts());
RuleHelper ruleHelper = new RuleHelper(LOGGER);
RuleHelper ruleHelper = new RuleHelper();
try {
ruleHelper.printRuleSet(ruleSet, ruleSelection, analyze.rule());
} catch (RuleException e) {
Expand Down

0 comments on commit 0cc9e37

Please sign in to comment.