public RuleSet parse(List<? extends RuleSource> sources) throws RuleException { RuleSetBuilder ruleSetBuilder = RuleSetBuilder.newInstance(); for (RuleSource source : sources) { for (RuleParserPlugin ruleParserPlugin : ruleParserPlugins) { if (ruleParserPlugin.accepts(source)) { ruleParserPlugin.parse(source, ruleSetBuilder); } } } return ruleSetBuilder.getRuleSet(); } }
public static RuleSetBuilder newInstance() { return new RuleSetBuilder(); }
/** * Converts a list of {@link JqassistantRules} to a rule set. * * @param rules * The {@link JqassistantRules}. * @throws RuleException * If rules are not consistent. */ private void convert(List<JqassistantRules> rules, RuleSource ruleSource, RuleSetBuilder builder) throws RuleException { for (JqassistantRules rule : rules) { List<SeverityRuleType> severityRuleTypes = rule.getConceptOrConstraintOrGroup(); for (SeverityRuleType severityRuleType : severityRuleTypes) { String id = severityRuleType.getId(); if (severityRuleType instanceof ConceptType) { Concept concept = createConcept(id, ruleSource, (ConceptType) severityRuleType); builder.addConcept(concept); } else if (severityRuleType instanceof ConstraintType) { Constraint constraint = createConstraint(id, ruleSource, (ConstraintType) severityRuleType); builder.addConstraint(constraint); } else if (severityRuleType instanceof GroupType) { Group group = createGroup(id, ruleSource, (GroupType) severityRuleType); builder.addGroup(group); } } } }
for (Map<String, Object> conceptSpec : concepts) { this.processConceptOrConstraint(conceptSpec, context, concept -> context.getBuilder().addConcept(concept), Concept.builder()); for (Map<String, Object> constraintSpec : constraints) { this.processConceptOrConstraint(constraintSpec, context, constraint -> context.getBuilder().addConstraint(constraint), Constraint.builder());
.build(); context.getBuilder().addGroup(group);
private void extractExecutableRule(RuleSource ruleSource, AbstractBlock executableRuleBlock, RuleSetBuilder builder) throws RuleException { Attributes attributes = new Attributes(executableRuleBlock.getAttributes()); String id = executableRuleBlock.id(); if (id == null) { throw new RuleException("An id attribute is required for the rule '" + executableRuleBlock + "' (i.e. '[[rule:id]]' is required."); } String description = attributes.getString(TITLE, ""); if (description == null) { LOGGER.info("Description of rule is missing: Using empty text for description (source='{}', id='{}').", ruleSource.getId(), id); } Map<String, Boolean> required = getRequiresConcepts(ruleSource, id, attributes); Map<String, Parameter> parameters = getParameters(attributes.getString(REQUIRES_PARAMETERS)); Executable<?> executable = getExecutable(executableRuleBlock, attributes); if (executable != null) { Verification verification = getVerification(attributes); Report report = getReport(executableRuleBlock); if (CONCEPT.equals(executableRuleBlock.getRole())) { Severity severity = getSeverity(attributes, getRuleConfiguration().getDefaultConceptSeverity()); Concept concept = Concept.builder().id(id).description(description).severity(severity).executable(executable).requiresConcepts(required) .parameters(parameters).verification(verification).report(report).ruleSource(ruleSource).build(); builder.addConcept(concept); } else if (CONSTRAINT.equals(executableRuleBlock.getRole())) { Severity severity = getSeverity(attributes, getRuleConfiguration().getDefaultConstraintSeverity()); Constraint constraint = Constraint.builder().id(id).description(description).severity(severity).executable(executable) .requiresConcepts(required).parameters(parameters).verification(verification).report(report).ruleSource(ruleSource).build(); builder.addConstraint(constraint); } } }
private void extractGroup(RuleSource ruleSource, AbstractBlock groupBlock, RuleSetBuilder ruleSetBuilder) throws RuleException { Attributes attributes = new Attributes(groupBlock.getAttributes()); Map<String, Severity> constraints = getGroupElements(attributes, INCLUDES_CONSTRAINTS); Map<String, Severity> concepts = getGroupElements(attributes, INCLUDES_CONCEPTS); Map<String, Severity> groups = getGroupElements(attributes, INCLUDES_GROUPS); Severity severity = getSeverity(attributes, getRuleConfiguration().getDefaultGroupSeverity()); Group group = Group.builder().id(groupBlock.id()).description(groupBlock.getTitle()).severity(severity).ruleSource(ruleSource).concepts(concepts) .constraints(constraints).groups(groups).build(); ruleSetBuilder.addGroup(group); }
protected AbstractJQARestService(Store store) throws PluginRepositoryException, RuleException { this.store = store; PluginConfigurationReader pluginConfigurationReader = new PluginConfigurationReaderImpl(); RulePluginRepository rulePluginRepository = new RulePluginRepositoryImpl(pluginConfigurationReader); List<RuleSource> ruleSources = rulePluginRepository.getRuleSources(); RuleSetReader ruleSetReader = new CompoundRuleSetReader(); RuleSetBuilder ruleSetBuilder = RuleSetBuilder.newInstance(); ruleSetReader.read(ruleSources, ruleSetBuilder); availableRules = ruleSetBuilder.getRuleSet(); }
protected RuleSet getAvailableRules() throws CliExecutionException { List<RuleSource> sources = new ArrayList<>(); if (rulesUrl != null) { sources.add(new UrlRuleSource(rulesUrl)); } else { File selectedDirectory = new File(ruleDirectory); // read rules from rules directory sources.addAll(readRulesDirectory(selectedDirectory)); List<RuleSource> ruleSources; try { ruleSources = pluginRepository.getRulePluginRepository().getRuleSources(); } catch (PluginRepositoryException e) { throw new CliExecutionException("Cannot get rule plugin repository.", e); } sources.addAll(ruleSources); } RuleSetBuilder ruleSetBuilder = RuleSetBuilder.newInstance(); try { ruleSetReader.read(sources, ruleSetBuilder); } catch (RuleException e) { throw new CliExecutionException("Cannot read rules.", e); } return ruleSetBuilder.getRuleSet(); }