[jboss-svn-commits] JBL Code SVN: r11380 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/brms/server/util and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Apr 26 16:59:22 EDT 2007


Author: tirelli
Date: 2007-04-26 16:59:22 -0400 (Thu, 26 Apr 2007)
New Revision: 11380

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/BRDRLPersitenceTest.java
Log:
JBRULES-647: updating BRDRLPersistence

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-04-26 20:41:24 UTC (rev 11379)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/BRDRLPersistence.java	2007-04-26 20:59:22 UTC (rev 11380)
@@ -85,108 +85,146 @@
     private void marshalLHS(StringBuffer buf,
                             RuleModel model) {
         IPattern[] lhs = model.lhs;
+        LHSPatternVisitor visitor = new LHSPatternVisitor( buf );
         for ( int i = 0; i < lhs.length; i++ ) {
             final IPattern cond = lhs[i];
-            if ( cond instanceof DSLSentence ) {
-                // need to decide what to do with DSL sentences
-                //render((DSLSentence) cond, buf);
-            } else if ( cond instanceof FactPattern ) {
-                marshalFact( buf,
-                             (FactPattern) cond );
-            } else if ( cond instanceof CompositeFactPattern ) {
-//                addComposite( lhsDescr,
-//                              (CompositeFactPattern) cond );
-            }
+            visitor.visit( cond );
         }
     }
 
-    private void marshalFact(StringBuffer buf,
-                             FactPattern pattern) {
-        buf.append( "\t\t" );
-        if ( pattern.boundName != null ) {
-            buf.append( pattern.boundName );
-            buf.append( " : " );
+    private void marshalRHS(StringBuffer buf,
+                            RuleModel model) {
+        IAction[] rhs = model.rhs;
+        RHSActionVisitor visitor = new RHSActionVisitor( buf );
+        for ( int i = 0; i < rhs.length; i++ ) {
+            final IAction action = rhs[i];
+            visitor.visit( action );
         }
-        if ( pattern.factType != null ) {
-            buf.append( pattern.factType );
+    }
+
+
+    public static class LHSPatternVisitor extends ReflectiveVisitor {
+        private StringBuffer buf;
+
+        public LHSPatternVisitor( StringBuffer b ) {
+            buf = b;
         }
-        buf.append( "( " );
+        
+        public void visitFactPattern( FactPattern pattern ) {
+            buf.append( "\t\t" );
+            generateFactPattern( pattern );
+            buf.append( "\n" );
+        }
+        
+        public void visitCompositeFactPattern( CompositeFactPattern pattern ) {
+            buf.append( "\t\t" );
+            if( CompositeFactPattern.COMPOSITE_TYPE_EXISTS.equals( pattern.type ) ) {
+                buf.append( pattern.type );
+                buf.append( " " );
+                this.generateFactPattern( pattern.patterns[0] );
+                buf.append( "\n" );
+            } else if( CompositeFactPattern.COMPOSITE_TYPE_NOT.equals( pattern.type ) ) {
+                buf.append( pattern.type );
+                buf.append( " " );
+                this.generateFactPattern( pattern.patterns[0] );
+                buf.append( "\n" );
+            } else if( CompositeFactPattern.COMPOSITE_TYPE_OR.equals( pattern.type ) ) {
+                buf.append( "( " );
+                for( int i = 0; i < pattern.patterns.length; i++ ) {
+                    if( i > 0 ) {
+                        buf.append( " " );
+                        buf.append( pattern.type );
+                        buf.append( " " );
+                    }
+                    this.generateFactPattern( pattern.patterns[0] );
+                }
+                buf.append( " )\n" );
+            }
+        }
 
-        for ( int i = 0; i < pattern.constraints.length; i++ ) {
-            if( i > 0 ) {
-                buf.append( ", " );
+        public void visitDSLSentence( final DSLSentence sentence ) {
+            buf.append( "\t\t" );
+            buf.append( sentence.sentence );
+            buf.append( "\n" );
+        }
+        
+        private void generateFactPattern(FactPattern pattern) {
+            if ( pattern.boundName != null ) {
+                buf.append( pattern.boundName );
+                buf.append( " : " );
             }
-            final Constraint constr = pattern.constraints[i];
-            if ( constr.constraintValueType == IConstraint.TYPE_PREDICATE ) {
-                buf.append( "( " );
-                buf.append( constr.value );
-                buf.append( " )" );
-            } else {
-                if ( constr.fieldBinding != null ) {
-                    buf.append( constr.fieldBinding );
-                    buf.append( " : " );
+            if ( pattern.factType != null ) {
+                buf.append( pattern.factType );
+            }
+            buf.append( "( " );
+
+            for ( int i = 0; i < pattern.constraints.length; i++ ) {
+                if( i > 0 ) {
+                    buf.append( ", " );
                 }
-                buf.append( constr.fieldName );
+                final Constraint constr = pattern.constraints[i];
+                if ( constr.constraintValueType == IConstraint.TYPE_PREDICATE ) {
+                    buf.append( "( " );
+                    buf.append( constr.value );
+                    buf.append( " )" );
+                } else {
+                    if ( constr.fieldBinding != null ) {
+                        buf.append( constr.fieldBinding );
+                        buf.append( " : " );
+                    }
+                    buf.append( constr.fieldName );
 
-                addFieldRestriction( buf,
-                                     constr.constraintValueType,
-                                     constr.operator,
-                                     constr.value );
+                    addFieldRestriction( buf,
+                                         constr.constraintValueType,
+                                         constr.operator,
+                                         constr.value );
 
-                if ( constr.connectives != null ) {
-                    for ( int j = 0; j < constr.connectives.length; j++ ) {
-                        final ConnectiveConstraint conn = constr.connectives[j];
-                        if ( conn.isANDConnective() ) {
-                            buf.append( " &" );
-                        } else if ( conn.isORConnective() ) {
-                            buf.append( " |" );
-                        } else {
-                            throw new IllegalStateException( "Unknown connective type/operator: [" + conn.operator + "]" );
+                    if ( constr.connectives != null ) {
+                        for ( int j = 0; j < constr.connectives.length; j++ ) {
+                            final ConnectiveConstraint conn = constr.connectives[j];
+                            if ( conn.isANDConnective() ) {
+                                buf.append( "&" );
+                            } else if ( conn.isORConnective() ) {
+                                buf.append( "|" );
+                            } else {
+                                throw new IllegalStateException( "Unknown connective type/operator: [" + conn.operator + "]" );
+                            }
+
+                            addFieldRestriction( buf,
+                                                 conn.constraintValueType,
+                                                 conn.operator,
+                                                 conn.value );
                         }
-
-                        addFieldRestriction( buf,
-                                             conn.constraintValueType,
-                                             conn.operator,
-                                             conn.value );
                     }
                 }
             }
+            buf.append( ")" );
         }
-        buf.append( ")\n" );
-    }
 
-    private void marshalRHS(StringBuffer buf,
-                            RuleModel model) {
-        IAction[] rhs = model.rhs;
-        RHSActionVisitor visitor = new RHSActionVisitor( buf );
-        for ( int i = 0; i < rhs.length; i++ ) {
-            final IAction action = rhs[i];
-            visitor.visit( action );
+        /**
+         * @param constr
+         * @param constrDescr
+         */
+        private void addFieldRestriction(final StringBuffer buf,
+                                         final int type,
+                                         final String operator,
+                                         final String value) {
+            buf.append( " " );
+            buf.append( operator );
+            buf.append( " " );
+            switch ( type ) {
+                case IConstraint.TYPE_RET_VALUE :
+                    buf.append( "( " );
+                    buf.append( operator );
+                    buf.append( " )" );
+                    break;
+                default :
+                    buf.append( value );
+            }
+            buf.append( " " );
         }
+        
     }
-
-
-    /**
-     * @param constr
-     * @param constrDescr
-     */
-    private void addFieldRestriction(final StringBuffer buf,
-                                     final int type,
-                                     final String operator,
-                                     final String value) {
-        buf.append( " " );
-        buf.append( operator );
-        buf.append( " " );
-        switch ( type ) {
-            case IConstraint.TYPE_RET_VALUE :
-                buf.append( "( " );
-                buf.append( operator );
-                buf.append( " )" );
-                break;
-            default :
-                buf.append( value );
-        }
-    }
     
     public static class RHSActionVisitor extends ReflectiveVisitor {
         private StringBuffer buf;

Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersitenceTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersitenceTest.java	2007-04-26 20:41:24 UTC (rev 11379)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/BRDRLPersitenceTest.java	2007-04-26 20:59:22 UTC (rev 11380)
@@ -51,11 +51,24 @@
 
     public void testMoreComplexRendering() {
         final RuleModel m = getComplexModel();
+        String expected = "rule \"Complex Rule\"\n" +
+                          "\tno-loop true\n" +
+                          "\tsalience -10\n" +
+                          "\tagenda-group \"aGroup\"\n" +
+                          "\twhen\n"+
+                          "\t\tp1 : Person( f1 : age < 42 )\n"+
+                          "\t\tnot Cancel( )\n"+
+                          "\tthen\n"+
+                          "\t\tp1.setStatus( \"rejected\" );\n"+
+                          "\t\tmodify( p1 );\n"+
+                          "\t\tretract( p1 );\n"+
+                          "\t\tSend an email to {administrator}\n"+
+                          "end\n";
 
         final String drl = p.marshal( m );
         //System.out.println( drl );
 
-        assertTrue( drl.indexOf( "org.drools" ) == -1 );
+        assertEquals( expected, drl );
 
     }
 




More information about the jboss-svn-commits mailing list