View Javadoc
1 package org.drools.semantics.java; 2 3 /* 4 $Id: ExprAnalyzer.java,v 1.6 2003/03/25 19:47:29 tdiesler 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 10 that the following conditions are met: 11 12 1. Redistributions of source code must retain copyright 13 statements and notices. Redistributions must also contain a 14 copy of this document. 15 16 2. Redistributions in binary form must reproduce the 17 above copyright notice, this list of conditions and the 18 following disclaimer in the documentation and/or other 19 materials provided with the distribution. 20 21 3. The name "drools" must not be used to endorse or promote 22 products derived from this Software without prior written 23 permission of The Werken Company. For written permission, 24 please contact bob@werken.com. 25 26 4. Products derived from this Software may not be called "drools" 27 nor may "drools" appear in their names without prior written 28 permission of The Werken Company. "drools" is a registered 29 trademark of The Werken Company. 30 31 5. Due credit should be given to The Werken Company. 32 (http://drools.werken.com/). 33 34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 OF THE POSSIBILITY OF SUCH DAMAGE. 46 47 */ 48 49 import antlr.RecognitionException; 50 import antlr.TokenStreamException; 51 import antlr.collections.AST; 52 import org.drools.rule.Declaration; 53 import org.drools.semantics.java.parser.JavaLexer; 54 import org.drools.semantics.java.parser.JavaRecognizer; 55 import org.drools.semantics.java.parser.JavaTokenTypes; 56 import org.drools.semantics.java.parser.JavaTreeParser; 57 58 import java.io.StringReader; 59 import java.util.HashSet; 60 import java.util.Iterator; 61 import java.util.Set; 62 63 /*** Expression analyzer. 64 * 65 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 66 * 67 * @version $Id: ExprAnalyzer.java,v 1.6 2003/03/25 19:47:29 tdiesler Exp $ 68 */ 69 public class ExprAnalyzer 70 { 71 // ------------------------------------------------------------ 72 // Constructors 73 // ------------------------------------------------------------ 74 75 /*** Construct. 76 */ 77 public ExprAnalyzer() 78 { 79 // intentionally left blank. 80 } 81 82 // ------------------------------------------------------------ 83 // Instance methods 84 // ------------------------------------------------------------ 85 86 /*** Analyze an expression. 87 * 88 * @param expr The expression to analyze. 89 * @param availDecls Total set of declarations available. 90 * 91 * @return The array of declarations used by the expression. 92 * 93 * @throws TokenStreamException If an error occurs in the lexer. 94 * @throws RecognitionException If an error occurs in the parser. 95 * @throws MissingDeclarationException If the expression requires 96 * a declaration not present in the available declarations. 97 */ 98 public Declaration[] analyze(String expr, 99 Declaration[] availDecls) 100 throws TokenStreamException, RecognitionException, MissingDeclarationException 101 { 102 JavaLexer lexer = new JavaLexer( new StringReader( expr ) ); 103 JavaRecognizer parser = new JavaRecognizer( lexer ); 104 105 parser.ruleCondition(); 106 107 AST ast = parser.getAST(); 108 109 return analyze( expr, 110 availDecls, 111 ast ); 112 } 113 114 /*** Analyze an expression. 115 * 116 * @param expr The expression to analyze. 117 * @param availDecls Total set of declarations available. 118 * @param ast The AST for the expression. 119 * 120 * @return The array of declarations used by the expression. 121 * 122 * @throws RecognitionException If an error occurs in the parser. 123 * @throws MissingDeclarationException If the expression requires 124 */ 125 private Declaration[] analyze(String expr, 126 Declaration[] availDecls, 127 AST ast) throws RecognitionException, MissingDeclarationException 128 { 129 JavaTreeParser treeParser = new JavaTreeParser(); 130 131 treeParser.init(); 132 133 treeParser.exprCondition( ast ); 134 135 Set availDeclSet = new HashSet(); 136 137 for ( int i = 0 ; i < availDecls.length ; ++i ) 138 { 139 availDeclSet.add( availDecls[i] ); 140 } 141 142 Set refs = new HashSet( treeParser.getVariableReferences() ); 143 144 Set declSet = new HashSet(); 145 146 Iterator declIter = availDeclSet.iterator(); 147 Declaration eachDecl = null; 148 149 while ( declIter.hasNext() ) 150 { 151 eachDecl = (Declaration) declIter.next(); 152 153 if ( refs.contains( eachDecl.getIdentifier() ) ) 154 { 155 declSet.add( eachDecl ); 156 declIter.remove(); 157 refs.remove( eachDecl.getIdentifier() ); 158 } 159 } 160 161 /* 162 if ( ! refs.isEmpty() ) 163 { 164 throw new MissingDeclarationException( expr, 165 (String) refs.iterator().next() ); 166 } 167 */ 168 169 Declaration[] decls = new Declaration[ declSet.size() ]; 170 171 declIter = declSet.iterator(); 172 eachDecl = null; 173 174 int i = 0; 175 176 while ( declIter.hasNext() ) 177 { 178 decls[ i++ ] = (Declaration) declIter.next(); 179 } 180 181 return decls; 182 } 183 }

This page was automatically generated by Maven