⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logictransformertest.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**
     * <pre>
     *                         _/|\_
     *                      __/  |  \__
     *                     /     |     \ 
     *                  __/      |      \__
     *                 /         |         \
     *                And       and        Not
     *               / | \      / \         |
     *             a  And d    e  Or        h
     *                / \        /  \      
     *               b  Not     f  Exists    
     *                   |           |      
     *                  Not          g   
     *                   |           
     *                   c         
     * </pre>
     * <pre>
     *                           _/|\__
     *                        __/  |   \___
     *                       /     |       \__
     *                    __/      |          \__
     *                   /         |             \__
     *                  /          |                \__
     *                 |           |                   \
     *                And          Or                 Not
     *              / | | \       /  \                 |  
     *            a   b d Not   And   And              i
     *                     |    / \  / |            
     *                    Not  e  f e Exists       
     *                     |           |        
     *                     c           g        
     * </pre>
     * 
     * @throws IOException
     * @throws ClassNotFoundException
     * 
     * 
     * 
     */
    public void xTestProcessTree() throws IOException,
                                  ClassNotFoundException,
                                  InvalidPatternException {
        final String a = "a";
        final String b = "b";
        final String c = "c";
        final String d = "d";
        final String e = "e";
        final String f = "f";
        final String g = "g";
        final String h = "h";
        final String i = "i";
        final String j = "j";
        final String k = "notAssertObject";

        final And and1 = new And();
        final And and2 = new And();
        and1.addChild( a );
        and1.addChild( and2 );
        and2.addChild( b );
        final Not not1 = new Not();
        final Not not2 = new Not();
        not1.addChild( not2 );
        not2.addChild( c );
        and2.addChild( not1 );
        and1.addChild( d );

        final And and3 = new And();
        and3.addChild( e );
        final Or or1 = new Or();
        and3.addChild( or1 );
        final Exists exist1 = new Exists();
        exist1.addChild( g );
        or1.addChild( exist1 );
        or1.addChild( h );

        final Not not3 = new Not();
        not3.addChild( i );

        final And root = new And();
        root.addChild( and1 );
        root.addChild( and3 );
        root.addChild( not3 );

        LogicTransformer.getInstance().processTree( root );

        // --------------------------------------
        // Test that the treesEqual method works
        // --------------------------------------

        // Check against itself
        assertEquals( root,
                      root );

        // Test against a known false tree
        final And testAnd1 = new And();
        testAnd1.addChild( a );
        testAnd1.addChild( b );
        final Or testOr2 = new Or();
        testOr2.addChild( c );
        testOr2.addChild( d );
        testAnd1.addChild( testOr2 );
        assertFalse( root.equals( testAnd1 ) );

        // ----------------------------------------------------------------------------------
        // Now construct the result tree so we can test root against what it
        // should look like
        // ----------------------------------------------------------------------------------

        // Get known correct tree
        // The binary stream was created from a handchecked correct output

        // Uncomment this when you need to output a new known correct tree
        // result
        // writeTree(root, "correct_processTree1.dat");
        final ObjectInputStream ois = new ObjectInputStream( this.getClass().getResourceAsStream( "/correct_processTree1.dat" ) );

        final And correctResultRoot = (And) ois.readObject();

        // Make sure they are equal
        assertEquals( correctResultRoot,
                      root );
    }

    public void testCloneable() {
        final String a = "a";
        final String b = "b";
        final String c = "c";
        final String d = "d";
        final String e = "e";
        final String f = "f";
        final String g = "g";
        final String h = "h";

        // Test against a known false tree
        final And and = new And();
        and.addChild( a );
        and.addChild( b );

        final Or or = new Or();
        or.addChild( c );
        or.addChild( d );
        and.addChild( or );
        final And and2 = new And();
        and2.addChild( e );
        and2.addChild( f );
        or.addChild( and2 );

        final Not not = new Not();
        and.addChild( not );
        final Or or2 = new Or();
        not.addChild( or2 );
        or2.addChild( g );
        or2.addChild( h );

        final GroupElement cloned = (GroupElement) and.clone();

        assertEquals( and,
                      cloned );

    }

    /**
     * 
     * 
     * /**
     * 
     * <pre>
     *                         _/|\_
     *                      __/  |  \__
     *                     /     |     \ 
     *                  __/      |      \__
     *                 /         |         \
     *                And       or         And
     *               /  \       / \        /  \
     *             a    Or     d   e      Not OR
     *                 / \                |  / | 
     *               b    c               f g Not
     *                                         |
     *                                         h
     *                  
     *                   
     *                  
     * </pre>
     * 
     * Each And is a Rete sub rule
     * 
     * <pre>
     *     
     *    
     *       And___     And___      And___      And___        And__    And___       And___    And___     
     *      ||| |  \   ||| |  \     ||| |  \   ||| |  \     ||| |  \  ||| |  \     ||| |  \  ||| |  \ 
     *      dab Not g  dab Not Not  dac Not g  dac Not Not  eab Not g eab Not Not  eac Not g eac Not Not
     *           |          |   |        |          |   |   |    |        |    |       |          |   |   
     *           f          f   h        f          f   h        f        f    h       f          f   h
     *                        
     *                        
     * </pre>
     * 
     * @throws IOException
     * @throws ClassNotFoundException
     * 
     * 
     * 
     * 
     * @throws IOException
     * @throws ClassNotFoundException
     * 
     */
    public void xTestTransform() throws IOException,
                                ClassNotFoundException,
                                InvalidPatternException {
        final String a = "a";
        final String b = "b";
        final String c = "c";
        final String d = "d";
        final String e = "e";
        final String f = "f";
        final String g = "g";
        final String h = "h";
        final String i = "i";

        final And and = new And();

        final And and1 = new And();
        and1.addChild( a );
        final Or or1 = new Or();
        or1.addChild( b );
        or1.addChild( c );
        and1.addChild( or1 );
        and.addChild( and1 );

        final Or or2 = new Or();
        or2.addChild( d );
        or2.addChild( e );
        and.addChild( or2 );

        final And and2 = new And();
        final Not not1 = new Not();
        not1.addChild( f );
        final Or or3 = new Or();
        or3.addChild( g );

        final Not not2 = new Not();
        not2.addChild( h );
        or3.addChild( not2 );

        // ---------------------------------------
        // Check a simple case no just one branch
        // ---------------------------------------
        And[] ands = LogicTransformer.getInstance().transform( and1 );
        assertLength( 2,
                      ands );
        assertTrue( ands[0] instanceof And );
        assertLength( 2,
                      ands[0].getChildren() );

        assertLength( 2,
                      ands[0].getChildren() );
        assertEquals( And.class,
                      ands[0].getClass() );
        assertEquals( And.class,
                      ands[0].getClass() );

        And newAnd = ands[0];
        assertContains( a,
                        newAnd.getChildren() );
        assertContains( b,
                        newAnd.getChildren() );

        newAnd = ands[1];
        assertContains( a,
                        newAnd.getChildren() );
        assertContains( c,
                        newAnd.getChildren() );

        ands = LogicTransformer.getInstance().transform( and );

        // Uncomment this when you need to output a new known correct tree
        // result
        // writeTree(ands, "correct_transform1.dat");

        // Now check the main tree

        // Get known correct tree
        // The binary stream was created from a handchecked correct output
        final ObjectInputStream ois = new ObjectInputStream( this.getClass().getResourceAsStream( "/correct_transform1.dat" ) );
        final And[] correctResultAnds = (And[]) ois.readObject();

        for ( int j = 0; j < ands.length; j++ ) {
            assertEquals( correctResultAnds[j],
                          ands[j] );
        }
    }

    private void writeTree(final Object object,
                           final String fileName) throws IOException {
        final String className = this.getClass().getName();

        File file = new File( this.getClass().getResource( className.substring( className.lastIndexOf( '.' ) + 1 ) + ".class" ).getFile() );

        file = new File( file.getParent(),
                         fileName );

        new ObjectOutputStream( new FileOutputStream( file ) ).writeObject( object );
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -