private boolean parseAttributes( RuleModel m, Map<String, AttributeDescr> attributes ) { boolean isJavaDialect = false; for ( Map.Entry<String, AttributeDescr> entry : attributes.entrySet() ) { String name = entry.getKey(); String value = entry.getValue().getValue(); RuleAttribute ruleAttribute = new RuleAttribute( name, value ); m.addAttribute( ruleAttribute ); isJavaDialect |= name.equals( "dialect" ) && value.equals( "java" ); } return isJavaDialect; }
public void onChange( ChangeEvent event ) { String attr = list.getItemText( list.getSelectedIndex() ); if ( attr.equals( RuleAttributeWidget.LOCK_LHS ) || attr.equals( RuleAttributeWidget.LOCK_RHS ) ) { model.addMetadata( new RuleMetadata( attr, "true" ) ); } else { model.addAttribute( new RuleAttribute( attr, "" ) ); } refresh.execute(); hide(); } } );
@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 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 testAttributes() { final RuleModel m = new RuleModel(); final RuleAttribute at = new RuleAttribute( "salience", "42" ); m.addAttribute( at ); assertEquals( 1, m.attributes.length ); assertEquals( at, m.attributes[0] ); final RuleAttribute at2 = new RuleAttribute( "agenda-group", "x" ); m.addAttribute( at2 ); assertEquals( 2, m.attributes.length ); assertEquals( at2, m.attributes[1] ); m.removeAttribute( 0 ); assertEquals( 1, m.attributes.length ); assertEquals( at2, m.attributes[0] ); } /*
@Test public void testBasics() { 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"; RuleModel m = new RuleModel(); m.addLhsItem( new FactPattern( "Person" ) ); m.addLhsItem( new FactPattern( "Accident" ) ); m.addAttribute( new RuleAttribute( "no-loop", "true" ) ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@Test public void testInsertLogical() { String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" + "\t\tAccident( )\n\tthen\n\t\tinsertLogical( new Report() );\nend\n"; final RuleModel m = new RuleModel(); m.addLhsItem( new FactPattern( "Person" ) ); m.addLhsItem( new FactPattern( "Accident" ) ); m.addAttribute( new RuleAttribute( "no-loop", "true" ) ); m.addRhsItem( new ActionInsertLogicalFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
m.addAttribute( new RuleAttribute( "dialect", "mvel" ) );
m.addAttribute( new RuleAttribute( "dialect", "java" ) );
m.name = "Complex Rule"; m.addAttribute( new RuleAttribute( "no-loop", "true" ) ); m.addAttribute( new RuleAttribute( "salience", "-10" ) ); m.addAttribute( new RuleAttribute( "agenda-group", "aGroup" ) );
@Test public void testAddGlobal() { 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() );\n\t\tresults.add(f);\nend\n"; final RuleModel m = new RuleModel(); m.addLhsItem( new FactPattern( "Person" ) ); m.addLhsItem( new FactPattern( "Accident" ) ); m.addAttribute( new RuleAttribute( "no-loop", "true" ) ); m.addRhsItem( new ActionInsertFact( "Report" ) ); ActionGlobalCollectionAdd add = new ActionGlobalCollectionAdd(); add.setGlobalName( "results" ); add.setFactName( "f" ); m.addRhsItem( add ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBigIntegerJava() { RuleModel m = new RuleModel(); m.name = "test literal biginteger"; m.addAttribute( new RuleAttribute( "dialect", "java" ) ); FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_NUMERIC_BIGINTEGER ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); ActionInsertFact ai = new ActionInsertFact( "Person" ); ai.addFieldValue( new ActionFieldValue( "field1", "55", DataType.TYPE_NUMERIC_BIGINTEGER ) ); m.addRhsItem( ai ); String expected = "rule \"test literal biginteger\" \n" + "\tdialect \"java\"\n when \n" + " Person(field1 == 44I ) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigInteger( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBigDecimalMvel() { RuleModel m = new RuleModel(); m.name = "test literal bigdecimal"; m.addAttribute( new RuleAttribute( "dialect", "mvel" ) ); FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_NUMERIC_BIGDECIMAL ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); ActionInsertFact ai = new ActionInsertFact( "Person" ); ai.addFieldValue( new ActionFieldValue( "field1", "55", DataType.TYPE_NUMERIC_BIGDECIMAL ) ); m.addRhsItem( ai ); String expected = "rule \"test literal bigdecimal\" \n" + "\tdialect \"mvel\"\n when \n" + " Person(field1 == 44B) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55B ); \n" + "insert( fact0 ); \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBigDecimalJava() { RuleModel m = new RuleModel(); m.name = "test literal bigdecimal"; m.addAttribute( new RuleAttribute( "dialect", "java" ) ); FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_NUMERIC_BIGDECIMAL ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); ActionInsertFact ai = new ActionInsertFact( "Person" ); ai.addFieldValue( new ActionFieldValue( "field1", "55", DataType.TYPE_NUMERIC_BIGDECIMAL ) ); m.addRhsItem( ai ); String expected = "rule \"test literal bigdecimal\" \n" + "\tdialect \"java\"\n when \n" + " Person(field1 == 44B) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigDecimal( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end"; checkMarshallUnmarshall( expected, m ); }
m.name = "test literal biginteger"; m.addAttribute( new RuleAttribute( "dialect", "mvel" ) );