/* // $Id: //guest/julian_hyde/saffron/src/main/saffron/rel/java/ExistsConverter.java#2 $ // Saffron preprocessor and data engine // Copyright (C) 2002 Julian Hyde <julian.hyde@mail.com> // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public // License as published by the Free Software Foundation; either // version 2 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Library General Public License for more details. // // You should have received a copy of the GNU Library General Public // License along with this library; if not, write to the // Free Software Foundation, Inc., 59 Temple Place - Suite 330, // Boston, MA 02111-1307, USA. // // See the COPYING file located in the top-level-directory of // the archive of this library for complete text of license. */ package saffron.rel.java; import openjava.ptree.*; import saffron.opt.CallingConvention; import saffron.opt.Cluster; import saffron.opt.Implementor; import saffron.rel.Rel; import saffron.rel.convert.Converter; import saffron.util.UnboundVariableCollector; import saffron.util.Util; /** * An <code>ExistsConverter</code> converts a relational expression node * ({@link saffron.rel.Rel}) to {@link saffron.opt.CallingConvention#EXISTS} * calling convention: an expression which yields <code>true</code> if the * relation returns at least one row. **/ public class ExistsConverter extends Converter { public ExistsConverter(Cluster cluster, Rel child, int inConvention) { super(cluster, child, inConvention); } // implement Rel public Object clone() { return new ExistsConverter(cluster, Util.clone(child), inConvention); } // implement Rel public int getConvention() { return CallingConvention.EXISTS; } public static boolean canConvert(int inConvention) { switch (inConvention) { case CallingConvention.JAVA: case CallingConvention.ARRAY: case CallingConvention.ITERATOR: case CallingConvention.MAP: case CallingConvention.HASHTABLE: case CallingConvention.ENUMERATION: case CallingConvention.COLLECTION: return true; default: return false; } } // implement Converter public Object implementJava(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent { // Generate // new Object() { // boolean anyRows(C0 v0, ...) { // <<start loop>> // return true; // <<end loop>> // return false; // } // }.anyRows(v0, ...) // Find all unbound variables in expressions in this tree UnboundVariableCollector unboundVars = Util.getUnboundVariables(this); StatementList stmtList = new StatementList(); implementor.pushStatementList(stmtList); Object o = implementor.implementChild(this, 0, child); Util.assert(o == null); implementor.popStatementList(stmtList); stmtList.add( new ReturnStatement( Literal.constantFalse())); return new MethodCall( new AllocationExpression( new TypeName("Object"), // "Object" null, // "()" // "public boolean anyRows(C0 v0, ...) { ... }" new MemberDeclarationList( new MethodDeclaration( new ModifierList(), new TypeName("boolean"), // "boolean" "anyRows", unboundVars.getParameterList(), // "(C0 v0, ...)" null, // throws nothing stmtList))), "anyRows", unboundVars.getArgumentList()); } case 0: // called from child { // Generate // return true; StatementList stmtList = implementor.getStatementList(); stmtList.add( new ReturnStatement( Literal.constantTrue())); return null; } default: throw Util.newInternal( "implement: ordinal=" + ordinal); } } // implement Converter public Object implementArray(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent // Generate // <<exp>>.length > 0 Expression exp = (Expression) implementor.implementChild( this, 0, child); return new BinaryExpression( exp, BinaryExpression.GREATER, Literal.constantZero()); default: throw Util.newInternal("implement: ordinal=" + ordinal); } } // implement Converter public Object implementIterator(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent // Generate // <<exp>>.hasNext() Expression exp = (Expression) implementor.implementChild( this, 0, child); return new MethodCall( exp, "hasNext", new ExpressionList()); default: throw Util.newInternal("implement: ordinal=" + ordinal); } } // implement Converter public Object implementMap(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent // Generate // !<<exp>>.isEmpty() Expression exp = (Expression) implementor.implementChild( this, 0, child); return new UnaryExpression( UnaryExpression.NOT, new MethodCall( exp, "isEmpty", new ExpressionList())); default: throw Util.newInternal("implement: ordinal=" + ordinal); } } // implement Converter public Object implementHashtable(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent // Generate // !<<exp>>.isEmpty() Expression exp = (Expression) implementor.implementChild( this, 0, child); return new UnaryExpression( UnaryExpression.NOT, new MethodCall( exp, "isEmpty", new ExpressionList())); default: throw Util.newInternal("implement: ordinal=" + ordinal); } } // implement Converter public Object implementEnumeration(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent // Generate // <<exp>>.hasNext() Expression exp = (Expression) implementor.implementChild( this, 0, child); return new MethodCall( exp, "hasNext", new ExpressionList()); default: throw Util.newInternal("implement: ordinal=" + ordinal); } } // implement Converter public Object implementCollection(Implementor implementor, int ordinal) { switch (ordinal) { case -1: // called from parent // Generate // <<exp>>.isEmpty() Expression exp = (Expression) implementor.implementChild( this, 0, child); return new UnaryExpression( UnaryExpression.NOT, new MethodCall( exp, "isEmpty", new ExpressionList())); default: throw Util.newInternal("implement: ordinal=" + ordinal); } } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1474 | Julian Hyde |
saffron: Aggregations are working. Renamed 'aggregator' to 'aggregation'. |
||
#1 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |