@Override public FSM parseToFSM() { boolean first = true; FSM tmp = null; boolean isSimple = true; for (Expression e : this.disj) { if (first) { tmp = e.parseToFSM(); first = false; if (e.getType() != RegularExpressionParser.Literal) isSimple = false; } else if (e.getType() == RegularExpressionParser.Literal && isSimple) { IntDomain dom = tmp.initState.transitions.iterator().next().domain; int val = Integer.parseInt(((Literal) e).lit); tmp.initState.transitions.iterator().next().domain = dom.union(val); } else { tmp = tmp.union(e.parseToFSM()); isSimple = false; } } return tmp; }
IntDomain getSetLiteral(SimpleNode node, int index) { SimpleNode child = (SimpleNode) node.jjtGetChild(index); if (child.getId() == JJTSETLITERAL) { switch (((ASTSetLiteral) child).getType()) { case 0: // interval SimpleNode grand_child_1 = (SimpleNode) child.jjtGetChild(0); SimpleNode grand_child_2 = (SimpleNode) child.jjtGetChild(1); if (grand_child_1.getId() == JJTINTFLATEXPR && grand_child_2.getId() == JJTINTFLATEXPR) { int i1 = ((ASTIntFlatExpr) grand_child_1).getInt(); int i2 = ((ASTIntFlatExpr) grand_child_2).getInt(); return new IntervalDomain(i1, i2); } case 1: // list IntDomain s = new IntervalDomain(); int el = -1111; int count = child.jjtGetNumChildren(); for (int i = 0; i < count; i++) { el = getScalarFlatExpr(child, i); s.unionAdapt(el); } return s; default: System.err.println("Set type not supported; compilation aborted."); System.exit(0); } } else if (child.getId() == JJTSCALARFLATEXPR) { switch (((ASTScalarFlatExpr) child).getType()) { case 0: // int case 1: // bool System.err.println("Set initialization fault; compilation aborted."); System.exit(0); break; case 2: // ident return dictionary.getSet(((ASTScalarFlatExpr) child).getIdent()); case 3: // array access return dictionary .getSetArray(((ASTScalarFlatExpr) child).getIdent())[ ((ASTScalarFlatExpr) child).getInt()]; case 4: // string case 5: // float System.err.println("Set initialization fault; compilation aborted."); System.exit(0); break; } } return new IntervalDomain(); }
int getArrayAnnotations(SimpleNode node, int i) { int j = i; int count = node.jjtGetNumChildren(); if (j < count) { SimpleNode child = (SimpleNode) node.jjtGetChild(j); while (j < count && child.getId() == JJTANNOTATION) { String id = ((ASTAnnotation) child).getAnnId(); annotations.add(id); if (id.equals("output_array")) { int no = child.jjtGetNumChildren(); if (no > 1 || ((SimpleNode) child.jjtGetChild(0)).getId() != JJTANNEXPR) { System.err.println( "More than one annotation expression in output_array annotation; execution aborted"); System.exit(0); return -1; } else { SimpleNode grandchild = (SimpleNode) child.jjtGetChild(0); int number = grandchild.jjtGetNumChildren(); if (number == 1) { SimpleNode arrayLiteral = (SimpleNode) grandchild.jjtGetChild(0); if (arrayLiteral.getId() == JJTARRAYLITERAL) { int numberSL = arrayLiteral.jjtGetNumChildren(); for (int p = 0; p < numberSL; p++) { SimpleNode setLiteral = (SimpleNode) arrayLiteral.jjtGetChild(p); if (((ASTSetLiteral) setLiteral).getType() == 0) { // interval int s_n = setLiteral.jjtGetNumChildren(); if (s_n == 2) { int low = ((ASTIntFlatExpr) setLiteral.jjtGetChild(0)).getInt(); int high = ((ASTIntFlatExpr) setLiteral.jjtGetChild(1)).getInt(); IntDomain indexes = new IntervalDomain(low, high); indexBounds.add(indexes); // System.out.println(indexes+"->"+indexes.min() // +"__"+indexes.max()); } else { System.err.println( "Unexpected set literal in output_array annotation; execution aborted"); System.exit(0); return -1; } } else if (((ASTSetLiteral) setLiteral).getType() == 1) { // list int s_n = setLiteral.jjtGetNumChildren(); IntDomain indexes = new IntervalDomain(); for (int k = 0; k < s_n; k++) { int el = ((ASTScalarFlatExpr) setLiteral.jjtGetChild(k)).getInt(); indexes.unionAdapt(el); } indexBounds.add(indexes); } else { System.err.println( "Unexpected set literal in output_array annotation; execution aborted"); System.exit(0); return -1; } } } else { System.err.println( "Wrong expression in output_array annotation; execution aborted"); System.exit(0); return -1; } } } } j++; if (j < count) child = (SimpleNode) node.jjtGetChild(j); } } return j; }
void generateArrayVariables(SimpleNode node, Tables table, Store store) { dictionary = table; annotations = new HashSet<String>(); indexBounds = new ArrayList<IntDomain>(); boolean output_array = false; OutputArrayAnnotation outArrayAnn = null; int type = getType(node); int initChild = getArrayAnnotations(node, 1); // node.dump(""); // System.out.println("*** Type = " + type + " init index = " + initChild); // System.out.println("*** Annotations: " + annotations + " " + indexBounds); String ident = ((ASTVarDeclItem) node).getIdent(); if (annotations.contains("output_array")) { output_array = true; outArrayAnn = new OutputArrayAnnotation(ident, indexBounds); } int size; IntVar[] varArrayInt; SetVar[] varArraySet; switch (type) { case 0: // array of int size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArrayInt = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { varArrayInt = getScalarFlatExpr_ArrayVar(store, node, initChild); for (int i = 0; i < varArrayInt.length; i++) if (!ground(varArrayInt[i])) table.addSearchVar(varArrayInt[i]); } else { // no init values varArrayInt = new IntVar[size]; for (int i = 0; i < size; i++) varArrayInt[i] = new IntVar(store, ident + "[" + i + "]", IntDomain.MinInt, IntDomain.MaxInt); table.addSearchArray(varArrayInt); } table.addVariableArray(ident, varArrayInt); if (output_array) { outArrayAnn.setArray(varArrayInt); table.addOutArray(outArrayAnn); } break; case 1: // array of int interval size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArrayInt = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { // array initialization varArrayInt = getScalarFlatExpr_ArrayVar(store, node, initChild); for (int i = 0; i < varArrayInt.length; i++) if (!ground(varArrayInt[i])) table.addSearchVar(varArrayInt[i]); } else { // no init values varArrayInt = new IntVar[size]; for (int i = 0; i < size; i++) varArrayInt[i] = new IntVar(store, ident + "[" + i + "]", lowInterval, highInterval); table.addSearchArray(varArrayInt); } table.addVariableArray(ident, varArrayInt); if (output_array) { outArrayAnn.setArray(varArrayInt); table.addOutArray(outArrayAnn); } break; case 2: // array of int list size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArrayInt = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { // array initialization varArrayInt = getScalarFlatExpr_ArrayVar(store, node, initChild); for (int i = 0; i < varArrayInt.length; i++) if (!ground(varArrayInt[i])) table.addSearchVar(varArrayInt[i]); } else { // no init values varArrayInt = new IntVar[size]; for (int i = 0; i < size; i++) { IntervalDomain dom = new IntervalDomain(); for (Integer e : intList) dom.unionAdapt(e.intValue(), e.intValue()); varArrayInt[i] = new IntVar(store, ident + "[" + i + "]", dom); } table.addSearchArray(varArrayInt); } table.addVariableArray(ident, varArrayInt); if (output_array) { outArrayAnn.setArray(varArrayInt); table.addOutArray(outArrayAnn); } break; case 3: // array of bool size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArrayInt = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { varArrayInt = getScalarFlatExpr_ArrayVar(store, node, initChild); } else { // no init values varArrayInt = new IntVar[size]; for (int i = 0; i < size; i++) varArrayInt[i] = new BooleanVar(store, ident + "[" + i + "]"); table.addSearchArray(varArrayInt); numberBooleanVariables += size; } table.addVariableArray(ident, varArrayInt); if (output_array) { outArrayAnn.setArray(varArrayInt); table.addOutArray(outArrayAnn); } break; case 4: // array of set int size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArraySet = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { // array initialization varArraySet = getSetFlatExpr_ArrayVar(store, node, initChild); } else { // no init values varArraySet = new SetVar[size]; for (int i = 0; i < size; i++) varArraySet[i] = new SetVar( store, ident + "[" + i + "]", new BoundSetDomain(IntDomain.MinInt, IntDomain.MaxInt)); table.addSearchSetArray(varArraySet); } table.addSetVariableArray(ident, varArraySet); if (output_array) { outArrayAnn.setArray(varArraySet); table.addOutArray(outArrayAnn); } break; case 5: // array of set interval size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArraySet = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { // array initialization varArraySet = getSetFlatExpr_ArrayVar(store, node, initChild); } else { // no init values varArraySet = new SetVar[size]; for (int i = 0; i < size; i++) varArraySet[i] = new SetVar( store, ident + "[" + i + "]", new BoundSetDomain(lowInterval, highInterval)); table.addSearchSetArray(varArraySet); } table.addSetVariableArray(ident, varArraySet); if (output_array) { outArrayAnn.setArray(varArraySet); table.addOutArray(outArrayAnn); } break; case 6: // array of set list size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArraySet = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { // array initialization varArraySet = getSetFlatExpr_ArrayVar(store, node, initChild); } else { // no init values varArraySet = new SetVar[size]; for (int i = 0; i < size; i++) { IntDomain sd = new IntervalDomain(); for (Integer e : intList) sd.unionAdapt(e.intValue(), e.intValue()); varArraySet[i] = new SetVar( store, ident + "[" + i + "]", new BoundSetDomain(new IntervalDomain(), sd)); } table.addSearchSetArray(varArraySet); } table.addSetVariableArray(ident, varArraySet); if (output_array) { outArrayAnn.setArray(varArraySet); table.addOutArray(outArrayAnn); } break; case 7: // array of bool set size = ((ASTVarDeclItem) node).getHighIndex() - ((ASTVarDeclItem) node).getLowIndex() + 1; varArraySet = null; if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) { // array initialization varArraySet = getSetFlatExpr_ArrayVar(store, node, initChild); } else { // no init values varArraySet = new SetVar[size]; for (int i = 0; i < size; i++) varArraySet[i] = new SetVar(store, ident + "[" + i + "]", new BoundSetDomain(0, 1)); table.addSearchSetArray(varArraySet); } table.addSetVariableArray(ident, varArraySet); if (output_array) { outArrayAnn.setArray(varArraySet); table.addOutArray(outArrayAnn); } break; default: System.err.println("Not supported type in array parameter; compilation aborted."); System.exit(0); } }
void pruneNodesWithSmallDegree() { // TODO filter arcs with x-variables first ? // It should work on fixpoint principle, so there is propagation in the chain of 2-degree nodes. for (Node node : nodes) { if (node.degree == 1) { Arc arc = node.adjacencyList[0]; ArcCompanion companion = arc.companion; if (companion != null && companion.xVar != null) { int flow = companion.flowOffset + arc.sister.capacity; if (arc.head == node) { assert (arc.sister.capacity == -node.balance) : "\n" + node + "\n" + arc; } else { assert (arc.sister.capacity == node.balance) : "\n" + node + "\n" + arc; } nVarIn(companion, flow, flow); } } else if (node.degree == 2) { Arc arc1 = node.adjacencyList[0]; Arc arc2 = node.adjacencyList[1]; ArcCompanion companion1 = arc1.companion; ArcCompanion companion2 = arc2.companion; if (companion1 != null && companion1.xVar != null && companion2 != null && companion2.xVar != null) { boolean differentDir; int shift = -companion1.flowOffset; if (arc1.head == node) { differentDir = (arc2.head != node); shift += node.balance; } else { differentDir = (arc2.head == node); shift -= node.balance; } if (differentDir) { shift += companion2.flowOffset; } else { shift -= companion2.flowOffset; } IntVar xVar1 = companion1.xVar; IntVar xVar2 = companion2.xVar; if (differentDir) { nVarInShift(companion1, xVar2.domain, -shift); nVarInShift(companion2, xVar1.domain, shift); } else { // TODO Double test this code. IntDomain xDom = xVar1.dom(); IntervalDomain yDomIn = new IntervalDomain(xDom.noIntervals() + 1); for (int i = xDom.noIntervals() - 1; i >= 0; i--) yDomIn.unionAdapt( new Interval(-shift - xDom.rightElement(i), -shift - xDom.leftElement(i))); nVarInShift(companion2, yDomIn, 0); IntDomain yDom = xVar2.domain; IntervalDomain xDomIn = new IntervalDomain(yDom.noIntervals() + 1); for (int i = yDom.noIntervals() - 1; i >= 0; i--) xDomIn.unionAdapt( new Interval(-shift - yDom.rightElement(i), -shift - yDom.leftElement(i))); nVarInShift(companion1, xDomIn, 0); } } } } }