Package kodkod.ast

Examples of kodkod.ast.Relation


        final Solution sol = Solution.triviallySatisfiable(stats, trivialInstance);
       
        final List<Formula> changes = new LinkedList<Formula>();
               
        for(Map.Entry<Relation, TupleSet> entry: trivialInstance.relationTuples().entrySet()) {
          final Relation r = entry.getKey();
         
          if (!translBounds.relations().contains(r)) {
            translBounds.bound(r, bounds.lowerBound(r), bounds.upperBound(r));
          }
         
          if (translBounds.lowerBound(r)!=translBounds.upperBound(r)) { // r may change
            if (entry.getValue().isEmpty()) {
              changes.add(r.some());
            } else {
              final Relation rmodel = Relation.nary(r.name()+"_"+trivial, r.arity());
              translBounds.boundExactly(rmodel, entry.getValue())
              changes.add(r.eq(rmodel).not());
            }
          }
        }
View Full Code Here


      final List<Formula> domConstraints = new LinkedList<Formula>();
     
      for(Decl decl : decls) { 
        final Decl skolemDecl = visit(decl);
       
        final Relation skolem = Relation.nary("$"+ skolemDecl.variable().name(), nonSkolems.size() + skolemDecl.variable().arity());
        reporter.skolemizing(decl, skolem, nonSkolemsView);
       
        final Expression skolemExpr = skolemExpr(skolemDecl, skolem);
       
        final Multiplicity mult = decl.multiplicity();
View Full Code Here

      for(int prevIndex = indeces.next(); indeces.hasNext(); ) {
        int curIndex = indeces.next();
        for(Iterator<RelationParts> rIter = relParts.iterator(); rIter.hasNext() && original.size() < predLength;) {
         
          RelationParts rparts = rIter.next();
          Relation r = rparts.relation;
         
          if (!rparts.representatives.contains(sym.min())) continue// r does not range over sym
         
          BooleanMatrix m = interpreter.interpret(r);
          for(IndexedEntry<BooleanValue> entry : m) {
            int permIndex = permutation(r.arity(), entry.index(), prevIndex, curIndex);
            BooleanValue permValue = m.get(permIndex);
            if (permIndex==entry.index() || atSameIndex(original, permValue, permuted, entry.value()))
              continue;
           
            original.add(entry.value());
View Full Code Here

   * @see #breakMatrixSymmetries(Map,boolean)
   */
  private final Formula breakAcyclic(RelationPredicate.Acyclic acyclic, boolean aggressive) {
    final IntSet[] colParts = symmetricColumnPartitions(acyclic.relation());
    if (colParts!=null) {
      final Relation relation = acyclic.relation();
      final IntSet upper = bounds.upperBound(relation).indexView();
      final IntSet reduced = Ints.bestSet(usize*usize);
      for(IntIterator tuples = upper.iterator(); tuples.hasNext(); ) {
        int tuple = tuples.next();
        int mirror = (tuple / usize) + (tuple % usize)*usize;
        if (tuple != mirror) {
          if (!upper.contains(mirror)) return null;
          if (!reduced.contains(mirror))
            reduced.add(tuple)
        }
      }
     
      // remove the partition from the set of symmetric partitions
      removePartition(colParts[0].min());
     
      if (aggressive) {
        bounds.bound(relation, bounds.universe().factory().setOf(2, reduced));
        return Formula.TRUE;
      } else {
        final Relation acyclicConst = Relation.binary("SYM_BREAK_CONST_"+acyclic.relation().name());
        bounds.boundExactly(acyclicConst, bounds.universe().factory().setOf(2, reduced));
        return relation.in(acyclicConst);
      }
    }
    return null;
View Full Code Here

   * 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));
         
View Full Code Here

       for(Map.Entry<String,XMLNode> e:nmap.entrySet()) if (e.getValue().is("field")) parseField(e.getKey());
       for(Map.Entry<String,XMLNode> e:nmap.entrySet()) if (e.getValue().is("skolem")) parseSkolem(e.getKey());
       for(Sig s:allsigs) if (!s.builtin) {
          TupleSet ts = expr2ts.remove(s);
          if (ts==null) ts = factory.noneOf(1); // If the sig was NOT mentioned in the XML file...
          Relation r = sol.addRel(s.label, ts, ts);
          sol.addSig(s, r);
          for(Field f: s.getFields()) {
              ts = expr2ts.remove(f);
              if (ts==null) ts=factory.noneOf(f.type().arity()); // If the field was NOT mentioned in the XML file...
              r = sol.addRel(s.label+"."+f.label, ts, ts);
              sol.addField(f, r);
          }
       }
       for(Map.Entry<Expr,TupleSet> e:expr2ts.entrySet()) {
          ExprVar v = (ExprVar)(e.getKey());
          TupleSet ts = e.getValue();
          Relation r = sol.addRel(v.label, ts, ts);
          sol.kr2type(r, v.type());
       }
       // Done!
       sol.solve(null, null, null, false);
    }
View Full Code Here

        this.seqidxBounds = seqidxBounds.unmodifiableView();
        bounds.boundExactly(KK_NEXT, next);
        bounds.boundExactly(KK_SEQIDX, this.seqidxBounds);
        Map<String,Expression> s2k = new HashMap<String,Expression>();
        for(String e: stringAtoms) {
            Relation r = Relation.unary("");
            Tuple t = factory.tuple(e);
            s2k.put(e, r);
            bounds.boundExactly(r, factory.range(t, t));
            stringBounds.add(t);
        }
View Full Code Here

     * @param lower - the lowerbound; can be null if you want it to be the empty set
     * @param upper - the upperbound; cannot be null; must contain everything in lowerbound
     */
    Relation addRel(String label, TupleSet lower, TupleSet upper) throws ErrorFatal {
       if (solved) throw new ErrorFatal("Cannot add a Kodkod relation since solve() has completed.");
       Relation rel = Relation.nary(label, upper.arity());
       if (lower == upper) {
          bounds.boundExactly(rel, upper);
       } else if (lower == null) {
          bounds.bound(rel, upper);
       } else {
View Full Code Here

        if (s==null) {
            for(ExprVar sk:frame.skolems) un.seen(sk.label);
            // Store up the skolems
            List<Object> skolems = new ArrayList<Object>();
            for(Map.Entry<Relation,Type> e: frame.rel2type.entrySet()) {
               Relation r = e.getKey(); if (!frame.eval.instance().contains(r)) continue;
               Type t = e.getValue();   if (t.arity() > r.arity()) continue; // Something is wrong; let's skip it
               while (t.arity() < r.arity()) t = UNIV.type().product(t);
               String n = Util.tail(r.name());
               while(n.length()>0 && n.charAt(0)=='$') n = n.substring(1);
               skolems.add(n);
               skolems.add(t);
               skolems.add(r);
            }
            // Find all suitable "next" or "prev" relations
            nexts = new LinkedHashMap<Sig,List<Tuple>>();
            for(Sig sig:frame.sigs) for(Field f: sig.getFields()) if (f.label.compareToIgnoreCase("next")==0) {
               List<List<PrimSig>> fold = f.type().fold();
               if (fold.size()==1) {
                  List<PrimSig> t = fold.get(0);
                  if (t.size()==3 && t.get(0).isOne!=null && t.get(1)==t.get(2) && !nexts.containsKey(t.get(1))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(1)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(t.get(0)).join(frame.a2k(f)));
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  } else if (t.size()==2 && t.get(0)==t.get(1) && !nexts.containsKey(t.get(0))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(0)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(f));
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  }
               }
            }
            for(Sig sig:frame.sigs) for(Field f: sig.getFields()) if (f.label.compareToIgnoreCase("prev")==0) {
               List<List<PrimSig>> fold = f.type().fold();
               if (fold.size()==1) {
                  List<PrimSig> t = fold.get(0);
                  if (t.size()==3 && t.get(0).isOne!=null && t.get(1)==t.get(2) && !nexts.containsKey(t.get(1))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(1)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(t.get(0)).join(frame.a2k(f)).transpose());
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  } else if (t.size()==2 && t.get(0)==t.get(1) && !nexts.containsKey(t.get(0))) {
                     TupleSet set = frame.eval.evaluate(frame.a2k(t.get(0)));
                     if (set.size()<=1) continue;
                     TupleSet next = frame.eval.evaluate(frame.a2k(f).transpose());
                     List<Tuple> test = isOrder(next, set);
                     if (test!=null) nexts.put(t.get(1), test);
                  }
               }
            }
            // Assign atom->name and atom->MostSignificantSig
            for(Tuple t:frame.eval.evaluate(Relation.INTS)) { frame.atom2sig.put(t.atom(0), SIGINT); }
            for(Tuple t:frame.eval.evaluate(KK_SEQIDX))     { frame.atom2sig.put(t.atom(0), SEQIDX); }
            for(Tuple t:frame.eval.evaluate(KK_STRING))     { frame.atom2sig.put(t.atom(0), STRING); }
            for(Sig sig:frame.sigs) if (sig instanceof PrimSig && !sig.builtin && ((PrimSig)sig).isTopLevel()) rename(frame, (PrimSig)sig, nexts, un);
            // These are redundant atoms that were not chosen to be in the final instance
            int unused=0;
            for(Tuple tuple:frame.eval.evaluate(Relation.UNIV)) {
               Object atom = tuple.atom(0);
               if (!frame.atom2sig.containsKey(atom)) { frame.atom2name.put(atom, "unused"+unused); unused++; }
            }
            // Add the skolems
            for(int num=skolems.size(), i=0; i<num-2; i=i+3) {
                String n = (String) skolems.get(i);
                while(n.length()>0 && n.charAt(0)=='$') n=n.substring(1);
                Type t = (Type) skolems.get(i+1);
                Relation r = (Relation) skolems.get(i+2);
                frame.addSkolem(un.make("$"+n), t, r);
            }
            return;
        }
        for(PrimSig c: s.children()) rename(frame, c, nexts, un);
        String signame = un.make(s.label.startsWith("this/") ? s.label.substring(5) : s.label);
        List<Tuple> list = new ArrayList<Tuple>();
        for(Tuple t: frame.eval.evaluate(frame.a2k(s))) list.add(t);
        List<Tuple> order = nexts.get(s);
        if (order!=null && order.size()==list.size() && order.containsAll(list)) { list=order; }
        int i = 0;
        for(Tuple t: list) {
           if (frame.atom2sig.containsKey(t.atom(0))) continue; // This means one of the subsig has already claimed this atom.
           String x = signame + "$" + i;
           i++;
           frame.atom2sig.put(t.atom(0), s);
           frame.atom2name.put(t.atom(0), x);
           ExprVar v = ExprVar.make(null, x, s.type());
           TupleSet ts = t.universe().factory().range(t, t);
           Relation r = Relation.unary(x);
           frame.eval.instance().add(r, ts);
           frame.a2k.put(v, r);
           frame.atoms.add(v);
        }
    }
View Full Code Here

    /** Simplify (a.(a->b)) into b when semantically equivalent */
    private final Expression condense(Expression x) {
       while (x instanceof BinaryExpression) {
          BinaryExpression b = (BinaryExpression)x;
          if (b.op() == ExprOperator.JOIN && b.left() instanceof Relation && b.right() instanceof BinaryExpression) {
             Relation r = (Relation) (b.left());
             try {
                if (sol.query(true, r, false).size()!=1) return x;
                if (sol.query(false, r, false).size()!=1) return x;
             } catch(Err er) {
                return x;
View Full Code Here

TOP

Related Classes of kodkod.ast.Relation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.