@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 ) ); }
@Test public void testRemoveItemLhs() { final RuleModel model = new RuleModel(); model.lhs = new IPattern[3]; final FactPattern x = new FactPattern( "Car" ); model.lhs[0] = x; x.setBoundName( "x" ); final FactPattern y = new FactPattern( "Car" ); model.lhs[1] = y; y.setBoundName( "y" ); final FactPattern other = new FactPattern( "House" ); model.lhs[2] = other; assertEquals( 3, model.lhs.length ); assertEquals( x, model.lhs[0] ); model.removeLhsItem( 0 ); assertEquals( 2, model.lhs.length ); assertEquals( y, model.lhs[0] ); }
@Test public void testIsBound() { FactPattern pat = new FactPattern(); pat.setBoundName("x"); assertTrue(pat.isBound()); pat = new FactPattern(); assertFalse(pat.isBound()); }
@Test public void testAllVariableBindings2() { final RuleModel model = new RuleModel(); model.lhs = new IPattern[1]; final FactPattern fp = new FactPattern( "Car" ); model.lhs[0] = fp; fp.setBoundName( "$c" ); SingleFieldConstraint sfc = new SingleFieldConstraintEBLeftSide( "make" ); sfc.getExpressionValue().appendPart( new ExpressionField( "make", "java.lang.String", SuggestionCompletionEngine.TYPE_STRING ) ); sfc.setFieldBinding( "$m" ); fp.addConstraint( sfc ); List<String> vars = model.getAllVariables(); assertEquals( 2, vars.size() ); assertEquals( "$c", vars.get( 0 ) ); assertEquals( "$m", vars.get( 1 ) ); }
private RuleModel getModelWithNoConstraints() { final RuleModel m = new RuleModel(); m.name = "Complex Rule"; final FactPattern pat = new FactPattern( "Person" ); pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldBinding( "f1" ); con.setFieldName( "age" ); // con.operator = "<"; // con.value = "42"; pat.addConstraint( con ); m.addLhsItem( pat ); return m; }
p.setBoundName( "$p" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_DATE );
private FactPattern visitFactPattern(FactPattern pattern) { FactPattern clone = new FactPattern(); clone.setBoundName( pattern.getBoundName() ); clone.setFactType( pattern.getFactType() ); clone.setNegated( pattern.isNegated() ); CEPWindow cloneCEPWindow = new CEPWindow(); cloneCEPWindow.setOperator( pattern.getWindow().getOperator() ); cloneCEPWindow.setParameters( cloneCEPWindowParameters( pattern.getWindow() ) ); clone.setWindow( cloneCEPWindow ); for ( FieldConstraint fc : pattern.getFieldConstraints() ) { clone.addConstraint( (FieldConstraint) visit( fc ) ); } return clone; }
final FactPattern x = new FactPattern( "Car" ); model.lhs[0] = x; x.setBoundName( "x" ); y.setBoundName( "y" );
private FactPattern visitFactPattern(FactPattern pattern) { FactPattern clone = new FactPattern(); clone.setBoundName( pattern.getBoundName() ); clone.setFactType( pattern.getFactType() ); clone.setNegated( pattern.isNegated() ); CEPWindow cloneCEPWindow = new CEPWindow(); cloneCEPWindow.setOperator( pattern.getWindow().getOperator() ); cloneCEPWindow.setParameters( cloneCEPWindowParameters( pattern.getWindow() ) ); clone.setWindow( cloneCEPWindow ); for ( FieldConstraint fc : pattern.getFieldConstraints() ) { clone.addConstraint( (FieldConstraint) visit( fc ) ); } return clone; }
@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" ) ); }
x.setBoundName( "x" ); y.setBoundName( "y" );
fp.setBoundName( "$brl1" );
@Test public void toStringText() { ExpressionFormLine efl = new ExpressionFormLine(); FactPattern fact = new FactPattern( "String" ); fact.setBoundName( "$v" ); efl.appendPart( new ExpressionVariable( fact ) ); efl.appendPart( new ExpressionMethod( "size", "int", SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ) ); assertEquals( "$v.size()", efl.getText() ); efl.setBinding( "$s" ); assertEquals( "$s: $v.size()", efl.getText( true ) ); } }
@Test public void testCopy() { ExpressionFormLine efl = new ExpressionFormLine(); FactPattern fp = new FactPattern(SuggestionCompletionEngine.TYPE_OBJECT); fp.setBoundName("$o"); efl.appendPart(new ExpressionVariable(fp)); ExpressionMethod em = new ExpressionMethod("aMethod", "aClass", "aType"); ExpressionFormLine param = new ExpressionFormLine(); param.appendPart(new ExpressionText("\"hello world\"")); em.putParam("hi", param); efl.appendPart(em); ExpressionCollectionIndex index = new ExpressionCollectionIndex("get", "aRetType", "aGeneric"); efl.appendPart(index); index = new ExpressionCollectionIndex("get2", "aRetType", "aGeneric"); efl.appendPart(index); System.out.println(efl.getText()); ExpressionFormLine copy = new ExpressionFormLine(efl); assertNotSame(efl, copy); assertEquals(efl.getText(), copy.getText()); for (ExpressionPart e1 = efl.getRootExpression(), e2 = copy.getRootExpression(); e1 != null; e1 = e1.getNext(), e2 = e2.getNext()) { assertNotSame(e1, e2); assertEquals(e1.getClass(), e2.getClass()); } }
fp.setBoundName( "$brl1" );
@Test public void testFactPattern_Basics() { RuleModel model = new RuleModel(); model.lhs = new IPattern[1]; FactPattern fp0 = new FactPattern(); fp0.setBoundName( "$t0" ); fp0.setFactType( "FT0" ); fp0.setNegated( true ); model.lhs[0] = fp0; RuleModelCloneVisitor cloneVisitor = new RuleModelCloneVisitor(); RuleModel clone = cloneVisitor.visitRuleModel( model ); assertEquals( 1, clone.lhs.length ); assertNotSame( model.lhs[0], clone.lhs[0] ); assertNotNull( clone.lhs[0] ); assertTrue( clone.lhs[0] instanceof FactPattern ); FactPattern fp0Clone = (FactPattern) clone.lhs[0]; assertEquals( fp0.getBoundName(), fp0Clone.getBoundName() ); assertEquals( fp0.getFactType(), fp0Clone.getFactType() ); assertEquals( fp0.isNegated(), fp0Clone.isNegated() ); }
@Test public void testCompositeFactPatternWithFromWithDSL() { final RuleModel m = new RuleModel(); m.name = "model"; final DSLSentence sen = new DSLSentence(); sen.setDefinition( "A DSL phrase" ); m.addLhsItem( sen ); final FactPattern fp1 = new FactPattern( "Data" ); fp1.setBoundName( "$d" ); m.addLhsItem( fp1 ); final CompositeFactPattern cp = new CompositeFactPattern( CompositeFactPattern.COMPOSITE_TYPE_NOT ); final FactPattern fp2 = new FactPattern( "Person" ); final FromCompositeFactPattern ffp1 = new FromCompositeFactPattern(); ffp1.setExpression( new ExpressionFormLine( new ExpressionVariable( fp1 ) ) ); ffp1.setFactPattern( fp2 ); cp.addFactPattern( ffp1 ); m.addLhsItem( cp ); final String actual = BRDRLPersistence.getInstance().marshal( m ); final String expected = "rule \"model\"\n" + "dialect \"mvel\"\n" + "when\n" + "A DSL phrase\n" + ">$d : Data( )\n" + ">not ( Person( ) from $d\n" + ")\n" + "then\n" + "end\n"; assertEqualsIgnoreWhitespace( expected, actual ); }
fp0.setBoundName( "$t0" ); fp0.setFactType( "FT0" ); fp0.setNegated( true );
private RuleModel getComplexModel() { final RuleModel m = new RuleModel(); m.addAttribute( new RuleAttribute( "no-loop", "true" ) ); final FactPattern pat = new FactPattern( "Person" ); pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFactType( "Person" ); con.setFieldBinding( "f1" ); con.setFieldName( "age" ); con.setOperator( "<" ); con.setValue( "42" ); pat.addConstraint( con ); m.addLhsItem( pat ); final CompositeFactPattern comp = new CompositeFactPattern( "not" ); comp.addFactPattern( new FactPattern( "Cancel" ) ); m.addLhsItem( comp ); final ActionUpdateField set = new ActionUpdateField(); set.variable = "p1"; set.addFieldValue( new ActionFieldValue( "status", "rejected", SuggestionCompletionEngine.TYPE_STRING ) ); m.addRhsItem( set ); final ActionRetractFact ret = new ActionRetractFact( "p1" ); m.addRhsItem( ret ); final DSLSentence sen = new DSLSentence(); sen.setDefinition( "Send an email to {administrator}" ); m.addRhsItem( sen ); return m; }
pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldBinding( "f1" );