Search in sources :

Example 1 with Literal

use of org.apache.asterix.external.classad.Literal in project asterixdb by apache.

the class ClassAdUnitTester method testExprList.

/*********************************************************************
     * Function: test_exprlist
     * Purpose: Test the ExprList class.
     *
     * @throws IOException
     *********************************************************************/
public static void testExprList(Parameters parameters, Results results, ClassAdObjectPool objectPool) throws IOException {
    System.out.println("Testing the ExprList class...");
    Literal literal10;
    Literal literal20;
    Literal literal21;
    List<ExprTree> vector1 = new ArrayList<ExprTree>();
    List<ExprTree> vector2 = new ArrayList<ExprTree>();
    ExprList list0;
    ExprList list0Copy;
    ExprList list1;
    ExprList list1Copy;
    ExprList list2;
    ExprList list2Copy;
    /* ----- Setup Literals, the vectors, then ExprLists ----- */
    literal10 = Literal.createReal("1.0", objectPool);
    literal20 = Literal.createReal("2.0", objectPool);
    literal21 = Literal.createReal("2.1", objectPool);
    vector1.add(literal10);
    vector2.add(literal20);
    vector2.add(literal21);
    list0 = new ExprList(objectPool);
    list1 = new ExprList(vector1, objectPool);
    list2 = new ExprList(vector2, objectPool);
    /* ----- Did the lists get made? ----- */
    test("Made list 0", (list0 != null), "Did the lists get made? 0", results);
    test("Made list 1", (list1 != null), "Did the lists get made? 1", results);
    test("Made list 2", (list2 != null), "Did the lists get made? 2", results);
    /* ----- Are these lists identical to themselves? ----- */
    test("ExprList identical 0", list0.sameAs(list0), "Are these lists identical to themselves? 0", results);
    test("ExprList identical 1", list1.sameAs(list1), "Are these lists identical to themselves? 1", results);
    test("ExprList identical 2", list2.sameAs(list2), "Are these lists identical to themselves? 2", results);
    /* ----- Are they different from each other? ----- */
    test("ExprLists different 0-1", !(list0.sameAs(list1)), "Are these lists different from each other? 0", results);
    test("ExprLists different 1-2", !(list1.sameAs(list2)), "Are these lists identical from each other? 1", results);
    test("ExprLists different 0-2", !(list0.sameAs(list2)), "Are these lists identical from each other? 2", results);
    /* ----- Check the size of the ExprLists to make sure they are ok ----- */
    test("ExprList size 0", (list0.size() == 0), "check list size? 0", results);
    test("ExprList size 1", (list1.size() == 1), "check list size? 1", results);
    test("ExprList size 2", (list2.size() == 2), "check list size? 2", results);
    /* ----- Make copies of the ExprLists ----- */
    list0Copy = (ExprList) list0.copy();
    list1Copy = (ExprList) list1.copy();
    list2Copy = (ExprList) list2.copy();
    /* ----- Did the copies get made? ----- */
    test("Made copy of list 0", (list0Copy != null), "Did the copies get made? 0", results);
    test("Made copy of list 1", (list1Copy != null), "Did the copies get made? 1", results);
    test("Made copy of list 2", (list2Copy != null), "Did the copies get made? 2", results);
    /* ----- Are they identical to the originals? ----- */
    test("ExprList self-identity 0", (list0.sameAs(list0Copy)), "Are they identical to the originals? 0", results);
    test("ExprList self-identity 1", (list1.sameAs(list1Copy)), "Are they identical to the originals? 1", results);
    test("ExprList self-identity 2", (list2.sameAs(list2Copy)), "Are they identical to the originals? 2", results);
    /* ----- Test adding and deleting from a list ----- */
    Literal add;
    add = Literal.createReal("2.2", objectPool);
    if (list2Copy != null) {
        list2Copy.insert(add);
        test("Edited list is different", !(list2.sameAs(list2Copy)), "Test adding and deleting from a list 0", results);
        list2Copy.erase(list2Copy.size() - 1);
        test("Twice Edited list is same", (list2.sameAs(list2Copy)), "Test adding and deleting from a list 1", results);
    }
    // Note that we do not delete the Literals that we created, because
    // they should have been deleted when the list was deleted.
    /* ----- Test an ExprList bug that Nate Mueller found ----- */
    ClassAd classad;
    ClassAdParser parser = new ClassAdParser(objectPool);
    MutableBoolean b = new MutableBoolean();
    boolean haveAttribute;
    boolean canEvaluate;
    Value value = new Value(objectPool);
    String listClassadText = "[foo = 3; have_foo = member(foo, {1, 2, 3});]";
    classad = parser.parseClassAd(listClassadText);
    haveAttribute = classad.evaluateAttrBool("have_foo", b);
    test("Can evaluate list in member function", (haveAttribute == true && b.booleanValue() == true), "Test an ExprList bug that Nate Mueller found 0", results);
    canEvaluate = classad.evaluateExpr("member(foo, {1, 2, blah, 3})", value);
    test("Can evaluate list in member() outside of ClassAd", canEvaluate == true, "Test an ExprList bug that Nate Mueller found 1", results);
    return;
}
Also used : ClassAdParser(org.apache.asterix.external.library.ClassAdParser) ClassAd(org.apache.asterix.external.classad.ClassAd) ExprList(org.apache.asterix.external.classad.ExprList) Literal(org.apache.asterix.external.classad.Literal) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) ArrayList(java.util.ArrayList) Value(org.apache.asterix.external.classad.Value) ExprTree(org.apache.asterix.external.classad.ExprTree) AMutableString(org.apache.asterix.om.base.AMutableString) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString)

Example 2 with Literal

use of org.apache.asterix.external.classad.Literal in project asterixdb by apache.

the class ClassAdParser method asterixParseClassAd.

private boolean asterixParseClassAd(ClassAd ad) throws IOException {
    TokenType tt;
    ad.clear();
    lexer.initialize(currentSource);
    if ((tt = lexer.consumeToken()) != TokenType.LEX_OPEN_BOX) {
        handleErrorParsing();
        return false;
    }
    tt = lexer.peekToken();
    TokenValue tv = objectPool.tokenValuePool.get();
    ExprTreeHolder tree = objectPool.mutableExprPool.get();
    while (tt != TokenType.LEX_CLOSE_BOX) {
        // Get the name of the expression
        tv.reset();
        tree.reset();
        tt = lexer.consumeToken(tv);
        if (tt == TokenType.LEX_SEMICOLON) {
            // that was bitten by this.
            continue;
        }
        if (tt != TokenType.LEX_IDENTIFIER) {
            throw new HyracksDataException("while parsing classad:  expected LEX_IDENTIFIER " + " but got " + Lexer.strLexToken(tt));
        }
        // consume the intermediate '='
        if ((tt = lexer.consumeToken()) != TokenType.LEX_BOUND_TO) {
            throw new HyracksDataException("while parsing classad:  expected LEX_BOUND_TO " + " but got " + Lexer.strLexToken(tt));
        }
        int positionBefore = lexer.getLexSource().getPosition();
        isExpr = false;
        // parse the expression
        parseExpression(tree);
        if (tree.getInnerTree() == null) {
            handleErrorParsing();
            throw new HyracksDataException("parse expression returned empty tree");
        }
        if ((!evaluateExpr || keepBoth) && isExpr && positionBefore >= 0) {
            // we will store a string representation of the expression
            int len = lexer.getLexSource().getPosition() - positionBefore - 2;
            // add it as it is to the classAd
            Literal lit = objectPool.literalPool.get();
            Value exprVal = objectPool.valuePool.get();
            exprVal.setStringValue((exprPrefix == null ? "" : exprPrefix) + String.valueOf(lexer.getLexSource().getBuffer(), positionBefore, len) + (exprSuffix == null ? "" : exprSuffix));
            Literal.createLiteral(lit, exprVal, NumberFactor.NO_FACTOR);
            if (!evaluateExpr) {
                ad.insert(tv.getStrValue().toString(), lit);
            } else {
                ad.insert(tv.getStrValue().toString() + exprFieldNameSuffix, lit);
            }
        }
        if (!isExpr || (evaluateExpr)) {
            // insert the attribute into the classad
            if (!ad.insert(tv.getStrValue().toString(), tree)) {
                handleErrorParsing();
                throw new HyracksDataException("Couldn't insert value to classad");
            }
        }
        // the next token must be a ';' or a ']'
        tt = lexer.peekToken();
        if (tt != TokenType.LEX_SEMICOLON && tt != TokenType.LEX_CLOSE_BOX) {
            handleErrorParsing();
            throw new HyracksDataException("while parsing classad:  expected LEX_SEMICOLON or " + "LEX_CLOSE_BOX but got " + Lexer.strLexToken(tt));
        }
        // while the first case accounts for optional beginning semicolons.
        while (tt == TokenType.LEX_SEMICOLON) {
            lexer.consumeToken();
            tt = lexer.peekToken();
        }
    }
    return true;
}
Also used : TokenType(org.apache.asterix.external.classad.Lexer.TokenType) ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) Literal(org.apache.asterix.external.classad.Literal) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) TokenValue(org.apache.asterix.external.classad.TokenValue)

Example 3 with Literal

use of org.apache.asterix.external.classad.Literal in project asterixdb by apache.

the class ClassAdParser method evaluateFunction.

public ExprTree evaluateFunction(String functionName, ExprList argList) throws HyracksDataException {
    Value val = objectPool.valuePool.get();
    AMutableNumberFactor factor = objectPool.numFactorPool.get();
    ExprTreeHolder tree = objectPool.mutableExprPool.get();
    ((Literal) argList.get(0)).getComponents(val, factor);
    AMutableCharArrayString string_value = objectPool.strPool.get();
    if (val.isStringValue(string_value)) {
        if (functionName.equalsIgnoreCase("absTime")) {
            tree.setInnerTree(Literal.createAbsTime(string_value, objectPool));
        } else if (functionName.equalsIgnoreCase("relTime")) {
            tree.setInnerTree(Literal.createRelTime(string_value, objectPool));
        } else {
            tree.setInnerTree(FunctionCall.createFunctionCall(functionName, argList, objectPool));
        }
    } else {
        tree.setInnerTree(FunctionCall.createFunctionCall(functionName, argList, objectPool));
    }
    return tree;
}
Also used : ExprTreeHolder(org.apache.asterix.external.classad.ExprTreeHolder) Literal(org.apache.asterix.external.classad.Literal) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) AMutableNumberFactor(org.apache.asterix.external.classad.AMutableNumberFactor) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString)

Example 4 with Literal

use of org.apache.asterix.external.classad.Literal in project asterixdb by apache.

the class ClassAdParser method shouldEvaluateAtParseTime.

public boolean shouldEvaluateAtParseTime(String functionName, ExprList argList) throws HyracksDataException {
    boolean should_eval = false;
    if (functionName.equalsIgnoreCase("absTime") || functionName.equalsIgnoreCase("relTime")) {
        if (argList.size() == 1 && argList.get(0).getKind() == NodeKind.LITERAL_NODE) {
            Value val = objectPool.valuePool.get();
            AMutableNumberFactor factor = objectPool.numFactorPool.get();
            ((Literal) argList.get(0)).getComponents(val, factor);
            if (val.isStringValue()) {
                should_eval = true;
            }
        }
    }
    return should_eval;
}
Also used : Literal(org.apache.asterix.external.classad.Literal) TokenValue(org.apache.asterix.external.classad.TokenValue) Value(org.apache.asterix.external.classad.Value) AMutableNumberFactor(org.apache.asterix.external.classad.AMutableNumberFactor)

Aggregations

Literal (org.apache.asterix.external.classad.Literal)4 Value (org.apache.asterix.external.classad.Value)4 TokenValue (org.apache.asterix.external.classad.TokenValue)3 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)2 AMutableNumberFactor (org.apache.asterix.external.classad.AMutableNumberFactor)2 ExprTreeHolder (org.apache.asterix.external.classad.ExprTreeHolder)2 ArrayList (java.util.ArrayList)1 ClassAd (org.apache.asterix.external.classad.ClassAd)1 ExprList (org.apache.asterix.external.classad.ExprList)1 ExprTree (org.apache.asterix.external.classad.ExprTree)1 TokenType (org.apache.asterix.external.classad.Lexer.TokenType)1 ClassAdParser (org.apache.asterix.external.library.ClassAdParser)1 AMutableString (org.apache.asterix.om.base.AMutableString)1 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)1 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)1