Search in sources :

Example 1 with AMutableCharArrayString

use of org.apache.asterix.external.classad.AMutableCharArrayString 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 2 with AMutableCharArrayString

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

the class ClassAdUnitTester method testValue.

/*********************************************************************
     * Function: test_value
     * Purpose: Test the Value class.
     *
     * @throws HyracksDataException
     *********************************************************************/
public static void testValue(Parameters parameters, Results results, ClassAdObjectPool objectPool) throws HyracksDataException {
    Value v = new Value(objectPool);
    boolean isExpectedType;
    System.out.println("Testing the Value class...");
    test("New value is undefined", (v.isUndefinedValue()), "test_value 1", results);
    test("New value isn't boolean", !(v.isBooleanValue()), "test_value 2", results);
    test("GetType gives UNDEFINED_VALUE", (v.getType() == ValueType.UNDEFINED_VALUE), "test_value 3", results);
    v.setErrorValue();
    test("Is error value", (v.isErrorValue()), "test_value 4", results);
    test("GetType gives ERROR_VALUE", (v.getType() == ValueType.ERROR_VALUE), "test_value 5", results);
    MutableBoolean b = new MutableBoolean();
    v.setBooleanValue(true);
    isExpectedType = v.isBooleanValue(b);
    test("Value is not undefined", !(v.isUndefinedValue()), "Value is not undefined", results);
    test("Value is boolean", (v.isBooleanValue()), "Value is boolean", results);
    test("Try 2: New value is boolean", (isExpectedType == true), "Try 2: New value is boolean", results);
    test("Boolean is true", (b.booleanValue() == true), "Boolean is true", results);
    test("GetType gives BOOLEAN_VALUE", (v.getType() == ValueType.BOOLEAN_VALUE), "GetType gives BOOLEAN_VALUE", results);
    AMutableDouble r = new AMutableDouble(0.0);
    v.setRealValue(1.0);
    isExpectedType = v.isRealValue(r);
    test("Value is real", isExpectedType, results);
    test("Real is 1.0", (r.getDoubleValue() == 1.0), results);
    test("GetType gives REAL_VALUE", (v.getType() == ValueType.REAL_VALUE), results);
    test("Real is a number", v.isNumber(), results);
    AMutableInt64 i = new AMutableInt64(0);
    v.setIntegerValue(1);
    isExpectedType = v.isIntegerValue(i);
    test("Value is integer", isExpectedType, results);
    test("Integer is 1", (i.getLongValue() == 1), results);
    test("GetType gives INTEGER_VALUE", (v.getType() == ValueType.INTEGER_VALUE), results);
    test("Integer is a number", v.isNumber(), results);
    AMutableCharArrayString s = new AMutableCharArrayString();
    v.setStringValue("Robert-Houdin");
    isExpectedType = v.isStringValue(s);
    test("Value is String", isExpectedType, results);
    test("String is 'Robert-Houdin'", (0 == s.compareTo("Robert-Houdin")), results);
    test("GetType gives STRING_VALUE", (v.getType() == ValueType.STRING_VALUE), results);
    ClassAdTime at = new ClassAdTime(10, 36000000);
    v.setAbsoluteTimeValue(at);
    at.setValue(0);
    at.setTimeZone(0);
    isExpectedType = v.isAbsoluteTimeValue(at);
    test("Value is absolute time", isExpectedType, results);
    test("Absolute time is 10, 0", (10 == at.getTime() && 36000000 == at.getOffset()), results);
    test("GetType gives ABSOLUTE_TIME_VALUE", (v.getType() == ValueType.ABSOLUTE_TIME_VALUE), results);
    ClassAdTime rt = new ClassAdTime(10, false);
    v.setRelativeTimeValue(10);
    isExpectedType = v.isRelativeTimeValue(rt);
    test("Value is relative time", isExpectedType, results);
    test("Relative time is 10", (10 == rt.getRelativeTime()), results);
    test("GetType gives RELATIVE_TIME_VALUE", (v.getType() == ValueType.RELATIVE_TIME_VALUE), results);
    ExprList l = new ExprList(objectPool);
    ExprList ll = new ExprList(objectPool);
    v.setListValue(l);
    isExpectedType = v.isListValue(ll);
    test("Value is list value", isExpectedType, results);
    test("List value is correct", l.equals(ll), results);
    test("GetType gives LIST_VALUE", (v.getType() == ValueType.LIST_VALUE), results);
    ExprList sl = new ExprList(true, objectPool);
    ll = new ExprList(true, objectPool);
    v.setListValue(sl);
    isExpectedType = v.isListValue(ll);
    test("Value is list value", isExpectedType, results);
    test("List value is correct", sl.equals(ll), results);
    test("GetType gives SLIST_VALUE", (v.getType() == ValueType.SLIST_VALUE), results);
    ClassAd c = new ClassAd(objectPool);
    c.insertAttr("test_int", 10);
    ClassAd cc = new ClassAd(objectPool);
    v.setClassAdValue(c);
    isExpectedType = v.isClassAdValue(cc);
    test("Value is ClassAd value", isExpectedType, results);
    test("ClassAd value is correct", c.equals(cc), results);
    test("GetType gives CLASSAD_VALUE", (v.getType() == ValueType.CLASSAD_VALUE), results);
    return;
}
Also used : ClassAd(org.apache.asterix.external.classad.ClassAd) ClassAdTime(org.apache.asterix.external.classad.ClassAdTime) ExprList(org.apache.asterix.external.classad.ExprList) MutableBoolean(org.apache.commons.lang3.mutable.MutableBoolean) AMutableDouble(org.apache.asterix.om.base.AMutableDouble) Value(org.apache.asterix.external.classad.Value) AMutableInt64(org.apache.asterix.om.base.AMutableInt64) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString)

Example 3 with AMutableCharArrayString

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

the class FunctionalTester method replace_variables.

/*********************************************************************
     * Function: replace_variables
     * Purpose:
     *
     * @throws HyracksDataException
     *********************************************************************/
public static boolean replace_variables(AMutableString mutableLine, State state, Parameters parameters, ClassAdObjectPool objectPool) throws HyracksDataException {
    boolean good_line;
    String error;
    good_line = true;
    error = "";
    Variable var = new Variable(objectPool);
    for (; ; ) {
        int dollar;
        int current_position;
        String variable_name;
        AMutableCharArrayString variable_value = new AMutableCharArrayString();
        current_position = 0;
        dollar = mutableLine.getStringValue().indexOf('$', current_position);
        if (dollar < 0) {
            break;
        }
        current_position = dollar + 1;
        if (!Character.isAlphabetic(mutableLine.getStringValue().charAt(current_position))) {
            good_line = false;
            error = "Bad variable name.";
            break;
        }
        current_position++;
        while (Character.isLetterOrDigit((mutableLine.getStringValue().charAt(current_position))) || mutableLine.getStringValue().charAt(current_position) == '_') {
            current_position++;
        }
        variable_name = mutableLine.getStringValue().substring(dollar + 1, current_position);
        var = variables.get(variable_name);
        if (var == null) {
            good_line = false;
            error = "Unknown variable '$";
            error += variable_name;
            error += "'";
            break;
        }
        var.getStringRepresentation(variable_value, objectPool);
        // We have to be careful with substr() because with gcc 2.96, it likes to
        // assert/except if you give it values that are too large.
        String end;
        if (current_position < mutableLine.getStringValue().length()) {
            end = mutableLine.getStringValue().substring(current_position);
        } else {
            end = "";
        }
        mutableLine.setValue(mutableLine.getStringValue().substring(0, dollar) + variable_value.toString() + end);
    }
    if (parameters.debug) {
        System.err.println("# after replacement: " + mutableLine.getStringValue());
    }
    if (!good_line) {
        print_error_message(error, state);
    }
    return good_line;
}
Also used : AMutableString(org.apache.asterix.om.base.AMutableString) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString) PrettyPrint(org.apache.asterix.external.classad.PrettyPrint) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString)

Example 4 with AMutableCharArrayString

use of org.apache.asterix.external.classad.AMutableCharArrayString 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 5 with AMutableCharArrayString

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

the class FunctionalTester method print_expr.

/*********************************************************************
     * Function: print_expr
     * Purpose:
     *
     * @throws HyracksDataException
     *********************************************************************/
public static void print_expr(ExprTree tree, State state, Parameters parameters, ClassAdObjectPool objectPool) throws HyracksDataException {
    AMutableCharArrayString output = new AMutableCharArrayString();
    if (state.format == PrintFormat.print_Compact) {
        ClassAdUnParser unparser = new ClassAdUnParser(objectPool);
        unparser.unparse(output, tree);
    } else if (state.format == PrintFormat.print_Pretty) {
        PrettyPrint unparser = new PrettyPrint(objectPool);
        unparser.unparse(output, tree);
    } else if (state.format == PrintFormat.print_XML) {
    /*
                                                            * ClassAdXMLUnParser unparser = new
                                                            * ClassAdXMLUnParser();
                                                            * unparser.SetCompactSpacing(true);
                                                            * unparser.Unparse(output, tree);
                                                            * } else if (state.format ==
                                                            * PrintFormat.print_XMLPretty) {
                                                            * ClassAdXMLUnParser unparser = new
                                                            * ClassAdXMLUnParser();
                                                            * unparser.SetCompactSpacing(false);
                                                            * unparser.Unparse(output, tree);
                                                            */
    }
    System.out.println(output);
}
Also used : PrettyPrint(org.apache.asterix.external.classad.PrettyPrint) ClassAdUnParser(org.apache.asterix.external.classad.ClassAdUnParser) AMutableCharArrayString(org.apache.asterix.external.classad.AMutableCharArrayString)

Aggregations

AMutableCharArrayString (org.apache.asterix.external.classad.AMutableCharArrayString)6 PrettyPrint (org.apache.asterix.external.classad.PrettyPrint)3 ClassAd (org.apache.asterix.external.classad.ClassAd)2 Value (org.apache.asterix.external.classad.Value)2 AMutableDouble (org.apache.asterix.om.base.AMutableDouble)2 AMutableInt64 (org.apache.asterix.om.base.AMutableInt64)2 AMutableString (org.apache.asterix.om.base.AMutableString)2 MutableBoolean (org.apache.commons.lang3.mutable.MutableBoolean)2 TreeSet (java.util.TreeSet)1 AMutableNumberFactor (org.apache.asterix.external.classad.AMutableNumberFactor)1 ClassAdTime (org.apache.asterix.external.classad.ClassAdTime)1 ClassAdUnParser (org.apache.asterix.external.classad.ClassAdUnParser)1 ExprList (org.apache.asterix.external.classad.ExprList)1 ExprTree (org.apache.asterix.external.classad.ExprTree)1 ExprTreeHolder (org.apache.asterix.external.classad.ExprTreeHolder)1 Literal (org.apache.asterix.external.classad.Literal)1 TokenValue (org.apache.asterix.external.classad.TokenValue)1 ClassAdParser (org.apache.asterix.external.library.ClassAdParser)1 AMutableInt32 (org.apache.asterix.om.base.AMutableInt32)1