|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
GroovyExprAnalyzer.java | 100% | 100% | 100% | 100% |
|
1 | package org.drools.semantics.groovy; | |
2 | ||
3 | /* | |
4 | * $Id: GroovyExprAnalyzer.java,v 1.2 2005/02/04 02:13:38 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2002 (C) The Werken Company. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use of this software and associated documentation | |
9 | * ("Software"), with or without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import java.util.Iterator; | |
45 | import java.util.LinkedList; | |
46 | import java.util.List; | |
47 | import java.util.Set; | |
48 | ||
49 | import org.codehaus.groovy.ast.ASTNode; | |
50 | import org.codehaus.groovy.ast.ClassNode; | |
51 | import org.codehaus.groovy.ast.MethodNode; | |
52 | import org.codehaus.groovy.ast.ModuleNode; | |
53 | import org.codehaus.groovy.control.SourceUnit; | |
54 | import org.drools.rule.Declaration; | |
55 | ||
56 | /** | |
57 | * Analyzes python expressions for all mentioned variables. | |
58 | * | |
59 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
60 | * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster </a> | |
61 | * | |
62 | * @version $Id: GroovyExprAnalyzer.java,v 1.2 2005/02/04 02:13:38 mproctor Exp $ | |
63 | */ | |
64 | public class GroovyExprAnalyzer | |
65 | { | |
66 | ||
67 | 21 | public GroovyExprAnalyzer() |
68 | { | |
69 | } | |
70 | ||
71 | // ------------------------------------------------------------ | |
72 | // Instance methods | |
73 | // ------------------------------------------------------------ | |
74 | ||
75 | /** | |
76 | * Analyze an expression. | |
77 | * | |
78 | * @param text The expression to analyze. | |
79 | * @param availDecls Total set of declarations available. | |
80 | * | |
81 | * @return The array of declarations used by the expression. | |
82 | * | |
83 | * @throws Exception If an error occurs while attempting to analyze the | |
84 | * expression. | |
85 | */ | |
86 | 21 | public Declaration[] analyze( String text, |
87 | List availDecls ) throws Exception | |
88 | { | |
89 | 21 | SourceUnit unit = SourceUnit.create( "groovy.script", text ); |
90 | 21 | unit.parse( ); |
91 | 21 | unit.convert( ); |
92 | 21 | ModuleNode module = unit.getAST( ); |
93 | ||
94 | 21 | ClassNode classNode = ( ClassNode ) module.getClasses( ).get( 0 ); |
95 | 21 | List methods = classNode.getDeclaredMethods( "run" ); |
96 | 21 | MethodNode method = ( MethodNode ) methods.get( 0 ); |
97 | 21 | ASTNode expr = method.getCode( ); |
98 | ||
99 | 21 | GroovyExprVisitor visitor = new GroovyExprVisitor( ); |
100 | 21 | expr.visit( visitor ); |
101 | 21 | Set refs = visitor.getVariables( ); |
102 | ||
103 | 21 | List decls = new LinkedList( ); |
104 | 21 | for ( Iterator declIter = availDecls.iterator( ); declIter.hasNext( ); ) |
105 | { | |
106 | 37 | Declaration eachDecl = ( Declaration ) declIter.next( ); |
107 | ||
108 | 37 | if ( refs.contains( eachDecl.getIdentifier( ) ) ) |
109 | { | |
110 | 19 | decls.add( eachDecl ); |
111 | 19 | refs.remove( eachDecl.getIdentifier( ) ); |
112 | } | |
113 | } | |
114 | ||
115 | 21 | return ( Declaration[] ) decls.toArray( new Declaration[ decls.size( ) ] ); |
116 | } | |
117 | } |
|