Search in sources :

Example 26 with Node

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

the class QueryRewriter method rewriteQuery.

private Node rewriteQuery(Node node0) {
    Node nodex;
    Prototype prototype = Prototype.of(node0);
    PrototypeInfo pi;
    if ((pi = infoByPrototype.get(prototype)) != null) {
        int length = pi.length;
        if (length <= 0)
            nodex = node0;
        else {
            Node[] ps = TreeUtil.elements(node0, length);
            if (pi.isSkipFirst)
                ps = Arrays.copyOfRange(ps, 1, ps.length, Node[].class);
            nodex = Tuple.of(ps);
        }
    } else
        nodex = node0;
    return nodex;
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node)

Example 27 with Node

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

the class SewingExpressionImpl method evaluator.

public Evaluate_ evaluator(Node node) {
    Tree tree = Tree.decompose(node);
    if (tree != null) {
        Operator op = tree.getOperator();
        Evaluate_ lhs, rhs;
        IntInt_Int fun;
        if (op == TermOp.TUPLE_) {
            Tree rightTree = Tree.decompose(tree.getRight());
            lhs = evaluator(tree.getLeft());
            rhs = evaluator(rightTree.getRight());
            fun = TreeUtil.evaluateOp(rightTree.getLeft());
        } else {
            lhs = evaluator(tree.getLeft());
            rhs = evaluator(tree.getRight());
            fun = TreeUtil.evaluateOp(op);
        }
        return env -> fun.apply(lhs.evaluate(env), rhs.evaluate(env));
    } else if (node instanceof Int) {
        int i = ((Int) node).number;
        return env -> i;
    } else {
        Clone_ f = clonerFactory.cloner(node);
        return env -> TreeUtil.evaluate(f.apply(env));
    }
}
Also used : Operator(suite.node.io.Operator) IntInt_Int(suite.primitive.IntInt_Int) Clone_(suite.lp.doer.ClonerFactory.Clone_) ClonerFactory(suite.lp.doer.ClonerFactory) EvaluatorFactory(suite.lp.doer.EvaluatorFactory) TermOp(suite.node.io.TermOp) Int(suite.node.Int) Operator(suite.node.io.Operator) TreeUtil(suite.node.util.TreeUtil) Tree(suite.node.Tree) Node(suite.node.Node) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) IntInt_Int(suite.primitive.IntInt_Int) IntInt_Int(suite.primitive.IntInt_Int) Int(suite.node.Int)

Example 28 with Node

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

the class SewingGeneralizerImpl method generalizer.

@Override
public Generalize_ generalizer(Node node) {
    List<Generalize_> funs = new ArrayList<>();
    Generalize_ fun;
    while (true) {
        Node node0 = node;
        Tree tree;
        if (node0 instanceof Atom) {
            Atom atom = (Atom) node0;
            String name = atom.name;
            if (ProverConstant.isCut(node0) || ProverConstant.isVariable(name)) {
                int index = vm.computeIndex(atom);
                fun = env -> env.get(index);
            } else if (ProverConstant.isWildcard(name))
                fun = env -> new Reference();
            else
                fun = env -> node0;
        } else if (node0 instanceof Dict) {
            Generalize_[][] array = // 
            Read.from2(// 
            ((Dict) node0).map).map(// 
            (key, value) -> new Generalize_[] { generalizer(key), generalizer(value) }).toArray(Generalize_[].class);
            int length = array.length;
            fun = env -> {
                @SuppressWarnings("unchecked") Pair<Node, Reference>[] pairs = new Pair[length];
                for (int i = 0; i < length; i++) pairs[i] = Pair.of(array[i][0].apply(env), Reference.of(array[i][1].apply(env)));
                return Dict.of(pairs);
            };
        } else if ((tree = Tree.decompose(node0)) != null) {
            Operator operator = tree.getOperator();
            if (operator != TermOp.OR____) {
                Generalize_ f = generalizer(tree.getLeft());
                funs.add(env -> Tree.of(operator, f.apply(env), null));
                node = tree.getRight();
                continue;
            } else {
                // delay generalizing for performance
                Generalize_ lf = generalizer(tree.getLeft());
                Generalize_ rf = generalizer(tree.getRight());
                fun = env -> Tree.of(operator, lf.apply(env), new Suspend(() -> rf.apply(env)));
            }
        } else if (node0 instanceof Tuple) {
            Generalize_[] fs = Read.from(((Tuple) node0).nodes).map(this::generalizer).toArray(Generalize_.class);
            int length = fs.length;
            fun = env -> {
                Node[] array = new Node[length];
                for (int i = 0; i < length; i++) array[i] = fs[i].apply(env);
                return Tuple.of(array);
            };
        } else
            fun = env -> node0;
        funs.add(fun);
        break;
    }
    if (1 < funs.size())
        return env -> {
            Tree t = Tree.of(null, null, null);
            Node node_ = t;
            for (Generalize_ fun_ : funs) {
                Tree t_ = Tree.decompose(node_);
                Tree.forceSetRight(t_, fun_.apply(env));
                node_ = t_.getRight();
            }
            return t.getRight();
        };
    else
        return funs.get(0);
}
Also used : Reference(suite.node.Reference) Read(suite.streamlet.Read) GeneralizerFactory(suite.lp.doer.GeneralizerFactory) TermOp(suite.node.io.TermOp) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Atom(suite.node.Atom) Suspend(suite.node.Suspend) Tuple(suite.node.Tuple) Operator(suite.node.io.Operator) Dict(suite.node.Dict) Operator(suite.node.io.Operator) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) Suspend(suite.node.Suspend) Atom(suite.node.Atom) Dict(suite.node.Dict) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 29 with Node

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

the class SewingProverImpl method save.

private Restore save(Runtime rt) {
    Cps cps0 = rt.cps;
    Env env0 = rt.env;
    Node query0 = rt.query;
    IList<Trampoline> cutPoint0 = rt.cutPoint;
    IList<Trampoline> rems0 = rt.rems;
    int pit0 = rt.trail.getPointInTime();
    Sink<Node> handler0 = rt.handler;
    return rt_ -> {
        rt_.cps = cps0;
        rt_.env = env0;
        rt_.query = query0;
        rt_.cutPoint = cutPoint0;
        rt_.rems = rems0;
        rt_.trail.unwind(pit0);
        rt_.handler = handler0;
    };
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Node(suite.node.Node) BindEnv(suite.lp.doer.BinderFactory.BindEnv) Env(suite.lp.sewing.Env)

Example 30 with Node

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

the class SewingProverImpl method compileCpsRules.

private Cps compileCpsRules(Prototype prototype, List<Rule> rules, TraceLevel traceLevel) {
    Streamlet<Cps> cpss = Read.from(rules).map(rule -> {
        Generalizer generalizer = new Generalizer();
        Node head = generalizer.generalize(rule.head);
        Node tail = generalizer.generalize(rule.tail);
        return compileCpsRule(head, tail);
    });
    Cps cps0 = orCps(cpss);
    return saveEnvCps(cps0);
}
Also used : Generalizer(suite.lp.doer.Generalizer) 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