Package org.drools.lang.descr

Examples of org.drools.lang.descr.FunctionDescr


            }
           
            if ( !packageDescr.getFunctions().isEmpty() ) {
              // add static imports for all functions
              for ( final Iterator it = packageDescr.getFunctions().iterator(); it.hasNext(); ) {
                  FunctionDescr functionDescr = (FunctionDescr) it.next();
                  final String functionClassName = this.pkg.getName() + "." + ucFirst( functionDescr.getName() );
                  functionDescr.setClassName( functionClassName );
                  this.pkg.addStaticImport( functionClassName + "." + functionDescr.getName() );
              }                                     
 
              // iterate and compile
              for ( final Iterator it = packageDescr.getFunctions().iterator(); it.hasNext(); ) {
                  addFunction( (FunctionDescr) it.next() );
              }
           
              // We need to compile all the functions, so scripting languages like mvel can find them
              this.dialectRegistry.compileAll();
             
              for ( final Iterator it = packageDescr.getFunctions().iterator(); it.hasNext(); ) {
                  FunctionDescr functionDescr = (FunctionDescr) it.next();
                  final String functionClassName = this.pkg.getName() + "." + ucFirst( functionDescr.getName() );
                  this.dialectRegistry.addStaticImport(functionClassName + "." + functionDescr.getName());
              }             
            }

            // iterate and compile
            for ( final Iterator it = packageDescr.getRules().iterator(); it.hasNext(); ) {
View Full Code Here


        if ( returnType == null || returnType.trim().equals( "" ) ) {
            throw new SAXParseException( "<function> requires a 'return-type' attribute",
                                         this.xmlPackageReader.getLocator() );
        }

        final FunctionDescr functionDescr = new FunctionDescr( name,
                                                               returnType );

        final Configuration[] parameters = config.getChildren( "parameter" );

        for ( int i = 0, length = parameters.length; i < length; i++ ) {
            final String identifier = parameters[i].getAttribute( "identifier" );
            if ( name == null || identifier.trim().equals( "" ) ) {
                throw new SAXParseException( "function's <parameter> requires an 'identifier' attribute",
                                             this.xmlPackageReader.getLocator() );
            }

            final String type = parameters[i].getAttribute( "type" );
            if ( type == null || type.trim().equals( "" ) ) {
                throw new SAXParseException( "function's <parameter> requires an 'type' attribute",
                                             this.xmlPackageReader.getLocator() );
            }

            functionDescr.addParameter( type,
                                        identifier );
        }

        // we allow empty, "", bodies - but make sure that we atleast have a body element
        final Configuration body = config.getChild( "body" );
        if ( body == null ) {
            throw new SAXParseException( "function must have a <body>",
                                         this.xmlPackageReader.getLocator() );
        }

        functionDescr.setText( body.getText() );

        final PackageDescr packageDescr = this.xmlPackageReader.getPackageDescr();

        packageDescr.addFunction( functionDescr );
View Full Code Here

        if ( returnType == null || returnType.trim().equals( "" ) ) {
            throw new SAXParseException( "<function> requires a 'return-type' attribute",
                                         this.xmlPackageReader.getLocator() );
        }

        final FunctionDescr functionDescr = new FunctionDescr( name,
                                                               returnType );

        final Configuration[] parameters = config.getChildren( "parameter" );

        for ( int i = 0, length = parameters.length; i < length; i++ ) {
            final String identifier = parameters[i].getAttribute( "identifier" );
            if ( name == null || identifier.trim().equals( "" ) ) {
                throw new SAXParseException( "function's <parameter> requires an 'identifier' attribute",
                                             this.xmlPackageReader.getLocator() );
            }

            final String type = parameters[i].getText();
            if ( type == null || type.trim().equals( "" ) ) {
                throw new SAXParseException( "function's <parameter> must specify a 'type'",
                                             this.xmlPackageReader.getLocator() );
            }

            functionDescr.addParameter( type,
                                        identifier );
        }

        // we allow empty, "", bodies - but make sure that we atleast have a body element
        final Configuration body = config.getChild( "body" );
        if ( body == null ) {
            throw new SAXParseException( "function must have a <body>",
                                         this.xmlPackageReader.getLocator() );
        }

        functionDescr.setText( body.getText() );

        final PackageDescr packageDescr = this.xmlPackageReader.getPackageDescr();

        packageDescr.addFunction( functionDescr );
View Full Code Here

        assertEquals( "com.sample.Yada",
                      yada.getType() );
        assertEquals( "yada",
                      yada.getIdentifier() );

        final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get( 0 );
        final List names = functionDescr.getParameterNames();
        assertEquals( "foo",
                      names.get( 0 ) );
        assertEquals( "bada",
                      names.get( 1 ) );

        final List types = functionDescr.getParameterTypes();
        assertEquals( "Bar",
                      types.get( 0 ) );
        assertEquals( "Bing",
                      types.get( 1 ) );

        assertEquals( "System.out.println(\"hello world\");",
                      functionDescr.getText().trim() );
    }
View Full Code Here

        assertEquals( "com.sample.Yada",
                      yada.getType() );
        assertEquals( "yada",
                      yada.getIdentifier() );

        final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get( 0 );
        final List names = functionDescr.getParameterNames();
        assertEquals( "foo",
                      names.get( 0 ) );
        assertEquals( "bada",
                      names.get( 1 ) );

        final List types = functionDescr.getParameterTypes();
        assertEquals( "Bar",
                      types.get( 0 ) );
        assertEquals( "Bing",
                      types.get( 1 ) );

        assertEquals( "System.out.println(\"hello world\");",
                      functionDescr.getText().trim() );

        final RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get( 0 );
        assertEquals( "my rule",
                      ruleDescr.getName() );
View Full Code Here

        assertEquals( "com.sample.Yada",
                      yada.getType() );
        assertEquals( "yada",
                      yada.getIdentifier() );

        final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get( 0 );
        final List names = functionDescr.getParameterNames();
        assertEquals( "foo",
                      names.get( 0 ) );
        assertEquals( "bada",
                      names.get( 1 ) );

        final List types = functionDescr.getParameterTypes();
        assertEquals( "Bar",
                      types.get( 0 ) );
        assertEquals( "Bing",
                      types.get( 1 ) );

        assertEquals( "System.out.println(\"hello world\");",
                      functionDescr.getText().trim() );

        final QueryDescr queryDescr = (QueryDescr) packageDescr.getRules().get( 0 );
        assertEquals( "my query",
                      queryDescr.getName() );
View Full Code Here

            }
        }          
    }
   
    public static FunctionDescr createFunctionDescr(SExpression name, LispForm params, List<SExpression> content) {
        FunctionDescr descr = new FunctionDescr(((LispAtom)name).getValue(), "Object" );
        for ( SExpression sExpr  : params.getSExpressions() ) {
            String param = ((LispAtom)sExpr).getValue().trim();
            if ( param.charAt( 0 ) == '"' ) {
                param = param.substring( 1 );
            }
           
            if ( param.charAt( param.length()-1 ) == '"' ) {
                param = param.substring( 0, param.length()-1 );
            }
            descr.addParameter"Object",  param );
        }
       
        descr.setContent( content );
       
        return descr;
    }
View Full Code Here

        packageDescr.addImport( new ImportDescr( "java.util.SortedSet" ) );
        packageDescr.addImport( new ImportDescr( "java.util.TreeSet" ) );
    }

    private static void addFunctions(PackageDescr packageDescr) {
        FunctionDescr functionDescr = new FunctionDescr( "foo",
                                                         "void" );
        functionDescr.addParameter( "String",
                                    "arg1" );
        String body = "Set myHashSet = new HashSet();" + "myHashSet.add(arg1);" + "List myArrayList = new ArrayList();" + "myArrayList.add(arg1);" + "List myLinkedList = new LinkedList();" + "myLinkedList.add(arg1);"
                      + "Set myTreeSet = new TreeSet();" + "myTreeSet.add(arg1);";
        functionDescr.setText( body );
        packageDescr.addFunction( functionDescr );
    }
View Full Code Here

        final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 );

        assertEqualsIgnoreWhitespace( "yourFunction(new String[3] {\"a\",\"b\",\"c\"});",
                                      (String) rule.getConsequence() );
        final FunctionDescr func = (FunctionDescr) pkg.getFunctions().get( 0 );

        assertEquals( "String[]",
                      func.getReturnType() );
        assertEquals( "args[]",
                      func.getParameterNames().get( 0 ) );
        assertEquals( "String",
                      func.getParameterTypes().get( 0 ) );
    }
View Full Code Here

        final List functions = pack.getFunctions();
        assertEquals( 2,
                      functions.size() );

        FunctionDescr func = (FunctionDescr) functions.get( 0 );
        assertEquals( "functionA",
                      func.getName() );
        assertEquals( "String",
                      func.getReturnType() );
        assertEquals( 2,
                      func.getParameterNames().size() );
        assertEquals( 2,
                      func.getParameterTypes().size() );
        assertEquals( 4,
                      func.getLine() );
        assertEquals( 0,
                      func.getColumn() );

        assertEquals( "String",
                      func.getParameterTypes().get( 0 ) );
        assertEquals( "s",
                      func.getParameterNames().get( 0 ) );

        assertEquals( "Integer",
                      func.getParameterTypes().get( 1 ) );
        assertEquals( "i",
                      func.getParameterNames().get( 1 ) );

        assertEqualsIgnoreWhitespace( "foo();",
                                      func.getText() );

        func = (FunctionDescr) functions.get( 1 );
        assertEquals( "functionB",
                      func.getName() );
        assertEqualsIgnoreWhitespace( "bar();",
                                      func.getText() );
    }
View Full Code Here

TOP

Related Classes of org.drools.lang.descr.FunctionDescr

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.