[jboss-svn-commits] JBL Code SVN: r14849 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/brms/server/util and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Sep 4 02:42:38 EDT 2007
Author: michael.neale at jboss.com
Date: 2007-09-04 02:42:38 -0400 (Tue, 04 Sep 2007)
New Revision: 14849
Added:
labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/MVEL_soundex.drl
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/BRDRLPersistence.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersistenceTest.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
Log:
JBRULES-1166
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/BRDRLPersistence.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/BRDRLPersistence.java 2007-09-04 05:19:46 UTC (rev 14848)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/BRDRLPersistence.java 2007-09-04 06:42:38 UTC (rev 14849)
@@ -23,7 +23,7 @@
/**
* This class persists the rule model to DRL and back
- *
+ *
* @author etirelli
*/
public class BRDRLPersistence
@@ -71,7 +71,7 @@
/**
* Marshal model attributes
- *
+ *
* @param buf
* @param model
*/
@@ -80,16 +80,16 @@
boolean hasDialect = false;
for ( int i = 0; i < model.attributes.length; i++ ) {
RuleAttribute attr = model.attributes[i];
-
+
buf.append( "\t" );
buf.append( attr );
-
+
buf.append( "\n" );
if (attr.attributeName.equals( "dialect" )) {
hasDialect = true;
- }
+ }
}
-//Un comment below for mvel
+//Un comment below for mvel
if (!hasDialect) {
RuleAttribute attr = new RuleAttribute("dialect", "mvel");
buf.append( "\t" );
@@ -100,7 +100,7 @@
/**
* Marshal LHS patterns
- *
+ *
* @param buf
* @param model
*/
@@ -156,7 +156,7 @@
}
if ( CompositeFactPattern.COMPOSITE_TYPE_EXISTS.equals( pattern.type ) ) {
buf.append( pattern.type );
- buf.append( " " );
+ buf.append( " " );
renderSubPattern( pattern );
buf.append( "\n" );
} else if ( CompositeFactPattern.COMPOSITE_TYPE_NOT.equals( pattern.type ) ) {
@@ -223,16 +223,18 @@
* This makes for more readable DRL in the most common cases.
*/
private void generateConstraint(FieldConstraint con, boolean nested) {
- if (con instanceof CompositeFieldConstraint) {
+ if (con instanceof CompositeFieldConstraint) {
CompositeFieldConstraint cfc = (CompositeFieldConstraint) con;
if (nested) buf.append( "( " );
FieldConstraint[] nestedConstraints = cfc.constraints;
- for ( int i = 0; i < nestedConstraints.length; i++ ) {
- generateConstraint( nestedConstraints[i] , true);
- if (i < (nestedConstraints.length - 1)) {
- //buf.append(" ) ");
- buf.append( cfc.compositeJunctionType + " ");
- //buf.append(" ( ");
+ if (nestedConstraints != null) {
+ for ( int i = 0; i < nestedConstraints.length; i++ ) {
+ generateConstraint( nestedConstraints[i] , true);
+ if (i < (nestedConstraints.length - 1)) {
+ //buf.append(" ) ");
+ buf.append( cfc.compositeJunctionType + " ");
+ //buf.append(" ( ");
+ }
}
}
if (nested) buf.append( ")" );
@@ -240,10 +242,10 @@
generateSingleFieldConstraint( (SingleFieldConstraint) con );
}
}
-
+
private void generateSingleFieldConstraint(final SingleFieldConstraint constr) {
if ( constr.constraintValueType == ISingleFieldConstraint.TYPE_PREDICATE ) {
- buf.append( "( " );
+ buf.append( "eval( " );
buf.append( constr.value );
buf.append( " )" );
} else {
@@ -285,7 +287,7 @@
if (operator == null) {
return;
}
-
+
buf.append( " " );
buf.append( operator );
buf.append( " " );
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersistenceTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersistenceTest.java 2007-09-04 05:19:46 UTC (rev 14848)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersistenceTest.java 2007-09-04 06:42:38 UTC (rev 14849)
@@ -38,7 +38,7 @@
}
public void testBasics() {
- String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" +
+ String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" +
"\t\tAccident( )\n\tthen\n\t\tinsert( new Report() );\nend\n";
final RuleModel m = new RuleModel();
m.addLhsItem( new FactPattern( "Person" ) );
@@ -56,7 +56,7 @@
public void testMoreComplexRendering() {
final RuleModel m = getComplexModel();
- String expected = "rule \"Complex Rule\"\n" +
+ String expected = "rule \"Complex Rule\"\n" +
"\tno-loop true\n" +
"\tsalience -10\n" +
"\tagenda-group \"aGroup\"\n" +
@@ -76,8 +76,8 @@
assertEquals( expected, drl );
}
-
-
+
+
public void testFieldBindingWithNoConstraints() {
//to satisfy JBRULES-850
RuleModel m = getModelWithNoConstraints();
@@ -115,7 +115,7 @@
//
// }
//
-
+
private RuleModel getModelWithNoConstraints() {
final RuleModel m = new RuleModel();
m.name = "Complex Rule";
@@ -130,10 +130,10 @@
pat.addConstraint( con );
m.addLhsItem( pat );
-
+
return m;
}
-
+
private RuleModel getComplexModel() {
final RuleModel m = new RuleModel();
m.name = "Complex Rule";
@@ -185,21 +185,21 @@
// m = BRXMLPersistence.getInstance().unmarshal( "" );
// assertNotNull( m );
// }
-
+
public void testCompositeConstraints() {
RuleModel m = new RuleModel();
m.name = "with composite";
-
+
FactPattern p1 = new FactPattern("Person");
p1.boundName = "p1";
m.addLhsItem( p1 );
-
+
FactPattern p = new FactPattern("Goober");
m.addLhsItem( p );
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.compositeJunctionType = CompositeFieldConstraint.COMPOSITE_TYPE_OR;
p.addConstraint( comp );
-
+
final SingleFieldConstraint X = new SingleFieldConstraint();
X.fieldName = "goo";
X.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
@@ -211,7 +211,7 @@
X.connectives[0].operator = "|| ==";
X.connectives[0].value = "bar";
comp.addConstraint( X );
-
+
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.fieldName = "goo2";
Y.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
@@ -226,82 +226,82 @@
Q1.operator = "==";
Q1.value = "whee";
Q1.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
-
+
comp2.addConstraint( Q1 );
-
+
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.fieldName = "gabba";
Q2.operator = "==";
Q2.value = "whee";
Q2.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
-
+
comp2.addConstraint( Q2 );
-
+
//now nest it
comp.addConstraint( comp2 );
-
-
-
+
+
+
final SingleFieldConstraint Z = new SingleFieldConstraint();
Z.fieldName = "goo3";
Z.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
Z.value = "foo";
Z.operator = "==";
-
+
p.addConstraint( Z );
-
+
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem( ass );
-
+
String actual = BRDRLPersistence.getInstance().marshal( m );
String expected = "rule \"with composite\" " +
" \tdialect \"mvel\"\n when " +
- "p1 : Person( ) " +
+ "p1 : Person( ) " +
"Goober( goo == \"foo\" || == \"bar\" || goo2 == \"foo\" || ( goo == \"whee\" && gabba == \"whee\" ), goo3 == \"foo\" )" +
" then " +
"insert( new Whee() );" +
"end";
assertEqualsIgnoreWhitespace( expected, actual );
-
+
}
-
+
public void testFieldsDeclaredButNoConstraints() {
RuleModel m = new RuleModel();
m.name = "boo";
-
+
FactPattern p = new FactPattern();
p.factType = "Person";
-
+
//this isn't an effective constraint, so it should be ignored.
p.addConstraint( new SingleFieldConstraint("field1") );
m.addLhsItem( p );
-
+
String actual = BRDRLPersistence.getInstance().marshal( m );
String expected = "rule \"boo\" \tdialect \"mvel\"\n when Person() then end";
-
+
assertEqualsIgnoreWhitespace( expected, actual );
-
+
SingleFieldConstraint con = (SingleFieldConstraint) p.constraintList.constraints[0];
con.fieldBinding = "q";
-
+
//now it should appear, as we are binding a var to it
-
+
actual = BRDRLPersistence.getInstance().marshal( m );
expected = "rule \"boo\" dialect \"mvel\" when Person(q : field1) then end";
-
+
assertEqualsIgnoreWhitespace( expected, actual );
-
-
+
+
}
-
+
public void testLiteralStrings() {
-
+
RuleModel m = new RuleModel();
m.name = "test literal strings";
-
+
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.fieldName = "field1";
@@ -309,32 +309,32 @@
con.value = "goo";
con.constraintValueType = SingleFieldConstraint.TYPE_LITERAL;
p.addConstraint( con );
-
-
+
+
SingleFieldConstraint con2 = new SingleFieldConstraint();
con2.fieldName = "field2";
con2.operator = "==";
con2.value = "variableHere";
con2.constraintValueType = SingleFieldConstraint.TYPE_VARIABLE;
p.addConstraint( con2 );
-
-
-
+
+
+
m.addLhsItem( p );
-
+
String result = BRDRLPersistence.getInstance().marshal( m );
-
+
assertEqualsIgnoreWhitespace( "rule \"test literal strings\"" +
"\tdialect \"mvel\"\n when " +
" Person(field1 == \"goo\", field2 == variableHere)" +
" then " +
"end", result );
-
-
-
+
+
+
}
-
+
private void assertEqualsIgnoreWhitespace(final String expected,
final String actual) {
final String cleanExpected = expected.replaceAll( "\\s+",
@@ -345,41 +345,68 @@
assertEquals( cleanExpected,
cleanActual );
}
-
+
public void testReturnValueConstraint() {
RuleModel m = new RuleModel();
m.name = "yeah";
-
+
FactPattern p = new FactPattern();
-
+
SingleFieldConstraint con = new SingleFieldConstraint();
con.constraintValueType = SingleFieldConstraint.TYPE_RET_VALUE;
con.value = "someFunc(x)";
con.operator = "==";
con.fieldName = "goo";
p.factType = "Goober";
-
+
p.addConstraint( con );
m.addLhsItem( p );
-
+
String actual = BRDRLPersistence.getInstance().marshal( m );
//System.err.println(actual);
-
-
+
+
String expected = "rule \"yeah\" " +
- "\tdialect \"mvel\"\n when " +
- "Goober( goo == ( someFunc(x) ) )" +
- " then " +
+ "\tdialect \"mvel\"\n when " +
+ "Goober( goo == ( someFunc(x) ) )" +
+ " then " +
"end";
assertEqualsIgnoreWhitespace( expected, actual);
}
-
-
+
+ public void testPredicateConstraint() {
+ RuleModel m = new RuleModel();
+ m.name = "yeah";
+
+ FactPattern p = new FactPattern();
+
+ SingleFieldConstraint con = new SingleFieldConstraint();
+ con.constraintValueType = SingleFieldConstraint.TYPE_PREDICATE;
+ con.value = "field soundslike 'poo'";
+
+ p.factType = "Goober";
+
+ p.addConstraint( con );
+ m.addLhsItem( p );
+
+ String actual = BRDRLPersistence.getInstance().marshal( m );
+ //System.err.println(actual);
+
+
+ String expected = "rule \"yeah\" " +
+ "\tdialect \"mvel\"\n when " +
+ "Goober( eval( field soundslike 'poo' ) )" +
+ " then " +
+ "end";
+ assertEqualsIgnoreWhitespace( expected, actual);
+ }
+
+
public void testConnective() {
-
+
RuleModel m = new RuleModel();
m.name = "test literal strings";
-
+
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.fieldName = "field1";
@@ -392,38 +419,38 @@
connective.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
connective.operator = "|| ==";
connective.value = "blah";
-
+
con.connectives = new ConnectiveConstraint[1];
con.connectives[0] = connective;
-
+
m.addLhsItem( p );
-
+
String result = BRDRLPersistence.getInstance().marshal( m );
-
+
String expected = "rule \"test literal strings\" " +
"\tdialect \"mvel\"\n when " +
- "Person( field1 == goo || == \"blah\" )" +
+ "Person( field1 == goo || == \"blah\" )" +
" then " +
"end";
assertEqualsIgnoreWhitespace( expected, result );
-
+
}
-
+
public void testInvalidComposite() throws Exception {
RuleModel m = new RuleModel();
CompositeFactPattern com = new CompositeFactPattern("not");
m.addLhsItem( com );
-
+
String s = BRDRLPersistence.getInstance().marshal( m );
assertNotNull(s);
-
+
m.addLhsItem( new CompositeFactPattern("or") );
m.addLhsItem( new CompositeFactPattern("exists") );
s = BRDRLPersistence.getInstance().marshal( m );
assertNotNull(s);
}
-
+
public void testAssertWithDSL() throws Exception {
RuleModel m = new RuleModel();
DSLSentence sen = new DSLSentence();
@@ -446,18 +473,18 @@
System.err.println(result);
assertTrue(result.indexOf( ">insertLogical" ) > -1);
}
-
+
public void testDefaultMVEL() {
RuleModel m = new RuleModel();
-
+
String s = BRDRLPersistence.getInstance().marshal( m );
assertTrue(s.indexOf( "mvel" ) > -1);
-
+
m.addAttribute( new RuleAttribute("dialect", "goober") );
s = BRDRLPersistence.getInstance().marshal( m );
assertFalse(s.indexOf( "mvel" ) > -1);
assertTrue(s.indexOf( "goober" ) > -1);
-
+
}
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java 2007-09-04 05:19:46 UTC (rev 14848)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/MiscTest.java 2007-09-04 06:42:38 UTC (rev 14849)
@@ -2,13 +2,13 @@
/*
* Copyright 2005 JBoss Inc
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -296,6 +296,7 @@
final DrlParser parser = new DrlParser();
final PackageDescr packageDescr = parser.parse( reader );
if ( parser.hasErrors() ) {
+ System.err.println(parser.getErrors());
Assert.fail( "Error messages in parser, need to sort this our (or else collect error messages)" );
}
// pre build the package
@@ -358,6 +359,22 @@
}
+ public void testMVELSoundex() throws Exception {
+
+ // read in the source
+ final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "MVEL_soundex.drl" ) );
+ final RuleBase ruleBase = loadRuleBase( reader );
+
+ final WorkingMemory workingMemory = ruleBase.newStatefulSession();
+
+ Cheese c = new Cheese("fubar", 2);
+
+
+ workingMemory.insert( c );
+ workingMemory.fireAllRules();
+ assertEquals(42, c.getPrice());
+ }
+
public void testLiteral() throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "literal_rule_test.drl" ) ) );
@@ -657,7 +674,7 @@
list.get( 0 ) );
}
- public void testJaninoEval() throws Exception {
+ public void testJaninoEval() throws Exception {
final PackageBuilderConfiguration config = new PackageBuilderConfiguration();
JavaDialectConfiguration javaConf = (JavaDialectConfiguration) config.getDialectConfiguration( "java" );
javaConf.setCompiler( JavaDialectConfiguration.JANINO );
@@ -1159,8 +1176,8 @@
e.getCause().getMessage() );
}
}
-
- public void testCustomConsequenceException() throws Exception {
+
+ public void testCustomConsequenceException() throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_ConsequenceException.drl" ) ) );
final Package pkg = builder.getPackage();
@@ -1168,7 +1185,7 @@
RuleBaseConfiguration conf = new RuleBaseConfiguration();
CustomConsequenceExceptionHandler handler = new CustomConsequenceExceptionHandler();
conf.setConsequenceExceptionHandler( handler );
-
+
final RuleBase ruleBase = getRuleBase(conf);
ruleBase.addPackage( pkg );
final WorkingMemory workingMemory = ruleBase.newStatefulSession();
@@ -1178,24 +1195,24 @@
workingMemory.insert( brie );
workingMemory.fireAllRules();
-
+
assertTrue( handler.isCalled() );
}
-
+
public static class CustomConsequenceExceptionHandler implements ConsequenceExceptionHandler {
-
+
private boolean called;
public void handleException(Activation activation,
WorkingMemory workingMemory,
Exception exception) {
- this.called = true;
+ this.called = true;
}
-
+
public boolean isCalled() {
return this.called;
}
-
+
}
public void testFunctionException() throws Exception {
@@ -3774,55 +3791,55 @@
assertEquals( results.get( 0 ),
events.next() );
}
-
+
public void testNotInStatelessSession() throws Exception {
final PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "test_NotInStatelessSession.drl" )) );
final Package pkg = builder.getPackage();
-
+
RuleBaseConfiguration conf = new RuleBaseConfiguration();
conf.setSequential( true );
final RuleBase ruleBase = getRuleBase(conf);
ruleBase.addPackage( pkg );
-
+
StatelessSession session = ruleBase.newStatelessSession();
List list = new ArrayList();
session.setGlobal( "list", list );
session.execute( "not integer" );
- assertEquals("not integer", list.get( 0 ) );
+ assertEquals("not integer", list.get( 0 ) );
}
-
+
public void testDynamicallyAddInitialFactRule() throws Exception {
PackageBuilder builder = new PackageBuilder();
String rule = "package org.drools.test\n global java.util.List list\n rule xxx\n when\n i:Integer()\nthen\n list.add(i);\nend";
builder.addPackageFromDrl( new StringReader( rule ) );
Package pkg = builder.getPackage();
-
+
final RuleBase ruleBase = getRuleBase();
ruleBase.addPackage( pkg );
-
+
StatefulSession session = ruleBase.newStatefulSession();
List list = new ArrayList();
session.setGlobal( "list", list );
-
+
session.insert( new Integer( 5) );
session.fireAllRules();
-
+
assertEquals( new Integer(5), list.get( 0 ) );
-
+
builder = new PackageBuilder();
rule = "package org.drools.test\n global java.util.List list\n rule xxx\n when\nthen\n list.add(\"x\");\nend";
builder.addPackageFromDrl( new StringReader( rule ) );
pkg = builder.getPackage();
-
+
// Make sure that this rule is fired as the Package is updated, it also tests that InitialFactImpl is still in the network
// even though the first rule didn't use it.
ruleBase.addPackage( pkg );
-
+
assertEquals( "x", list.get( 1 ) );
-
- }
+ }
+
// FIXME
public void FIXMEtestEvalRewriteWithSpecialOperators() throws Exception {
final PackageBuilder builder = new PackageBuilder();
@@ -3895,12 +3912,12 @@
assertEquals( item41, list.get( index++ ) );
assertEquals( order5, list.get( index++ ) );
assertEquals( order5, list.get( index++ ) );
-
-
+
+
}
-
+
public void testImportColision() throws Exception {
-
+
final PackageBuilder builder = new PackageBuilder();
final PackageBuilder builder2 = new PackageBuilder();
builder.addPackageFromDrl( new InputStreamReader( getClass().getResourceAsStream( "nested1.drl" ) ) );
@@ -3965,9 +3982,9 @@
assertEquals( 1,
results.size() );
- assertEquals( 1,
+ assertEquals( 1,
cheesery.getCheeses().size() );
- assertEquals( results.get( 0 ),
+ assertEquals( results.get( 0 ),
cheesery.getCheeses().get( 0 ) );
}
Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/MVEL_soundex.drl
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/MVEL_soundex.drl (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/MVEL_soundex.drl 2007-09-04 06:42:38 UTC (rev 14849)
@@ -0,0 +1,13 @@
+package MVELTest
+
+dialect "mvel"
+
+import org.drools.Cheese
+
+
+rule "Hello World"
+ when
+ c : Cheese( eval(type soundslike "foobar") )
+ then
+ c.setPrice(42);
+end
Property changes on: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/integrationtests/MVEL_soundex.drl
___________________________________________________________________
Name: svn:eol-style
+ native
More information about the jboss-svn-commits
mailing list