@Override public String getSource( final Path path, final RuleModel model ) { return new StringBuilder().append( BRDRLPersistence.getInstance().marshal( model ) ).toString(); }
@Override public String getSource( final Path path, final RuleModel model ) { return new StringBuilder().append( BRDRLPersistence.getInstance().marshal( model ) ).toString(); }
private void checkMarshallUnmarshall( String expected, RuleModel m ) { String drl = brlPersistence.marshal( m ); assertNotNull( drl ); if ( expected != null ) { assertEqualsIgnoreWhitespace( expected, drl ); } RuleModel unmarshalledModel = brlPersistence.unmarshal( drl ); if ( expected != null ) { assertEqualsIgnoreWhitespace( expected, brlPersistence.marshal( unmarshalledModel ) ); } else { assertEquals( drl, brlPersistence.marshal( unmarshalledModel ) ); } }
@Test public void testAttr() { RuleModel m = new RuleModel(); m.attributes = new RuleAttribute[ 1 ]; m.attributes[ 0 ] = new RuleAttribute( "enabled", "true" ); final String drl = brlPersistence.marshal( m ); assertTrue( drl.indexOf( "enabled true" ) > 0 ); }
@Test 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 ); }
@Test public void testNotMultiPatterns() throws Exception { RuleModel m = getCompositeFOL( CompositeFactPattern.COMPOSITE_TYPE_NOT ); String result = BRDRLPersistence.getInstance().marshal( m ); assertTrue( result.indexOf( "not (Person( age == 42 ) and Person( age == 43 ))" ) > 0 ); checkMarshallUnmarshall( result, m ); }
@Test public void testFieldBindingWithNoConstraints() { // to satisfy JBRULES-850 RuleModel m = getModelWithNoConstraints(); String s = BRDRLPersistence.getInstance().marshal( m ); // System.out.println(s); assertTrue( s.contains( "Person( f1 : age)" ) ); checkMarshallUnmarshall( s, m ); }
@Test public void testExistsMultiPatterns() throws Exception { RuleModel m = getCompositeFOL( CompositeFactPattern.COMPOSITE_TYPE_EXISTS ); String result = BRDRLPersistence.getInstance().marshal( m ); assertTrue( result.indexOf( "exists (Person( age == 42 ) and Person( age == 43 ))" ) > 0 ); checkMarshallUnmarshall( result, m ); }
@Test public void testFreeForm() { RuleModel m = new RuleModel(); m.name = "with composite"; m.lhs = new IPattern[ 1 ]; m.rhs = new IAction[ 1 ]; FreeFormLine fl = new FreeFormLine(); fl.setText( "Person()" ); m.lhs[ 0 ] = fl; FreeFormLine fr = new FreeFormLine(); fr.setText( "fun()" ); m.rhs[ 0 ] = fr; String drl = brlPersistence.marshal( m ); assertNotNull( drl ); assertTrue( drl.indexOf( "Person()" ) > 0 ); assertTrue( drl.indexOf( "fun()" ) > drl.indexOf( "Person()" ) ); }
@Test public void testMoreComplexRenderingWithDsl() { final RuleModel m = getComplexModel( true ); String expected = "rule \"Complex Rule\"\n" + "\tno-loop true\n" + "\tsalience -10\n" + "\tagenda-group \"aGroup\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\t\t>p1 : Person( f1 : age < 42 )\n" + "\t\t>not (Cancel( )) \n" + "\tthen\n" + "\t\t>p1.setStatus( \"rejected\" );\n" + "\t\t>update( p1 );\n" + "\t\t>retract( p1 );\n" + "\t\tSend an email to administrator\n" + "end\n"; checkMarshallUnmarshall( expected, m ); String drl = brlPersistence.marshal( m ); assertEqualsIgnoreWhitespace( expected, drl ); String dslFile = "[then]Send an email to {administrator}=sendMailTo({administrator});"; RuleModel unmarshalledModel = brlPersistence.unmarshalUsingDSL( drl, null, dslFile ); IAction[] actions = unmarshalledModel.rhs; DSLSentence dslSentence = (DSLSentence) actions[ actions.length - 1 ]; assertEquals( "Send an email to {administrator}", dslSentence.getDefinition() ); assertEqualsIgnoreWhitespace( expected, brlPersistence.marshal( unmarshalledModel ) ); }
@Test 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 ); }
@Test public void testLockOnActive() { RuleModel m = new RuleModel(); m.addAttribute( new RuleAttribute( "lock-on-active", "true" ) ); m.addAttribute( new RuleAttribute( "auto-focus", "true" ) ); m.addAttribute( new RuleAttribute( "duration", "42" ) ); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "lock-on-active true" ) > -1 ); assertTrue( s.indexOf( "auto-focus true" ) > -1 ); assertTrue( s.indexOf( "duration 42" ) > -1 ); checkMarshallUnmarshall( s, m ); }
@Test public void testDSLExpansionLHS() { final String dslDefinition = "The credit rating is {rating:ENUM:Applicant.creditRating}"; final String drlExpected = "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "The credit rating is AA\n" + "then\n" + "end"; final DSLSentence dsl = new DSLSentence(); dsl.setDefinition( dslDefinition ); //The following line is normally performed by the UI when the user sets values dsl.getValues().get( 0 ).setValue( "AA" ); //Append DSL to RuleModel to check marshalling final RuleModel m = new RuleModel(); m.name = "r1"; m.addLhsItem( dsl ); final String drlActual = brlPersistence.marshal( m ); assertEqualsIgnoreWhitespace( drlExpected, drlActual ); }
@Test public void textIsNotNullOperator() { final RuleModel m = new RuleModel(); m.name = "IsNotNullOperator"; final FactPattern pat = new FactPattern( "Person" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "age" ); con.setOperator( "!= null" ); pat.addConstraint( con ); m.addLhsItem( pat ); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "Person( age != null )" ) != -1 ); checkMarshallUnmarshall( s, m ); }
@Test public void textIsNullOperator() { final RuleModel m = new RuleModel(); m.name = "IsNullOperator"; final FactPattern pat = new FactPattern( "Person" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "age" ); con.setOperator( "== null" ); pat.addConstraint( con ); m.addLhsItem( pat ); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "Person( age == null )" ) != -1 ); checkMarshallUnmarshall( s, m ); }
@Test public void testSingleExists() throws Exception { RuleModel m = new RuleModel(); m.name = "or"; CompositeFactPattern cp = new CompositeFactPattern( CompositeFactPattern.COMPOSITE_TYPE_EXISTS ); FactPattern p1 = new FactPattern( "Person" ); SingleFieldConstraint sf1 = new SingleFieldConstraint( "age" ); sf1.setOperator( "==" ); sf1.setValue( "42" ); p1.addConstraint( sf1 ); cp.addFactPattern( p1 ); m.addLhsItem( cp ); String result = BRDRLPersistence.getInstance().marshal( m ); assertTrue( result.indexOf( "exists (Person( age == 42 )) " ) > 0 ); checkMarshallUnmarshall( result, m ); }
@Test public void testAssertWithDSL() throws Exception { RuleModel m = new RuleModel(); DSLSentence sen = new DSLSentence(); sen.setDefinition( "I CAN HAS DSL" ); m.addRhsItem( sen ); ActionInsertFact ins = new ActionInsertFact( "Shizzle" ); ActionFieldValue val = new ActionFieldValue( "goo", "42", "Numeric" ); ins.setFieldValues( new ActionFieldValue[ 1 ] ); ins.getFieldValues()[ 0 ] = val; m.addRhsItem( ins ); ActionInsertLogicalFact insL = new ActionInsertLogicalFact( "Shizzle" ); ActionFieldValue valL = new ActionFieldValue( "goo", "42", "Numeric" ); insL.setFieldValues( new ActionFieldValue[ 1 ] ); insL.getFieldValues()[ 0 ] = valL; m.addRhsItem( insL ); String result = BRDRLPersistence.getInstance().marshal( m ); assertTrue( result.indexOf( ">insert" ) > -1 ); assertTrue( result.indexOf( ">insertLogical" ) > -1 ); }
@Test public void testFieldsDeclaredButNoConstraints() { RuleModel m = new RuleModel(); m.name = "boo"; FactPattern p = new FactPattern( "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"; checkMarshallUnmarshall( expected, m ); SingleFieldConstraint con = (SingleFieldConstraint) p.getConstraintList().getConstraint( 0 ); con.setFieldBinding( "q" ); // now it should appear, as we are binding a var to it expected = "rule \"boo\" dialect \"mvel\" when Person(q : field1) then end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testOrComposite() throws Exception { RuleModel m = new RuleModel(); m.name = "or"; CompositeFactPattern cp = new CompositeFactPattern( CompositeFactPattern.COMPOSITE_TYPE_OR ); FactPattern p1 = new FactPattern( "Person" ); SingleFieldConstraint sf1 = new SingleFieldConstraint( "age" ); sf1.setOperator( "==" ); sf1.setValue( "42" ); p1.addConstraint( sf1 ); cp.addFactPattern( p1 ); FactPattern p2 = new FactPattern( "Person" ); SingleFieldConstraint sf2 = new SingleFieldConstraint( "age" ); sf2.setOperator( "==" ); sf2.setValue( "43" ); p2.addConstraint( sf2 ); cp.addFactPattern( p2 ); m.addLhsItem( cp ); String result = BRDRLPersistence.getInstance().marshal( m ); assertTrue( result.indexOf( "( Person( age == 42 ) or Person( age == 43 ) )" ) > 0 ); checkMarshallUnmarshall( result, m ); }
@Test public void testLHSExpressionString2() { RuleModel m = new RuleModel(); m.name = "test expressionsString2"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide(); con.getExpressionLeftSide().appendPart( new ExpressionUnboundFact( p ) ); con.getExpressionLeftSide().appendPart( new ExpressionField( "field1", "java.lang.String", DataType.TYPE_STRING ) ); con.setOperator( "==" ); con.setValue( "Cheddar" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); String expected = "rule \"test expressionsString2\"" + "\tdialect \"mvel\"\n when " + " Person( field1 == \"Cheddar\" )" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }