01: /*
02: * Copyright 2006 JBoss Inc
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.drools.rule.builder.dialect.java;
18:
19: import java.util.Iterator;
20:
21: import org.drools.lang.descr.RuleDescr;
22: import org.drools.rule.builder.RuleBuildContext;
23: import org.drools.rule.builder.RuleClassBuilder;
24: import org.drools.util.StringUtils;
25:
26: /**
27: * @author etirelli
28: *
29: */
30: public class JavaRuleClassBuilder implements RuleClassBuilder {
31:
32: /* (non-Javadoc)
33: * @see org.drools.rule.builder.dialect.java.RuleClassBuilder#buildRule(org.drools.rule.builder.BuildContext, org.drools.rule.builder.dialect.java.BuildUtils, org.drools.lang.descr.RuleDescr)
34: */
35: public void buildRule(final RuleBuildContext context) {
36: final JavaDialect dialect = (JavaDialect) context.getDialect();
37:
38: // If there is no compiled code, return
39: if (context.getMethods().isEmpty()) {
40: dialect.setRuleClass(null);
41: return;
42: }
43: final String lineSeparator = System
44: .getProperty("line.separator");
45:
46: final StringBuffer buffer = new StringBuffer();
47: buffer.append("package " + context.getPkg().getName() + ";"
48: + lineSeparator);
49:
50: for (final Iterator it = context.getPkg().getImports()
51: .iterator(); it.hasNext();) {
52: buffer.append("import " + it.next() + ";" + lineSeparator);
53: }
54:
55: for (final Iterator it = context.getPkg().getStaticImports()
56: .iterator(); it.hasNext();) {
57: buffer.append("import static " + it.next() + ";"
58: + lineSeparator);
59: }
60:
61: final RuleDescr ruleDescr = context.getRuleDescr();
62:
63: buffer.append("public class "
64: + StringUtils.ucFirst(ruleDescr.getClassName()) + " {"
65: + lineSeparator);
66: buffer
67: .append(" private static final long serialVersionUID = 400L;"
68: + lineSeparator);
69:
70: for (int i = 0, size = context.getMethods().size() - 1; i < size; i++) {
71: buffer.append(context.getMethods().get(i) + lineSeparator);
72: }
73:
74: final String[] lines = buffer.toString().split(lineSeparator);
75:
76: ruleDescr.setConsequenceOffset(lines.length + 1);
77:
78: buffer.append(context.getMethods().get(
79: context.getMethods().size() - 1)
80: + lineSeparator);
81: buffer.append("}");
82:
83: dialect.setRuleClass(buffer.toString());
84: }
85: }
|