private String createCodeForQuantifiedExpression(QuantifiedExpression quantExpr) { CodeList list = new CodeList(); addImports("org.pcollections.PCollection"); Declaration decl = (Declaration) quantExpr.getFirst_isQuantifiedDeclOf_omega().getThat(); // Declarations and variable iteration loops int declaredVars = 0; String tabs = ""; int simpleDecls = 0; // quantifier Quantifier quantifier = (Quantifier) quantExpr.getFirst_isQuantifierOf_omega().getThat(); switch (quantifier.get_type()) { case FORALL: break; case EXISTS: break; case EXISTSONE: list.add(new CodeSnippet("boolean result = false;")); break; } scope.blockBegin(); for (IsSimpleDeclOf_isSimpleDeclOf_omega simpleDeclInc : decl.getIsSimpleDeclOf_isSimpleDeclOf_omegaIncidences()) { SimpleDeclaration simpleDecl = (SimpleDeclaration) simpleDeclInc.getThat(); Expression domain = (Expression) simpleDecl.getFirst_isTypeExprOfDeclaration_omega().getThat(); CodeSnippet simpleDeclSnippet = new CodeSnippet(); simpleDeclSnippet.setVariable("simpleDeclNum", Integer.toString(simpleDecls)); simpleDeclSnippet.add( tabs + "PCollection domain_#simpleDeclNum# = (PCollection) " + createCodeForExpression(domain) + ";"); list.add(simpleDeclSnippet); for (IsDeclaredVarOf_isDeclaredVarOf_omega declaredVarInc : simpleDecl.getIsDeclaredVarOf_isDeclaredVarOf_omegaIncidences()) { declaredVars++; Variable var = (Variable) declaredVarInc.getThat(); CodeSnippet varIterationSnippet = new CodeSnippet(); varIterationSnippet.setVariable("variableName", var.get_name()); varIterationSnippet.setVariable("simpleDeclNum", Integer.toString(simpleDecls)); varIterationSnippet.add(tabs + "for (Object #variableName# : domain_#simpleDeclNum#) {"); tabs += "\t"; scope.addVariable(var.get_name()); list.add(varIterationSnippet); } simpleDecls++; } // condition if (decl.getFirst_isConstraintOf_omega() != null) { CodeSnippet constraintSnippet = new CodeSnippet(); constraintSnippet.add(tabs + "boolean constraint = true;"); for (IsConstraintOf_isConstraintOf_omega constraintInc : decl.getIsConstraintOf_isConstraintOf_omegaIncidences()) { Expression constrExpr = (Expression) constraintInc.getThat(); constraintSnippet.add( tabs + "constraint = constraint && (Boolean) " + createCodeForExpression(constrExpr) + ";"); } constraintSnippet.add(tabs + "if (constraint)"); list.add(constraintSnippet); } // main expression Expression resultDefinition = (Expression) quantExpr.getFirst_isBoundExprOfQuantifiedExpr_omega().getThat(); CodeSnippet iteratedExprSnip = new CodeSnippet(); switch (quantifier.get_type()) { case FORALL: iteratedExprSnip.add( tabs + "if ( ! (Boolean) " + createCodeForExpression(resultDefinition) + ") return false;"); break; case EXISTS: iteratedExprSnip.add( tabs + "if ( (Boolean) " + createCodeForExpression(resultDefinition) + ") return true;"); break; case EXISTSONE: iteratedExprSnip.add( tabs + "if ( (Boolean) " + createCodeForExpression(resultDefinition) + ") {"); iteratedExprSnip.add(tabs + "\tif (result) {"); iteratedExprSnip.add(tabs + "\t\treturn false; //two elements exists"); iteratedExprSnip.add(tabs + "\t} else {"); iteratedExprSnip.add(tabs + "\t\tresult = true; //first element found"); iteratedExprSnip.add(tabs + "\t}"); iteratedExprSnip.add(tabs + "}"); } list.add(iteratedExprSnip); // closing parantheses for interation loops for (int curLoop = 0; curLoop < declaredVars; curLoop++) { StringBuilder tabBuff = new StringBuilder(); for (int i = 1; i < (declaredVars - curLoop); i++) { tabBuff.append("\t"); } tabs = tabBuff.toString(); list.add(new CodeSnippet(tabs + "}")); } switch (quantifier.get_type()) { case FORALL: list.add(new CodeSnippet("return true;")); break; case EXISTSONE: list.add(new CodeSnippet("return result;")); break; case EXISTS: list.add(new CodeSnippet("return false;")); } scope.blockEnd(); String retVal = createMethod(list); return retVal; }
private String createCodeForComprehension(Comprehension compr) { addImports("de.uni_koblenz.jgralab.JGraLab"); addImports("org.pcollections.PCollection"); CodeList list = new CodeList(); CodeSnippet initSnippet = new CodeSnippet(); if (compr instanceof ListComprehension) { initSnippet.add("PCollection result = JGraLab.vector();"); } if (compr instanceof SetComprehension) { initSnippet.add("PCollection result = JGraLab.set();"); } if (compr instanceof MapComprehension) { addImports("org.pcollections.PMap"); initSnippet.add("PMap result = JGraLab.map();"); } list.add(initSnippet); Declaration decl = (Declaration) compr.getFirst_isCompDeclOf_omega().getThat(); // Declarations and variable iteration loops int declaredVars = 0; String tabs = ""; int simpleDecls = 0; scope.blockBegin(); for (IsSimpleDeclOf_isSimpleDeclOf_omega simpleDeclInc : decl.getIsSimpleDeclOf_isSimpleDeclOf_omegaIncidences()) { SimpleDeclaration simpleDecl = (SimpleDeclaration) simpleDeclInc.getThat(); Expression domain = (Expression) simpleDecl.getFirst_isTypeExprOfDeclaration_omega().getThat(); CodeSnippet simpleDeclSnippet = new CodeSnippet(); simpleDeclSnippet.setVariable("simpleDeclNum", Integer.toString(simpleDecls)); simpleDeclSnippet.add( tabs + "PCollection domain_#simpleDeclNum# = (PCollection) " + createCodeForExpression(domain) + ";"); list.add(simpleDeclSnippet); for (IsDeclaredVarOf_isDeclaredVarOf_omega declaredVarInc : simpleDecl.getIsDeclaredVarOf_isDeclaredVarOf_omegaIncidences()) { declaredVars++; Variable var = (Variable) declaredVarInc.getThat(); CodeSnippet varIterationSnippet = new CodeSnippet(); varIterationSnippet.setVariable("variableName", var.get_name()); varIterationSnippet.setVariable("simpleDeclNum", Integer.toString(simpleDecls)); varIterationSnippet.add(tabs + "for (Object #variableName# : domain_#simpleDeclNum#) {"); tabs += "\t"; scope.addVariable(var.get_name()); list.add(varIterationSnippet); } simpleDecls++; } // condition if (decl.getFirst_isConstraintOf_omega() != null) { CodeSnippet constraintSnippet = new CodeSnippet(); constraintSnippet.add(tabs + "boolean constraint = true;"); for (IsConstraintOf_isConstraintOf_omega constraintInc : decl.getIsConstraintOf_isConstraintOf_omegaIncidences()) { Expression constrExpr = (Expression) constraintInc.getThat(); constraintSnippet.add( tabs + "constraint = constraint && (Boolean) " + createCodeForExpression(constrExpr) + ";"); } constraintSnippet.add(tabs + "if (constraint)"); list.add(constraintSnippet); } // main expression CodeSnippet iteratedExprSnip = new CodeSnippet(); if (compr instanceof MapComprehension) { Expression keyExpr = (Expression) ((MapComprehension) compr).getFirst_isKeyExprOfComprehension_omega().getThat(); Expression valueExpr = (Expression) ((MapComprehension) compr).getFirst_isValueExprOfComprehension_omega().getThat(); iteratedExprSnip.add( tabs + "result.put(" + createCodeForExpression(keyExpr) + "," + createCodeForExpression(valueExpr) + ");"); } else { Expression resultDefinition = (Expression) compr.getFirst_isCompResultDefOf_omega().getThat(); iteratedExprSnip.add( tabs + "result = result.plus(" + createCodeForExpression(resultDefinition) + ");"); } list.add(iteratedExprSnip); // closing parantheses for interation loops for (int curLoop = 0; curLoop < declaredVars; curLoop++) { StringBuilder tabBuff = new StringBuilder(); for (int i = 1; i < (declaredVars - curLoop); i++) { tabBuff.append("\t"); } tabs = tabBuff.toString(); list.add(new CodeSnippet(tabs + "}")); } list.add(new CodeSnippet("return result;")); scope.blockEnd(); String retVal = createMethod(list); return retVal; }