Search in sources :

Example 76 with Term

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

the class TestJUnit method testHasMoreElements1.

@SuppressWarnings("unchecked")
public void testHasMoreElements1() {
    StringBuffer sb = new StringBuffer();
    Query q = new Query("atom_chars(prolog, Cs), member(C, Cs)");
    Map<String, Term> soln;
    q.open();
    while (q.hasMoreElements()) {
        soln = (Map<String, Term>) q.nextElement();
        sb.append(((Atom) soln.get("C")).name());
    }
    q.close();
    assertEquals("Query#hasMoreElements() + Query#nextElement() work as intended", "prolog", sb.toString());
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term)

Example 77 with Term

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

the class TestJUnit method testVariableBinding1.

public void testVariableBinding1() {
    Term lhs = new Compound("p", new Term[] { new Variable("X"), new Variable("Y") });
    Term rhs = new Compound("p", new Term[] { new Atom("a"), new Atom("b") });
    Term goal = new Compound("=", new Term[] { lhs, rhs });
    Map<String, Term> soln = new Query(goal).oneSolution();
    assertTrue("two Variables with different names can bind to distinct atoms", soln != null && (soln.get("X")).name().equals("a") && (soln.get("Y")).name().equals("b"));
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom)

Example 78 with Term

use of org.jpl7.Term in project cassandra by apache.

the class SetType method fromJSONObject.

@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
    if (parsed instanceof String)
        parsed = Json.decodeJson((String) parsed);
    if (!(parsed instanceof List))
        throw new MarshalException(String.format("Expected a list (representing a set), but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    List list = (List) parsed;
    Set<Term> terms = new HashSet<>(list.size());
    for (Object element : list) {
        if (element == null)
            throw new MarshalException("Invalid null element in set");
        terms.add(elements.fromJSONObject(element));
    }
    return new Sets.DelayedValue(elements, terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) Term(org.apache.cassandra.cql3.Term)

Example 79 with Term

use of org.jpl7.Term in project cassandra by apache.

the class ListType method fromJSONObject.

@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
    if (parsed instanceof String)
        parsed = Json.decodeJson((String) parsed);
    if (!(parsed instanceof List))
        throw new MarshalException(String.format("Expected a list, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    List list = (List) parsed;
    List<Term> terms = new ArrayList<>(list.size());
    for (Object element : list) {
        if (element == null)
            throw new MarshalException("Invalid null element in list");
        terms.add(elements.fromJSONObject(element));
    }
    return new Lists.DelayedValue(terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) Term(org.apache.cassandra.cql3.Term)

Example 80 with Term

use of org.jpl7.Term in project cassandra by apache.

the class MapType method fromJSONObject.

@Override
public Term fromJSONObject(Object parsed) throws MarshalException {
    if (parsed instanceof String)
        parsed = Json.decodeJson((String) parsed);
    if (!(parsed instanceof Map))
        throw new MarshalException(String.format("Expected a map, but got a %s: %s", parsed.getClass().getSimpleName(), parsed));
    Map<Object, Object> map = (Map<Object, Object>) parsed;
    Map<Term, Term> terms = new HashMap<>(map.size());
    for (Map.Entry<Object, Object> entry : map.entrySet()) {
        if (entry.getKey() == null)
            throw new MarshalException("Invalid null key in map");
        if (entry.getValue() == null)
            throw new MarshalException("Invalid null value in map");
        terms.put(keys.fromJSONObject(entry.getKey()), values.fromJSONObject(entry.getValue()));
    }
    return new Maps.DelayedValue(keys, terms);
}
Also used : MarshalException(org.apache.cassandra.serializers.MarshalException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Term(org.apache.cassandra.cql3.Term) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

Term (org.jpl7.Term)62 Query (org.jpl7.Query)42 Variable (org.jpl7.Variable)24 Atom (org.jpl7.Atom)21 Compound (org.jpl7.Compound)21 Map (java.util.Map)12 BigInteger (java.math.BigInteger)6 Integer (org.jpl7.Integer)6 org.jpl7.fli.term_t (org.jpl7.fli.term_t)4 Term (org.apache.cassandra.cql3.Term)3 MarshalException (org.apache.cassandra.serializers.MarshalException)3 PrologException (org.jpl7.PrologException)3 JPLException (org.jpl7.JPLException)2 IntHolder (org.jpl7.fli.IntHolder)2 URL (java.net.URL)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 TestSuite (junit.framework.TestSuite)1 DoubleHolder (org.jpl7.fli.DoubleHolder)1 Int64Holder (org.jpl7.fli.Int64Holder)1 ObjectHolder (org.jpl7.fli.ObjectHolder)1