Search in sources :

Example 1 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class ClassAdToADMTest method testEscaping.

/**
    *
    */
public void testEscaping() {
    try {
        ClassAdObjectPool objectPool = new ClassAdObjectPool();
        ClassAd pAd = new ClassAd(objectPool);
        String[] files = new String[] { "/classad/escapes.txt" };
        ClassAdParser parser = new ClassAdParser(objectPool);
        CharArrayLexerSource lexerSource = new CharArrayLexerSource();
        for (String path : files) {
            List<Path> paths = new ArrayList<>();
            Map<String, String> config = new HashMap<>();
            config.put(ExternalDataConstants.KEY_RECORD_START, "[");
            config.put(ExternalDataConstants.KEY_RECORD_END, "]");
            paths.add(Paths.get(getClass().getResource(path).toURI()));
            FileSystemWatcher watcher = new FileSystemWatcher(paths, null, false);
            LocalFSInputStream in = new LocalFSInputStream(watcher);
            SemiStructuredRecordReader recordReader = new SemiStructuredRecordReader();
            recordReader.configure(in, config);
            try {
                Value val = new Value(objectPool);
                while (recordReader.hasNext()) {
                    val.reset();
                    IRawRecord<char[]> record = recordReader.next();
                    lexerSource.setNewSource(record.get());
                    parser.setLexerSource(lexerSource);
                    parser.parseNext(pAd);
                    Assert.assertEquals("[ Args = \"“-1 0.1 0.1 0.5 2e-07 0.001 10 -1”\"; GlobalJobId = \"submit-4.chtc.wisc.edu#3724038.0#1462893042\" ]", pAd.toString());
                }
            } finally {
                recordReader.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : Path(java.nio.file.Path) ClassAdObjectPool(org.apache.asterix.external.classad.object.pool.ClassAdObjectPool) ClassAd(org.apache.asterix.external.classad.ClassAd) HashMap(java.util.HashMap) CharArrayLexerSource(org.apache.asterix.external.classad.CharArrayLexerSource) ArrayList(java.util.ArrayList) CaseInsensitiveString(org.apache.asterix.external.classad.CaseInsensitiveString) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ClassAdParser(org.apache.asterix.external.library.ClassAdParser) FileSystemWatcher(org.apache.asterix.external.util.FileSystemWatcher) SemiStructuredRecordReader(org.apache.asterix.external.input.record.reader.stream.SemiStructuredRecordReader) Value(org.apache.asterix.external.classad.Value) LocalFSInputStream(org.apache.asterix.external.input.stream.LocalFSInputStream)

Example 2 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class ClassAdUnitTester method testClassad.

/*********************************************************************
     * Function: test_classad
     * Purpose: Test the ClassAd class.
     * @param objectPool
     *
     * @throws IOException
     *********************************************************************/
public static void testClassad(Parameters parameters, Results results, ClassAdObjectPool objectPool) throws IOException {
    ClassAdParser parser = new ClassAdParser(objectPool);
    boolean haveAttribute;
    boolean success;
    System.out.println("Testing the ClassAd class...");
    String input_basic = "[ A = 3; B = 4.0; C = \"babyzilla\"; D = true; E = {1}; F = [ AA = 3; ]; G =\"deleteme\";]";
    ClassAd basic = new ClassAd(objectPool);
    AMutableInt64 i = new AMutableInt64(0);
    MutableBoolean b = new MutableBoolean();
    AMutableDouble r = new AMutableDouble(0);
    AMutableCharArrayString s = new AMutableCharArrayString();
    ClassAd c = new ClassAd(objectPool);
    // ExprList *l;
    basic = parser.parseClassAd(input_basic);
    /* ----- Test EvaluateAttr* ----- */
    haveAttribute = basic.evaluateAttrInt("A", i);
    test("Have attribute A", (haveAttribute == true), "test_classad 1", results);
    test("A is 3", (i.getLongValue() == 3), "test_classad 2", results);
    haveAttribute = basic.evaluateAttrReal("B", r);
    test("Have attribute B", (haveAttribute == true), "test_classad 3", results);
    test("B is 4.0", (r.getDoubleValue() == 4.0), "test_classad 4", results);
    haveAttribute = basic.evaluateAttrString("C", s);
    test("Have attribute C", (haveAttribute == true), "test_classad 5", results);
    test("C is 'babyzilla'", (s.compareTo("babyzilla") == 0), "test_classad 6", results);
    haveAttribute = basic.evaluateAttrBool("D", b);
    test("Have attribute D", (haveAttribute == true), "test_classad 7", results);
    test("D is true", (b.booleanValue() == true), "test_classad 8", results);
    /* ----- Test basic insert and delete ----- */
    success = basic.insertAttr("new", 4);
    test("InsertAttr claims to have worked", (success == true), "test_classad 9", results);
    haveAttribute = basic.evaluateAttrInt("new", i);
    test("Have new attribute", (haveAttribute == true), "test_classad 10", results);
    test("new attribute is 4", i.getLongValue() == 4, "test_classad 11", results);
    success = basic.delete("new");
    test("Delete claims to have worked", (success == true), "test_classad 12", results);
    haveAttribute = basic.evaluateAttrInt("new", i);
    test("New attribute was deleted", (haveAttribute == false), "test_classad 13", results);
    success = basic.delete("G");
    test("DELETE claims to have worked", (success == true), "test_classad 14", results);
    haveAttribute = basic.evaluateAttrString("G", s);
    test("Attribute G was deleted", (haveAttribute == false), "test_classad 15", results);
    basic = null;
    /* ----- Test GetExternalReferences ----- */
    String inputRef = "[ Rank=Member(\"LCG-2_1_0\",other.Environment) ? other.Time/seconds : other.Time/minutes; minutes=60; ]";
    TreeSet<String> refs = new TreeSet<String>();
    ExprTree rank;
    c = parser.parseClassAd(inputRef);
    test("Made classad_ref", (c != null), "Test GetExternalReferences 1", results);
    if (c != null) {
        rank = c.lookup("Rank");
        test("Rank exists", (rank != null), "Test GetExternalReferences 2", results);
        if (rank != null) {
            boolean haveReferences;
            if ((haveReferences = c.getExternalReferences(rank, refs, true))) {
                test("have_references", (haveReferences == true), "Test GetExternalReferences 3", results);
                if (haveReferences) {
                    boolean haveEnvironment;
                    boolean haveTime;
                    boolean haveSeconds;
                    boolean haveOther;
                    haveEnvironment = false;
                    haveTime = false;
                    haveSeconds = false;
                    haveOther = false;
                    for (String entry : refs) {
                        if (entry.compareTo("other.Environment") == 0) {
                            haveEnvironment = true;
                        } else if (entry.compareTo("other.Time") == 0) {
                            haveTime = true;
                        } else if (entry.compareTo("seconds") == 0) {
                            haveSeconds = true;
                        } else {
                            haveOther = true;
                        }
                    }
                    test("Have external reference to Environment", (haveEnvironment == true), "Test GetExternalReferences 4", results);
                    test("Have external reference to Time", (haveTime == true), "Test GetExternalReferences 5", results);
                    test("Have external reference to seconds", (haveSeconds == true), "Test GetExternalReferences 6", results);
                    test("Have no other external references", (haveOther != true), "Test GetExternalReferences 7", results);
                }
            }
        }
        c = null;
    }
    // This ClassAd may cause problems. Perhaps a memory leak.
    // This test is only useful when run under valgrind.
    String memoryProblemClassad = "[ Updates = [status = \"request_completed\"; timestamp = absTime(\"2004-12-16T18:10:59-0600]\")] ]";
    c = parser.parseClassAd(memoryProblemClassad);
    /* ----- Test Parsing multiple ClassAds ----- */
    String twoClassads = "[ a = 3; ][ b = 4; ]";
    ClassAd classad1 = new ClassAd(objectPool);
    ClassAd classad2 = new ClassAd(objectPool);
    AMutableInt32 offset = new AMutableInt32(0);
    parser.parseClassAd(twoClassads, classad1, offset);
    test("Have good offset #1", offset.getIntegerValue() == 10, "Test Parsing multiple ClassAds 1", results);
    parser.parseClassAd(twoClassads, classad2, offset);
    test("Have good offset #2", offset.getIntegerValue() == 20, "Test Parsing multiple ClassAds 2", results);
    /* ----- Test chained ClassAds ----- */
    // classad1 and classad2 from above test are used.
    ClassAd classad3 = new ClassAd(objectPool);
    classad1.chainToAd(classad2);
    test("classad1's parent is classad2", classad1.getChainedParentAd().equals(classad2), "Test chained ClassAds 1", results);
    haveAttribute = classad1.evaluateAttrInt("b", i);
    test("chain has attribute b from parent", (haveAttribute == true), "Test chained ClassAds 2", results);
    test("chain attribute b from parent is 4", (i.getLongValue() == 4), "Test chained ClassAds 3", results);
    haveAttribute = classad1.evaluateAttrInt("a", i);
    test("chain has attribute a from self", (haveAttribute == true), "Test chained ClassAds 4", results);
    test("chain attribute a is 3", (i.getLongValue() == 3), "Test chained ClassAds 5", results);
    // Now we modify classad2 (parent) to contain "a".
    success = classad2.insertAttr("a", 7);
    test("insert a into parent", (success == true), "Test chained ClassAds 6", results);
    haveAttribute = classad1.evaluateAttrInt("a", i);
    test("chain has attribute a from self (overriding parent)", (haveAttribute == true), "Test chained ClassAds 7", results);
    test("chain attribute a is 3 (overriding parent)", (i.getLongValue() == 3), "Test chained ClassAds 8", results);
    haveAttribute = classad2.evaluateAttrInt("a", i);
    test("chain parent has attribute a", (haveAttribute == true), "Test chained ClassAds 9", results);
    test("chain parent attribute a is 7", (i.getLongValue() == 7), "Test chained ClassAds 10", results);
    success = classad3.copyFromChain(classad1);
    test("copy from chain succeeded", (success == true), "Test chained ClassAds 11", results);
    haveAttribute = classad3.evaluateAttrInt("b", i);
    test("copy of chain has attribute b", (haveAttribute == true), "Test chained ClassAds 12", results);
    test("copy of chain has attribute b==4", (i.getLongValue() == 4), "Test chained ClassAds 13", results);
    success = classad3.insertAttr("c", 6);
    test("insert into copy of chain succeeded", (success == true), "Test chained ClassAds 14", results);
    classad3.copyFromChain(classad1);
    haveAttribute = classad3.evaluateAttrInt("c", i);
    test("copy of chain is clean", (haveAttribute == false), "Test chained ClassAds 15", results);
    classad3.insertAttr("c", 6);
    success = classad3.updateFromChain(classad1);
    test("update from chain succeeded", (success == true), "Test chained ClassAds 16", results);
    haveAttribute = classad3.evaluateAttrInt("c", i);
    test("update from chain is merged", (haveAttribute == true), "Test chained ClassAds 17", results);
    test("update from chain has attribute c==6", (i.getLongValue() == 6), "Test chained ClassAds 18", results);
}
Also used : ClassAdParser(org.apache.asterix.external.library.ClassAdParser) ClassAd(org.apache.asterix.external.classad.ClassAd) TreeSet(java.util.TreeSet) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) ExprTree(org.apache.asterix.external.classad.ExprTree) AMutableString(org.apache.asterix.om.base.AMutableString) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableInt32(org.apache.asterix.om.base.AMutableInt32) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString)

Example 3 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser 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 4 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class ClassAdUnitTester method testParsing.

/*********************************************************************
     * Function: test_parsing
     * Purpose: Test parsing that isn't ClassAd-specific. (ClassAd-specific
     * is in test_clasad
     *
     * @throws IOException
     *********************************************************************/
public static void testParsing(Parameters parameters, Results results, ClassAdObjectPool objectPool) throws IOException {
    ClassAdParser parser = new ClassAdParser(objectPool);
    ExprTree tree;
    // My goal is to ensure that these expressions don't crash
    // They should also return a null tree
    tree = parser.ParseExpression("true || false || ;");
    test("Bad or doesn't crash & isn't bogus", tree == null, "true || false || ;", results);
    tree = parser.ParseExpression("true && false && ;");
    test("Bad and doesn't crash & isn't bogus", tree == null, "true && false && ;", results);
    tree = parser.ParseExpression("3 | 4 | ;");
    test("Bad and doesn't crash & isn't bogus", tree == null, "3 | 4 | ;", results);
    tree = parser.ParseExpression("3 ^ 4 ^ ;");
    test("Bad exclusive or doesn't crash & isn't bogus", tree == null, "3 ^ 4 ^ ;", results);
    tree = parser.ParseExpression("3 & 4 & ;");
    test("Bad bitwise and doesn't crash & isn't bogus", tree == null, "3 & 4 & ;", results);
    tree = parser.ParseExpression("3 == 4 ==  ;");
    test("Bad equality doesn't crash & isn't bogus", tree == null, "3 == 4 ==  ;", results);
    tree = parser.ParseExpression("1 < 3 < ;");
    test("Bad relational doesn't crash & isn't bogus", tree == null, "1 < 3 < ;", results);
    tree = parser.ParseExpression("1 + 3 + ;");
    test("Bad shift doesn't crash & isn't bogus", tree == null, "1 + 3 + ;", results);
    tree = parser.ParseExpression("1 + 3 + ;");
    test("Bad additive doesn't crash & isn't bogus", tree == null, "1 + 3 + ;", results);
    tree = parser.ParseExpression("1 * 3 * ;");
    test("Bad multiplicative doesn't crash & isn't bogus", tree == null, "1 * 3 * ;", results);
}
Also used : ClassAdParser(org.apache.asterix.external.library.ClassAdParser) ExprTree(org.apache.asterix.external.classad.ExprTree)

Example 5 with ClassAdParser

use of org.apache.asterix.external.library.ClassAdParser in project asterixdb by apache.

the class FunctionalTester method get_expr.

/*********************************************************************
     * Function: get_expr
     * Purpose:
     *
     * @throws IOException
     *********************************************************************/
public static ExprTree get_expr(AMutableString line, State state, Parameters parameters, ClassAdObjectPool objectPool) throws IOException {
    int offset;
    ExprTree tree;
    ClassAdParser parser = new ClassAdParser(objectPool);
    StringLexerSource lexer_source = new StringLexerSource(line.getStringValue());
    tree = parser.parseExpression(lexer_source, false);
    offset = lexer_source.getCurrentLocation();
    shorten_line(line, offset);
    if (tree == null) {
        print_error_message("Missing expression", state);
    }
    return tree;
}
Also used : ClassAdParser(org.apache.asterix.external.library.ClassAdParser) StringLexerSource(org.apache.asterix.external.classad.StringLexerSource) ExprTree(org.apache.asterix.external.classad.ExprTree) PrettyPrint(org.apache.asterix.external.classad.PrettyPrint)

Aggregations

ClassAdParser (org.apache.asterix.external.library.ClassAdParser)9 ExprTree (org.apache.asterix.external.classad.ExprTree)5 ArrayList (java.util.ArrayList)4 ClassAd (org.apache.asterix.external.classad.ClassAd)4 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 CaseInsensitiveString (org.apache.asterix.external.classad.CaseInsensitiveString)3 Value (org.apache.asterix.external.classad.Value)3 ClassAdObjectPool (org.apache.asterix.external.classad.object.pool.ClassAdObjectPool)3 SemiStructuredRecordReader (org.apache.asterix.external.input.record.reader.stream.SemiStructuredRecordReader)3 LocalFSInputStream (org.apache.asterix.external.input.stream.LocalFSInputStream)3 FileSystemWatcher (org.apache.asterix.external.util.FileSystemWatcher)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)3 IOException (java.io.IOException)2 AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)2 CharArrayLexerSource (org.apache.asterix.external.classad.CharArrayLexerSource)2 PrettyPrint (org.apache.asterix.external.classad.PrettyPrint)2 StringLexerSource (org.apache.asterix.external.classad.StringLexerSource)2 AMutableString (org.apache.asterix.om.base.AMutableString)2 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)2