Search in sources :

Example 6 with Cached

use of com.devonfw.cobigen.api.annotation.Cached in project cobigen by devonfw.

the class MatcherEvaluatorImpl method matches.

@Cached
@Override
public boolean matches(Object matcherInput, List<Matcher> matcherList, TriggerInterpreter triggerInterpreter) {
    boolean matcherSetMatches = false;
    LOG.debug("Check matchers for TriggerInterpreter[type='{}'] ...", triggerInterpreter.getType());
    MATCHER_LOOP: for (Matcher matcher : matcherList) {
        MatcherTo matcherTo = new MatcherTo(matcher.getType(), matcher.getValue(), matcherInput);
        LOG.trace("Check {} ...", matcherTo);
        if (triggerInterpreter.getMatcher().matches(matcherTo)) {
            switch(matcher.getAccumulationType()) {
                case NOT:
                    LOG.trace("NOT Matcher matches -> trigger match fails.");
                    matcherSetMatches = false;
                    break MATCHER_LOOP;
                case OR:
                case AND:
                    LOG.trace("Matcher matches.");
                    matcherSetMatches = true;
                    break;
                default:
            }
        } else {
            if (matcher.getAccumulationType() == AccumulationType.AND) {
                LOG.trace("AND Matcher does not match -> trigger match fails.");
                matcherSetMatches = false;
                break MATCHER_LOOP;
            }
        }
    }
    LOG.debug("Matcher declarations " + (matcherSetMatches ? "match the input." : "do not match the input."));
    return matcherSetMatches;
}
Also used : Matcher(com.devonfw.cobigen.impl.config.entity.Matcher) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Cached(com.devonfw.cobigen.api.annotation.Cached)

Example 7 with Cached

use of com.devonfw.cobigen.api.annotation.Cached 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)

Aggregations

Cached (com.devonfw.cobigen.api.annotation.Cached)7 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)3 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)3 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)2 TemplatesConfiguration (com.devonfw.cobigen.impl.config.TemplatesConfiguration)2 ContainerMatcher (com.devonfw.cobigen.impl.config.entity.ContainerMatcher)2 ArrayList (java.util.ArrayList)2 InputReader (com.devonfw.cobigen.api.extension.InputReader)1 IncrementTo (com.devonfw.cobigen.api.to.IncrementTo)1 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)1 Matcher (com.devonfw.cobigen.impl.config.entity.Matcher)1 Template (com.devonfw.cobigen.impl.config.entity.Template)1 List (java.util.List)1