Example #1
0
 private void printInstances(Instance[] solutions, IPrologTermOutput pto) {
   pto.openList();
   for (final Instance instance : solutions) {
     if (instance != null) {
       pto.openList();
       for (int i = 0; i < variables.length; i++) {
         final RelationInfo relinfo = variables[i];
         final TupleSet tupleSet = instance.tuples(relinfo.getRelation());
         final TupleType tupleType = relinfo.getTupleType();
         pto.openTerm(tupleType.isSingleton() ? "b" : "s");
         pto.printAtom(relinfo.getId());
         if (tupleType.isSingleton()) {
           final Tuple tuple = tupleSet.isEmpty() ? null : tupleSet.iterator().next();
           writeTuple(pto, tupleType, tupleSet, tuple);
         } else {
           pto.openList();
           writeTupleSet(pto, tupleType, tupleSet);
           pto.closeList();
         }
         pto.closeTerm();
       }
       pto.closeList();
     }
   }
   pto.closeList();
 }
  @Test
  public final void testTotalOrdering() {
    bounds.bound(to1, factory.area(factory.tuple("0", "0"), factory.tuple("4", "4")));
    bounds.bound(ord1, factory.setOf("0", "1", "2", "3", "4"));
    bounds.bound(first1, bounds.upperBound(ord1));
    bounds.bound(last1, bounds.upperBound(ord1));
    final Formula ordered1 = to1.totalOrder(ord1, first1, last1);
    assertNotNull(solve(to1.some().and(ordered1)));
    assertPrimVarNum(0);
    assertAuxVarNum(0);
    assertClauseNum(0);

    bounds.bound(r1, factory.range(factory.tuple("0"), factory.tuple("4")));
    assertNotNull(solve(to1.join(r1).some().and(ordered1)));
    assertPrimVarNum(bounds.upperBound(r1).size());

    bounds.boundExactly(r1, bounds.upperBound(r1));
    assertNotNull(solve(to1.join(r1).some().and(ordered1)));
    assertPrimVarNum(0);

    bounds.bound(to2, factory.setOf("5", "6", "7", "8", "9").product(factory.setOf("5", "7", "8")));
    bounds.bound(ord2, factory.setOf("5", "7", "8"));
    bounds.bound(first2, bounds.upperBound(ord2));
    bounds.bound(last2, bounds.upperBound(ord2));
    final Formula ordered2 = to2.totalOrder(ord2, first2, last2);
    assertNotNull(solve(to1.difference(to2).some().and(ordered2).and(ordered1)));
    assertPrimVarNum(0);
    assertAuxVarNum(0);
    assertClauseNum(0);

    bounds.bound(to3, factory.allOf(2));
    bounds.bound(ord3, factory.allOf(1));
    bounds.bound(first3, factory.setOf("9"));
    bounds.bound(last3, factory.setOf("8"));
    final Formula ordered3 = to3.totalOrder(ord3, first3, last3);
    assertNotNull(solve(to3.product(to1).some().and(ordered1).and(ordered3)));
    assertPrimVarNum(bounds.upperBound(to3).size() + bounds.upperBound(ord3).size() + 2);

    // SAT solver takes a while
    //		bounds.boundExactly(r2, factory.setOf(factory.tuple("9","8")));
    //		assertNotNull(solve(r2.in(to3).and(ordered3)));
    bounds.bound(to3, factory.allOf(2));
    bounds.bound(ord3, factory.setOf("3"));
    bounds.bound(first3, factory.allOf(1));
    bounds.bound(last3, factory.allOf(1));

    Instance instance = solve(ordered3);
    assertNotNull(instance);
    assertTrue(instance.tuples(to3).isEmpty());
    assertTrue(instance.tuples(ord3).equals(bounds.upperBound(ord3)));
    assertTrue(instance.tuples(first3).equals(bounds.upperBound(ord3)));
    assertTrue(instance.tuples(last3).equals(bounds.upperBound(ord3)));
  }
Example #3
0
 public Map<String, TupleSet> nextSolution() {
   Map<String, TupleSet> result = null;
   Instance instance = nextInstance();
   if (instance != null) {
     result = new HashMap<String, TupleSet>();
     for (int i = 0; i < variables.length; i++) {
       final RelationInfo relinfo = variables[i];
       final Relation relation = relinfo.getRelation();
       result.put(relinfo.getId(), instance.tuples(relation));
     }
   }
   return result;
 }
 /** Prints the given solution using the given options to the console */
 void print(Instance instance, Options options) {
   final Evaluator eval = new Evaluator(instance, options);
   final int n = instance.tuples(queen).size();
   for (int i = 0; i < n; i++) {
     Expression ci = IntConstant.constant(i).toExpression();
     for (int j = 0; j < n; j++) {
       Expression cj = IntConstant.constant(j).toExpression();
       if (eval.evaluate(x.join(ci).intersection(y.join(cj)).some())) {
         System.out.print(" Q");
       } else {
         System.out.print(" .");
       }
     }
     System.out.println();
   }
   //		System.out.println(instance);
 }