Search in sources :

Example 56 with Node

use of suite.node.Node in project suite by stupidsing.

the class LogicCompilerLevel1Test method testMemberOfMember.

@Test
public void testMemberOfMember() {
    RuleSet rs = Suite.newRuleSet(List.of("auto.sl"));
    Node goal = Suite.parse("source .lln, member .lln .ln, member .ln .n, sink .n");
    Node input = Suite.parse("((1, 2,), (3, 4,),)");
    List<Node> results = FindUtil.collectList(finder(rs, goal), input);
    System.out.println(results);
    assertTrue(results.size() == 4);
}
Also used : RuleSet(suite.lp.kb.RuleSet) Node(suite.node.Node) Test(org.junit.Test)

Example 57 with Node

use of suite.node.Node in project suite by stupidsing.

the class BinderFactoryTest method test.

private void test(String pattern, String match) {
    for (BinderFactory bf : new BinderFactory[] { // 
    new CompileBinderImpl0(), // 
    new CompileBinderImpl(), new SewingBinderImpl() }) {
        Node node = new Generalizer().generalize(Suite.parse(pattern));
        Bind_ p = bf.binder(node);
        BindEnv be = new BindEnv(bf.mapper().env());
        assertTrue(p.test(be, Suite.parse(match)));
    }
}
Also used : Bind_(suite.lp.doer.BinderFactory.Bind_) BindEnv(suite.lp.doer.BinderFactory.BindEnv) Node(suite.node.Node) SewingBinderImpl(suite.lp.sewing.impl.SewingBinderImpl) CompileBinderImpl(suite.lp.compile.impl.CompileBinderImpl) CompileBinderImpl0(suite.lp.compile.impl.CompileBinderImpl0)

Example 58 with Node

use of suite.node.Node in project suite by stupidsing.

the class Comparer method compare.

@Override
public int compare(Node n0, Node n1) {
    n0 = n0.finalNode();
    n1 = n1.finalNode();
    Class<? extends Node> clazz0 = n0.getClass();
    Class<? extends Node> clazz1 = n1.getClass();
    int c = Integer.compare(order.get(clazz0), order.get(clazz1));
    if (c == 0)
        if (clazz0 == Atom.class)
            return ((Atom) n0).name.compareTo(((Atom) n1).name);
        else if (clazz0 == Dict.class) {
            Map<Node, Reference> m0 = ((Dict) n0).map;
            Map<Node, Reference> m1 = ((Dict) n1).map;
            Set<Node> keys = new HashSet<>();
            keys.addAll(m0.keySet());
            keys.addAll(m1.keySet());
            for (Node key : Read.from(keys).sort(this::compare)) c = c != 0 ? c : Object_.compare(m0.get(key), m1.get(key));
            return c;
        } else if (clazz0 == Int.class)
            return Integer.compare(((Int) n0).number, ((Int) n1).number);
        else if (clazz0 == Reference.class)
            return Integer.compare(((Reference) n0).getId(), ((Reference) n1).getId());
        else if (clazz0 == Str.class)
            return ((Str) n0).value.compareTo(((Str) n1).value);
        else if (Tree.class.isAssignableFrom(clazz0)) {
            Tree t0 = (Tree) n0;
            Tree t1 = (Tree) n1;
            c = t0.getOperator().getPrecedence() - t1.getOperator().getPrecedence();
            c = c != 0 ? c : compare(t0.getLeft(), t1.getLeft());
            c = c != 0 ? c : compare(t0.getRight(), t1.getRight());
            return c;
        } else if (clazz0 == Tuple.class) {
            Node[] nodes0 = ((Tuple) n0).nodes;
            Node[] nodes1 = ((Tuple) n1).nodes;
            int i = 0, l = min(nodes0.length, nodes1.length);
            while (c == 0 && i < l) c = compare(nodes0[i], nodes1[i]);
            if (c == 0)
                c = Integer.compare(nodes0.length, nodes1.length);
            return c;
        } else
            return Integer.compare(n0.hashCode(), n1.hashCode());
    else
        return c;
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Atom(suite.node.Atom) Int(suite.node.Int) Str(suite.node.Str) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple) TreeTuple(suite.node.tree.TreeTuple) HashSet(java.util.HashSet)

Example 59 with Node

use of suite.node.Node in project suite by stupidsing.

the class Rewrite method rewrite.

public Node rewrite(Source<Node[]> source, Node node) {
    Trail trail = new Trail();
    return rewrite(node0 -> {
        Node node1;
        if (!(node0 instanceof Reference)) {
            int pit = trail.getPointInTime();
            Node[] ft = source.source();
            if (Binder.bind(node0, ft[0], trail))
                node1 = ft[1];
            else {
                trail.unwind(pit);
                node1 = node0;
            }
        } else
            node1 = node0;
        return node1;
    }, node);
}
Also used : Trail(suite.lp.Trail) Reference(suite.node.Reference) Node(suite.node.Node)

Example 60 with Node

use of suite.node.Node in project suite by stupidsing.

the class TermKey method equals.

@Override
public boolean equals(Object object) {
    if (Object_.clazz(object) == TermKey.class) {
        Node node1 = ((TermKey) object).node;
        TermLister tl0 = new TermLister(node);
        TermLister tl1 = new TermLister(node1);
        return Objects.equals(tl0, tl1);
    } else
        return false;
}
Also used : Node(suite.node.Node)

Aggregations

Node (suite.node.Node)139 Tree (suite.node.Tree)50 Reference (suite.node.Reference)41 Atom (suite.node.Atom)37 Int (suite.node.Int)33 ArrayList (java.util.ArrayList)32 Pair (suite.adt.pair.Pair)25 List (java.util.List)24 TermOp (suite.node.io.TermOp)21 Read (suite.streamlet.Read)21 Test (org.junit.Test)20 Map (java.util.Map)19 Suite (suite.Suite)18 Generalizer (suite.lp.doer.Generalizer)18 Fail (suite.util.Fail)18 Fun (suite.util.FunUtil.Fun)18 Str (suite.node.Str)17 Trail (suite.lp.Trail)16 RuleSet (suite.lp.kb.RuleSet)16 Tuple (suite.node.Tuple)16