Search in sources :

Example 21 with TriggerInterpreter

use of com.devonfw.cobigen.api.extension.TriggerInterpreter 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 22 with TriggerInterpreter

use of com.devonfw.cobigen.api.extension.TriggerInterpreter in project cobigen by devonfw.

the class ConfigurationInterpreterImpl method resolveTemplateDestinationPath.

@Override
public Path resolveTemplateDestinationPath(Path targetRootPath, TemplateTo template, Object input) {
    Trigger trigger = this.configurationHolder.readContextConfiguration().getTrigger(template.getTriggerId());
    InputValidator.validateTrigger(trigger);
    TriggerInterpreter triggerInterpreter = PluginRegistry.getTriggerInterpreter(trigger.getType());
    // the GenerationReportTo won't be further processed
    Variables variables = new ContextVariableResolver(input, trigger).resolveVariables(triggerInterpreter, new GenerationReportTo());
    Template templateEty = this.configurationHolder.readTemplatesConfiguration(trigger).getTemplate(template.getId());
    try {
        String resolvedDestinationPath = new PathExpressionResolver(variables).evaluateExpressions(templateEty.getUnresolvedTargetPath());
        return targetRootPath.resolve(resolvedDestinationPath).normalize();
    } catch (UnknownContextVariableException e) {
        throw new CobiGenRuntimeException("Could not resolve path '" + templateEty.getUnresolvedTargetPath() + "' for input '" + (input instanceof Object[] ? Arrays.toString((Object[]) input) : input.toString()) + "' and template '" + templateEty.getAbsoluteTemplatePath() + "'. Available variables: " + variables.toString());
    }
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Variables(com.devonfw.cobigen.impl.config.entity.Variables) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) ContextVariableResolver(com.devonfw.cobigen.impl.model.ContextVariableResolver) PathExpressionResolver(com.devonfw.cobigen.impl.config.resolver.PathExpressionResolver) UnknownContextVariableException(com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException) Template(com.devonfw.cobigen.impl.config.entity.Template)

Aggregations

TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)22 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)15 InputReader (com.devonfw.cobigen.api.extension.InputReader)14 MatcherInterpreter (com.devonfw.cobigen.api.extension.MatcherInterpreter)13 MatcherToMatcher (com.devonfw.cobigen.api.matchers.MatcherToMatcher)13 File (java.io.File)11 CobiGen (com.devonfw.cobigen.api.CobiGen)10 AbstractApiTest (com.devonfw.cobigen.systemtest.common.AbstractApiTest)10 Test (org.junit.Test)10 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)5 HashMap (java.util.HashMap)5 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)4 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)4 List (java.util.List)4 VariableAssignmentToMatcher (com.devonfw.cobigen.api.matchers.VariableAssignmentToMatcher)3 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)3 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)3 Charset (java.nio.charset.Charset)3 Path (java.nio.file.Path)3 Cached (com.devonfw.cobigen.api.annotation.Cached)2