Package kodkod.instance

Examples of kodkod.instance.TupleSet


       if (expr==Relation.NONE) return factory.noneOf(1);
       if (expr==Relation.INTS) return makeMutable ? sigintBounds.clone() : sigintBounds;
       if (expr==KK_SEQIDX) return makeMutable ? seqidxBounds.clone() : seqidxBounds;
       if (expr==KK_STRING) return makeMutable ? stringBounds.clone() : stringBounds;
       if (expr instanceof Relation) {
          TupleSet ans = findUpper ? bounds.upperBound((Relation)expr) : bounds.lowerBound((Relation)expr);
          if (ans!=null) return makeMutable ? ans.clone() : ans;
       }
       else if (expr instanceof BinaryExpression) {
          BinaryExpression b = (BinaryExpression)expr;
          if (b.op() == ExprOperator.UNION) {
             TupleSet left = query(findUpper, b.left(), true);
             TupleSet right = query(findUpper, b.right(), false);
             left.addAll(right);
             return left;
          }
       }
       throw new ErrorFatal("Unknown expression encountered during bounds computation: "+expr);
View Full Code Here


    }

    /** Shrink the bounds for the given relation; throws an exception if the new bounds is not sameAs/subsetOf the old bounds. */
    void shrink(Relation relation, TupleSet lowerBound, TupleSet upperBound) throws Err {
       if (solved) throw new ErrorFatal("Cannot shrink a Kodkod relation since solve() has completed.");
       TupleSet oldL = bounds.lowerBound(relation);
       TupleSet oldU = bounds.upperBound(relation);
       if (oldU.containsAll(upperBound) && upperBound.containsAll(lowerBound) && lowerBound.containsAll(oldL)) {
          bounds.bound(relation, lowerBound, upperBound);
       } else {
          throw new ErrorAPI("Inconsistent bounds shrinking on relation: "+relation);
       }
    }
View Full Code Here

    public A4TupleSet eval(Sig sig) {
       try {
          if (!solved || eval==null) return new A4TupleSet(factory.noneOf(1), this);
          A4TupleSet ans = evalCache.get(sig);
          if (ans!=null) return ans;
          TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, sig));
          ans = new A4TupleSet(ts, this);
          evalCache.put(sig, ans);
          return ans;
       } catch(Err er) {
          return new A4TupleSet(factory.noneOf(1), this);
View Full Code Here

    public A4TupleSet eval(Field field) {
       try {
          if (!solved || eval==null) return new A4TupleSet(factory.noneOf(field.type().arity()), this);
          A4TupleSet ans = evalCache.get(field);
          if (ans!=null) return ans;
          TupleSet ts = eval.evaluate((Expression) TranslateAlloyToKodkod.alloy2kodkod(this, field));
          ans = new A4TupleSet(ts, this);
          evalCache.put(field, ans);
          return ans;
       } catch(Err er) {
          return new A4TupleSet(factory.noneOf(field.type().arity()), this);
View Full Code Here

       final List<Tuple> list = new ArrayList<Tuple>(n);
       if (b.size() == 0 && n <= 1) return list;
       if (b.size() != n-1) return null;
       // Find the starting element
       Tuple head = null;
       TupleSet right = b.project(1);
       for(Tuple x: u) if (!right.contains(x)) {head = x; break;}
       if (head==null) return null;
       final TupleFactory f = head.universe().factory();
       // Form the list
       list.add(head);
       while(true) {
View Full Code Here

            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

             System.out.println("MergeEquivalent: "+a+" "+b); System.out.flush();
          }
       } else*/
       if (a instanceof Relation || b instanceof Relation) {
          try {
            TupleSet a0 = sol.query(false, a, false), a1 = sol.query(true, a, false);
            TupleSet b0 = sol.query(false, b, false), b1 = sol.query(true, b, false);
            if (a instanceof Relation && a0.size()<b0.size() && b0.containsAll(a0) && a1.containsAll(b0)) {
                rep.debug("Comment: Simplify "+a+" "+(a1.size()-a0.size())+"->"+(a1.size()-b0.size())+"\n");
                sol.shrink((Relation)a, a0=b0, a1);
            }
            if (a instanceof Relation && a1.size()>b1.size() && b1.containsAll(a0) && a1.containsAll(b1)) {
                rep.debug("Comment: Simplify "+a+" "+(a1.size()-a0.size())+"->"+(b1.size()-a0.size())+"\n");
                sol.shrink((Relation)a, a0, a1=b1);
            }
            if (b instanceof Relation && b0.size()<a0.size() && a0.containsAll(b0) && b1.containsAll(a0)) {
                rep.debug("Comment: Simplify "+b+" "+(b1.size()-b0.size())+"->"+(b1.size()-a0.size())+"\n");
                sol.shrink((Relation)b, b0=a0, b1);
            }
            if (b instanceof Relation && b1.size()>a1.size() && a1.containsAll(b0) && b1.containsAll(a1)) {
                rep.debug("Comment: Simplify "+b+" "+(b1.size()-b0.size())+"->"+(a1.size()-b0.size())+"\n");
                sol.shrink((Relation)b, b0, b1=a1);
            }
          } catch(Exception ex) {
             // safe to ignore;
          }
View Full Code Here

       a = condense(a);
       b = condense(b);
       if (a instanceof Relation) {
          try {
             Relation r = (Relation)a;
             TupleSet ub = sol.query(true, r, false), lb = sol.query(false, r, false), t = sol.approximate(b);
             t.retainAll(ub);
             if (!t.containsAll(lb)) { rep.debug("Comment: Simplify "+a+" "+ub.size()+"->false\n"); return false; } // This means the upperbound is shrunk BELOW the lowerbound.
             if (t.size() < ub.size()) { rep.debug("Comment: Simplify "+a+" "+ub.size()+"->"+t.size()+"\n"); sol.shrink(r,lb,t); }
          } catch(Throwable ex) {
             rep.debug("Comment: Simplify "+a+" exception: "+ex+"\n"+MailBug.dump(ex).trim()+"\n"); // Not fatal; let's report it to the debug() reporter
          }
       }
       return true;
View Full Code Here

        file.printf("Universe universe = new Universe(atomlist);%n");
        file.printf("TupleFactory factory = universe.factory();%n");
        file.printf("Bounds bounds = new Bounds(universe);%n%n");
        for(Relation r:bounds.relations()) {
            String n=map.get(r);
            TupleSet upper=bounds.upperBound(r);
            TupleSet lower=bounds.lowerBound(r);
            printTupleset(n+"_upper", upper, atomMap);
            if (upper.equals(lower)) {
                file.printf("bounds.boundExactly(%s, %s_upper);%n%n",n,n);
            }
            else if (lower.size()==0) {
                file.printf("bounds.bound(%s, %s_upper);%n%n",n,n);
            }
            else {
                printTupleset(n+"_lower", lower, atomMap);
                file.printf("bounds.bound(%s, %s_lower, %s_upper);%n%n",n,n,n);
View Full Code Here

    /** Computes the lowerbound from bottom-up; it will also set a suitable initial value for each sig's upperbound.
     * Precondition: sig is not a builtin sig
     */
    private TupleSet computeLowerBound(List<Tuple> atoms, final PrimSig sig) throws Err {
        int n = sc.sig2scope(sig);
        TupleSet lower = factory.noneOf(1);
        for(PrimSig c:sig.children()) lower.addAll(computeLowerBound(atoms, c));
        TupleSet upper = lower.clone();
        boolean isExact = sc.isExact(sig);
        if (isExact || sig.isTopLevel()) for(n=n-upper.size(); n>0; n--) {
           Tuple atom = atoms.remove(atoms.size()-1);
           // If MUST<SCOPE and s is exact, then add fresh atoms to both LOWERBOUND and UPPERBOUND.
           // If MUST<SCOPE and s is inexact but toplevel, then add fresh atoms to the UPPERBOUND.
           if (isExact) lower.add(atom);
           upper.add(atom);
        }
        lb.put(sig, lower);
        ub.put(sig, upper);
        return lower;
    }
View Full Code Here

TOP

Related Classes of kodkod.instance.TupleSet

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.