Search in sources :

Example 11 with Compound

use of org.jpl7.Compound in project packages-jpl by SWI-Prolog.

the class TestJUnit method testBigInteger2.

public void testBigInteger2() {
    BigInteger b = new BigInteger("12345678901234567890123456789");
    // too big for a long
    Term i = new Integer(b);
    Term g = new Compound("is", new Term[] { new Variable("X"), i });
    Term x = Query.oneSolution(g).get("X");
    assertTrue("X is an org.jpl7.Integer", x.isInteger());
    assertTrue("X is a big org.jpl7.Integer", x.isBigInteger());
    assertTrue("X's value is as expected", x.bigValue().equals(b));
}
Also used : Integer(org.jpl7.Integer) BigInteger(java.math.BigInteger) Variable(org.jpl7.Variable) BigInteger(java.math.BigInteger) Compound(org.jpl7.Compound) Term(org.jpl7.Term)

Example 12 with Compound

use of org.jpl7.Compound in project packages-jpl by SWI-Prolog.

the class TestJUnit method testWouter1.

public void testWouter1() {
    // Wouter says this fails under OS X Mavericks
    // 10.9 x86-64
    // too big for an int
    long n = 7381783232223l;
    Compound term = new Compound("is", new Term[] { new Variable("X"), new org.jpl7.Integer(n) });
    Map<String, Term>[] solutions = new Query(term).allSolutions();
    assertEquals(1, solutions.length);
    Map<String, Term> solution = solutions[0];
    assertTrue(solution.containsKey("X"));
    Object result = solution.get("X");
    assertTrue(result instanceof org.jpl7.Integer);
    assertEquals(n, ((org.jpl7.Integer) result).longValue());
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Integer(org.jpl7.Integer) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Map(java.util.Map)

Example 13 with Compound

use of org.jpl7.Compound in project packages-jpl by SWI-Prolog.

the class TestJUnit method testVariableBinding3.

public void testVariableBinding3() {
    Variable X = new Variable("X");
    Term lhs = new Compound("p", new Term[] { X, X });
    Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
    Term goal = new Compound("=", new Term[] { lhs, rhs });
    assertFalse("two references to the same (named) Variable cannot unify with differing atoms", new Query(goal).hasSolution());
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom)

Example 14 with Compound

use of org.jpl7.Compound in project packages-jpl by SWI-Prolog.

the class TestJUnit method testArrayToList1.

public void testArrayToList1() {
    Term l2 = Util.termArrayToList(new Term[] { new Atom("a"), new Atom("b"), new Atom("c"), new Atom("d"), new Atom("e") });
    Query q9 = new Query(new Compound("append", new Term[] { new Variable("Xs"), new Variable("Ys"), l2 }));
    assertTrue("append(Xs,Ys,[a,b,c,d,e]) has 6 solutions", q9.allSolutions().length == 6);
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom)

Example 15 with Compound

use of org.jpl7.Compound in project packages-jpl by SWI-Prolog.

the class TestJUnit method testRef7.

public void testRef7() {
    Term badJRef = new Compound("@", new Term[] { new Atom("foobar") });
    try {
        // should throw exception
        badJRef.jrefToObject();
        // shouldn't
        fail("@(foobar) .jrefToObject() shoulda thrown JPLException");
    // get
    // to
    // here
    } catch (JPLException e) {
        // expected exception class
        if (e.getMessage().endsWith("term is not a JRef")) {
        // OK: an appropriate exception was thrown
        } else {
            fail("@(foobar) .jrefToObject() threw wrong JPLException: " + e);
        }
    } catch (Exception e) {
        fail("@(foobar) .jrefToObject() threw wrong exception class: " + e);
    }
}
Also used : JPLException(org.jpl7.JPLException) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom) PrologException(org.jpl7.PrologException) JPLException(org.jpl7.JPLException)

Aggregations

Compound (org.jpl7.Compound)21 Term (org.jpl7.Term)21 Variable (org.jpl7.Variable)17 Query (org.jpl7.Query)16 Atom (org.jpl7.Atom)13 Map (java.util.Map)4 Integer (org.jpl7.Integer)4 BigInteger (java.math.BigInteger)3 PrologException (org.jpl7.PrologException)2 JPLException (org.jpl7.JPLException)1 DoubleHolder (org.jpl7.fli.DoubleHolder)1 Int64Holder (org.jpl7.fli.Int64Holder)1 IntHolder (org.jpl7.fli.IntHolder)1 ObjectHolder (org.jpl7.fli.ObjectHolder)1 StringHolder (org.jpl7.fli.StringHolder)1 org.jpl7.fli.term_t (org.jpl7.fli.term_t)1