01: package org.drools.rule.builder;
02:
03: /*
04: * Copyright 2005 JBoss Inc
05: *
06: * Licensed under the Apache License, Version 2.0 (the "License");
07: * you may not use this file except in compliance with the License.
08: * You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: import org.drools.RuntimeDroolsException;
20: import org.drools.base.SalienceInteger;
21: import org.drools.lang.descr.QueryDescr;
22: import org.drools.lang.descr.RuleDescr;
23: import org.drools.rule.GroupElement;
24: import org.drools.rule.Pattern;
25: import org.drools.spi.Salience;
26:
27: /**
28: * This builds the rule structure from an AST.
29: * Generates semantic code where necessary if semantics are used.
30: * This is an internal API.
31: */
32: public class RuleBuilder {
33:
34: // Constructor
35: public RuleBuilder() {
36: }
37:
38: /**
39: * Build the give rule into the
40: * @param pkg
41: * @param ruleDescr
42: * @return
43: */
44: public void build(final RuleBuildContext context) {
45: RuleDescr ruleDescr = context.getRuleDescr();
46:
47: final RuleConditionBuilder builder = (RuleConditionBuilder) context
48: .getDialect().getBuilder(ruleDescr.getLhs().getClass());
49: if (builder != null) {
50: Pattern prefixPattern = null;
51: if (context.getRuleDescr() instanceof QueryDescr) {
52: prefixPattern = context.getDialect().getQueryBuilder()
53: .build(context,
54: (QueryDescr) context.getRuleDescr());
55: }
56: final GroupElement ce = (GroupElement) builder.build(
57: context, ruleDescr.getLhs(), prefixPattern);
58:
59: context.getRule().setLhs(ce);
60: } else {
61: throw new RuntimeDroolsException(
62: "BUG: builder not found for descriptor class "
63: + ruleDescr.getLhs().getClass());
64: }
65:
66: // Build the consequence and generate it's invoker/s
67: // generate the main rule from the previously generated s.
68: if (!(ruleDescr instanceof QueryDescr)) {
69: // do not build the consequence if we have a query
70:
71: context.getDialect().getConsequenceBuilder().build(context);
72: }
73:
74: String salienceText = context.getRuleDescr().getSalience();
75:
76: try {
77: // First see if its an Integer
78: if (salienceText != null && !salienceText.equals("")) {
79: Salience salience = new SalienceInteger(Integer
80: .parseInt(salienceText));
81: context.getRule().setSalience(salience);
82: }
83: } catch (Exception e) {
84: // It wasn't an integer, so build as an expression
85: context.getDialect().getSalienceBuilder().build(context);
86: }
87:
88: RuleClassBuilder classBuilder = context.getDialect()
89: .getRuleClassBuilder();
90: if (classBuilder != null) {
91: classBuilder.buildRule(context);
92: }
93: }
94: }
|