Java Code Examples for kodkod.ast.Decls#and()

The following examples show how to use kodkod.ast.Decls#and() . 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: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(decls) and returns the cached value, if any. If a replacement
 * has not been cached, visits each of the children's variable and expression.
 * If nothing changes, the argument is cached and returned, otherwise a
 * replacement Decls object is cached and returned.
 *
 * @return { d: Decls | d.size = decls.size && all i: [0..d.size) |
 *         d.declarations[i] = decls.declarations[i].accept(delegate) }
 */
@Override
public Decls visit(Decls decls) {
    Decls ret = lookup(decls);
    if (ret != null)
        return ret;

    Decls visitedDecls = null;
    boolean allSame = true;
    for (Decl decl : decls) {
        Decls newDecl = visit(decl);
        if (newDecl != decl)
            allSame = false;
        visitedDecls = (visitedDecls == null) ? newDecl : visitedDecls.and(newDecl);
    }
    ret = allSame ? decls : visitedDecls;
    return cache(decls, ret);
}
 
Example 2
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a formula that properly constrains the given skolem's domain.
 *
 * @requires !nonSkolems.isEmpty()
 * @return a formula that properly constrains the given skolem's domain.
 */
private Formula domainConstraint(Decl skolemDecl, Relation skolem) {
    final Iterator<DeclInfo> itr = nonSkolems.iterator();
    Decls rangeDecls = null;
    while (itr.hasNext()) {
        Decl d = itr.next().decl;
        Decl dd = d.variable().oneOf(d.expression());
        rangeDecls = rangeDecls != null ? rangeDecls.and(dd) : dd;
    }
    // System.out.println(skolemDecl.expression());
    Expression skolemDomain = skolem;
    for (int i = 0, max = skolemDecl.variable().arity(); i < max; i++) {
        skolemDomain = skolemDomain.join(Expression.UNIV);
    }
    return skolemDomain.in(Formula.TRUE.comprehension(rangeDecls));
}
 
Example 3
Source File: GRA013_026.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
private final Formula cliqueAxiom(Expression color) {
    final Variable[] vars = new Variable[cliqueSize];
    for (int i = 0; i < cliqueSize; i++) {
        vars[i] = Variable.unary("V" + i);
    }
    final List<Expression> members = new ArrayList<Expression>(cliqueSize);
    for (int i = 0, max = cliqueSize - 1; i < max; i++) {
        final List<Expression> tmp = new ArrayList<Expression>();
        for (int j = i + 1; j < cliqueSize; j++) {
            tmp.add(vars[j]);
        }
        members.add(vars[i].product(Expression.union(tmp)));
    }
    Decls d = vars[0].oneOf(node);
    for (int i = 1; i < cliqueSize; i++) {
        d = d.and(vars[i].oneOf(vars[i - 1].join(lessThan)));
    }
    return Expression.union(members).in(color).implies(goalToBeProved()).forAll(d);
}
 
Example 4
Source File: GRA013_026.java    From kodkod with MIT License 6 votes vote down vote up
private final Formula cliqueAxiom(Expression color) {
	final Variable[] vars = new Variable[cliqueSize];
	for(int i = 0; i < cliqueSize; i++) { 
		vars[i] = Variable.unary("V"+i);
	}
	final List<Expression> members = new ArrayList<Expression>(cliqueSize);
	for(int i = 0, max = cliqueSize-1; i < max; i++) { 
		final List<Expression> tmp = new ArrayList<Expression>();
		for(int j = i+1; j < cliqueSize; j++) { 
			tmp.add(vars[j]);
		}
		members.add(vars[i].product(Expression.union(tmp)));
	}
	Decls d = vars[0].oneOf(node);
	for(int i = 1; i < cliqueSize; i++) { 
		d = d.and(vars[i].oneOf(vars[i-1].join(lessThan)));
	}
	return Expression.union(members).in(color).implies(goalToBeProved()).forAll(d);
}
 
Example 5
Source File: AbstractReplacer.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(decls) and returns the cached value, if any.  
 * If a replacement has not been cached, visits each of the children's 
 * variable and expression.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement Decls object is cached and returned.
 * @return { d: Decls | d.size = decls.size && 
 *                      all i: [0..d.size) | d.declarations[i] = decls.declarations[i].accept(this) } 
 */
public Decls visit(Decls decls) { 
	Decls ret = lookup(decls);
	if (ret!=null) return ret;
	
	Decls visitedDecls = null;
	boolean allSame = true;
	for(Decl decl : decls) {
		Decls newDecl = visit(decl);
		if (newDecl != decl) 
			allSame = false;
		visitedDecls = (visitedDecls==null) ? newDecl : visitedDecls.and(newDecl);
	}
	ret = allSame ? decls : visitedDecls;
	return cache(decls, ret);
}
 
Example 6
Source File: NUM378.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final Decls decls(Variable[] vars) {
    Decls d = vars[0].oneOf(UNIV);
    for (int i = 1; i < vars.length; i++) {
        d = d.and(vars[i].oneOf(UNIV));
    }
    return d;
}
 
Example 7
Source File: Viktor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the equations to be satisfied.
 *
 * @return equations to be satisfied.
 */
public final Formula equations() {

    // each b <= cols-1
    Formula f0 = Formula.TRUE;
    final IntConstant colConst = IntConstant.constant(cols - 1);
    for (IntExpression bi : b) {
        f0 = f0.and(bi.lte(colConst));
    }

    final Variable[] y = new Variable[rows];
    for (int i = 0; i < rows; i++) {
        y[i] = Variable.unary("y" + i);
    }

    Decls decls = y[0].oneOf(INTS);
    for (int i = 1; i < rows; i++)
        decls = decls.and(y[i].oneOf(INTS));

    Formula f1 = Formula.TRUE;
    final Expression[] combo = new Expression[rows];
    for (int i = 0; i < cols; i++) {
        for (int j = i + 1; j < cols; j++) {
            for (int k = j + 1; k < cols; k++) {
                Formula f2 = Formula.TRUE;
                for (int m = 0; m < rows; m++) {
                    combo[0] = a[m][i];
                    combo[1] = a[m][j];
                    combo[2] = a[m][k];
                    f2 = f2.and(conditionalSum(combo, y, 0, rows - 1).eq(b[m]));
                }
                f1 = f1.and(f2.not().forAll(decls));
            }
        }
    }
    return f0.and(f1);
}
 
Example 8
Source File: NUM378.java    From kodkod with MIT License 5 votes vote down vote up
private final Decls decls(Variable[] vars) {
	Decls d = vars[0].oneOf(UNIV);
	for(int i = 1; i < vars.length; i++) {
		d = d.and(vars[i].oneOf(UNIV));
	}
	return d;
}
 
Example 9
Source File: Viktor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the equations to be satisfied.
 * @return equations to be satisfied.
 */
public final Formula equations() {
	
	// each b <= cols-1
	Formula f0 = Formula.TRUE;
	final IntConstant colConst = IntConstant.constant(cols-1);
	for(IntExpression bi: b) {
		f0 = f0.and(bi.lte(colConst));
	}
	
	final Variable[] y = new Variable[rows];
	for(int i = 0; i < rows; i++) {
		y[i] = Variable.unary("y"+i);
	}
	
	Decls decls = y[0].oneOf(INTS);
	for(int i = 1; i < rows; i++)
		decls = decls.and(y[i].oneOf(INTS));
	
	Formula f1 = Formula.TRUE;
	final Expression[] combo = new Expression[rows];
	for(int i = 0; i < cols; i++) {
		for(int j = i+1; j < cols; j++) {
			for(int k = j+1; k < cols; k++) {
				Formula f2 = Formula.TRUE;
				for(int m = 0; m < rows; m++) {
					combo[0] = a[m][i];
					combo[1] = a[m][j];
					combo[2] = a[m][k];
					f2 = f2.and(conditionalSum(combo, y, 0, rows-1).eq(b[m]));
				}
				f1 = f1.and(f2.not().forAll(decls));
			}
		}
	}
	return f0.and(f1); 
}
 
Example 10
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/**
	 * Returns a formula that properly constrains the given skolem's domain.
	 * @requires !nonSkolems.isEmpty()
	 * @return a formula that properly constrains the given skolem's domain.
	 */ 
	private Formula domainConstraint(Decl skolemDecl, Relation skolem) { 
		final Iterator<DeclInfo> itr = nonSkolems.iterator();
		Decls rangeDecls = itr.next().decl;
		while(itr.hasNext()) {
			rangeDecls = rangeDecls.and(itr.next().decl);
		}
//		System.out.println(skolemDecl.expression());
		Expression skolemDomain = skolem;
		for(int i = 0, max = skolemDecl.variable().arity(); i < max; i++) { 
			skolemDomain = skolemDomain.join(Expression.UNIV);
		}
		return skolemDomain.in(Formula.TRUE.comprehension(rangeDecls));	
	}
 
Example 11
Source File: JenaTranslator.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private Expression restrictExistsWithFilter(Set<Variable> lhsVars, Pair<Relation, List<Pair<Variable, Domain>>> rightRelation, Formula filters, Expression tuples) {
	Expression t = null;
	Decls d = null;
	Set<Variable> filterVars = filters==null? Collections.<Variable>emptySet(): ASTUtils.gatherVariables(filters);
	for(Pair<Variable,Domain> v : rightRelation.snd) {
		if (lhsVars.contains(v.fst) || filterVars.contains(v.fst)) {
			t = t==null? v.fst: t.product(v.fst);
			if (!lhsVars.contains(v.fst)) {
				Decl x = v.fst.oneOf(QuadTableRelations.nodes.union(NULL));
				d = d==null? x: d.and(x);
			}
		}
	}
	return t.in(tuples).and(filters).comprehension(d);
}
 
Example 12
Source File: BugTests.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFelix_03062008_2() {
    Relation x5 = Relation.unary("Role");
    Relation x6 = Relation.unary("Session");

    List<String> atomlist = Arrays.asList("Role$0", "Session$0", "Session$1");

    Universe universe = new Universe(atomlist);
    TupleFactory factory = universe.factory();
    Bounds bounds = new Bounds(universe);

    TupleSet x5_upper = factory.noneOf(1);
    x5_upper.add(factory.tuple("Role$0"));
    bounds.bound(x5, x5_upper);

    TupleSet x6_upper = factory.noneOf(1);
    x6_upper.add(factory.tuple("Session$0"));
    x6_upper.add(factory.tuple("Session$1"));
    bounds.bound(x6, x6_upper);

    Variable x11 = Variable.unary("x_a");
    Decls x10 = x11.oneOf(x6);
    Variable x15 = Variable.unary("x_b");
    Decls x14 = x15.oneOf(x5);
    Variable x17 = Variable.unary("x_c");
    Decls x16 = x17.oneOf(x5);
    Decls x13 = x14.and(x16);
    Expression x20 = x15.product(x17);
    Expression x19 = x11.product(x20);
    Formula x18 = x19.some();
    Formula x12 = x18.forSome(x13);
    Formula x9 = x12.forAll(x10);
    Formula x24 = x5.some();
    Formula x23 = x24.not();
    Formula x28 = x5.eq(x5);
    Formula x29 = x6.eq(x6);
    Formula x25 = x28.and(x29);
    Formula x22 = x23.and(x25);
    Formula x8 = x9.and(x22).and(x5.no()).and(x6.no());

    Solver solver = new Solver();
    solver.options().setSolver(SATFactory.DefaultSAT4J);
    solver.options().setBitwidth(2);
    // solver.options().setFlatten(false);
    solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
    solver.options().setSymmetryBreaking(20);
    solver.options().setSkolemDepth(2);

    System.out.flush();

    Solution sol = solver.solve(x8, bounds);
    Instance inst = sol.instance();
    assertNotNull(inst);

    for (Relation rel : inst.relations()) {
        if (rel != x5 && rel != x6) {
            final TupleSet range = inst.tuples(x6).product(inst.tuples(x5));
            assertTrue(range.containsAll(inst.tuples(rel)));
        }
    }
}
 
Example 13
Source File: RegressionTests.java    From kodkod with MIT License 4 votes vote down vote up
@Test
public final void testFelix_03062008_2() {
	Relation x5 = Relation.unary("Role");
	Relation x6 = Relation.unary("Session");

	List<String> atomlist = Arrays.asList("Role$0", "Session$0", "Session$1");

	Universe universe = new Universe(atomlist);
	TupleFactory factory = universe.factory();
	Bounds bounds = new Bounds(universe);

	TupleSet x5_upper = factory.noneOf(1);
	x5_upper.add(factory.tuple("Role$0"));
	bounds.bound(x5, x5_upper);

	TupleSet x6_upper = factory.noneOf(1);
	x6_upper.add(factory.tuple("Session$0"));
	x6_upper.add(factory.tuple("Session$1"));
	bounds.bound(x6, x6_upper);

	Variable x11=Variable.unary("x_a");
	Decls x10=x11.oneOf(x6);
	Variable x15=Variable.unary("x_b");
	Decls x14=x15.oneOf(x5);
	Variable x17=Variable.unary("x_c");
	Decls x16=x17.oneOf(x5);
	Decls x13=x14.and(x16);
	Expression x20=x15.product(x17);
	Expression x19=x11.product(x20);
	Formula x18=x19.some();
	Formula x12=x18.forSome(x13);
	Formula x9=x12.forAll(x10);
	Formula x24=x5.some();
	Formula x23=x24.not();
	Formula x28=x5.eq(x5);
	Formula x29=x6.eq(x6);
	Formula x25=x28.and(x29);
	Formula x22=x23.and(x25);
	Formula x8=x9.and(x22).and(x5.no()).and(x6.no());

	Solver solver = new Solver();
	solver.options().setSolver(SATFactory.DefaultSAT4J);
	solver.options().setBitwidth(2);
	solver.options().setIntEncoding(Options.IntEncoding.TWOSCOMPLEMENT);
	solver.options().setSymmetryBreaking(20);
	solver.options().setSkolemDepth(2);

	System.out.flush();

	Solution sol = solver.solve(x8, bounds);
	Instance inst = sol.instance();
	assertNotNull(inst);


	for(Relation rel : inst.relations()) { 
		if (rel!=x5 && rel!=x6) {
			final TupleSet range = inst.tuples(x6).product(inst.tuples(x5));
			assertTrue(range.containsAll(inst.tuples(rel)));
		}
	}
}