Search in sources :

Example 41 with Node

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

the class Rewrite_ method map.

public static Node map(Node node, Iterate<Node> fun) {
    NodeRead nr = NodeRead.of(node);
    List<Pair<Node, Node>> children1 = new ArrayList<>();
    boolean isSame = true;
    for (Pair<Node, Node> pair : nr.children) {
        Node child0 = pair.t1;
        Node childx = fun.apply(child0);
        if (child0 != childx) {
            isSame = false;
            children1.add(Pair.of(pair.t0, childx));
        } else
            children1.add(pair);
    }
    if (isSame)
        return node;
    else
        return new NodeWrite(nr.type, nr.terminal, nr.op, children1).node;
}
Also used : Node(suite.node.Node) ArrayList(java.util.ArrayList) Pair(suite.adt.pair.Pair)

Example 42 with Node

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

the class PrettyPrinter method prettyPrint_.

// op0 for avoiding unnecessary indenting; prec0 for parenthesizing
private void prettyPrint_(Node node, Operator op0, int prec0) {
    int x = getX(), y = getY();
    int length = lengthEstimator.getEstimatedLength(node);
    // line too long?
    if (node instanceof Tree) {
        Tree tree = (Tree) node;
        Operator op = tree.getOperator();
        int prec = op.getPrecedence();
        boolean isNeedPars = prec <= prec0;
        int parsIndent = 0, parsIndent0 = 0;
        if (isNeedPars) {
            parsIndent = currentLineIndent;
            parsIndent0 = incrementIndent();
            append("(");
        }
        if (lineLength < x + length)
            if (isLookingLikeList(op, node))
                prettyPrintList(op, node);
            else {
                Node left = tree.getLeft();
                Node right = tree.getRight();
                Assoc assoc = op.getAssoc();
                int leftPrec = prec - (assoc == Assoc.LEFT ? 1 : 0);
                int rightPrec = prec - (assoc == Assoc.RIGHT ? 1 : 0);
                if (op == TermOp.BRACES)
                    leftPrec = rightPrec = 0;
                Tree tree1 = Tree.decompose(right, op);
                Node r0 = tree1 != null ? tree1.getLeft() : null;
                int es0 = lengthEstimator.getEstimatedLength(left);
                int es1 = r0 != null ? lengthEstimator.getEstimatedLength(r0) : lineLength;
                int opLength = op.getName().length();
                // breaks "a + b + xxx" in the second operator
                if (// 
                assoc == Assoc.RIGHT && // 
                x + es0 + es1 + opLength < lineLength && r0 != preferLineBreakBeforeKeyword) {
                    prettyPrint_(left, op, leftPrec);
                    OperatorPosition opPos = appendOperator(op);
                    prettyPrint_(right, op, rightPrec);
                    closeBraces(op, opPos);
                } else {
                    // breaks after the operator
                    boolean isIncRightIndent = op != op0;
                    int indent0 = 0;
                    prettyPrint_(left, op, leftPrec);
                    if (isIncRightIndent)
                        indent0 = incrementIndent();
                    OperatorPosition opPos;
                    if (getLineSize() + lengthEstimator.getEstimatedLength(right) < squeezeLineLength)
                        opPos = appendOperator(op);
                    else
                        opPos = appendOperatorLineFeed(op);
                    prettyPrint_(right, op, rightPrec);
                    closeBraces(op, opPos);
                    if (isIncRightIndent)
                        revertIndent(indent0);
                }
            }
        else
            append(Formatter.dump(node));
        if (isNeedPars) {
            if (y != getY())
                nl(parsIndent);
            append(")");
            revertIndent(parsIndent0);
        }
    } else {
        if (node == lineBreakBeforeKeyword && !isLineBegin())
            nl();
        // space sufficient
        append(Formatter.dump(node));
    }
}
Also used : Operator(suite.node.io.Operator) Node(suite.node.Node) Tree(suite.node.Tree) Assoc(suite.node.io.Operator.Assoc)

Example 43 with Node

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

the class Chr method chrIf.

private Streamlet<State> chrIf(Streamlet<State> states, Trail trail, Node if_) {
    Prototype prototype = Prototype.of(if_);
    Fun<State, Streamlet<State>> fun = state -> {
        ISet<Node> facts = getFacts(state, prototype);
        Predicate<Node> bindFun = bindFun(trail, if_);
        return facts.streamlet().filter(bindFun).map(node -> setFacts(state, prototype, facts.remove(node)));
    };
    return states.concatMap(fun);
}
Also used : Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Prover(suite.lp.doer.Prover) Read(suite.streamlet.Read) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IMap(suite.immutable.IMap) ISet(suite.immutable.ISet) TermOp(suite.node.io.TermOp) To(suite.util.To) Fun(suite.util.FunUtil.Fun) Tree(suite.node.Tree) ArrayList(java.util.ArrayList) Node(suite.node.Node) Pair(suite.adt.pair.Pair) List(java.util.List) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Prototype(suite.lp.kb.Prototype) Binder(suite.lp.doer.Binder) Generalizer(suite.lp.doer.Generalizer) Fail(suite.util.Fail) Prototype(suite.lp.kb.Prototype) Streamlet(suite.streamlet.Streamlet) ISet(suite.immutable.ISet) Predicate(java.util.function.Predicate)

Example 44 with Node

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

the class Chr method chr.

private Streamlet<State> chr(State state, Rule rule) {
    Generalizer generalizer = new Generalizer();
    Trail trail = new Trail();
    Streamlet<State> states = Read.each(state);
    for (Node if_ : rule.ifs) states = chrIf(states, trail, generalizer.generalize(if_));
    for (Node given : rule.givens) states = chrGiven(states, trail, generalizer.generalize(given));
    states = chrWhen(states, generalizer.generalize(rule.when));
    for (Node then : rule.thens) states = chrThen(states, generalizer.generalize(then));
    return states;
}
Also used : Generalizer(suite.lp.doer.Generalizer) Trail(suite.lp.Trail) Node(suite.node.Node)

Example 45 with Node

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

the class Chr method chr.

public Collection<Node> chr(Collection<Node> facts) {
    State state = new State(IMap.empty());
    for (Node fact : facts) {
        Prototype prototype = Prototype.of(fact);
        state = setFacts(state, prototype, getFacts(state, prototype).replace(fact));
    }
    State state1;
    while ((state1 = chr(state)) != null) state = state1;
    List<Node> nodes1 = new ArrayList<>();
    for (Pair<Prototype, ISet<Node>> e : state.factsByPrototype) nodes1.addAll(To.list(e.t1));
    return nodes1;
}
Also used : Prototype(suite.lp.kb.Prototype) Node(suite.node.Node) ArrayList(java.util.ArrayList) ISet(suite.immutable.ISet)

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