Search in sources :

Example 11 with MatcherTo

use of com.devonfw.cobigen.api.to.MatcherTo in project cobigen by devonfw.

the class TriggerMatchingEvaluatorImpl method getMatchingTriggers.

@Cached
@Override
public List<Trigger> getMatchingTriggers(Object matcherInput) {
    LOG.debug("Retrieve matching trigger. input {}, hash: {}", matcherInput, matcherInput.hashCode());
    List<Trigger> matchingTrigger = Lists.newLinkedList();
    for (Trigger trigger : this.configurationHolder.readContextConfiguration().getTriggers()) {
        TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
        if (triggerInterpreter == null) {
            continue;
        // trigger interpreter not yet activated as the plug-in was not yet used.
        // unfortunately the invariant here is, that the CobiGen user has once called CobigenImpl#read
        // to get the matcher input
        }
        InputValidator.validateTriggerInterpreter(triggerInterpreter, trigger);
        LOG.debug("Check {} to match the input.", trigger);
        if (triggerInterpreter.getInputReader().isValidInput(matcherInput)) {
            LOG.debug("Matcher input is marked as valid.");
            boolean triggerMatches = this.matcherEvaluator.matches(matcherInput, trigger.getMatcher(), triggerInterpreter);
            if (triggerMatches) {
                matchingTrigger.add(trigger);
            }
            // recognized as a container.
            if (!triggerMatches) {
                LOG.debug("Check container matchers ...");
                FOR_CONTAINERMATCHER: for (ContainerMatcher containerMatcher : trigger.getContainerMatchers()) {
                    MatcherTo containerMatcherTo = new MatcherTo(containerMatcher.getType(), containerMatcher.getValue(), matcherInput);
                    LOG.debug("Check {} ...", containerMatcherTo);
                    if (triggerInterpreter.getMatcher().matches(containerMatcherTo)) {
                        LOG.debug("Match! Retrieve objects from container ...", containerMatcherTo);
                        // keep backward-compatibility
                        List<Object> containerResources;
                        if (containerMatcher.isRetrieveObjectsRecursively()) {
                            containerResources = triggerInterpreter.getInputReader().getInputObjectsRecursively(matcherInput, Charsets.UTF_8);
                        } else {
                            // the charset does not matter as we just want to see whether there is one
                            // matcher for one of the container resources
                            containerResources = triggerInterpreter.getInputReader().getInputObjects(matcherInput, Charsets.UTF_8);
                        }
                        LOG.debug("{} objects retrieved.", containerResources.size());
                        // check if at least one container element matches the matcher declarations
                        for (Object resource : containerResources) {
                            if (this.matcherEvaluator.matches(resource, trigger.getMatcher(), triggerInterpreter)) {
                                LOG.debug("At least one object from container matches.");
                                triggerMatches = true;
                                break FOR_CONTAINERMATCHER;
                            }
                        }
                        LOG.debug("No element of the container is matched.");
                    }
                }
                if (triggerMatches) {
                    matchingTrigger.add(new Trigger(trigger, true));
                }
            }
            LOG.debug("{} {}", trigger, triggerMatches ? "matches." : "does not match.");
        }
    }
    return matchingTrigger;
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) ContainerMatcher(com.devonfw.cobigen.impl.config.entity.ContainerMatcher) List(java.util.List) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 12 with MatcherTo

use of com.devonfw.cobigen.api.to.MatcherTo in project cobigen by devonfw.

the class ContextVariableResolver method resolveVariables.

/**
 * Resolves all {@link VariableAssignment}s by using the given {@link TriggerInterpreter}
 *
 * @param triggerInterpreter to be used
 * @param report is getting filled as side-effect
 * @param parent the parent {@link Variables} to inherit.
 * @return the mapping of variable to value
 * @throws InvalidConfigurationException if there are {@link VariableAssignment}s, which could not be resolved
 */
public Variables resolveVariables(TriggerInterpreter triggerInterpreter, Variables parent, GenerationReportTo report) throws InvalidConfigurationException {
    Variables variables = new Variables(parent);
    for (Matcher m : this.trigger.getMatcher()) {
        MatcherTo matcherTo = new MatcherTo(m.getType(), m.getValue(), this.input);
        if (triggerInterpreter.getMatcher().matches(matcherTo)) {
            Map<String, String> resolvedVariables;
            try {
                resolvedVariables = triggerInterpreter.getMatcher().resolveVariables(matcherTo, getVariableAssignments(m), report);
            } catch (InvalidConfigurationException e) {
                throw e;
            } catch (Throwable e) {
                throw new PluginProcessingException(e);
            }
            InputValidator.validateResolvedVariables(resolvedVariables);
            variables.putAll(resolvedVariables);
        }
    }
    return variables;
}
Also used : Variables(com.devonfw.cobigen.impl.config.entity.Variables) Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) PluginProcessingException(com.devonfw.cobigen.impl.exceptions.PluginProcessingException) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Aggregations

MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)12 OpenAPIMatcher (com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher)7 Test (org.junit.Test)7 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)5 ArrayList (java.util.ArrayList)4 Cached (com.devonfw.cobigen.api.annotation.Cached)3 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)3 VariableAssignmentTo (com.devonfw.cobigen.api.to.VariableAssignmentTo)3 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)2 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)2 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)2 EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)2 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 InputReader (com.devonfw.cobigen.api.extension.InputReader)1 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)1 Variables (com.devonfw.cobigen.impl.config.entity.Variables)1 PluginProcessingException (com.devonfw.cobigen.impl.exceptions.PluginProcessingException)1 List (java.util.List)1