01: package org.drools.compiler;
02:
03: import java.io.IOException;
04: import java.io.Reader;
05:
06: import org.drools.CheckedDroolsException;
07: import org.drools.RuleBase;
08: import org.drools.RuleBaseFactory;
09: import org.drools.rule.Package;
10:
11: /**
12: * A high level helper class for creating rulebases from source.
13: * For additional control, refer to the DrlParser and PackageBuilder classes.
14: * You can have much more detailed control with the lower level classes.
15: *
16: * @author Michael Neale
17: */
18: public class RuleBaseLoader {
19:
20: private static final RuleBaseLoader INSTANCE = new RuleBaseLoader();
21: private static int engineType = RuleBase.RETEOO;
22:
23: private RuleBaseLoader() {
24: }
25:
26: /** Get an instance of the loader */
27: public static RuleBaseLoader getInstance() {
28: return RuleBaseLoader.INSTANCE;
29: }
30:
31: /**
32: * Create a new RuleBase from the drl source.
33: * Uses the current default engine type.
34: */
35: public RuleBase loadFromReader(final Reader drl)
36: throws CheckedDroolsException, IOException {
37: final PackageBuilder builder = new PackageBuilder();
38: builder.addPackageFromDrl(drl);
39: return makeRuleBase(builder);
40: }
41:
42: /**
43: * Create a new RuleBase from the drl and dsl source.
44: * Uses the current default engine type.
45: */
46: public RuleBase loadFromReader(final Reader drl, final Reader dsl)
47: throws CheckedDroolsException, IOException {
48: final PackageBuilder builder = new PackageBuilder();
49: builder.addPackageFromDrl(drl, dsl);
50: return makeRuleBase(builder);
51: }
52:
53: /** Replace the default engine type that will be used (default is RETEOO) */
54: public void setDefaultEngineType(final int type) {
55: RuleBaseLoader.engineType = type;
56: }
57:
58: private RuleBase makeRuleBase(final PackageBuilder builder)
59: throws CheckedDroolsException {
60: if (builder.hasErrors()) {
61: throw new CheckedDroolsException(
62: "There were errors in the rule source: "
63: + builder.getErrors().toString());
64: }
65: final Package binaryPackage = builder.getPackage();
66:
67: final RuleBase rb = RuleBaseFactory
68: .newRuleBase(RuleBaseLoader.engineType);
69: try {
70: rb.addPackage(binaryPackage);
71: } catch (final Exception e) {
72: throw new CheckedDroolsException(
73: "Unable to add compiled package to rulebase. Nested error is: "
74: + e.getMessage());
75: }
76: return rb;
77: }
78:
79: }
|