Package org.drools.workbench.models.guided.dtree.shared.model

Examples of org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree


        PortablePreconditions.checkNotNull( "globals",
                                            globals );
        PortablePreconditions.checkNotNull( "dmo",
                                            dmo );

        final GuidedDecisionTree model = new GuidedDecisionTree();
        model.setTreeName( baseFileName );

        //De-serialize Package name
        final String packageName = PackageNameParser.parsePackageName( drl );
        model.setPackageName( packageName );

        //De-serialize imports
        final Imports imports = ImportsParser.parseImports( drl );
        for ( Import item : imports.getImports() ) {
            model.getImports().addImport( item );
        }

        //Split DRL into separate rules
        final List<String> rules = new ArrayList<String>();
        final String[] lines = drl.split( System.getProperty( "line.separator" ) );

        StringBuilder sb = null;
        for ( String line : lines ) {
            if ( line.toLowerCase().startsWith( "rule" ) ) {
                sb = new StringBuilder();
            }
            if ( sb != null ) {
                sb.append( line ).append( "\n" );
            }
            if ( line.toLowerCase().startsWith( "end" ) ) {
                rules.add( sb.toString() );
                sb = null;
            }
        }

        //Build a linear Path of Nodes for each rule
        final List<GuidedDecisionTreeParserExtendedError> rulesParserContent = new ArrayList<GuidedDecisionTreeParserExtendedError>();
        for ( String rule : rules ) {
            final GuidedDecisionTreeParserExtendedError ruleParserContent = new GuidedDecisionTreeParserExtendedError();
            rulesParserContent.add( ruleParserContent );

            try {

                final RuleModel rm = RuleModelDRLPersistenceImpl.getInstance().unmarshal( rule,
                                                                                          globals,
                                                                                          dmo );
                ruleParserContent.setOriginalRuleName( rm.name );
                ruleParserContent.setOriginalDrl( rule );

                for ( IPattern p : rm.lhs ) {
                    ruleParserContent.getNodes().addAll( visit( p,
                                                                model,
                                                                dmo,
                                                                ruleParserContent.getMessages() ) );
                }
                for ( IAction a : rm.rhs ) {
                    ruleParserContent.getNodes().addAll( visit( a,
                                                                getTypesOnPath( ruleParserContent.getNodes() ),
                                                                model,
                                                                dmo,
                                                                ruleParserContent.getMessages() ) );

                }

            } catch ( Exception e ) {
                ruleParserContent.getMessages().add( new DefaultParserMessage( e.getMessage() ) );

            }
        }

        //Combine Paths into a single tree.
        for ( GuidedDecisionTreeParserExtendedError ruleParserContent : rulesParserContent ) {
            Node activeModelNode = null;
            boolean error = !ruleParserContent.getMessages().isEmpty();
            for ( int index = 0; !error && index < ruleParserContent.getNodes().size(); index++ ) {
                final Node node = ruleParserContent.getNodes().get( index );
                switch ( index ) {
                    case 0:
                        if ( !( node instanceof TypeNode ) ) {
                            ruleParserContent.getMessages().add( new InvalidRootParserMessage() );
                            error = true;
                            break;
                        }
                        final TypeNode root = (TypeNode) node;
                        if ( model.getRoot() == null ) {
                            model.setRoot( root );
                        } else if ( !root.equals( model.getRoot() ) ) {
                            ruleParserContent.getMessages().add( new AmbiguousRootParserMessage( root.getClassName() ) );
                            error = true;
                            break;
                        }
                        activeModelNode = model.getRoot();
                        break;

                    default:
                        if ( !activeModelNode.getChildren().contains( node ) ) {
                            //If the active node in the Model doesn't contain the child add it as a new child
                            activeModelNode.addChild( node );
                            activeModelNode = node;
                        } else {
                            //Otherwise swap out the Node in the Path for the existing one in the Model
                            activeModelNode = activeModelNode.getChildren().get( activeModelNode.getChildren().indexOf( node ) );
                        }
                }
            }
            if ( !ruleParserContent.getMessages().isEmpty() ) {
                model.getParserErrors().add( new GuidedDecisionTreeParserError( ruleParserContent.getOriginalRuleName(),
                                                                                ruleParserContent.getOriginalDrl(),
                                                                                ruleParserContent.getMessages() ) );
            }
        }
View Full Code Here


                "when \n" +
                "  Person( floatField == \"abc\" )\n" +
                "then \n" +
                "end";

        final GuidedDecisionTree expected = new GuidedDecisionTree();
        expected.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        expected.setRoot( type );

        addModelField( "Person",
                       "this",
                       "Person",
                       DataType.TYPE_THIS );
        addModelField( "Person",
                       "floatField",
                       Float.class.getName(),
                       DataType.TYPE_NUMERIC_FLOAT );

        final GuidedDecisionTree model = GuidedDecisionTreeDRLPersistence.getInstance().unmarshal( drl,
                                                                                                   "test",
                                                                                                   dmo );

        assertNotNull( model );
        assertEquals( expected.getTreeName(),
                      model.getTreeName() );

        assertNull( model.getRoot() );

        assertEquals( 1,
                      model.getParserErrors().size() );
        assertEquals( "test_0",
                      model.getParserErrors().get( 0 ).getOriginalRuleName() );
        assertEqualsIgnoreWhitespace( drl,
                                      model.getParserErrors().get( 0 ).getOriginalDrl() );
        assertNotNull( model.getParserErrors().get( 0 ).getMessages() );
        assertEquals( 1,
                      model.getParserErrors().get( 0 ).getMessages().size() );
        assertTrue( model.getParserErrors().get( 0 ).getMessages().get( 0 ) instanceof DataTypeConversionErrorParserMessage );

        final String drl2 = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( drl,
                                      drl2 );
    }
View Full Code Here

                "when \n" +
                "  Person( integerField == \"abc\" )\n" +
                "then \n" +
                "end";

        final GuidedDecisionTree expected = new GuidedDecisionTree();
        expected.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        expected.setRoot( type );

        addModelField( "Person",
                       "this",
                       "Person",
                       DataType.TYPE_THIS );
        addModelField( "Person",
                       "integerField",
                       Integer.class.getName(),
                       DataType.TYPE_NUMERIC_INTEGER );

        final GuidedDecisionTree model = GuidedDecisionTreeDRLPersistence.getInstance().unmarshal( drl,
                                                                                                   "test",
                                                                                                   dmo );

        assertNotNull( model );
        assertEquals( expected.getTreeName(),
                      model.getTreeName() );

        assertNull( model.getRoot() );

        assertEquals( 1,
                      model.getParserErrors().size() );
        assertEquals( "test_0",
                      model.getParserErrors().get( 0 ).getOriginalRuleName() );
        assertEqualsIgnoreWhitespace( drl,
                                      model.getParserErrors().get( 0 ).getOriginalDrl() );
        assertNotNull( model.getParserErrors().get( 0 ).getMessages() );
        assertEquals( 1,
                      model.getParserErrors().get( 0 ).getMessages().size() );
        assertTrue( model.getParserErrors().get( 0 ).getMessages().get( 0 ) instanceof DataTypeConversionErrorParserMessage );

        final String drl2 = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( drl,
                                      drl2 );
    }
View Full Code Here

                "when \n" +
                "  Person( longField == \"abc\" )\n" +
                "then \n" +
                "end";

        final GuidedDecisionTree expected = new GuidedDecisionTree();
        expected.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        expected.setRoot( type );

        addModelField( "Person",
                       "this",
                       "Person",
                       DataType.TYPE_THIS );
        addModelField( "Person",
                       "longField",
                       Long.class.getName(),
                       DataType.TYPE_NUMERIC_LONG );

        final GuidedDecisionTree model = GuidedDecisionTreeDRLPersistence.getInstance().unmarshal( drl,
                                                                                                   "test",
                                                                                                   dmo );

        assertNotNull( model );
        assertEquals( expected.getTreeName(),
                      model.getTreeName() );

        assertNull( model.getRoot() );

        assertEquals( 1,
                      model.getParserErrors().size() );
        assertEquals( "test_0",
                      model.getParserErrors().get( 0 ).getOriginalRuleName() );
        assertEqualsIgnoreWhitespace( drl,
                                      model.getParserErrors().get( 0 ).getOriginalDrl() );
        assertNotNull( model.getParserErrors().get( 0 ).getMessages() );
        assertEquals( 1,
                      model.getParserErrors().get( 0 ).getMessages().size() );
        assertTrue( model.getParserErrors().get( 0 ).getMessages().get( 0 ) instanceof DataTypeConversionErrorParserMessage );

        final String drl2 = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( drl,
                                      drl2 );
    }
View Full Code Here

                "when \n" +
                "  Person( shortField == \"abc\" )\n" +
                "then \n" +
                "end";

        final GuidedDecisionTree expected = new GuidedDecisionTree();
        expected.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        expected.setRoot( type );

        addModelField( "Person",
                       "this",
                       "Person",
                       DataType.TYPE_THIS );
        addModelField( "Person",
                       "shortField",
                       Short.class.getName(),
                       DataType.TYPE_NUMERIC_SHORT );

        final GuidedDecisionTree model = GuidedDecisionTreeDRLPersistence.getInstance().unmarshal( drl,
                                                                                                   "test",
                                                                                                   dmo );

        assertNotNull( model );
        assertEquals( expected.getTreeName(),
                      model.getTreeName() );

        assertNull( model.getRoot() );

        assertEquals( 1,
                      model.getParserErrors().size() );
        assertEquals( "test_0",
                      model.getParserErrors().get( 0 ).getOriginalRuleName() );
        assertEqualsIgnoreWhitespace( drl,
                                      model.getParserErrors().get( 0 ).getOriginalDrl() );
        assertNotNull( model.getParserErrors().get( 0 ).getMessages() );
        assertEquals( 1,
                      model.getParserErrors().get( 0 ).getMessages().size() );
        assertTrue( model.getParserErrors().get( 0 ).getMessages().get( 0 ) instanceof DataTypeConversionErrorParserMessage );

        final String drl2 = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( drl,
                                      drl2 );
    }
View Full Code Here

                "when \n" +
                "  Cheese( )\n" +
                "then \n" +
                "end \n";

        final GuidedDecisionTree expected = new GuidedDecisionTree();
        expected.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        expected.setRoot( type );

        addModelField( "Person",
                       "this",
                       "Person",
                       DataType.TYPE_THIS );
        addModelField( "Cheese",
                       "this",
                       "Cheese",
                       DataType.TYPE_THIS );

        final GuidedDecisionTree model = GuidedDecisionTreeDRLPersistence.getInstance().unmarshal( drl1 + drl2,
                                                                                                   "test",
                                                                                                   dmo );

        assertNotNull( model );
        assertEquals( expected.getTreeName(),
                      model.getTreeName() );

        assertNotNull( model.getRoot() );
        assertEquals( type.getClassName(),
                      model.getRoot().getClassName() );
        assertFalse( model.getRoot().isBound() );

        assertEquals( 0,
                      model.getRoot().getChildren().size() );

        assertEquals( 1,
                      model.getParserErrors().size() );
        assertEquals( "test_1",
                      model.getParserErrors().get( 0 ).getOriginalRuleName() );
        assertEqualsIgnoreWhitespace( drl2,
                                      model.getParserErrors().get( 0 ).getOriginalDrl() );
        assertNotNull( model.getParserErrors().get( 0 ).getMessages() );
        assertEquals( 1,
                      model.getParserErrors().get( 0 ).getMessages().size() );
        assertTrue( model.getParserErrors().get( 0 ).getMessages().get( 0 ) instanceof AmbiguousRootParserMessage );

        final String drl3 = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( drl1 + drl2,
                                      drl3 );
    }
View Full Code Here

                "when\n" +
                "  Person()\n" +
                "then\n" +
                "end";

        final GuidedDecisionTree model = new GuidedDecisionTree();
        model.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        model.setRoot( type );

        final String drl = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( expected,
                                      drl );
    }
View Full Code Here

                "when\n" +
                "  Person( name == \"Michael\" )\n" +
                "then\n" +
                "end";

        final GuidedDecisionTree model = new GuidedDecisionTree();
        model.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        final ConstraintNode c1 = new ConstraintNodeImpl( "Person",
                                                          "name",
                                                          "==",
                                                          new StringValue( "Michael" ) );
        model.setRoot( type );
        type.addChild( c1 );

        final String drl = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( expected,
                                      drl );
View Full Code Here

                "when\n" +
                "  Person( name )\n" +
                "then\n" +
                "end";

        final GuidedDecisionTree model = new GuidedDecisionTree();
        model.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        final ConstraintNode c1 = new ConstraintNodeImpl( "Person",
                                                          "name" );
        model.setRoot( type );
        type.addChild( c1 );

        final String drl = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( expected,
                                      drl );
View Full Code Here

                "when\n" +
                "  Person( name == \"Michael\", age == 41 )\n" +
                "then\n" +
                "end";

        final GuidedDecisionTree model = new GuidedDecisionTree();
        model.setTreeName( "test" );

        final TypeNode type = new TypeNodeImpl( "Person" );
        final ConstraintNode c1 = new ConstraintNodeImpl( "Person",
                                                          "name",
                                                          "==",
                                                          new StringValue( "Michael" ) );
        final ConstraintNode c2 = new ConstraintNodeImpl( "Person",
                                                          "age",
                                                          "==",
                                                          new IntegerValue( 41 ) );
        model.setRoot( type );
        type.addChild( c1 );
        c1.addChild( c2 );

        final String drl = GuidedDecisionTreeDRLPersistence.getInstance().marshal( model );
        assertEqualsIgnoreWhitespace( expected,
View Full Code Here

TOP

Related Classes of org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.