Search in sources :

Example 1 with LinkedOp

use of uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp in project mkgmap by openstreetmap.

the class ExpressionArrangerTest method testPrepareOr.

@Test
public void testPrepareOr() {
    Op op = createOp("a=3 | b < 2 | c=*");
    op = arranger.arrange(op);
    Iterator<Op> it = arranger.prepareForSave(op);
    boolean first = true;
    while (it.hasNext()) {
        Op o = it.next();
        assertTrue("Prepared OR should all be LinkedOp's", o instanceof LinkedOp);
        assertEquals("Only first should have first flag set", first, ((LinkedOp) o).isFirstPart());
        first = false;
        assertTrue(isSolved(o));
        System.out.println(o);
    }
}
Also used : Op(uk.me.parabola.mkgmap.osmstyle.eval.Op) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) Test(org.junit.Test)

Example 2 with LinkedOp

use of uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp in project mkgmap by openstreetmap.

the class ExpressionArrangerTest method testPrepareOrSimple.

@Test
public void testPrepareOrSimple() {
    // Just one or
    Op op = createOp("a=3 | b < 2");
    op = arranger.arrange(op);
    Iterator<Op> it = arranger.prepareForSave(op);
    boolean first = true;
    while (it.hasNext()) {
        Op o = it.next();
        assertTrue("Prepared OR should all be LinkedOp's", o instanceof LinkedOp);
        assertEquals("Only first should have first flag set", first, ((LinkedOp) o).isFirstPart());
        first = false;
        assertTrue(isSolved(o));
        System.out.println(o);
    }
}
Also used : Op(uk.me.parabola.mkgmap.osmstyle.eval.Op) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) Test(org.junit.Test)

Example 3 with LinkedOp

use of uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp in project mkgmap by openstreetmap.

the class RuleSet method compileOp.

private Op compileOp(HashMap<String, Op> tests, Op op) {
    if (op instanceof AbstractBinaryOp) {
        AbstractBinaryOp binOp = (AbstractBinaryOp) op;
        binOp.setFirst(compileOp(tests, binOp.getFirst()));
        binOp.setSecond(compileOp(tests, binOp.getSecond()));
    }
    if (op instanceof LinkedOp) {
        // LinkedOp is referenced by other OPs, don't replace it
        return op;
    }
    String test = op.toString();
    Op commonOp = tests.get(test);
    if (commonOp == null) {
        if (op instanceof AbstractOp)
            ((AbstractOp) op).resetCache();
        tests.put(test, op);
        commonOp = op;
    }
    return commonOp;
}
Also used : LinkedBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedBinaryOp) AbstractBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractBinaryOp) AbstractOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp) Op(uk.me.parabola.mkgmap.osmstyle.eval.Op) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) AbstractOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) AbstractBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractBinaryOp)

Example 4 with LinkedOp

use of uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp in project mkgmap by openstreetmap.

the class RuleSet method compile.

/**
 * Compile the rules and reset caches. Detect common sub-expressions and
 * make sure that all rules use the same instance of these common
 * sub-expressions.
 */
private void compile() {
    HashMap<String, Op> tests = new HashMap<String, Op>();
    for (Rule rule : rules) {
        Op op;
        if (rule instanceof ExpressionRule)
            op = ((ExpressionRule) rule).getOp();
        else if (rule instanceof ActionRule)
            op = ((ActionRule) rule).getOp();
        else {
            log.error("unexpected rule instance");
            continue;
        }
        if (op instanceof AbstractBinaryOp) {
            AbstractBinaryOp binOp = (AbstractBinaryOp) op;
            binOp.setFirst(compileOp(tests, binOp.getFirst()));
            binOp.setSecond(compileOp(tests, binOp.getSecond()));
            op = compileOp(tests, binOp);
        } else if (op instanceof AbstractOp) {
            op = compileOp(tests, op);
        } else if (op instanceof LinkedBinaryOp) {
            ((LinkedBinaryOp) op).setFirst(compileOp(tests, ((LinkedBinaryOp) op).getFirst()));
            ((LinkedBinaryOp) op).setSecond(compileOp(tests, ((LinkedBinaryOp) op).getSecond()));
        } else if (op instanceof LinkedOp) {
            Op wrappedOp = compileOp(tests, ((LinkedOp) op).getFirst());
            op.setFirst(wrappedOp);
        } else {
            log.error("unexpected op instance");
            continue;
        }
        if (rule instanceof ExpressionRule)
            ((ExpressionRule) rule).setOp(op);
        else if (rule instanceof ActionRule)
            ((ActionRule) rule).setOp(op);
        else {
            log.error("unexpected rule instance");
            continue;
        }
    }
    cacheId = 0;
    compiled = true;
}
Also used : LinkedBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedBinaryOp) AbstractBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractBinaryOp) AbstractOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp) Op(uk.me.parabola.mkgmap.osmstyle.eval.Op) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) AbstractOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp) HashMap(java.util.HashMap) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) AbstractBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractBinaryOp) LinkedBinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedBinaryOp) Rule(uk.me.parabola.mkgmap.reader.osm.Rule)

Example 5 with LinkedOp

use of uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp in project mkgmap by openstreetmap.

the class ExpressionArranger method prepareForSave.

/**
 * Prepare this expression for saving.
 *
 * If necessary we combine with an exists clause.
 *
 * The main work is if this is an OR, we have to split it up and
 * prepare each term separately.
 */
public Iterator<Op> prepareForSave(Op op) {
    List<Op> saveList = new ArrayList<>();
    switch(op.getType()) {
        case AND:
        default:
            saveList.add(prepareWithExists(op));
            break;
        case OR:
            Op last = op;
            LinkedOp prev = null;
            for (Op second = op; second != null && second.isType(OR); second = second.getSecond()) {
                Op term = second.getFirst();
                LinkedOp lop = LinkedOp.create(prepareWithExists(term), second == op);
                if (prev != null)
                    prev.setLink(lop);
                prev = lop;
                saveList.add(lop);
                last = second;
            }
            LinkedOp lop = LinkedOp.create(prepareWithExists(last.getSecond()), false);
            if (prev != null)
                prev.setLink(lop);
            saveList.add(lop);
            break;
    }
    return saveList.iterator();
}
Also used : NotExistsOp(uk.me.parabola.mkgmap.osmstyle.eval.NotExistsOp) AbstractOp(uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp) Op(uk.me.parabola.mkgmap.osmstyle.eval.Op) GTEOp(uk.me.parabola.mkgmap.osmstyle.eval.GTEOp) ValueOp(uk.me.parabola.mkgmap.osmstyle.eval.ValueOp) LTEOp(uk.me.parabola.mkgmap.osmstyle.eval.LTEOp) LTOp(uk.me.parabola.mkgmap.osmstyle.eval.LTOp) NotEqualOp(uk.me.parabola.mkgmap.osmstyle.eval.NotEqualOp) EqualsOp(uk.me.parabola.mkgmap.osmstyle.eval.EqualsOp) RegexOp(uk.me.parabola.mkgmap.osmstyle.eval.RegexOp) ExistsOp(uk.me.parabola.mkgmap.osmstyle.eval.ExistsOp) BinaryOp(uk.me.parabola.mkgmap.osmstyle.eval.BinaryOp) GTOp(uk.me.parabola.mkgmap.osmstyle.eval.GTOp) NotRegexOp(uk.me.parabola.mkgmap.osmstyle.eval.NotRegexOp) OrOp(uk.me.parabola.mkgmap.osmstyle.eval.OrOp) AndOp(uk.me.parabola.mkgmap.osmstyle.eval.AndOp) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) LinkedOp(uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp) ArrayList(java.util.ArrayList)

Aggregations

LinkedOp (uk.me.parabola.mkgmap.osmstyle.eval.LinkedOp)5 Op (uk.me.parabola.mkgmap.osmstyle.eval.Op)5 AbstractOp (uk.me.parabola.mkgmap.osmstyle.eval.AbstractOp)3 Test (org.junit.Test)2 AbstractBinaryOp (uk.me.parabola.mkgmap.osmstyle.eval.AbstractBinaryOp)2 LinkedBinaryOp (uk.me.parabola.mkgmap.osmstyle.eval.LinkedBinaryOp)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AndOp (uk.me.parabola.mkgmap.osmstyle.eval.AndOp)1 BinaryOp (uk.me.parabola.mkgmap.osmstyle.eval.BinaryOp)1 EqualsOp (uk.me.parabola.mkgmap.osmstyle.eval.EqualsOp)1 ExistsOp (uk.me.parabola.mkgmap.osmstyle.eval.ExistsOp)1 GTEOp (uk.me.parabola.mkgmap.osmstyle.eval.GTEOp)1 GTOp (uk.me.parabola.mkgmap.osmstyle.eval.GTOp)1 LTEOp (uk.me.parabola.mkgmap.osmstyle.eval.LTEOp)1 LTOp (uk.me.parabola.mkgmap.osmstyle.eval.LTOp)1 NotEqualOp (uk.me.parabola.mkgmap.osmstyle.eval.NotEqualOp)1 NotExistsOp (uk.me.parabola.mkgmap.osmstyle.eval.NotExistsOp)1 NotRegexOp (uk.me.parabola.mkgmap.osmstyle.eval.NotRegexOp)1 OrOp (uk.me.parabola.mkgmap.osmstyle.eval.OrOp)1