Search in sources :

Example 1 with Data

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

the class Intrinsics method drain.

public static Node drain(IntrinsicCallback callback, IPointer<Node> pointer) {
    Intrinsic drain = new Intrinsic() {

        public Node invoke(IntrinsicCallback callback1, List<Node> inputs) {
            IPointer<Node> pointer1 = Data.get(inputs.get(0));
            Node head;
            if ((head = pointer1.head()) != null) {
                Node left = callback1.enclose(Intrinsics.id_, head);
                Node right = callback1.enclose(this::invoke, new Data<>(pointer1.tail()));
                return Tree.of(TermOp.OR____, left, right);
            } else
                return Atom.NIL;
        }
    };
    return callback.yawn(callback.enclose(drain, new Data<>(pointer)));
}
Also used : Node(suite.node.Node) List(java.util.List) Data(suite.node.Data)

Example 2 with Data

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

the class ProveTracer method expandWithTrace.

public Node expandWithTrace(Node query, Prover prover, Iterate<Node> expand) {
    Node query1 = new Cloner().clone(query);
    if (currentDepth < 64) {
        Record record0 = currentRecord;
        int depth0 = currentDepth;
        Record record = new Record(record0, query1, currentDepth + 1);
        Data<Source<Boolean>> enter = new Data<>(() -> {
            currentRecord = record;
            currentDepth = record.depth;
            record.start = records.size();
            records.add(record);
            return Boolean.TRUE;
        });
        Data<Source<Boolean>> leaveOk = new Data<>(() -> {
            currentRecord = record0;
            currentDepth = depth0;
            record.nOkays++;
            return Boolean.TRUE;
        });
        Data<Source<Boolean>> leaveFail = new Data<>(() -> {
            currentRecord = record0;
            currentDepth = depth0;
            record.end = records.size();
            return Boolean.FALSE;
        });
        Node alt = prover.getAlternative();
        Node rem = prover.getRemaining();
        prover.setAlternative(Tree.of(TermOp.OR____, leaveFail, alt));
        prover.setRemaining(Tree.of(TermOp.AND___, leaveOk, rem));
        query = expand.apply(query);
        query = Tree.of(TermOp.AND___, enter, query);
    } else
        query = expand.apply(query);
    return query;
}
Also used : Node(suite.node.Node) Data(suite.node.Data) Source(suite.util.FunUtil.Source)

Example 3 with Data

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

the class Prover method prove0.

public boolean prove0(Node query) {
    rem = OK;
    alt = FAIL;
    while (true) {
        // logUtil.info(Formatter.dump(query));
        query = query.finalNode();
        if (query instanceof Tree) {
            Tree tree = (Tree) query;
            Node left = tree.getLeft(), right = tree.getRight();
            switch((TermOp) tree.getOperator()) {
                case OR____:
                    int pit = trail.getPointInTime();
                    Node bt = new Data<Source<Boolean>>(() -> {
                        trail.unwind(pit);
                        return Boolean.TRUE;
                    });
                    alt = andTree(bt, orTree(andTree(right, rem), alt));
                    query = left;
                    continue;
                case AND___:
                    rem = andTree(right, rem);
                    query = left;
                    continue;
                case EQUAL_:
                    query = isSuccess(bind(left, right));
                    break;
                default:
            }
        } else if (query instanceof Data) {
            query = isSuccess(Data.<Source<Boolean>>get(query).source());
            continue;
        }
        Boolean b = systemPredicates.call(query);
        if (b != null)
            query = isSuccess(b);
        // not handled above
        if (query == OK)
            if (rem != OK) {
                query = rem;
                rem = OK;
            } else
                return true;
        else if (query == FAIL)
            if (alt != FAIL) {
                query = alt;
                alt = FAIL;
                rem = OK;
            } else
                return false;
        else {
            boolean isTrace = config.isTrace();
            if (isTrace) {
                Set<String> whites = Suite.tracePredicates;
                Set<String> blacks = Suite.noTracePredicates;
                Prototype prototype = Prototype.of(query);
                Node head = prototype != null ? prototype.head : null;
                Atom atom = head instanceof Atom ? (Atom) head : null;
                String name = atom != null ? atom.name : null;
                isTrace &= whites == null || whites.contains(name);
                isTrace &= blacks == null || !blacks.contains(name);
            }
            if (!isTrace)
                query = expand(query);
            else
                query = tracer.expandWithTrace(query, this, this::expand);
        }
    }
}
Also used : Set(java.util.Set) RuleSet(suite.lp.kb.RuleSet) Prototype(suite.lp.kb.Prototype) TermOp(suite.node.io.TermOp) Node(suite.node.Node) Tree(suite.node.Tree) Data(suite.node.Data) Source(suite.util.FunUtil.Source) Atom(suite.node.Atom)

Example 4 with Data

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

the class Formatter method format_.

private void format_(Node node, int parentPrec) {
    new // 
    SwitchNode<Node>(// 
    node).doIf(Atom.class, n -> {
        sb.append(quoteAtomIfRequired(n.name));
    }).doIf(Data.class, n -> {
        Object data = n.data;
        if (data instanceof Chars)
            sb.append("Chars<" + quoteStringIfRequired(data.toString()) + ">");
        else if (data instanceof Node)
            sb.append("Data<" + data.toString() + ">");
        else
            sb.append("Data<" + data.getClass().getSimpleName() + ">");
    }).doIf(Dict.class, n -> {
        sb.append("dict<");
        for (Entry<Node, Reference> e : n.map.entrySet()) {
            format(e.getKey(), TermOp.getLeftPrec(TermOp.AND___));
            sb.append(":");
            format(e.getValue(), TermOp.getLeftPrec(TermOp.AND___));
            sb.append(",");
        }
        sb.append(">");
    }).doIf(Int.class, n -> {
        sb.append(n.number);
    }).doIf(Reference.class, n -> {
        sb.append(n.name());
    }).doIf(Str.class, n -> {
        sb.append(quoteStringIfRequired(n.value));
    }).doIf(Tree.class, n -> {
        formatTree(parentPrec, n);
    }).doIf(Tuple.class, n -> {
        sb.append("tuple<");
        for (Node n_ : n.nodes) {
            format(n_, TermOp.getLeftPrec(TermOp.AND___));
            sb.append(", ");
        }
        sb.append(">");
    }).doIf(Node.class, n -> {
        sb.append(n.getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode(n)));
    }).nonNullResult();
}
Also used : Reference(suite.node.Reference) Set(java.util.Set) Chars(suite.primitive.Chars) Tree(suite.node.Tree) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) Data(suite.node.Data) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Atom(suite.node.Atom) Entry(java.util.Map.Entry) Int(suite.node.Int) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Str(suite.node.Str) Entry(java.util.Map.Entry) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Chars(suite.primitive.Chars) Atom(suite.node.Atom)

Example 5 with Data

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

the class CommandDispatcher method elaborate.

private void elaborate(Node node0, Sink<Node> sink) {
    int[] count = { 0 };
    NodeEnv<Atom> ne = new CompileGeneralizerImpl().g(node0).source();
    Node node1 = ne.node;
    Node elab = new Data<Source<Boolean>>(() -> {
        String dump = ne.dumpVariables();
        if (!dump.isEmpty())
            opt.prompt().println(dump);
        count[0]++;
        return Boolean.FALSE;
    });
    sink.sink(Tree.of(TermOp.AND___, node1, elab));
    if (count[0] == 1)
        opt.prompt().println(count[0] + " solution\n");
    else
        opt.prompt().println(count[0] + " solutions\n");
}
Also used : CompileGeneralizerImpl(suite.lp.compile.impl.CompileGeneralizerImpl) Node(suite.node.Node) Data(suite.node.Data) Atom(suite.node.Atom)

Aggregations

Data (suite.node.Data)8 Node (suite.node.Node)8 Atom (suite.node.Atom)5 Source (suite.util.FunUtil.Source)5 Tree (suite.node.Tree)4 List (java.util.List)3 RuleSet (suite.lp.kb.RuleSet)3 Int (suite.node.Int)3 TermOp (suite.node.io.TermOp)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 Suite (suite.Suite)2 Pair (suite.adt.pair.Pair)2 Formatter (suite.node.io.Formatter)2 TreeUtil (suite.node.util.TreeUtil)2 Fail (suite.util.Fail)2 HashSet (java.util.HashSet)1