Search in sources :

Example 6 with Atom

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

the class SewingProverImpl method traceLevel.

private TraceLevel traceLevel(Prototype prototype) {
    TraceLevel traceLevel;
    if (Suite.isProverTrace) {
        Node head = prototype.head;
        String name = head instanceof Atom ? ((Atom) head).name : null;
        traceLevel = // 
        name != null && // 
        !name.startsWith("member") && !name.startsWith("rbt-") ? TraceLevel.TRACE : TraceLevel.NONE;
    } else
        traceLevel = TraceLevel.NONE;
    return traceLevel;
}
Also used : Node(suite.node.Node) Atom(suite.node.Atom)

Example 7 with Atom

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

the class SewingProverImpl method compileCps.

private Cps compileCps(BinderFactory bf, Node node, Cps cpsx) {
    List<Node> list;
    Tree tree;
    Node[] m;
    Cps cps;
    if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size()) {
        cps = cpsx;
        for (Node n : List_.reverse(list)) cps = compileCps(bf, n, cps);
    } else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
        cps = orCps(Read.from(list).map(n -> compileCps(bf, n, cpsx)));
    else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
        boolean b = complexity(m[0]) <= complexity(m[1]);
        Node n0 = b ? m[0] : m[1];
        Node n1 = b ? m[1] : m[0];
        Bind_ p = bf.binder(n1);
        Clone_ f = bf.cloner(n0);
        cps = rt -> p.test(rt, f.apply(rt.env)) ? cpsx : null;
    } else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
        cps = compileCpsCallPredicate(bf, ((Atom) m[0]).name, m[1], node, cpsx);
    else if (node instanceof Atom) {
        String name = ((Atom) node).name;
        if (String_.equals(name, ""))
            cps = cpsx;
        else if (String_.equals(name, "fail"))
            cps = rt -> null;
        else
            cps = compileCpsCallPredicate(bf, name, Atom.NIL, node, cpsx);
    } else if (node instanceof Reference) {
        Clone_ f = bf.cloner(node);
        cps = rt -> compileCps(passThru, f.apply(rt.env), cpsx);
    } else if ((tree = Tree.decompose(node)) != null)
        cps = compileCpsCallPredicate(bf, tree.getOperator().getName(), node, node, cpsx);
    else if (node instanceof Tuple)
        cps = compileCpsCallPredicate(bf, node, cpsx);
    else
        cps = Fail.t("cannot understand " + node);
    return cps;
}
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) Reference(suite.node.Reference) Node(suite.node.Node) Atom(suite.node.Atom) Bind_(suite.lp.doer.BinderFactory.Bind_) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 8 with Atom

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

the class SewingProverTest method testPerformance.

@Test
public void testPerformance() {
    RuleSet rs = Suite.newRuleSet();
    Atom pred = Atom.of("q");
    Atom tail = Atom.NIL;
    for (int i = 0; i < 65536; i++) rs.addRule(Rule.of(Tree.of(TermOp.IS____, Tree.of(TermOp.TUPLE_, pred, Int.of(i)), tail)));
    ProverFactory sp = new SewingProverImpl(rs);
    ProverConfig pc = new ProverConfig(rs);
    Prove_ test = sp.prover(Suite.parse("q 32768"));
    Source<Stopwatch<Boolean>> trial = () -> Stopwatch.of(() -> {
        boolean isOk = true;
        for (int i = 0; i < 65536; i++) isOk &= test.test(pc);
        assertTrue(isOk);
        return isOk;
    });
    for (int i = 0; i < 8; i++) trial.source();
    Stopwatch<Boolean> sw = trial.source();
    System.out.println(sw.duration);
    assertTrue(sw.duration < 300);
}
Also used : RuleSet(suite.lp.kb.RuleSet) ProverConfig(suite.lp.Configuration.ProverConfig) Stopwatch(suite.os.Stopwatch) Prove_(suite.lp.doer.ProverFactory.Prove_) Atom(suite.node.Atom) ProverFactory(suite.lp.doer.ProverFactory) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) Test(org.junit.Test)

Example 9 with Atom

use of suite.node.Atom 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 10 with Atom

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

the class Context method findAtom.

public synchronized Atom findAtom(String key, Fun<String, Atom> fun) {
    Atom atom;
    WeakReference<Atom> ref = atomPool.get(key);
    if (ref == null || (atom = ref.get()) == null)
        atomPool.put(key, new WeakReference<>(atom = fun.apply(key)));
    return atom;
}
Also used : WeakReference(java.lang.ref.WeakReference) Atom(suite.node.Atom)

Aggregations

Atom (suite.node.Atom)22 Node (suite.node.Node)17 Reference (suite.node.Reference)11 Tree (suite.node.Tree)11 TermOp (suite.node.io.TermOp)9 ArrayList (java.util.ArrayList)7 Pair (suite.adt.pair.Pair)7 Int (suite.node.Int)7 List (java.util.List)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Suite (suite.Suite)5 Generalizer (suite.lp.doer.Generalizer)5 Prototype (suite.lp.kb.Prototype)5 RuleSet (suite.lp.kb.RuleSet)5 Data (suite.node.Data)5 TreeUtil (suite.node.util.TreeUtil)5 Read (suite.streamlet.Read)5 Fail (suite.util.Fail)5 Operator (suite.node.io.Operator)4