001: /*
002: * Copyright 2006 JBoss Inc
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.drools.rule.builder.dialect.java;
018:
019: import java.util.Arrays;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.drools.compiler.Dialect;
024: import org.drools.compiler.RuleError;
025: import org.drools.lang.descr.RuleDescr;
026: import org.drools.rule.Declaration;
027: import org.drools.rule.builder.ConsequenceBuilder;
028: import org.drools.rule.builder.RuleBuildContext;
029: import org.drools.spi.PatternExtractor;
030:
031: /**
032: * @author etirelli
033: *
034: */
035: public class JavaConsequenceBuilder extends AbstractJavaBuilder
036: implements ConsequenceBuilder {
037:
038: /* (non-Javadoc)
039: * @see org.drools.semantics.java.builder.ConsequenceBuilder#buildConsequence(org.drools.semantics.java.builder.BuildContext, org.drools.semantics.java.builder.BuildUtils, org.drools.lang.descr.RuleDescr)
040: */
041: public void build(final RuleBuildContext context) {
042:
043: // pushing consequence LHS into the stack for variable resolution
044: context.getBuildStack().push(context.getRule().getLhs());
045:
046: final String className = "consequence";
047:
048: final RuleDescr ruleDescr = context.getRuleDescr();
049:
050: Dialect.AnalysisResult analysis = context.getDialect()
051: .analyzeBlock(context, ruleDescr,
052: (String) ruleDescr.getConsequence());
053: final List[] usedIdentifiers = analysis.getBoundIdentifiers();
054:
055: final Declaration[] declarations = new Declaration[usedIdentifiers[0]
056: .size()];
057:
058: for (int i = 0, size = usedIdentifiers[0].size(); i < size; i++) {
059: declarations[i] = context.getDeclarationResolver()
060: .getDeclaration((String) usedIdentifiers[0].get(i));
061: }
062:
063: final Map map = createVariableContext(className, null, context,
064: declarations, null, (String[]) usedIdentifiers[1]
065: .toArray(new String[usedIdentifiers[1].size()]));
066: map.put("text", ((JavaDialect) context.getDialect())
067: .getKnowledgeHelperFixer().fix(
068: (String) ruleDescr.getConsequence()));
069:
070: // Must use the rule declarations, so we use the same order as used in the generated invoker
071: final List list = Arrays.asList(context.getRule()
072: .getDeclarations());
073:
074: //final int[] indexes = new int[declarations.length];
075: final Integer[] indexes = new Integer[declarations.length];
076:
077: final Boolean[] notPatterns = new Boolean[declarations.length];
078: for (int i = 0, length = declarations.length; i < length; i++) {
079: indexes[i] = new Integer(list.indexOf(declarations[i]));
080: notPatterns[i] = (declarations[i].getExtractor() instanceof PatternExtractor) ? new Boolean(
081: false)
082: : new Boolean(true);
083: if ((indexes[i]).intValue() == -1) {
084: context
085: .getErrors()
086: .add(
087: new RuleError(
088: context.getRule(),
089: ruleDescr,
090: null,
091: "Internal Error : Unable to find declaration in list while generating the consequence invoker"));
092: }
093: }
094:
095: map.put("indexes", indexes);
096:
097: map.put("notPatterns", notPatterns);
098:
099: generatTemplates("consequenceMethod", "consequenceInvoker",
100: context, className, map, context.getRule(), ruleDescr);
101:
102: // popping Rule.getLHS() from the build stack
103: context.getBuildStack().pop();
104: }
105:
106: }
|