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

📄 pythoninterp.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if ( null == text )
            {
                return null;
            }

            BufferedReader br = new BufferedReader( new InputStreamReader( new ByteArrayInputStream( text.getBytes( ) ) ) );

            StringBuffer unindentedText = new StringBuffer( text.length( ) );

            int lineNo = 0;
            try
            {
                String indent = null;
                for ( String line = br.readLine( ); null != line; line = br.readLine( ) )
                {
                    lineNo++;
                    if ( "".equals( line.trim( ) ) )
                    {
                        // Blank lines are passed through unmodified
                        unindentedText.append( line + LINE_SEPARATOR );
                        continue;
                    }

                    if ( null == indent )
                    {
                        // The first non-bank line determines
                        // the outer indention level
                        indent = line.substring( 0,
                                                 line.indexOf( line.trim( ) ) );
                    }

                    if ( line.length( ) < indent.length( ) || !line.startsWith( indent ) )
                    {
                        // This can catch some poorly indented Python syntax
                        throw new RuntimeException( "Bad Text Indention: Line " + lineNo + ": |" + formatForException( line ) + "|" + LINE_SEPARATOR + formatForException( text ) );
                    }

                    // Remove the outer most indention from the line
                    if ( line.startsWith( indent ) )
                    {
                        unindentedText.append( line.substring( indent.length( ) ) );
                    }
                    unindentedText.append( LINE_SEPARATOR );
                }
            }
            catch ( IOException e )
            {
                throw new RuntimeException( e.getMessage( ) );
            }

            // Remove extraneous trailing LINE_SEPARATOR
            if ( unindentedText.length( ) > 0 )
            {
                unindentedText.deleteCharAt( unindentedText.length( ) - 1 );
            }

            return unindentedText.toString( );
        }
        catch ( Exception e )
        {
            // [TODO]
            // The whole point of this try/catch block is to ensure that
            // exceptions make it out to the user; it seems something is
            // swallowing everything except RuntimeExceptions.
            if ( e instanceof RuntimeException )
            {
                throw (RuntimeException) e;
            }

            throw new RuntimeException( e.getMessage( ) );
        }
    }

    /**
     * Helper method to format the text block for display in error messages.
     * Since Python syntax errors can easily occur due to bad indention, this
     * method replaces all tabs with "{{tab}}" and all spaces with ".".
     * 
     * @param text
     *            the text to be formatted
     * @return the text with all tabs and spaces replaced for easier viewing
     */
    private static String formatForException(String text)
    {
        StringBuffer sbuf = new StringBuffer( text.length( ) * 2 );
        for ( int i = 0, max = text.length( ); i < max; i++ )
        {
            final char nextChar = text.charAt( i );
            if ( '\t' == nextChar )
            {
                sbuf.append( "{{tab}}" );
            }
            else
            {
                sbuf.append( nextChar );
            }
        }

        return sbuf.toString( );
    }

    // ------------------------------------------------------------
    // Instance methods
    // ------------------------------------------------------------

    /**
     * Retrieve the text to evaluate.
     * 
     * @return The text to evaluate.
     */
    public String getText()
    {
        return this.origininalText;
    }

    protected Rule getRule()
    {
        return this.rule;
    }

    /**
     * Retrieve the compiled code.
     * 
     * @return The code.
     */
    protected PyCode getCode()
    {
        if ( this.code == null )
        {
            compile( );
        }
        return this.code;
    }

    /**
     * Retrieve the AST node.
     * 
     * @return The node.
     */
    protected modType getNode()
    {
        return this.node;
    }

    protected PyDictionary getGlobals()
    {
        return this.globals;
    }

    /**
     * Configure a <code>PyDictionary</code> using a <code>Tuple</code> for
     * variable bindings.
     * 
     * @param tuple
     *            Tuple containing variable bindings.
     * 
     * @return The dictionary
     */
    protected PyDictionary setUpDictionary(Tuple tuple,
                                           Iterator declIter) throws Exception
    {
        Declaration eachDecl;

        ObjectType objectType;
        String type;
        Class clazz;
        int nestedClassPosition;
        int dotPosition;

        PyDictionary dict = new PyDictionary( );

        // dict.setdefault( new PyString( "q" ), qFunc ); //add tenerary
        // function

        RuleBaseContext ruleBaseContext = rule.getRuleSet( ).getRuleBaseContext( );
        ClassLoader cl = (ClassLoader) ruleBaseContext.get( "smf-classLoader" );
        if ( cl == null )
        {
            cl = Thread.currentThread( ).getContextClassLoader( );
            ruleBaseContext.put( "smf-classLoader",
                                 cl );
        }

        if ( cl == null )
        {
            cl = getClass( ).getClassLoader( );
            ruleBaseContext.put( "smf-classLoader",
                                 cl );
        }
        

        while ( declIter.hasNext( ) )
        {
            eachDecl = (Declaration) declIter.next( );

            dict.setdefault( new PyString( eachDecl.getIdentifier( ).intern( ) ),
                             Py.java2py( tuple.get( eachDecl ) ) );

            objectType = eachDecl.getObjectType( );

            if ( objectType instanceof ClassObjectType )
            {
                clazz = ((ClassObjectType) objectType).getType( );
                type = clazz.getName( );

                nestedClassPosition = type.indexOf( '$' );

                if ( nestedClassPosition != -1 )
                {
                    type = type.substring( 0,
                                           nestedClassPosition );
                    clazz = cl.loadClass( type );
                }

                if ( type.indexOf( "java.lang" ) == -1 )
                {
                    dotPosition = type.lastIndexOf( '.' );
                    if ( dotPosition != -1 )
                    {
                        type = type.substring( dotPosition + 1 );
                    }
                    dict.setdefault( new PyString( type.intern( ) ),
                                     Py.java2py( clazz ) );
                }
            }

            WorkingMemory workingMemory = tuple.getWorkingMemory( );

            dict.setdefault( new PyString( "drools".intern( ) ),
                             Py.java2py( new DefaultKnowledgeHelper( this.rule,
                                                                     tuple ) ) );

            Map appDataMap = workingMemory.getApplicationDataMap( );

            for ( Iterator keyIter = appDataMap.keySet( ).iterator( ); keyIter.hasNext( ); )
            {
                String key = (String) keyIter.next( );
                Object value = appDataMap.get( key );

                dict.setdefault( new PyString( key.intern( ) ),
                                 Py.java2py( value ) );

                clazz = value.getClass( );
                type = clazz.getName( );

                nestedClassPosition = type.indexOf( '$' );

                if ( nestedClassPosition != -1 )
                {
                    type = type.substring( 0,
                                           nestedClassPosition );
                    clazz = cl.loadClass( type );
                }

                if ( type.indexOf( "java.lang" ) == -1 )
                {
                    dotPosition = type.lastIndexOf( '.' );
                    if ( dotPosition != -1 )
                    {
                        type = type.substring( dotPosition + 1 );
                    }
                    dict.setdefault( new PyString( type.intern( ) ),
                                     Py.java2py( clazz ) );
                }
            }
        }

        return dict;
    }
}

⌨️ 快捷键说明

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