kodkod.ast.RelationPredicate Java Examples

The following examples show how to use kodkod.ast.RelationPredicate. 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: AbstractVoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Visits the children of the predicate if this.visited(pred) returns false.
 * Otherwise does nothing.
 *
 * @ensures pred.relation.accept(this) && (pred.name = FUNCTION =>
 *          pred.domain.accept(this) && pred.range.accept(this)) && (pred.name =
 *          TOTAL_ORDERING => pred.ordered.accept(this) &&
 *          pred.first.accept(this) && pred.last.accept(this) )
 */
@Override
public void visit(RelationPredicate pred) {
    if (visited(pred))
        return;
    pred.relation().accept(this);
    if (pred.name() == RelationPredicate.Name.FUNCTION) {
        final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
        fp.domain().accept(this);
        fp.range().accept(this);
    } else if (pred.name() == RelationPredicate.Name.TOTAL_ORDERING) {
        final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
        tp.ordered().accept(this);
        tp.first().accept(this);
        tp.last().accept(this);
    }
}
 
Example #2
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean visit(RelationPredicate pred) {
    Boolean ans = get(pred);
    if (ans != null)
        return ans;
    pred.relation().accept(this);
    if (pred.name() == RelationPredicate.Name.FUNCTION) {
        final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
        fp.domain().accept(this);
        fp.range().accept(this);
    } else if (pred.name() == RelationPredicate.Name.TOTAL_ORDERING) {
        final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
        tp.ordered().accept(this);
        tp.first().accept(this);
        tp.last().accept(this);
    }
    return noHOL(pred);
}
 
Example #3
Source File: AbstractDetector.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(predicate) and returns the cached value, if any. If no cached
 * value exists, visits each child, caches the disjunction of the children's
 * return values and returns it.
 *
 * @return let x = lookup(predicate) | x != null => x, cache(predicate, some n:
 *         predicate.children | n.accept(this))
 */
@Override
public Boolean visit(RelationPredicate predicate) {
    final Boolean ret = lookup(predicate);
    if (ret != null)
        return ret;
    if (predicate.relation().accept(this))
        return cache(predicate, true);
    if (predicate.name() == RelationPredicate.Name.FUNCTION) {
        final RelationPredicate.Function fp = (RelationPredicate.Function) predicate;
        return cache(predicate, fp.domain().accept(this) || fp.range().accept(this));
    } else if (predicate.name() == RelationPredicate.Name.TOTAL_ORDERING) {
        final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) predicate;
        return cache(predicate, tp.ordered().accept(this) || tp.first().accept(this) || tp.last().accept(this));
    }
    return cache(predicate, false);
}
 
Example #4
Source File: AbstractCollector.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(predicate) and returns the cached value, if any.  
 * If no cached value exists, visits each child, caches the
 * disjunction of the children's return values and returns it. 
 * @return let x = lookup(predicate) | 
 *          x != null => x,  
 *          cache(predicate, some n: predicate.children | n.accept(this)) 
 */
public Set<T> visit(RelationPredicate pred) {
	Set<T> ret = lookup(pred);
	if (ret!=null) return ret;
	ret = newSet();
	ret.addAll(pred.relation().accept(this));
	switch(pred.name()) {
	case ACYCLIC : 
		break;
	case FUNCTION :
		final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
		ret.addAll(fp.domain().accept(this));
		ret.addAll(fp.range().accept(this));
		break;
	case TOTAL_ORDERING : 
		final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
		ret.addAll(tp.ordered().accept(this));
		ret.addAll(tp.first().accept(this));
		ret.addAll(tp.last().accept(this));
		break;
	default :
		throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
	}
	return cache(pred,ret);
}
 
Example #5
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls super.visit(pred) after disabling skolemization and returns the result.
 *
 * @return super.visit(pred)
 **/
@Override
public final Formula visit(RelationPredicate pred) {
    final int oldDepth = skolemDepth;
    skolemDepth = -1; // cannot skolemize inside a relation predicate
    final Formula ret = super.visit(pred);
    skolemDepth = oldDepth;
    return source(ret, pred);
}
 
Example #6
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(pred) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the formula's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement formula is cached and returned.
 * @return { p: RelationPredicate | p.name = pred.name && p.relation = pred.relation.accept(this) &&
 *                                  p.name = FUNCTION => p.targetMult = pred.targetMult && 
 *                                                       p.domain = pred.domain.accept(this) &&
 *                                                       p.range = pred.range.accept(this),
 *                                  p.name = TOTAL_ORDERING => p.ordered = pred.ordered.accept(this) &&
 *                                                             p.first = pred.first.accept(this) &&
 *                                                             p.last = pred.last.accept(this) }
 */
public Formula visit(RelationPredicate pred) {
	Formula ret = lookup(pred);
	if (ret!=null) return ret;

	final Relation r = (Relation)pred.relation().accept(this);
	switch(pred.name()) {
	case ACYCLIC :  
		ret = (r==pred.relation()) ? pred : r.acyclic(); 
		break;
	case FUNCTION :
		final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
		final Expression domain = fp.domain().accept(this);
		final Expression range = fp.range().accept(this);
		ret = (r==fp.relation() && domain==fp.domain() && range==fp.range()) ?
				fp : 
				(fp.targetMult()==Multiplicity.ONE ? r.function(domain, range) : r.partialFunction(domain,range));
		break;
	case TOTAL_ORDERING : 
		final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
		final Relation ordered = (Relation) tp.ordered().accept(this);
		final Relation first = (Relation)tp.first().accept(this);
		final Relation last = (Relation)tp.last().accept(this);
		ret = (r==tp.relation() && ordered==tp.ordered() && first==tp.first() && last==tp.last()) ? 
				tp : r.totalOrder(ordered, first, last);
		break;
	default :
		throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
	}
	return cache(pred,ret);
}
 
Example #7
Source File: SymmetryBreaker.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Sorts the predicates in the given array in the ascending order of 
 * the names of the predicates' relations, and returns it.
 * @return broken'
 * @ensures all i: [0..preds.size()) | all j: [0..i) | 
 *            broken[j].relation.name <= broken[i].relation.name 
 */
private static final <P extends RelationPredicate> P[] sort(final P[] preds) {
	final Comparator<RelationPredicate> cmp = new Comparator<RelationPredicate>() {
		public int compare(RelationPredicate o1, RelationPredicate o2) {
			return String.valueOf(o1.relation().name()).compareTo(String.valueOf(o2.relation().name()));
		}
	};
	Arrays.sort(preds, cmp);
	return preds;
}
 
Example #8
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Records the visit to this predicate if it is not negated.
 */
@Override
public void visit(RelationPredicate pred) {
    if (visited(pred))
        return;
    if (!negated) {
        preds.get(pred.name()).add(pred);
    }
}
 
Example #9
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new collector.
 *
 * @ensures this.negated' = false
 */
PredicateCollector(Set<Node> sharedNodes) {
    this.sharedNodes = sharedNodes;
    this.visited = new IdentityHashMap<Node,Boolean>();
    this.negated = false;
    preds = new EnumMap<RelationPredicate.Name,Set<RelationPredicate>>(RelationPredicate.Name.class);
    preds.put(ACYCLIC, new IdentityHashSet<RelationPredicate>(4));
    preds.put(TOTAL_ORDERING, new IdentityHashSet<RelationPredicate>(4));
    preds.put(FUNCTION, new IdentityHashSet<RelationPredicate>(8));
}
 
Example #10
Source File: AbstractVoidVisitor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Visits the children of the predicate  if
 * this.visited(pred) returns false.  Otherwise does nothing.
 * @ensures pred.relation.accept(this) && 
 *          (pred.name = FUNCTION => pred.domain.accept(this) && pred.range.accept(this)) && 
 *          (pred.name = TOTAL_ORDERING => 
 *            pred.ordered.accept(this) && pred.first.accept(this) && pred.last.accept(this) )
 */
public void visit(RelationPredicate pred) {
	if (visited(pred)) return;
	pred.relation().accept(this);
	if (pred.name()==RelationPredicate.Name.FUNCTION) {
		final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
		fp.domain().accept(this);
		fp.range().accept(this);
	} else if (pred.name()==RelationPredicate.Name.TOTAL_ORDERING) {
		final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
		tp.ordered().accept(this);
		tp.first().accept(this);
		tp.last().accept(this);
	}
}
 
Example #11
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(RelationPredicate pred) {
    if (visited(pred))
        return;

    if (pred.name() == RelationPredicate.Name.FUNCTION) {
        final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
        visit(fp, fp.name(), fp.domain(), fp.range());
    } else if (pred.name() == RelationPredicate.Name.TOTAL_ORDERING) {
        final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
        visit(tp, tp.name(), tp.ordered(), tp.first(), tp.last());
    } else {
        throw new IllegalArgumentException("Unknown predicate: " + pred);
    }
}
 
Example #12
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * @ensures appends the tokenization of the given node to this.tokens
 */
@Override
public void visit(RelationPredicate node) {
    switch (node.name()) {
        case ACYCLIC :
            append("acyclic");
            append("[");
            node.relation().accept(this);
            append("]");
            break;
        case FUNCTION :
            RelationPredicate.Function func = (RelationPredicate.Function) node;
            append("function");
            append("[");
            func.relation().accept(this);
            colon();
            func.domain().accept(this);
            infix("->");
            keyword(func.targetMult());
            func.range().accept(this);
            append("]");
            break;
        case TOTAL_ORDERING :
            RelationPredicate.TotalOrdering ord = (RelationPredicate.TotalOrdering) node;
            append("ord");
            append("[");
            ord.relation().accept(this);
            comma();
            ord.ordered().accept(this);
            comma();
            ord.first().accept(this);
            comma();
            ord.last().accept(this);
            append("]");
            break;
        default :
            throw new AssertionError("unreachable");
    }

}
 
Example #13
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls super.visit(pred) after disabling skolemization and returns the result. 
 * @return super.visit(pred) 
 **/
public final Formula visit(RelationPredicate pred) {
	final int oldDepth = skolemDepth;
	skolemDepth = -1; // cannot skolemize inside a relation predicate
	final Formula ret = super.visit(pred);
	skolemDepth = oldDepth;
	return source(ret,pred);
}
 
Example #14
Source File: Translator.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * 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) = simplified.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;			
		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);
		}
		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 #15
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures appends the tokenization of the given node to this.tokens */
public void visit(RelationPredicate node) {
	switch(node.name()) { 
	case ACYCLIC : 
		append("acyclic");
		append("[");
		node.relation().accept(this);
		append("]");
		break;
	case FUNCTION : 
		RelationPredicate.Function func = (RelationPredicate.Function)node;
		append("function");
		append("[");
		func.relation().accept(this);
		colon();
		func.domain().accept(this);
		infix("->");
		keyword(func.targetMult());
		func.range().accept(this);
		append("]");
		break;
	case TOTAL_ORDERING : 
		RelationPredicate.TotalOrdering ord = (RelationPredicate.TotalOrdering)node;
		append("ord");
		append("[");
		ord.relation().accept(this);
		comma();
		ord.ordered().accept(this);
		comma();
		ord.first().accept(this);
		comma();
		ord.last().accept(this);
		append("]");
		break;
	default:
		throw new AssertionError("unreachable");
	}
	
}
 
Example #16
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
public void visit(RelationPredicate pred) {
	if (visited(pred)) return;
	
	if (pred.name()==RelationPredicate.Name.FUNCTION) {
		final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
		visit(fp, fp.name(),  fp.domain(), fp.range() );
	} else if (pred.name()==RelationPredicate.Name.TOTAL_ORDERING) {
		final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
		visit(tp, tp.name(),  tp.ordered(), tp.first(), tp.last() );
	} else {
		throw new IllegalArgumentException("Unknown predicate: " + pred);
	}
}
 
Example #17
Source File: SymmetryBreaker.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Sorts the predicates in the given array in the ascending order of the names
 * of the predicates' relations, and returns it.
 *
 * @return broken'
 * @ensures all i: [0..preds.size()) | all j: [0..i) | broken[j].relation.name
 *          <= broken[i].relation.name
 */
private static final <P extends RelationPredicate> P[] sort(final P[] preds) {
    final Comparator<RelationPredicate> cmp = new Comparator<RelationPredicate>() {

        @Override
        public int compare(RelationPredicate o1, RelationPredicate o2) {
            return String.valueOf(o1.relation().name()).compareTo(String.valueOf(o2.relation().name()));
        }
    };
    Arrays.sort(preds, cmp);
    return preds;
}
 
Example #18
Source File: Translator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #19
Source File: Translator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an annotated formula f such that f.node is equivalent to
 * annotated.node with its <tt>truePreds</tt> replaced with the constant formula
 * TRUE and the remaining predicates replaced with equivalent constraints.
 *
 * @requires truePreds in annotated.predicates()[RelationnPredicate.NAME]
 * @requires truePreds are trivially true with respect to this.bounds
 * @return an annotated formula f such that f.node is equivalent to
 *         annotated.node with its <tt>truePreds</tt> replaced with the constant
 *         formula TRUE and the remaining predicates replaced with equivalent
 *         constraints.
 */
private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Set<RelationPredicate> truePreds) {
    final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) {

        @Override
        public Formula visit(RelationPredicate pred) {
            Formula ret = lookup(pred);
            if (ret != null)
                return ret;
            return truePreds.contains(pred) ? cache(pred, Formula.TRUE) : cache(pred, pred.toConstraints());
        }
    };
    Formula x = annotated.node().accept(inliner);
    return annotate(x);
}
 
Example #20
Source File: AnnotatedNode.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new collector.
 * @ensures this.negated' = false
 */
PredicateCollector(Set<Node> sharedNodes) {
	this.sharedNodes = sharedNodes;
	this.visited = new IdentityHashMap<Node,Boolean>();
	this.negated = false;
	preds = new EnumMap<RelationPredicate.Name, Set<RelationPredicate>>(RelationPredicate.Name.class);	
	preds.put(ACYCLIC, new IdentityHashSet<RelationPredicate>(4));
	preds.put(TOTAL_ORDERING, new IdentityHashSet<RelationPredicate>(4));
	preds.put(FUNCTION, new IdentityHashSet<RelationPredicate>(8));
}
 
Example #21
Source File: AnnotatedNode.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Records the visit to this predicate if it is not negated.
 */
public void visit(RelationPredicate pred) {
	if (visited(pred)) return;
	if (!negated) {
		preds.get(pred.name()).add(pred);
	}
}
 
Example #22
Source File: AbstractCollector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(predicate) and returns the cached value, if any. If no cached
 * value exists, visits each child, caches the disjunction of the children's
 * return values and returns it.
 *
 * @return let x = lookup(predicate) | x != null => x, cache(predicate, some n:
 *         predicate.children | n.accept(this))
 */
@Override
public Set<T> visit(RelationPredicate pred) {
    Set<T> ret = lookup(pred);
    if (ret != null)
        return ret;
    ret = newSet();
    ret.addAll(pred.relation().accept(this));
    switch (pred.name()) {
        case ACYCLIC :
            break;
        case FUNCTION :
            final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
            ret.addAll(fp.domain().accept(this));
            ret.addAll(fp.range().accept(this));
            break;
        case TOTAL_ORDERING :
            final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
            ret.addAll(tp.ordered().accept(this));
            ret.addAll(tp.first().accept(this));
            ret.addAll(tp.last().accept(this));
            break;
        default :
            throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
    }
    return cache(pred, ret);
}
 
Example #23
Source File: AbstractDetector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(predicate) and returns the cached value, if any.  
 * If no cached value exists, visits each child, caches the
 * disjunction of the children's return values and returns it. 
 * @return let x = lookup(predicate) | 
 *          x != null => x,  
 *          cache(predicate, some n: predicate.children | n.accept(this)) 
 */
public Boolean visit(RelationPredicate predicate) {
	final Boolean ret = lookup(predicate);
	if (ret!=null) return ret;
	if (predicate.relation().accept(this)) 
		return cache(predicate, true);
	if (predicate.name()==RelationPredicate.Name.FUNCTION) {
		final RelationPredicate.Function fp = (RelationPredicate.Function) predicate;
		return cache(predicate, fp.domain().accept(this) || fp.range().accept(this));
	} else if (predicate.name()==RelationPredicate.Name.TOTAL_ORDERING) {
		final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) predicate;
		return cache(predicate, tp.ordered().accept(this) || tp.first().accept(this) || tp.last().accept(this));
	}
	return cache(predicate, false);
}
 
Example #24
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(pred) and returns the cached value, if any. If a replacement has
 * not been cached, visits the formula's children. If nothing changes, the
 * argument is cached and returned, otherwise a replacement formula is cached
 * and returned.
 *
 * @return { p: RelationPredicate | p.name = pred.name && p.relation =
 *         pred.relation.accept(delegate) && p.name = FUNCTION => p.targetMult =
 *         pred.targetMult && p.domain = pred.domain.accept(delegate) && p.range
 *         = pred.range.accept(delegate), p.name = TOTAL_ORDERING => p.ordered =
 *         pred.ordered.accept(delegate) && p.first =
 *         pred.first.accept(delegate) && p.last = pred.last.accept(delegate) }
 */
@Override
public Formula visit(RelationPredicate pred) {
    Formula ret = lookup(pred);
    if (ret != null)
        return ret;

    final Relation r = (Relation) pred.relation().accept(delegate);
    switch (pred.name()) {
        case ACYCLIC :
            ret = (r == pred.relation()) ? pred : r.acyclic();
            break;
        case FUNCTION :
            final RelationPredicate.Function fp = (RelationPredicate.Function) pred;
            final Expression domain = fp.domain().accept(delegate);
            final Expression range = fp.range().accept(delegate);
            ret = (r == fp.relation() && domain == fp.domain() && range == fp.range()) ? fp : (fp.targetMult() == Multiplicity.ONE ? r.function(domain, range) : r.partialFunction(domain, range));
            break;
        case TOTAL_ORDERING :
            final RelationPredicate.TotalOrdering tp = (RelationPredicate.TotalOrdering) pred;
            final Relation ordered = (Relation) tp.ordered().accept(delegate);
            final Relation first = (Relation) tp.first().accept(delegate);
            final Relation last = (Relation) tp.last().accept(delegate);
            ret = (r == tp.relation() && ordered == tp.ordered() && first == tp.first() && last == tp.last()) ? tp : r.totalOrder(ordered, first, last);
            break;
        default :
            throw new IllegalArgumentException("unknown relation predicate: " + pred.name());
    }
    return cache(pred, ret);
}
 
Example #25
Source File: Translator.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns an annotated formula f such that f.node is equivalent to annotated.node
 * with its <tt>truePreds</tt> replaced with the constant formula TRUE and the remaining
 * predicates replaced with equivalent constraints.
 * @requires truePreds in annotated.predicates()[RelationnPredicate.NAME]
 * @requires truePreds are trivially true with respect to this.bounds
 * @return an annotated formula f such that f.node is equivalent to annotated.node
 * with its <tt>truePreds</tt> replaced with the constant formula TRUE and the remaining
 * predicates replaced with equivalent constraints.
 */
private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Set<RelationPredicate> truePreds) {
	final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) {
		public Formula visit(RelationPredicate pred) {
			Formula ret = lookup(pred);
			if (ret!=null) return ret;
			return truePreds.contains(pred) ? cache(pred, Formula.TRUE) : cache(pred, pred.toConstraints());
		}
	};
	return annotate(annotated.node().accept(inliner));	
}
 
Example #26
Source File: PrettyPrinter.java    From kodkod with MIT License 4 votes vote down vote up
/** @return true if the given formula should be parenthesized when a 
 * child of a compound parent */
private boolean parenthesize(Formula child) { 
	return !(child instanceof NotFormula || child instanceof ConstantFormula || 
			 child instanceof RelationPredicate);
}
 
Example #27
Source File: SymmetryBreaker.java    From kodkod with MIT License 4 votes vote down vote up
/**
	 * If possible, breaks symmetry on the given total ordering predicate and returns a formula
	 * f such that the meaning of total with respect to this.bounds is equivalent to the
	 * meaning of f with respect to this.bounds'. If symmetry cannot be broken on the given predicate, returns null.  
	 * 
	 * <p>We break symmetry on the relation constrained by the given predicate iff 
	 * total.first, total.last, and total.ordered have the same upper bound, which, when 
	 * cross-multiplied with itself gives the upper bound of total.relation. Assuming that this is the case, 
	 * we then break symmetry on total.relation, total.first, total.last, and total.ordered using one of the methods
	 * described in {@linkplain #breakMatrixSymmetries(Map, boolean)}; the method used depends
	 * on the value of the "aggressive" flag.
	 * The partition that formed the upper bound of total.ordered is removed from this.symmetries.</p>
	 * 
	 * @return null if symmetry cannot be broken on total; otherwise returns a formula
	 * f such that the meaning of total with respect to this.bounds is equivalent to the
	 * meaning of f with respect to this.bounds' 
	 * @ensures this.symmetries and this.bounds are modified as described in {@linkplain #breakMatrixSymmetries(Map, boolean)}
	 * iff total.first, total.last, and total.ordered have the same upper bound, which, when 
	 * cross-multiplied with itself gives the upper bound of total.relation
	 * 
	 * @see #breakMatrixSymmetries(Map,boolean)
	 */
	private final Formula breakTotalOrder(RelationPredicate.TotalOrdering total, boolean aggressive) {
		final Relation first = total.first(), last = total.last(), ordered = total.ordered(), relation = total.relation();
		final IntSet domain = bounds.upperBound(ordered).indexView();		
	
		if (symmetricColumnPartitions(ordered)!=null && 
			bounds.upperBound(first).indexView().contains(domain.min()) && 
			bounds.upperBound(last).indexView().contains(domain.max())) {
			
			// construct the natural ordering that corresponds to the ordering of the atoms in the universe
			final IntSet ordering = Ints.bestSet(usize*usize);
			int prev = domain.min();
			for(IntIterator atoms = domain.iterator(prev+1, usize); atoms.hasNext(); ) {
				int next = atoms.next();
				ordering.add(prev*usize + next);
				prev = next;
			}
			
			if (ordering.containsAll(bounds.lowerBound(relation).indexView()) &&
				bounds.upperBound(relation).indexView().containsAll(ordering)) {
				
				// remove the ordered partition from the set of symmetric partitions
				removePartition(domain.min());
				
				final TupleFactory f = bounds.universe().factory();
				
				if (aggressive) {
					bounds.boundExactly(first, f.setOf(f.tuple(1, domain.min())));
					bounds.boundExactly(last, f.setOf(f.tuple(1, domain.max())));
					bounds.boundExactly(ordered, bounds.upperBound(total.ordered()));
					bounds.boundExactly(relation, f.setOf(2, ordering));
					
					return Formula.TRUE;
					
				} else {
					final Relation firstConst = Relation.unary("SYM_BREAK_CONST_"+first.name());
					final Relation lastConst = Relation.unary("SYM_BREAK_CONST_"+last.name());
					final Relation ordConst = Relation.unary("SYM_BREAK_CONST_"+ordered.name());
					final Relation relConst = Relation.binary("SYM_BREAK_CONST_"+relation.name());
					bounds.boundExactly(firstConst, f.setOf(f.tuple(1, domain.min())));
					bounds.boundExactly(lastConst, f.setOf(f.tuple(1, domain.max())));
					bounds.boundExactly(ordConst, bounds.upperBound(total.ordered()));
					bounds.boundExactly(relConst, f.setOf(2, ordering));
					
					return Formula.and(first.eq(firstConst), last.eq(lastConst), ordered.eq(ordConst), relation.eq(relConst));
//					return first.eq(firstConst).and(last.eq(lastConst)).and( ordered.eq(ordConst)).and( relation.eq(relConst));
				}

			}
		}
		
		return null;
	}
 
Example #28
Source File: FormulaFlattener.java    From kodkod with MIT License 4 votes vote down vote up
/** @see #visitFormula(Formula) */
public final void visit(RelationPredicate pred)		{ visitFormula(pred); }
 
Example #29
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/** @effects appends the tokenization of the given node to this.tokens */
public void visit(RelationPredicate node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	switch(node.name()) { 
	case ACYCLIC : 
		append("acyclic");
		append("[");
		node.relation().accept(this);
		append("]");
		break;
	case FUNCTION : 
		RelationPredicate.Function func = (RelationPredicate.Function)node;
		append("function");
		append("[");
		func.relation().accept(this);
		colon();
		func.domain().accept(this);
		infix("->");
		keyword(func.targetMult());
		func.range().accept(this);
		append("]");
		break;
	case TOTAL_ORDERING : 
		RelationPredicate.TotalOrdering ord = (RelationPredicate.TotalOrdering)node;
		append("ord");
		append("[");
		ord.relation().accept(this);
		comma();
		ord.ordered().accept(this);
		comma();
		ord.first().accept(this);
		comma();
		ord.last().accept(this);
		append("]");
		break;
	default:
		throw new AssertionError("unreachable");
	}
	top = oldTop;
}
 
Example #30
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
/** @return true if the given formula should be parenthesized when a 
 * child of a compound parent */
private boolean parenthesize(Formula child) { 
	return !(child instanceof NotFormula || child instanceof ConstantFormula || 
			 child instanceof RelationPredicate);
}