Search in sources :

Example 6 with Str

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

the class RecursiveFactorizerTest method operatorNode.

private Node operatorNode(Operator op, List<Node> nodes) {
    Str s = new Str("");
    Str name = new Str(op.toString());
    return treeNode(() -> s, name, nodes);
}
Also used : Str(suite.node.Str)

Example 7 with Str

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

the class RecursiveFactorizerTest method terminalNode.

private Node terminalNode(String s) {
    Dict dict = new Dict();
    dict.map.put(Atom.of("chars"), Reference.of(new Str(s)));
    return Tree.of(TermOp.COLON_, Atom.of(FTerminal.class.getName()), dict);
}
Also used : Str(suite.node.Str) Dict(suite.node.Dict)

Example 8 with Str

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

the class Binder method bind.

public static boolean bind(Node n0, Node n1, Trail trail) {
    n0 = n0.finalNode();
    n1 = n1.finalNode();
    if (n0 == n1)
        return true;
    Class<? extends Node> clazz0 = n0.getClass();
    Class<? extends Node> clazz1 = n1.getClass();
    if (clazz0 == Reference.class) {
        trail.addBind((Reference) n0, n1);
        return true;
    } else if (clazz1 == Reference.class) {
        trail.addBind((Reference) n1, n0);
        return true;
    }
    if (clazz0 == Dict.class && clazz1 == Dict.class) {
        Map<Node, Reference> map0 = ((Dict) n0).map;
        Map<Node, Reference> map1 = ((Dict) n1).map;
        boolean b = true;
        for (Node key : List_.concat(map0.keySet(), map1.keySet())) {
            Node v0 = map0.computeIfAbsent(key, k -> new Reference());
            Node v1 = map1.computeIfAbsent(key, k -> new Reference());
            b &= bind(v0, v1, trail);
        }
        return b;
    } else if (clazz0 == Int.class && clazz1 == Int.class)
        return ((Int) n0).number == ((Int) n1).number;
    else if (clazz0 == Str.class && clazz1 == Str.class)
        return Objects.equals(((Str) n0).value, ((Str) n1).value);
    else if (Tree.class.isAssignableFrom(clazz0) && Tree.class.isAssignableFrom(clazz1)) {
        Tree t0 = (Tree) n0;
        Tree t1 = (Tree) n1;
        return // 
        t0.getOperator() == t1.getOperator() && // 
        bind(t0.getLeft(), t1.getLeft(), trail) && bind(t0.getRight(), t1.getRight(), trail);
    } else if (clazz0 == Tuple.class && clazz1 == Tuple.class) {
        Node[] nodes0 = ((Tuple) n0).nodes;
        Node[] nodes1 = ((Tuple) n1).nodes;
        boolean b = nodes0.length == nodes1.length;
        if (b) {
            for (int i = 0; i < nodes0.length; i++) b &= bind(nodes0[i], nodes1[i], trail);
        }
        return b;
    } else
        return false;
}
Also used : Str(suite.node.Str) Reference(suite.node.Reference) Dict(suite.node.Dict) Node(suite.node.Node) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 9 with Str

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

the class Grapher method load.

public void load(DataInputStream dis) throws IOException {
    int size = dis.readInt();
    id = dis.readInt();
    for (int index = 0; index < size; index++) {
        ReadType type = ReadType.of(dis.readByte());
        Node terminal;
        Operator op;
        List<IntIntPair> children = new ArrayList<>();
        if (type == ReadType.TERM) {
            char ch = (char) dis.readByte();
            switch(ch) {
                case 'a':
                    terminal = Atom.of(dis.readUTF());
                    break;
                case 'i':
                    terminal = Int.of(dis.readInt());
                    break;
                case 'r':
                    terminal = new Reference();
                    break;
                case 's':
                    terminal = new Str(dis.readUTF());
                    break;
                default:
                    terminal = Fail.t("unknown type " + ch);
            }
        } else
            terminal = null;
        if (type == ReadType.TREE) {
            op = TermOp.find(dis.readUTF());
            children.add(IntIntPair.of(0, dis.readInt() + index));
            children.add(IntIntPair.of(0, dis.readInt() + index));
        } else
            op = null;
        if (type == ReadType.DICT || type == ReadType.TUPLE) {
            int size1 = dis.readInt();
            for (int i = 0; i < size1; i++) {
                int i0 = type != ReadType.DICT ? 0 : dis.readInt() + index;
                int i1 = dis.readInt() + index;
                children.add(IntIntPair.of(i0, i1));
            }
        }
        gns.add(new GN(type, terminal, op, children));
    }
}
Also used : Str(suite.node.Str) ReadType(suite.node.io.Rewrite_.ReadType) Reference(suite.node.Reference) Node(suite.node.Node) ArrayList(java.util.ArrayList) IntIntPair(suite.primitive.adt.pair.IntIntPair)

Example 10 with Str

use of suite.node.Str 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

Str (suite.node.Str)12 Node (suite.node.Node)8 ArrayList (java.util.ArrayList)5 Dict (suite.node.Dict)5 Atom (suite.node.Atom)4 Reference (suite.node.Reference)4 Tree (suite.node.Tree)4 List (java.util.List)3 HashSet (java.util.HashSet)2 Suite (suite.Suite)2 Inspect (suite.inspect.Inspect)2 Chars (suite.primitive.Chars)2 Fun (suite.util.FunUtil.Fun)2 FileReader (java.io.FileReader)1 Array (java.lang.reflect.Array)1 Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1