public CodeBlock createBody() { CodeList code = new CodeList(); addImports("de.uni_koblenz.jgralab.greql2.executable.*"); addImports("de.uni_koblenz.jgralab.Graph"); code.add(staticFieldSnippet); code.add(staticInitializerSnippet); code.add(classFieldSnippet); Greql2Expression rootExpr = graph.getFirstGreql2Expression(); CodeSnippet method = new CodeSnippet(); method.add(""); method.add("private Graph datagraph;"); method.add(""); method.add( "public Object execute(de.uni_koblenz.jgralab.Graph graph, java.util.Map<String, Object> boundVariables) {"); method.add("\tObject result = null;"); method.add("\tdatagraph = graph;"); // create code for bound variables scope.blockBegin(); for (IsBoundVarOf_isBoundVarOf_omega inc : rootExpr.getIsBoundVarOf_isBoundVarOf_omegaIncidences()) { Variable var = (Variable) inc.getThat(); scope.addVariable(var.get_name()); method.add( "\tObject " + var.get_name() + " = boundVariables.get(\"" + var.get_name() + "\");"); } code.add(method); // create code for main query expression IsQueryExprOf_isQueryExprOf_omega inc = rootExpr.getFirst_isQueryExprOf_omega(); Expression queryExpr = (Expression) inc.getThat(); code.add(new CodeSnippet("\tresult = " + createCodeForExpression(queryExpr) + ";")); // create code for store as CodeSnippet endOfMethod = new CodeSnippet(); for (IsIdOfStoreClause_isIdOfStoreClause_omega storeInc : rootExpr.getIsIdOfStoreClause_isIdOfStoreClause_omegaIncidences()) { Identifier ident = (Identifier) storeInc.getThat(); endOfMethod.add( "\tboundVariables.put(\"" + ident.get_name() + "\"," + ident.get_name() + ");"); } scope.blockEnd(); // create code for return and method end endOfMethod.add("\treturn result;"); endOfMethod.add("}"); code.add(endOfMethod); // add generated methods code.add(new CodeSnippet("", "")); for (CodeBlock methodBlock : createdMethods) { code.addNoIndent(methodBlock); code.add(new CodeSnippet("", "")); } staticInitializerSnippet.add("}"); return code; }
private String createCodeForForwardElementSet(ForwardElementSet fws) { DFA dfa = null; // PathDescription pathDescr = (PathDescription) // fws.getFirst_isPathOf_GoesTo_PathExpression().getThat(); // PathDescriptionEvaluator pathDescrEval = (PathDescriptionEvaluator) // vertexEvalGraphMarker.getMark(pathDescr); // dfa = ((NFA)pathDescrEval.getResult()).getDFA(); NFA nfa = null; // NFA.createSimpleIncidenceTransition_Db(); dfa = nfa.getDFA(); Expression startElementExpr = (Expression) fws.getFirst_isStartExprOf_omega().getThat(); CodeList list = new CodeList(); addImports("org.pcollections.PCollection"); addImports("org.pcollections.PSet"); addImports("java.util.HashSet"); addImports("java.util.BitSet"); addImports("de.uni_koblenz.jgralab.JGraLab"); addImports("de.uni_koblenz.jgralab.GraphElement"); addImports("de.uni_koblenz.jgralab.Incidence"); addImports("de.uni_koblenz.jgralab.greql2.types.pathsearch.ElementStateQueue"); CodeSnippet initSnippet = new CodeSnippet(); list.add(initSnippet); initSnippet.add("PSet<GraphElement> resultSet = JGraLab.set();"); initSnippet.add("//one BitSet for each state"); initSnippet.add("HashSet<GraphElement>[] markedElements = new HashSet[#stateCount#];"); initSnippet.setVariable("stateCount", Integer.toString(dfa.stateList.size())); initSnippet.add("for (int i=0; i<#stateCount#;i++) {"); initSnippet.add("\tmarkedElements[i] = new HashSet(100);"); initSnippet.add("}"); initSnippet.add("BitSet finalStates = new BitSet();"); for (State s : dfa.stateList) { if (s.isFinal) { initSnippet.add("finalStates.set(" + s.number + ");"); } } initSnippet.add( "GraphElement startElement = (GraphElement)" + createCodeForExpression(startElementExpr) + ";"); initSnippet.add("ElementStateQueue queue = new ElementStateQueue();"); initSnippet.add("markedElements[" + dfa.initialState.number + "].add(startElement);"); initSnippet.add("int stateNumber;"); initSnippet.add("GraphElement element;"); initSnippet.add("int nextStateNumber;"); initSnippet.add("GraphElement nextElement;"); initSnippet.add("boolean isVertex;"); initSnippet.add("queue.put((GraphElement)v, " + dfa.initialState.number + ");"); initSnippet.add("while (queue.hasNext()) {"); initSnippet.add("\telement = queue.currentElement;"); initSnippet.add("\tstateNumber = queue.currentState;"); initSnippet.add("\tif (finalStates.get(stateNumber)) {"); initSnippet.add("\t\tresultSet = resultSet.plus(element);"); initSnippet.add("\t}"); initSnippet.add("\tisVertex = element instanceof Vertex;"); initSnippet.add("\tfor (Incidence inc = element.getFirstIncidence();"); initSnippet.add( "\t\tinc != null; inc = isVertex ? inc.getNextIncidenceAtVertex() : inc.getNextIncidenceAtEdge()) {"); initSnippet.add("\t\tswitch (stateNumber) {"); for (State curState : dfa.stateList) { CodeList stateCodeList = new CodeList(); list.add(stateCodeList); stateCodeList.add(new CodeSnippet("\t\tcase " + curState.number + ":")); for (Transition curTrans : curState.outTransitions) { System.out.println( "Handling transition " + curTrans.getStartState().number + " --> " + curTrans.endState.number + ":" + curTrans); CodeList transitionCodeList = new CodeList(); stateCodeList.add(transitionCodeList); CodeSnippet transBeginSnippet = new CodeSnippet(); transitionCodeList.addNoIndent(transBeginSnippet); // Generate code to get next vertex and state number if (curTrans.consumesIncidence()) { transBeginSnippet.add("\t\t\tnextElement = isVertex ? inc.getEdge() : inc.getVertex();"); } else { transBeginSnippet.add("\t\t\tnextElement = element;"); } // Generate code to check if next element is marked transBeginSnippet.add( "\t\t\tif (!markedElements[" + curTrans.endState.number + "].contains(nextElement)) {"); transitionCodeList.add(createCodeForTransition(curTrans), 2); transitionCodeList.add(new CodeSnippet("\t\t\t}")); } stateCodeList.add(new CodeSnippet("\t\tbreak;//break case block")); } CodeSnippet finalSnippet = new CodeSnippet(); finalSnippet.add("\t\t}"); finalSnippet.add("\t}"); finalSnippet.add("}"); finalSnippet.add("return resultSet;"); list.add(finalSnippet); return createMethod(list); }