Search in sources :

Example 71 with Node

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

the class ClonerFactoryTest method test.

private void test(String pattern, String match) {
    for (ClonerFactory cf : new ClonerFactory[] { new CompileClonerImpl(), new SewingClonerImpl() }) {
        Node node = new Generalizer().generalize(Suite.parse(pattern));
        Clone_ p = cf.cloner(node);
        assertTrue(Binder.bind(p.apply(cf.mapper().env()), Suite.parse(match), new Trail()));
    }
}
Also used : Trail(suite.lp.Trail) CompileClonerImpl(suite.lp.compile.impl.CompileClonerImpl) SewingClonerImpl(suite.lp.sewing.impl.SewingClonerImpl) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_)

Example 72 with Node

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

the class SymbolicTest method testCubic.

@Test
public void testCubic() {
    verifySimplify("(a * x + b) ^ 3", // 
    "" + // 
    "(a * a * a) * x * x * x" + // 
    " + ((3 * a * a) * b) * x * x" + // 
    " + ((3 * a) * b * b) * x" + " + b * b * b");
    verifySimplify("(a * x + neg b) ^ 3", // 
    "" + // 
    "(a * a * a) * x * x * x" + // 
    " + ((neg 3 * a * a) * b) * x * x" + // 
    " + ((3 * a) * b * b) * x" + " + neg 1 * b * b * b");
    Node poly = // 
    rw.replace(// 
    x, // 
    Suite.parse("y + neg (b * inv (3 * a))"), Suite.parse("a * x * x * x + b * x * x + c * x + d"));
    verifySimplify(poly, // 
    "" + // 
    "a * y * y * y" + // 
    " + (c + (neg inv 3 * inv a) * b * b) * y" + " + d + ((neg inv 3 * inv a) * b) * c + ((2 * inv 27) * inv (a * a)) * b * b * b");
}
Also used : Node(suite.node.Node) Test(org.junit.Test)

Example 73 with Node

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

the class Chr method chrThen.

private Streamlet<State> chrThen(Streamlet<State> states, Node then) {
    Generalizer generalizer = new Generalizer();
    Atom a = atom(".a"), b = atom(".b");
    if (Binder.bind(then, generalizer.generalize(Suite.substitute(".0 = .1", a, b)), new Trail())) {
        // built-in syntactic equality
        Reference from = generalizer.getVariable(a);
        Reference to = generalizer.getVariable(b);
        states = states.map(new Fun<>() {

            public State apply(State state) {
                IMap<Prototype, ISet<Node>> factsByPrototype1 = IMap.empty();
                for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) factsByPrototype1 = factsByPrototype1.put(e.t0, replace(e.t1));
                return new State(factsByPrototype1);
            }

            private ISet<Node> replace(ISet<Node> facts) {
                ISet<Node> facts1 = ISet.empty();
                for (Node node : facts) facts1 = facts1.replace(rw.replace(from, to, node));
                return facts1;
            }
        });
    }
    return states.map(state -> {
        Prototype prototype = Prototype.of(then);
        ISet<Node> facts = getFacts(state, prototype);
        return setFacts(state, prototype, facts.replace(then));
    });
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Prototype(suite.lp.kb.Prototype) Reference(suite.node.Reference) Node(suite.node.Node) ISet(suite.immutable.ISet) Atom(suite.node.Atom) Fun(suite.util.FunUtil.Fun)

Example 74 with Node

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

the class Chr method chrGiven.

private Streamlet<State> chrGiven(Streamlet<State> states, Trail trail, Node given) {
    Prototype prototype = Prototype.of(given);
    return states.filter(state -> {
        ISet<Node> facts = getFacts(state, prototype);
        Predicate<Node> bindFun = bindFun(trail, given);
        return facts.streamlet().isAny(bindFun);
    });
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node)

Example 75 with Node

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

the class Chr method addRule.

public void addRule(Node node) {
    Rule rule = new Rule();
    while (node != Atom.of("end")) {
        Tree t0 = Tree.decompose(node, TermOp.TUPLE_);
        Tree t1 = t0 != null ? Tree.decompose(t0.getRight(), TermOp.TUPLE_) : null;
        if (t1 != null) {
            Node key = t0.getLeft();
            Node value = t1.getLeft();
            node = t1.getRight();
            if (key == Atom.of("given"))
                rule.givens = To.list(Tree.iter(value));
            else if (key == Atom.of("if"))
                rule.ifs = To.list(Tree.iter(value));
            else if (key == Atom.of("then"))
                rule.thens = To.list(Tree.iter(value));
            else if (key == Atom.of("when"))
                rule.when = value;
            else
                Fail.t("invalid key " + key);
        } else
            Fail.t("invalid rule " + node);
    }
    rules.add(rule);
}
Also used : Node(suite.node.Node) Tree(suite.node.Tree)

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