Search in sources :

Example 11 with Dict

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

the class Grapher method ungraph_.

private Node ungraph_(int id) {
    int size = gns.size();
    List<Node> nodes = // 
    Read.from(// 
    gns).map(gn -> {
        switch(gn.type) {
            case DICT:
                return new Dict();
            case TERM:
                return gn.terminal;
            case TREE:
                return Tree.of(gn.op, null, null);
            case TUPLE:
                return Tuple.of(new Node[gn.children.size()]);
            default:
                return Fail.t();
        }
    }).toList();
    for (int i = 0; i < size; i++) {
        GN gn = gns.get(i);
        Node node = nodes.get(i);
        List<Pair<Node, Node>> children = Read.from(gn.children).map(p -> Pair.of(nodes.get(p.t0), nodes.get(p.t1))).toList();
        switch(gn.type) {
            case DICT:
                ((Dict) node).map.putAll(Read.from2(children).mapValue(Reference::of).collect(As::map));
                break;
            case TERM:
                break;
            case TREE:
                Tree tree = (Tree) node;
                Tree.forceSetLeft(tree, children.get(0).t1);
                Tree.forceSetRight(tree, children.get(1).t1);
                break;
            case TUPLE:
                Node[] list = ((Tuple) node).nodes;
                for (int j = 0; j < children.size(); j++) list[j] = children.get(j).t1;
        }
    }
    return nodes.get(id);
}
Also used : DataInputStream(java.io.DataInputStream) Read(suite.streamlet.Read) HashMap(java.util.HashMap) Deque(java.util.Deque) ArrayList(java.util.ArrayList) Node(suite.node.Node) HashSet(java.util.HashSet) ProverConstant(suite.lp.doer.ProverConstant) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) IdentityKey(suite.adt.IdentityKey) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Dict(suite.node.Dict) Reference(suite.node.Reference) Trail(suite.lp.Trail) IntIntPair(suite.primitive.adt.pair.IntIntPair) Set(java.util.Set) IOException(java.io.IOException) NodeRead(suite.node.io.Rewrite_.NodeRead) NodeHead(suite.node.io.Rewrite_.NodeHead) IntObjMap(suite.primitive.adt.map.IntObjMap) Tree(suite.node.Tree) Objects(java.util.Objects) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Entry(java.util.Map.Entry) As(suite.streamlet.As) ArrayDeque(java.util.ArrayDeque) Int(suite.node.Int) Fail(suite.util.Fail) Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) Dict(suite.node.Dict) Reference(suite.node.Reference) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple) IntIntPair(suite.primitive.adt.pair.IntIntPair) Pair(suite.adt.pair.Pair)

Example 12 with Dict

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

the class FactorizeResult method rewrite.

public static FactorizeResult rewrite(FactorizeResult frfrom, FactorizeResult frto, FactorizeResult fr0) {
    Generalizer generalizer = new Generalizer();
    Iterate<Node> rewrite = n0 -> {
        Node[] m = Suite.pattern(FTerminal.class.getName() + ":.0").match(n0);
        Node n1 = m != null ? m[0] : null;
        Node n2 = n1 instanceof Dict ? ((Dict) n1).map.get(Atom.of("chars")) : null;
        Node n3 = n2 != null ? n2.finalNode() : null;
        String s = n3 instanceof Str ? ((Str) n3).value : null;
        boolean b = s != null && s.startsWith(ProverConstant.variablePrefix) && s.substring(1).matches("[0-9]*");
        return b ? generalizer.generalize(Atom.of(s)) : n0;
    };
    Fun<FactorizeResult, Node> parse = fr -> rw.rewrite(rewrite, nodify.nodify(FNode.class, fr.node));
    Node nodeFrom = parse.apply(frfrom);
    Node nodeTo = parse.apply(frto);
    FNode fn0 = fr0.node;
    Node node0 = nodify.nodify(FNode.class, fn0);
    Node nodex = rw.rewrite(nodeFrom, nodeTo, node0);
    FNode fnx = nodify.unnodify(FNode.class, nodex);
    return new FactorizeResult(fr0.pre, fnx, fr0.post);
}
Also used : Suite(suite.Suite) Singleton(suite.node.util.Singleton) Inspect(suite.inspect.Inspect) List_(suite.util.List_) To(suite.util.To) Chars(suite.primitive.Chars) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CharsBuilder(suite.primitive.Chars.CharsBuilder) Iterate(suite.util.FunUtil.Iterate) List(java.util.List) ProverConstant(suite.lp.doer.ProverConstant) Rewrite(suite.node.util.Rewrite) Atom(suite.node.Atom) Nodify(suite.util.Nodify) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Str(suite.node.Str) Str(suite.node.Str) Generalizer(suite.lp.doer.Generalizer) Dict(suite.node.Dict) Node(suite.node.Node)

Aggregations

Dict (suite.node.Dict)12 Node (suite.node.Node)9 Reference (suite.node.Reference)8 Str (suite.node.Str)8 Tree (suite.node.Tree)8 Atom (suite.node.Atom)7 List (java.util.List)6 Tuple (suite.node.Tuple)6 ArrayList (java.util.ArrayList)5 Read (suite.streamlet.Read)5 HashSet (java.util.HashSet)4 Pair (suite.adt.pair.Pair)4 ProverConstant (suite.lp.doer.ProverConstant)4 Int (suite.node.Int)4 TermOp (suite.node.io.TermOp)4 Entry (java.util.Map.Entry)3 Set (java.util.Set)3 Operator (suite.node.io.Operator)3 Chars (suite.primitive.Chars)3 Fun (suite.util.FunUtil.Fun)3