Java Code Examples for kodkod.ast.operator.Multiplicity#ONE

The following examples show how to use kodkod.ast.operator.Multiplicity#ONE . 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: Comprehension.java    From kodkod with MIT License 5 votes vote down vote up
/**  
 * Constructs a comprehension expression with the specified decls
 * and formula
 * 
 * @ensures this.decls' = decls && this.formula' = formula
 * @throws NullPointerException  decls = null || formula = null
 */
Comprehension(Decls declarations, Formula formula) {
    if (formula == null) throw new NullPointerException("null formula");
    for(Decl decl : declarations) { 
    	if (decl.variable().arity()>1 || decl.multiplicity()!=Multiplicity.ONE)
    		throw new IllegalArgumentException("Cannot have a higher order declaration in a comprehension: "+decl);
    }
    this.decls = declarations;
    this.formula = formula;
}
 
Example 2
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** 
 * @effects this.tokens' = 
 *   concat[ this.tokens, tokenize[ node.variable ], ":", tokenize[ node.expression ] 
 **/
public void visit(Decl node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	node.variable().accept(this);
	colon();
	if (node.multiplicity()!=Multiplicity.ONE) {
		append(node.multiplicity());
		space();
	}
	node.expression().accept(this);
	top = oldTop;
}
 
Example 3
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * @ensures this.tokens' = 
 *   concat[ this.tokens, tokenize[ node.variable ], ":", tokenize[ node.expression ] 
 **/
public void visit(Decl node) {
	node.variable().accept(this);
	colon();
	if (node.multiplicity()!=Multiplicity.ONE) {
		append(node.multiplicity());
		space();
	}
	node.expression().accept(this);
}
 
Example 4
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(decl) and returns the cached value, if any.  
 * If a translation has not been cached, translates decl.expression,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(decl) | 
 *   some t => t, cache(decl, decl.expression.accept(this))
 */
public final BooleanMatrix visit(Decl decl) {
	BooleanMatrix matrix = lookup(decl);
	if (matrix!=null) return matrix;
	if (decl.multiplicity()!=Multiplicity.ONE)
		throw new HigherOrderDeclException(decl);
	return cache(decl, decl.expression().accept(this));
}
 
Example 5
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 6
Source File: SumExpression.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a sum expression
 * @ensures this.decls' = decls && this.intExpr' = intExpr
 * @throws IllegalArgumentException  some d: decls.children | d.multiplicty != ONE
 */
SumExpression(Decls decls, IntExpression intExpr) {
	for(Decl d : decls) {
		if (d.multiplicity()!=Multiplicity.ONE)
			throw new IllegalArgumentException(d + " is not a scalar declaration.");
	}
	this.decls = decls;
	this.intExpr = intExpr;
}
 
Example 7
Source File: RelationPredicate.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Constructs a new function predicate over the given relation and domain, 
 * with the specified target multiplicity.
 * @ensures this.name' = FUNCTION && this.relation' = relation && this.domain' = domain &&
 *          this.range' = range
 * @throws IllegalArgumentException  relation.arity != 2 || domain.arity != 1 || range.arity != 1 || 
 *                                    targetMult !in ONE + LONE
 */
Function(Relation relation, Expression domain, Multiplicity targetMult, Expression range) {
	super(relation);
	if (targetMult != Multiplicity.ONE && targetMult != Multiplicity.LONE)
		throw new IllegalArgumentException("invalid target multiplicity for a function: " + targetMult);
	if (domain.arity() != 1 || range.arity() != 1)
		throw new IllegalArgumentException("invalid arity: " + domain + " or " + range);
	this.targetMult = targetMult;
	this.domain = domain;
	this.range = range;
}
 
Example 8
Source File: HamiltonianCycle2.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns an ext encoded instance of HamiltonianCycle2.
 * @return  an ext encoded instance of HamiltonianCycle2.
 */
public static HamiltonianCycle2 extEncoding(String file, Graph.Format format ) {
	final Graph<?> graph = format.parse(file);
	final Relation edges = Relation.binary("edges");
	final Relation vertex = Relation.unary("vertex");
	final Relation[] pts = new Relation[graph.nodes().size()];
	for(int i = 0; i < pts.length; i++) { pts[i] = Relation.unary("p"+i); }

	final Universe univ = new Universe(graph.nodes());
	final Bounds bounds = new Bounds(univ);
	final TupleFactory f = univ.factory();
	
	final TupleSet edgeBound = f.noneOf(2);
	for(Object from : graph.nodes()) {
		for (Object to : graph.edges(from))
			edgeBound.add(f.tuple(from, to));
	}
	
	bounds.boundExactly(edges, edgeBound);
	
	bounds.boundExactly(pts[0], f.setOf(graph.start()==null ? univ.atom(0) : graph.start()));
	for(int i = 1; i < pts.length; i++) { 
		bounds.bound(pts[i], f.range(f.tuple(univ.atom(1)), f.tuple(univ.atom(univ.size()-1))));
	}
	bounds.boundExactly(vertex, f.allOf(1));
	
	return new HamiltonianCycle2(bounds, pts, Multiplicity.ONE, vertex, edges);
}
 
Example 9
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * @ensures this.tokens' = concat[ this.tokens, tokenize[ node.variable ], ":",
 *          tokenize[ node.expression ]
 **/
@Override
public void visit(Decl node) {
    node.variable().accept(this);
    colon();
    if (node.multiplicity() != Multiplicity.ONE) {
        append(node.multiplicity());
        space();
    }
    node.expression().accept(this);
}
 
Example 10
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private boolean noNewHOLSkolems(Collection<Relation> newSkolems, Collection<Relation> oldSkolems) {
    Set<Relation> diff = new HashSet<Relation>(newSkolems);
    diff.removeAll(oldSkolems);
    for (Relation sk : diff) {
        Decl d = sk.getSkolemVarDecl();
        if (d != null && d.multiplicity() != Multiplicity.ONE)
            return false;
    }
    return true;
}
 
Example 11
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(decl) and returns the cached value, if any. If a translation has
 * not been cached, translates decl.expression, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(decl) | some t => t, cache(decl,
 *         decl.expression.accept(this))
 */
@Override
public final BooleanMatrix visit(Decl decl) {
    BooleanMatrix matrix = lookup(decl);
    if (matrix != null)
        return matrix;
    if (decl.multiplicity() != Multiplicity.ONE)
        throw new HigherOrderDeclException(decl);
    return cache(decl, decl.expression().accept(this));
}
 
Example 12
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 13
Source File: SumExpression.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a sum expression
 *
 * @ensures this.decls' = decls && this.intExpr' = intExpr
 * @throws IllegalArgumentException some d: decls.children | d.multiplicty !=
 *             ONE
 */
SumExpression(Decls decls, IntExpression intExpr) {
    for (Decl d : decls) {
        if (d.multiplicity() != Multiplicity.ONE)
            throw new IllegalArgumentException(d + " is not a scalar declaration.");
    }
    this.decls = decls;
    this.intExpr = intExpr;
}
 
Example 14
Source File: RelationPredicate.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new function predicate over the given relation and domain, with
 * the specified target multiplicity.
 *
 * @ensures this.name' = FUNCTION && this.relation' = relation && this.domain' =
 *          domain && this.range' = range
 * @throws IllegalArgumentException relation.arity != 2 || domain.arity != 1 ||
 *             range.arity != 1 || targetMult !in ONE + LONE
 */
Function(Relation relation, Expression domain, Multiplicity targetMult, Expression range) {
    super(relation);
    if (targetMult != Multiplicity.ONE && targetMult != Multiplicity.LONE)
        throw new IllegalArgumentException("invalid target multiplicity for a function: " + targetMult);
    if (domain.arity() != 1 || range.arity() != 1)
        throw new IllegalArgumentException("invalid arity: " + domain + " or " + range);
    this.targetMult = targetMult;
    this.domain = domain;
    this.range = range;
}
 
Example 15
Source File: Variable.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the declaration that constrains this variable to be bound to exactly
 * one element of the given expression: 'this: one expr'.
 *
 * @return {d: Decl | d.variable = this && d.multiplicity = ONE && d.expression
 *         = expr }
 * @throws NullPointerException expr = null
 * @throws IllegalArgumentException this.arity != expr.arity || expr.arity != 1
 */
public Decl oneOf(Expression expr) {
    return new Decl(this, Multiplicity.ONE, expr);
}
 
Example 16
Source File: Variable.java    From kodkod with MIT License 2 votes vote down vote up
/**
 * Returns the declaration that constrains this variable to 
 * be bound to exactly one element of the given expression:  'this: one expr'.
 * @return {d: Decl | d.variable = this && d.multiplicity = ONE && d.expression = expr }
 * @throws NullPointerException  expr = null
 * @throws IllegalArgumentException  this.arity != expr.arity || expr.arity != 1
 */
public Decl oneOf(Expression expr) {
	return new Decl(this, Multiplicity.ONE, expr);
}
 
Example 17
Source File: Relation.java    From kodkod with MIT License 2 votes vote down vote up
/**
 * Returns a formula stating that this relation is a total function
 * with the specified domain and range.
 * @return {f: Formula | f <=> this in domain->range && all v: domain | one v.this }
 * @throws NullPointerException  domain = null || range = null
 * @throws IllegalArgumentException  domain.arity != 1 || range.arity != 1
 * @throws IllegalArgumentException  this.arity != 2
 */
public Formula function(Expression domain, Expression range) {
		return new RelationPredicate.Function(this, domain, Multiplicity.ONE, range);
}
 
Example 18
Source File: Relation.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a formula stating that this relation is a total function with the
 * specified domain and range.
 *
 * @return {f: Formula | f <=> this in domain->range && all v: domain | one
 *         v.this }
 * @throws NullPointerException domain = null || range = null
 * @throws IllegalArgumentException domain.arity != 1 || range.arity != 1
 * @throws IllegalArgumentException this.arity != 2
 */
public Formula function(Expression domain, Expression range) {
    return new RelationPredicate.Function(this, domain, Multiplicity.ONE, range);
}