private List<IPattern> cloneDefinition( List<IPattern> definition ) { RuleModelCloneVisitor visitor = new RuleModelCloneVisitor(); RuleModel rm = new RuleModel(); for ( IPattern pattern : definition ) { rm.addLhsItem( pattern ); } RuleModel rmClone = visitor.visitRuleModel( rm ); List<IPattern> clone = new ArrayList<IPattern>(); for ( IPattern pattern : rmClone.lhs ) { clone.add( pattern ); } return clone; }
private List<IAction> cloneDefinition( List<IAction> definition ) { RuleModelCloneVisitor visitor = new RuleModelCloneVisitor(); RuleModel rm = new RuleModel(); for ( IAction action : definition ) { rm.addRhsItem( action ); } RuleModel rmClone = visitor.visitRuleModel( rm ); List<IAction> clone = new ArrayList<IAction>(); for ( IAction action : rmClone.rhs ) { clone.add( action ); } return clone; }
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(); } } );
/** * This will get a list of all bound variables (LHS and RHS), including bound fields.. */ public List<String> getAllVariables() { List<String> result = new ArrayList<String>(); result.addAll( this.getAllLHSVariables() ); result.addAll( this.getAllRHSVariables() ); return result; }
@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 ); }
@Test public void testEnumNoType() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( type == \"CheeseType.CHEDDAR\" )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "type" ); con.setOperator( "==" ); con.setValue( "CheeseType.CHEDDAR" ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@Test public void testBoundFromCompositeFactFinder() { final RuleModel model = new RuleModel(); model.getLHSBoundFact( "x" ) ); model.getLHSBoundFacts().size() ); assertEquals( "x", model.getLHSBoundFacts().get( 0 ) ); model.getLHSBoundField( "a" ) ); model.getLHSBindingType( "x" ) ); assertEquals( "String", model.getLHSBindingType( "a" ) ); model.getLHSParentFactPatternForBinding( "a" ) ); model.getAllLHSVariables().size() ); assertTrue( model.getAllLHSVariables().contains( "x" ) ); assertTrue( model.getAllLHSVariables().contains( "a" ) ); model.rhs[0] = set; assertTrue( model.isBoundFactUsed( "x" ) ); assertFalse( model.removeLhsItem( 0 ) ); assertEquals( 1, model.lhs.length );
@Test public void testIsDSLEnhanced() throws Exception { RuleModel m = new RuleModel(); assertFalse( m.hasDSLSentences() ); m.addLhsItem( new FactPattern() ); assertFalse( m.hasDSLSentences() ); m.addRhsItem( new ActionSetField( "q" ) ); assertFalse( m.hasDSLSentences() ); m.addLhsItem( new DSLSentence() ); assertTrue( m.hasDSLSentences() ); m.addRhsItem( new DSLSentence() ); assertTrue( m.hasDSLSentences() ); m = new RuleModel(); m.addLhsItem( new DSLSentence() ); assertTrue( m.hasDSLSentences() ); m = new RuleModel(); m.addRhsItem( new DSLSentence() ); assertTrue( m.hasDSLSentences() ); }
@Test public void testGetVariableNameForRHS() { RuleModel m = new RuleModel(); m.name = "blah"; FactPattern pat = new FactPattern( "Person" ); pat.setBoundName( "pat" ); m.addLhsItem( pat ); List l = m.getAllVariables(); assertEquals( 1, l.size() ); assertEquals( "pat", l.get( 0 ) ); }
public RuleModelVisitor( IAction action, Map<InterpolationVariable, Integer> vars ) { this.vars = vars; this.model.addRhsItem( action ); }
@Test public void testMetaData() { final RuleModel m = new RuleModel(); m.addMetadata( rm ); assertEquals( 1, m.metadataList.length ); RuleMetadata gm = m.getMetaData( "foo" ); assertNotNull( gm ); m.addMetadata( rm2 ); assertEquals( 2, m.metadataList.length ); m.removeMetadata( 0 ); assertEquals( 1, m.metadataList.length ); gm = m.getMetaData( "foo" ); assertNull( gm ); m.updateMetadata( rm ); gm = m.getMetaData( "foo" ); assertNotNull( gm ); m.updateMetadata( rm ); gm = m.getMetaData( "foo" ); assertNotNull( gm ); assertEquals( "bar2",
public void addLhsItem( final IPattern pat, boolean append ) { this.addLhsItem( pat, append ? this.lhs.length : 0 ); }
@Test public void testBoundFactFinder() { final RuleModel model = new RuleModel(); assertNull( model.getLHSBoundFact( "x" ) ); model.lhs = new IPattern[3]; x.setBoundName( "x" ); assertNotNull( model.getLHSBoundFact( "x" ) ); assertEquals( x, model.getLHSBoundFact( "x" ) ); model.getLHSBoundFact( "y" ) ); assertEquals( x, model.getLHSBoundFact( "x" ) ); model.rhs[0] = set; assertTrue( model.isBoundFactUsed( "x" ) ); assertFalse( model.isBoundFactUsed( "y" ) ); assertFalse( model.removeLhsItem( 0 ) ); assertEquals( 3, model.lhs.length ); assertTrue( model.isBoundFactUsed( "q" ) ); assertFalse( model.isBoundFactUsed( "x" ) );
@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 ); }
private RuleModel getRuleModel( ExpandedDRLInfo expandedDRLInfo ) { //De-serialize model RuleDescr ruleDescr = parseDrl( expandedDRLInfo ); RuleModel model = new RuleModel(); model.name = ruleDescr.getName(); //De-serialize Package name final String packageName = PackageNameParser.parsePackageName( expandedDRLInfo.plainDrl ); model.setPackageName( packageName ); //De-serialize imports final Imports imports = ImportsParser.parseImports( expandedDRLInfo.plainDrl ); for ( Import item : imports.getImports() ) { model.getImports().addImport( item ); } boolean isJavaDialect = parseAttributes( model, ruleDescr.getAttributes() ); Map<String, String> boundParams = parseLhs( model, ruleDescr.getLhs(), expandedDRLInfo ); parseRhs( model, expandedDRLInfo.consequence != null ? expandedDRLInfo.consequence : (String)ruleDescr.getConsequence(), isJavaDialect, boundParams, expandedDRLInfo ); return model; }
@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 testAllVariableBindings() { final RuleModel model = new RuleModel(); model.lhs = new IPattern[2]; final FactPattern x = new FactPattern( "Car" ); model.lhs[0] = x; x.setBoundName( "boundFact" ); SingleFieldConstraint sfc = new SingleFieldConstraint( "q" ); x.addConstraint( sfc ); sfc.setFieldBinding( "field1" ); SingleFieldConstraint sfc2 = new SingleFieldConstraint( "q" ); x.addConstraint( sfc2 ); sfc2.setFieldBinding( "field2" ); model.lhs[1] = new CompositeFactPattern(); List vars = model.getAllVariables(); assertEquals( 3, vars.size() ); assertEquals( "boundFact", vars.get( 0 ) ); assertEquals( "field1", vars.get( 1 ) ); assertEquals( "field2", vars.get( 2 ) ); assertTrue( model.isVariableNameUsed( "field2" ) ); }
@Test public void testGenerateEmptyDRL() { String expected = "rule \"null\"\n\tdialect \"mvel\"\n\twhen\n\tthen\nend\n"; checkMarshallUnmarshall( expected, new RuleModel() ); }
private boolean isLHSFieldTypeEquivalent( String boundVariable ) { String boundFieldType = this.model.getLHSBindingType( boundVariable ); //If the fieldTypes are SuggestionCompletionEngine.TYPE_COMPARABLE check the enums are equivalent if ( boundFieldType.equals( DataType.TYPE_COMPARABLE ) ) { if ( !this.fieldType.equals( DataType.TYPE_COMPARABLE ) ) { return false; } FieldConstraint fc = this.model.getLHSBoundField( boundVariable ); if ( fc instanceof SingleFieldConstraint ) { String fieldName = ( (SingleFieldConstraint) fc ).getFieldName(); String parentFactTypeForBinding = this.model.getLHSParentFactPatternForBinding( boundVariable ).getFactType(); String[] dd = this.sce.getEnumValues( parentFactTypeForBinding, fieldName ); return isEnumEquivalent( dd ); } return false; } return isBoundVariableApplicable( boundFieldType ); }
private List<String> getApplicableBindings() { List<String> bindings = new ArrayList<String>(); //Examine LHS Fact and Field bindings and RHS (new) Fact bindings for ( String v : modeller.getModel().getAllVariables() ) { //LHS FactPattern FactPattern fp = modeller.getModel().getLHSBoundFact( v ); if ( fp != null ) { if ( isLHSFactTypeEquivalent( v ) ) { bindings.add( v ); } } //LHS FieldConstraint FieldConstraint fc = modeller.getModel().getLHSBoundField( v ); if ( fc != null ) { if ( isLHSFieldTypeEquivalent( v ) ) { bindings.add( v ); } } //RHS ActionInsertFact ActionInsertFact aif = modeller.getModel().getRHSBoundFact( v ); if ( aif != null ) { if ( isRHSFieldTypeEquivalent( v ) ) { bindings.add( v ); } } } return bindings; }