use of org.apache.asterix.external.classad.ExprList 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.ExprList 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.ExprList in project asterixdb by apache.
the class ClassAdParser method parsePrimaryExpression.
// PrimaryExpression .= Identifier
// | FunctionCall
// | '.' Identifier
// | '(' Expression ')'
// | Literal
// FunctionCall .= Identifier ArgumentList
// ( Constant may be
// boolean,undefined,error,string,integer,real,classad,list )
// ( ArgumentList non-terminal includes parentheses )
private boolean parsePrimaryExpression(ExprTreeHolder tree) throws IOException {
ExprTreeHolder treeL;
TokenValue tv = objectPool.tokenValuePool.get();
TokenType tt;
tree.setInnerTree(null);
switch((tt = lexer.peekToken(tv))) {
// identifiers
case LEX_IDENTIFIER:
isExpr = true;
lexer.consumeToken();
// check for funcion call
if ((tt = lexer.peekToken()) == TokenType.LEX_OPEN_PAREN) {
ExprList argList = objectPool.exprListPool.get();
if (!parseArgumentList(argList)) {
tree.setInnerTree(null);
return false;
}
;
// string literal
if (shouldEvaluateAtParseTime(tv.getStrValue().toString(), argList)) {
tree.setInnerTree(evaluateFunction(tv.getStrValue().toString(), argList));
} else {
tree.setInnerTree(FunctionCall.createFunctionCall(tv.getStrValue().toString(), argList, objectPool));
}
} else {
// I don't think this is ever hit
tree.setInnerTree(AttributeReference.createAttributeReference(null, tv.getStrValue(), false, objectPool));
}
return (tree.getInnerTree() != null);
case LEX_SELECTION:
isExpr = true;
lexer.consumeToken();
if ((tt = lexer.consumeToken(tv)) == TokenType.LEX_IDENTIFIER) {
// the boolean final arg signifies that reference is absolute
tree.setInnerTree(AttributeReference.createAttributeReference(null, tv.getStrValue(), true, objectPool));
return (tree.size() != 0);
}
// not an identifier following the '.'
throw new HyracksDataException("need identifier in selection expression (got" + Lexer.strLexToken(tt) + ")");
// parenthesized expression
case LEX_OPEN_PAREN:
{
isExpr = true;
lexer.consumeToken();
treeL = objectPool.mutableExprPool.get();
parseExpression(treeL);
if (treeL.getInnerTree() == null) {
tree.resetExprTree(null);
return false;
}
if ((tt = lexer.consumeToken()) != TokenType.LEX_CLOSE_PAREN) {
throw new HyracksDataException("exptected LEX_CLOSE_PAREN, but got " + Lexer.strLexToken(tt));
// tree.resetExprTree(null);
// return false;
}
// assume make operation will return a new tree
tree.setInnerTree(Operation.createOperation(Operation.OpKind_PARENTHESES_OP, treeL, objectPool));
return (tree.size() != 0);
}
// constants
case LEX_OPEN_BOX:
{
isExpr = true;
ClassAd newAd = objectPool.classAdPool.get();
if (!parseClassAd(newAd)) {
tree.resetExprTree(null);
return false;
}
tree.setInnerTree(newAd);
}
return true;
case LEX_OPEN_BRACE:
{
isExpr = true;
ExprList newList = objectPool.exprListPool.get();
if (!parseExprList(newList)) {
tree.setInnerTree(null);
return false;
}
tree.setInnerTree(newList);
}
return true;
case LEX_UNDEFINED_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setUndefinedValue();
tree.setInnerTree(Literal.createLiteral(val, objectPool));
return (tree.getInnerTree() != null);
}
case LEX_ERROR_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setErrorValue();
tree.setInnerTree(Literal.createLiteral(val, objectPool));
return (tree.getInnerTree() != null);
}
case LEX_BOOLEAN_VALUE:
{
Value val = objectPool.valuePool.get();
MutableBoolean b = new MutableBoolean();
tv.getBoolValue(b);
lexer.consumeToken();
val.setBooleanValue(b);
tree.setInnerTree(Literal.createLiteral(val, objectPool));
return (tree.getInnerTree() != null);
}
case LEX_INTEGER_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setIntegerValue(tv.getIntValue());
tree.setInnerTree(Literal.createLiteral(val, tv.getFactor(), objectPool));
return (tree.getInnerTree() != null);
}
case LEX_REAL_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setRealValue(tv.getRealValue());
tree.setInnerTree(Literal.createLiteral(val, tv.getFactor(), objectPool));
return (tree.getInnerTree() != null);
}
case LEX_STRING_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setStringValue(tv.getStrValue());
tree.setInnerTree(Literal.createLiteral(val, objectPool));
return (tree.getInnerTree() != null);
}
case LEX_ABSOLUTE_TIME_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setAbsoluteTimeValue(tv.getTimeValue());
tree.setInnerTree(Literal.createLiteral(val, objectPool));
return (tree.getInnerTree() != null);
}
case LEX_RELATIVE_TIME_VALUE:
{
Value val = objectPool.valuePool.get();
lexer.consumeToken();
val.setRelativeTimeValue(tv.getTimeValue().getRelativeTime());
tree.setInnerTree(Literal.createLiteral(val, objectPool));
return (tree.getInnerTree() != null);
}
default:
tree.setInnerTree(null);
return false;
}
}
use of org.apache.asterix.external.classad.ExprList in project asterixdb by apache.
the class ClassAdParser method parseExprList.
public boolean parseExprList(ExprList list, boolean full) throws IOException {
TokenType tt;
ExprTreeHolder tree = objectPool.mutableExprPool.get();
ExprList loe = objectPool.exprListPool.get();
if ((tt = lexer.consumeToken()) != TokenType.LEX_OPEN_BRACE) {
throw new HyracksDataException("while parsing expression list: expected LEX_OPEN_BRACE" + " but got " + Lexer.strLexToken(tt));
// return false;
}
tt = lexer.peekToken();
while (tt != TokenType.LEX_CLOSE_BRACE) {
// parse the expression
parseExpression(tree);
if (tree.getInnerTree() == null) {
throw new HyracksDataException("while parsing expression list: expected " + "LEX_CLOSE_BRACE or LEX_COMMA but got " + Lexer.strLexToken(tt));
}
// insert the expression into the list
loe.add(tree);
// the next token must be a ',' or a '}'
tt = lexer.peekToken();
if (tt == TokenType.LEX_COMMA) {
lexer.consumeToken();
} else if (tt != TokenType.LEX_CLOSE_BRACE) {
throw new HyracksDataException("while parsing expression list: expected " + "LEX_CLOSE_BRACE or LEX_COMMA but got " + Lexer.strLexToken(tt));
}
}
lexer.consumeToken();
list.setValue(ExprList.createExprList(loe, objectPool));
// if a full parse was requested, ensure that input is exhausted
if (full && (lexer.consumeToken() != TokenType.LEX_END_OF_INPUT)) {
list.clear();
throw new HyracksDataException("while parsing expression list: expected " + "LEX_END_OF_INPUT for full parse but got " + Lexer.strLexToken(tt));
}
return true;
}
Aggregations