public void prepareWeavingRules(SModel inputModel) throws GenerationCanceledException, GenerationFailureException { Iterable<TemplateWeavingRule> rules = myGenerator.getRuleManager().getWeaving_MappingRules(); myReadyRules.clear(); final BlockedReductionsData ruleBlocks = myGenerator.getBlockedReductionsData(); final FastNodeFinder nodeFinder = FastNodeFinderManager.get(inputModel); for (TemplateWeavingRule rule : rules) { boolean includeInheritors = rule.applyToInheritors(); for (SNode applicableNode : nodeFinder.getNodes(rule.getApplicableConcept2(), includeInheritors)) { if (ruleBlocks.isWeavingBlocked(applicableNode, rule)) { continue; } QueryExecutionContext executionContext = myGenerator.getExecutionContext(applicableNode); if (executionContext == null) { continue; } TemplateExecutionEnvironment environment = new TemplateExecutionEnvironmentImpl( new TemplateProcessor(myGenerator), executionContext, new ReductionTrack(myGenerator.getBlockedReductionsData())); DefaultTemplateContext context = new DefaultTemplateContext(environment, applicableNode, null); if (executionContext.isApplicable(rule, context)) { // if there are too many ArmedWeavingRule instances (i.e. a lot of applicable SNode), // it's easy to refactor AWR to keep list of applicable nodes and to recreate TEE on // demand myReadyRules.add(new ArmedWeavingRule(rule, environment, applicableNode)); ruleBlocks.blockWeaving(applicableNode, rule); } } } }
public void apply() throws GenerationFailureException, GenerationCanceledException { for (ArmedWeavingRule rule : myReadyRules) { if (rule.apply()) { myGenerator.setChanged(); } } }
private boolean checkContext(SNode contextNode) { TemplateGenerator generator = myEnv.getGenerator(); if (contextNode == null) { myEnv .getLogger() .error( myRule.getRuleNode(), "weaving rule: cannot find context node", GeneratorUtil.describe(myApplicableNode, "input node")); return false; } // Additional check - context should be generated from the same root SNode contextRoot = contextNode.getContainingRoot(); SModel model = contextRoot.getModel(); if (model == null) { return reportErrorIfStrict( "bad context for weaving rule: no root for context " + contextNode); } SNode originalContextRoot = generator.getOriginalRootByGenerated(contextRoot); if (originalContextRoot == null) { return reportErrorIfStrict( String.format( "bad context for weaving rule: %s is generated by 'create root' rule", contextRoot)); } if (myApplicableNode.getModel() == null) return true; SNode inputRoot = myApplicableNode.getContainingRoot(); if (originalContextRoot != inputRoot) { String msg = "bad context for weaving rule: %s is generated from %s , while input node is from %s"; return reportErrorIfStrict(String.format(msg, contextRoot, originalContextRoot, inputRoot)); } return true; }