001: package org.drools.integrationtests.waltz;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.InputStreamReader;
022: import java.io.Reader;
023: import java.util.regex.Matcher;
024: import java.util.regex.Pattern;
025:
026: import junit.framework.TestCase;
027:
028: import org.drools.PackageIntegrationException;
029: import org.drools.RuleBase;
030: import org.drools.RuleIntegrationException;
031: import org.drools.WorkingMemory;
032: import org.drools.compiler.DrlParser;
033: import org.drools.compiler.DroolsParserException;
034: import org.drools.compiler.PackageBuilder;
035: import org.drools.lang.descr.PackageDescr;
036: import org.drools.rule.InvalidPatternException;
037: import org.drools.rule.Package;
038:
039: /**
040: * This is a sample file to launch a rule package from a rule source file.
041: */
042: public abstract class Waltz extends TestCase {
043:
044: protected abstract RuleBase getRuleBase() throws Exception;
045:
046: public void testWaltz() {
047: try {
048:
049: //load up the rulebase
050: final RuleBase ruleBase = readRule();
051: final WorkingMemory workingMemory = ruleBase
052: .newStatefulSession();
053:
054: workingMemory.setGlobal("sysout", System.out);
055:
056: // DebugWorkingMemoryEventListener wmListener = new DebugWorkingMemoryEventListener();
057: // DebugAgendaEventListener agendaListener = new DebugAgendaEventListener();
058: // workingMemory.addEventListener( wmListener );
059: // workingMemory.addEventListener( agendaListener );
060:
061: //go !
062: this .loadLines(workingMemory, "waltz50.dat");
063:
064: //final Stage stage = new Stage( Stage.START );
065: //workingMemory.assertObject( stage );
066:
067: final long start = System.currentTimeMillis();
068:
069: final Stage stage = new Stage(Stage.DUPLICATE);
070: workingMemory.insert(stage);
071: workingMemory.fireAllRules();
072:
073: final long end = System.currentTimeMillis();
074: System.out.println(end - start);
075: } catch (final Throwable t) {
076: t.printStackTrace();
077: fail(t.getMessage());
078: }
079: }
080:
081: /**
082: * Please note that this is the "low level" rule assembly API.
083: */
084: private RuleBase readRule() throws Exception,
085: DroolsParserException, RuleIntegrationException,
086: PackageIntegrationException, InvalidPatternException {
087: //read in the source
088: final Reader reader = new InputStreamReader(Waltz.class
089: .getResourceAsStream("waltz.drl"));
090: final DrlParser parser = new DrlParser();
091: final PackageDescr packageDescr = parser.parse(reader);
092:
093: //pre build the package
094: final PackageBuilder builder = new PackageBuilder();
095: builder.addPackage(packageDescr);
096: final Package pkg = builder.getPackage();
097:
098: //add the package to a rulebase
099: final RuleBase ruleBase = getRuleBase();
100: ruleBase.addPackage(pkg);
101: return ruleBase;
102: }
103:
104: private void loadLines(final WorkingMemory wm, final String filename)
105: throws IOException {
106: final BufferedReader reader = new BufferedReader(
107: new InputStreamReader(Waltz.class
108: .getResourceAsStream(filename)));
109: final Pattern pat = Pattern
110: .compile(".*make line \\^p1 ([0-9]*) \\^p2 ([0-9]*).*");
111: String line = reader.readLine();
112: while (line != null) {
113: final Matcher m = pat.matcher(line);
114: if (m.matches()) {
115: final Line l = new Line(Integer.parseInt(m.group(1)),
116: Integer.parseInt(m.group(2)));
117: wm.insert(l);
118: }
119: line = reader.readLine();
120: }
121: reader.close();
122: }
123:
124: }
|