use of org.apache.asterix.external.classad.ClassAd 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);
}
}
use of org.apache.asterix.external.classad.ClassAd 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);
}
use of org.apache.asterix.external.classad.ClassAd 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;
}
use of org.apache.asterix.external.classad.ClassAd 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;
}
use of org.apache.asterix.external.classad.ClassAd in project asterixdb by apache.
the class ClassAdParserTest method test.
/**
*
*/
public void test() {
try {
// test here
ClassAdObjectPool objectPool = new ClassAdObjectPool();
ClassAd pAd = new ClassAd(objectPool);
String szInput;
String[] files = new String[] { "/classad/testdata.txt" };
BufferedReader infile = null;
for (String path : files) {
infile = Files.newBufferedReader(Paths.get(URLDecoder.decode(getClass().getResource(path).getPath(), "UTF-8")), StandardCharsets.UTF_8);
szInput = infile.readLine();
while (szInput != null) {
if (szInput.trim().length() == 0) {
// ClassAdChain completed
pAd.clear();
objectPool.reset();
} else if (!pAd.insert(szInput)) {
// Problem
System.out.println("BARFED ON:" + szInput);
assert (false);
}
szInput = infile.readLine();
}
infile.close();
}
} catch (Exception e) {
assertTrue(false);
}
assertTrue(true);
}
Aggregations