kodkod.ast.Node Java Examples
The following examples show how to use
kodkod.ast.Node.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnnotatedNode.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * Returns true if n has already been visited with the current value of the * negated flag; otherwise returns false. * * @ensures records that n is being visited with the current value of the * negated flag * @return true if n has already been visited with the current value of the * negated flag; otherwise returns false. */ @Override protected final boolean visited(Node n) { if (sharedNodes.contains(n)) { if (!visited.containsKey(n)) { // first visit visited.put(n, Boolean.valueOf(negated)); return false; } else { final Boolean visit = visited.get(n); if (visit == null || visit == negated) { // already visited // with same // negated value return true; } else { // already visited with different negated value visited.put(n, null); return false; } } } return false; }
Example #2
Source File: ResolutionBasedProof.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * * @see kodkod.engine.Proof#core() */ @Override public final Iterator<TranslationRecord> core() { if (coreFilter == null) { coreFilter = new RecordFilter() { final IntSet coreVariables = StrategyUtils.coreVars(solver.proof()); final Set<Formula> coreNodes = connectedCore(coreVariables); @Override public boolean accept(Node node, Formula translated, int literal, Map<Variable,TupleSet> env) { return coreNodes.contains(translated) && coreVariables.contains(StrictMath.abs(literal)); } }; } return log().replay(coreFilter); }
Example #3
Source File: A4Solution.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * If this solution is unsatisfiable and its unsat core is available, then * return the core; else return an empty set. */ public Pair<Set<Pos>,Set<Pos>> highLevelCore() { if (hCoreCache != null) return hCoreCache; Set<Pos> ans1 = new LinkedHashSet<Pos>(), ans2 = new LinkedHashSet<Pos>(); if (hCore != null) for (Node f : hCore) { Object x = k2pos(f); if (x instanceof Pos) { // System.out.println("F: "+f+" at "+x+"\n"); // System.out.flush(); ans1.add((Pos) x); } else if (x instanceof Expr) { Expr expr = (Expr) x; Pos p = ((Expr) x).span(); ans1.add(p); // System.out.println("F: "+f+" by // "+p.x+","+p.y+"->"+p.x2+","+p.y2+" for "+x+"\n\n"); // System.out.flush(); for (Func func : expr.findAllFunctions()) ans2.add(func.getBody().span()); } } return hCoreCache = new Pair<Set<Pos>,Set<Pos>>(Collections.unmodifiableSet(ans1), Collections.unmodifiableSet(ans2)); }
Example #4
Source File: TrivialProof.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * * @see kodkod.engine.Proof#highLevelCore() */ @Override public final Map<Formula,Node> highLevelCore() { if (coreRoots == null) { final Iterator<TranslationRecord> itr = core(); final Set<Formula> roots = log().roots(); coreRoots = new LinkedHashMap<Formula,Node>(); while (itr.hasNext()) { TranslationRecord rec = itr.next(); if (roots.contains(rec.translated())) coreRoots.put(rec.translated(), rec.node()); } coreRoots = Collections.unmodifiableMap(coreRoots); } return coreRoots; }
Example #5
Source File: TrivialProof.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * Constructs a proof finder for the given log. * * @ensures this.log' = log */ NodePruner(TranslationLog log) { visited = new IdentityHashSet<Node>(); relevant = new IdentityHashSet<Node>(); final RecordFilter filter = new RecordFilter() { @Override public boolean accept(Node node, Formula translated, int literal, Map<Variable,TupleSet> env) { return env.isEmpty(); } }; constNodes = new LinkedHashMap<Formula,Boolean>(); for (Iterator<TranslationRecord> itr = log.replay(filter); itr.hasNext();) { TranslationRecord rec = itr.next(); int lit = rec.literal(); if (Math.abs(lit) != Integer.MAX_VALUE) { constNodes.remove(rec.translated()); } else if (lit == Integer.MAX_VALUE) { constNodes.put(rec.translated(), Boolean.TRUE); } else { constNodes.put(rec.translated(), Boolean.FALSE); } } }
Example #6
Source File: FormulaFlattener.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * * @see kodkod.ast.visitor.AbstractVoidVisitor#visited(kodkod.ast.Node) */ @Override protected boolean visited(Node n) { if (shared.contains(n)) { if (visited.containsKey(n)) { final Boolean val = visited.get(n); if (val == null || val.booleanValue() == negated) { return true; } else { visited.put(n, null); return false; } } else { visited.put(n, Boolean.valueOf(negated)); return false; } } return false; }
Example #7
Source File: TrivialProof.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} * * @see kodkod.engine.Proof#core() */ @Override public final Iterator<TranslationRecord> core() { if (coreFilter == null) { coreFilter = new RecordFilter() { final Set<Node> coreNodes = NodePruner.relevantNodes(log(), coreRoots == null ? log().roots() : coreRoots.keySet()); @Override public boolean accept(Node node, Formula translated, int literal, Map<Variable,TupleSet> env) { return coreNodes.contains(translated); } }; } return log().replay(coreFilter); }
Example #8
Source File: AnnotatedNode.java From kodkod with MIT License | 6 votes |
/** * Returns true if n has already been visited with the current value of the * negated flag; otherwise returns false. * @ensures records that n is being visited with the current value of the negated flag * @return true if n has already been visited with the current value of the * negated flag; otherwise returns false. */ @Override protected final boolean visited(Node n) { if (sharedNodes.contains(n)) { if (!visited.containsKey(n)) { // first visit visited.put(n, Boolean.valueOf(negated)); return false; } else { final Boolean visit = visited.get(n); if (visit==null || visit==negated) { // already visited with same negated value return true; } else { // already visited with different negated value visited.put(n, null); return false; } } } return false; }
Example #9
Source File: FileLogger.java From kodkod with MIT License | 6 votes |
/** * Constructs a new file log for the sources of the given annotated formula, * using the provided fixed map, file, and tuplefactory. * @requires all f: annotated.node.*children & Formula | logMap.get(f) = freeVariables(f) * @requires the file was written by a FileLogger using the given map */ FileLog(AnnotatedNode<Formula> annotated, FixedMap<Formula, Variable[]> logMap, File file, Bounds bounds) { this.file = file; this.bounds = bounds; this.roots = Nodes.conjuncts(annotated.node()); final int size = logMap.entrySet().size(); this.original = new Node[size]; this.translated = new Formula[size]; this.freeVars = new Variable[size][]; int index = 0; for(Map.Entry<Formula, Variable[]> e : logMap.entrySet()) { translated[index] = e.getKey(); original[index] = annotated.sourceOf(e.getKey()); freeVars[index] = e.getValue(); index++; } }
Example #10
Source File: AnnotatedNode.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * Visits the given comprehension, quantified formula, or sum expression. The * method returns TRUE if the creator body contains any variable not bound by * the decls; otherwise returns FALSE. */ private Boolean visit(Node creator, Decls decls, Node body) { Boolean ret = lookup(creator); if (ret != null) return ret; boolean retVal = false; for (Decl decl : decls) { retVal = decl.expression().accept(this) || retVal; varsInScope.push(decl.variable()); } retVal = ((Boolean) body.accept(this)) || retVal; for (int i = decls.size(); i > 0; i--) { varsInScope.pop(); } return cache(creator, retVal); }
Example #11
Source File: PrenexNFConverter.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
public static AnnotatedNode<Formula> toPNF(AnnotatedNode<Formula> annotated) { final PrenexNFConverter pnfConv = new PrenexNFConverter(annotated.sharedNodes()); List<Formula> conj = new ArrayList<Formula>(); for (Formula f : Nodes.allConjuncts(annotated.node(), null)) conj.add(f.accept(pnfConv)); Formula ans = Formula.and(conj); final List<Formula> roots = new ArrayList<Formula>(pnfConv.annotations.size()); roots.addAll(pnfConv.annotations.keySet()); for (Iterator<Map.Entry<Formula,Node>> itr = pnfConv.annotations.entrySet().iterator(); itr.hasNext();) { final Map.Entry<Formula,Node> entry = itr.next(); final Node source = annotated.sourceOf(entry.getValue()); if (entry.getKey() == source) { itr.remove(); } else { entry.setValue(source); } } return AnnotatedNode.annotate(ans, pnfConv.annotations); }
Example #12
Source File: TrivialProof.java From kodkod with MIT License | 5 votes |
/** * {@inheritDoc} * @see kodkod.engine.Proof#core() */ public final Iterator<TranslationRecord> core() { if (coreFilter==null) { coreFilter = new RecordFilter() { final Set<Node> coreNodes = NodePruner.relevantNodes(log(), coreRoots==null ? log().roots() : coreRoots.keySet()); public boolean accept(Node node, Formula translated, int literal, Map<Variable, TupleSet> env) { return coreNodes.contains(translated) ; } }; } return log().replay(coreFilter); }
Example #13
Source File: UCoreStats.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
@Override void printFalse(String check, Formula formula, Bounds bounds, Solution sol) { print(check, formula, bounds, sol.outcome(), sol.stats()); System.out.println("trivial core:"); for (Node f : sol.proof().highLevelCore().values()) { System.out.println(PrettyPrinter.print(f, 2, 100)); } }
Example #14
Source File: AnnotatedNode.java From kodkod with MIT License | 5 votes |
/** * Visits the given comprehension, quantified formula, or sum expression. * The method returns TRUE if the creator body contains any * variable not bound by the decls; otherwise returns FALSE. */ private Boolean visit(Node creator, Decls decls, Node body) { Boolean ret = lookup(creator); if (ret!=null) return ret; boolean retVal = false; for(Decl decl : decls) { retVal = decl.expression().accept(this) || retVal; varsInScope.push(decl.variable()); } retVal = ((Boolean)body.accept(this)) || retVal; for(int i = decls.size(); i > 0; i--) { varsInScope.pop(); } return cache(creator, retVal); }
Example #15
Source File: ResolutionBasedProof.java From kodkod with MIT License | 5 votes |
/** * Returns the connected core based on the given set of * core variables. * @requires coreVar = StrategyUtils.coreVars(solver.proof()); * @return let formulas = (this.log.records[int] & literal.{i: int | abs(i) in coreVars}).formula | * connected = {f: formulas | some s: set coreNodes | f + this.log.formula in s and (s - this.log.formula).~components in s } */ private Set<Formula> connectedCore(final IntSet coreVars) { final Set<Formula> coreNodes = new IdentityHashSet<Formula>(); final RecordFilter filter = new RecordFilter() { public boolean accept(Node node, Formula translated, int literal, Map<Variable,TupleSet> env) { return coreVars.contains(StrictMath.abs(literal)); } }; for(Iterator<TranslationRecord> itr = log().replay(filter); itr.hasNext(); ) { coreNodes.add(itr.next().translated()); } final Set<Formula> connected = new IdentityHashSet<Formula>(); final AbstractVoidVisitor traverser = new AbstractVoidVisitor() { final Set<Node> visited = new IdentityHashSet<Node>(); /** * Returns true if the given node has been visited before or if * it is not contained in this.nodes set. Otherwise adds * the node to the connected set and returns false. * @ensures this.visited' = this.visited + n * @ensures n !in this.visited && n in coreNodes => * connected' = connected + n else connected' = connected * @return n in visited || n !in coreNodes */ protected boolean visited(Node n) { if (visited.add(n) && coreNodes.contains(n)) { connected.add((Formula)n); return false; } return true; } }; for(Formula root: log().roots()) { root.accept(traverser); } return connected; }
Example #16
Source File: PrettyPrinter.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * @ensures this.tokenize' = (parenthesize => concat [ this.tokens, "(", * tokenize[child], ")" ] else concat [ this.tokens, tokenize[child] ] */ private void visitChild(Node child, boolean parenthesize) { if (parenthesize) { append("("); } child.accept(this); if (parenthesize) { append(")"); } }
Example #17
Source File: FormulaFlattener.java From kodkod with MIT License | 5 votes |
/** * Constructs a flattener for a formula in which the given nodes are shared. */ private FormulaFlattener(Set<Node> shared, boolean breakupQuantifiers) { this.conjuncts = new LinkedHashMap<Formula, Node>(); this.shared = shared; this.visited = new IdentityHashMap<Node,Boolean>(); this.negated = false; this.breakupQuantifiers = breakupQuantifiers; }
Example #18
Source File: TranslateKodkodToJava.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Given a node, return its name (if no name has been chosen, then make a new * name) */ private String makename(Node obj) { if (map.containsKey(obj)) return null; String name = "x" + (map.size()); map.put(obj, name); return name; }
Example #19
Source File: PrettyPrinter.java From kodkod with MIT License | 5 votes |
private void edge(Node n1, Node n2) { if (n2 instanceof LeafExpression || n2 instanceof ConstantFormula || n2 instanceof IntConstant) { } graph.append(id(n1)); graph.append("->"); graph.append(id(n2)); graph.append(";\n"); }
Example #20
Source File: HOL2ProcTranslator.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
@Override protected void start(Node n) { stack.push(n); // ***NOTE*** assumes the formula is already in NNF !!! boolean skolemizableSoFar = skolemizable.empty() ? true : skolemizable.lastElement(); if (!skolemizableSoFar) { skolemizable.push(false); } else { if ((n instanceof BinaryFormula && ((BinaryFormula) n).op() == FormulaOperator.AND) || (n instanceof NaryFormula && ((NaryFormula) n).op() == FormulaOperator.AND) || (n instanceof QuantifiedFormula && ((QuantifiedFormula) n).quantifier() == Quantifier.SOME)) skolemizable.push(true); else skolemizable.push(false); } }
Example #21
Source File: Skolemizer.java From kodkod with MIT License | 5 votes |
/** * Caches the given replacement for the specified node, if * the node is a syntactically shared expression, int expression or declaration with * no free variables. Otherwise does nothing. The method returns * the replacement node. * @return replacement */ @Override protected final <N extends Node> N cache(N node, N replacement) { if (cache.containsKey(node)) { cache.put(node, replacement); } return replacement; }
Example #22
Source File: PrettyPrinter.java From kodkod with MIT License | 5 votes |
private void visit(Node parent, Object label, Iterator<? extends Node> children) { if (visited(parent)) return; node(parent, label.toString()); while(children.hasNext()) { Node child = children.next(); child.accept(this); edge(parent, child); } }
Example #23
Source File: AbstractDetector.java From kodkod with MIT License | 5 votes |
/** * Caches the given value for the specified node, if * this is a caching visitor, and returns Boolean.valueOf(val). * @ensures n in this.cached => this.cache' = this.cache ++ n->Boolean.valueOf(val), this.cache' = this.cache * @return Boolean.valueOf(val) */ protected Boolean cache(Node n, boolean val) { final Boolean ret = Boolean.valueOf(val); if (cached.contains(n)) cache.put(n, ret); return ret; }
Example #24
Source File: AnnotatedNode.java From kodkod with MIT License | 5 votes |
/** * Returns the shared internal nodes of this.node. This method should * be called only after this visitor has been applied to this.node. * @return {n: Node | #(n.~children & node.*children) > 1 } */ IdentityHashSet<Node> sharedNodes() { final IdentityHashSet<Node> shared = new IdentityHashSet<Node>(numSharedNodes); for(Map.Entry<Node,Boolean> entry : sharingStatus.entrySet()) { if (entry.getValue()==Boolean.TRUE) shared.add(entry.getKey()); } return shared; }
Example #25
Source File: Translator.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Returns an annotated formula f such that f.node is equivalent to * annotated.node with its <tt>simplified</tt> predicates replaced with their * corresponding Formulas and the remaining predicates replaced with equivalent * constraints. The annotated formula f will contain transitive source * information for each of the subformulas of f.node. Specifically, let t be a * subformula of f.node, and s be a descdendent of annotated.node from which t * was derived. Then, f.source[t] = annotated.source[s]. * </p> * * @requires simplified.keySet() in * annotated.predicates()[RelationPredicate.NAME] * @requires no disj p, p': simplified.keySet() | simplified.get(p) = * simplifed.get(p') // this must hold in order to maintain the * invariant that each subformula of the returned formula has exactly * one source * @requires for each p in simplified.keySet(), the formulas "p and * [[this.bounds]]" and "simplified.get(p) and [[this.bounds]]" are * equisatisfiable * @return an annotated formula f such that f.node is equivalent to * annotated.node with its <tt>simplified</tt> predicates replaced with * their corresponding Formulas and the remaining predicates replaced * with equivalent constraints. */ private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Map<RelationPredicate,Formula> simplified) { final Map<Node,Node> sources = new IdentityHashMap<Node,Node>(); final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) { private RelationPredicate source = null; @Override protected <N extends Node> N cache(N node, N replacement) { if (replacement instanceof Formula) { if (source == null) { final Node nsource = annotated.sourceOf(node); if (replacement != nsource) sources.put(replacement, nsource); } else { sources.put(replacement, source); } } return super.cache(node, replacement); } @Override public Formula visit(RelationPredicate pred) { Formula ret = lookup(pred); if (ret != null) return ret; source = pred; if (simplified.containsKey(pred)) { ret = simplified.get(pred).accept(this); } else { ret = pred.toConstraints().accept(this); } source = null; return cache(pred, ret); } }; return annotate(annotated.node().accept(inliner), sources); }
Example #26
Source File: A4Solution.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * If this solution is unsatisfiable and its unsat core is available, then * return the core; else return an empty set. */ public Set<Pos> lowLevelCore() { if (lCoreCache != null) return lCoreCache; Set<Pos> ans1 = new LinkedHashSet<Pos>(); if (lCore != null) for (Node f : lCore) { Object y = k2pos(f); if (y instanceof Pos) ans1.add((Pos) y); else if (y instanceof Expr) ans1.add(((Expr) y).span()); } return lCoreCache = Collections.unmodifiableSet(ans1); }
Example #27
Source File: Skolemizer.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Caches the given replacement for the specified node, if the node is a * syntactically shared expression, int expression or declaration with no free * variables. Otherwise does nothing. The method returns the replacement node. * * @return replacement */ @Override protected final <N extends Node> N cache(N node, N replacement) { if (cache.containsKey(node)) { cache.put(node, replacement); } return replacement; }
Example #28
Source File: Skolemizer.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Constructs a skolem replacer from the given arguments. */ private Skolemizer(AnnotatedNode<Formula> annotated, Bounds bounds, Options options) { super(annotated.sharedNodes()); // only cache intermediate computations for expressions with no free // variables // and formulas with no free variables and no quantified descendents for (Node n : annotated.sharedNodes()) { final AbstractDetector fvdetect = annotated.freeVariableDetector(); final AbstractDetector qdetect = annotated.quantifiedFormulaDetector(); if (!(Boolean) n.accept(fvdetect)) { if (!(n instanceof Formula) || !((Boolean) n.accept(qdetect))) this.cache.put(n, null); } } this.reporter = options.reporter(); this.bounds = bounds; this.interpreter = LeafInterpreter.overapproximating(bounds, options); this.repEnv = Environment.empty(); this.nonSkolems = new ArrayList<DeclInfo>(); this.nonSkolemsView = new AbstractList<Decl>() { @Override public Decl get(int index) { return nonSkolems.get(index).decl; } @Override public int size() { return nonSkolems.size(); } }; this.topSkolemConstraints = new ArrayList<Formula>(); this.negated = false; this.skolemDepth = options.skolemDepth(); }
Example #29
Source File: AnnotatedNode.java From kodkod with MIT License | 5 votes |
/** * Constructs a new annotator for the given node and source map. * @ensures this.node' = node && this.source' = node.*components<:iden ++ source */ private AnnotatedNode(N node, Map<? extends Node, ? extends Node> source) { this.node = node; final SharingDetector detector = new SharingDetector(); node.accept(detector); this.sharedNodes = Collections.unmodifiableSet(detector.sharedNodes()); this.source = source; }
Example #30
Source File: Skolemizer.java From kodkod with MIT License | 5 votes |
/** * Constructs a skolem replacer from the given arguments. */ private Skolemizer(AnnotatedNode<Formula> annotated, Bounds bounds, Options options) { super(annotated.sharedNodes()); // only cache intermediate computations for expressions with no free variables // and formulas with no free variables and no quantified descendants for(Node n: annotated.sharedNodes()) { final AbstractDetector fvdetect = annotated.freeVariableDetector(); final AbstractDetector qdetect = annotated.quantifiedFormulaDetector(); if (!(Boolean)n.accept(fvdetect)) { if (!(n instanceof Formula) || !((Boolean)n.accept(qdetect))) this.cache.put(n, null); } } this.reporter = options.reporter(); this.bounds = bounds; this.interpreter = LeafInterpreter.overapproximating(bounds, options); this.repEnv = Environment.empty(); this.nonSkolems = new ArrayList<DeclInfo>(); this.nonSkolemsView = new AbstractList<Decl>() { public Decl get(int index) { return nonSkolems.get(index).decl; } public int size() { return nonSkolems.size(); } }; this.topSkolemConstraints = new ArrayList<Formula>(); this.negated = false; this.skolemDepth = options.skolemDepth(); }