def test_reduce(self): pyflwor.compile(''' for x in <asdf> collect x.tree as x.attr with function(prev, next) { if prev == None then next else prev.combine(next) } ''')
def test_flwr_attrvalue(self): pyflwor.compile('''for x in <asfd>, z in <asdf>, y in sdaf.asdf(asdf, asdf)[1] let y = <x/asdf>, y1 = <Afd>, y2 = <asdf>, y3 = <asdf> let x = <adf> where every x in <y> satisfies (q == z and (<y> is not <z>)) return 'asdf':asdf, "one":1, "2.0":2.0''') pyflwor.compile('''for x in <asfd>, z in <asdf>, y in sdaf.asdf(asdf, asdf)[1] let y = <x/asdf>, y1 = <Afd>, y2 = <asdf>, y3 = <asdf> let x = <adf> let q = sadf.asdf().asfd[1](1,2,3) where every x in <y> satisfies (q == z and (<y> is not <z>)) return 'asdf':asdf, "one":1, "2.0":2.0''')
def add(cmds, args): '''usage: query add name . name = name of the query Add a query, the text of the query is added through an editor session with editor defined in the enviroment variable EDITOR.''' name = args query = self.edittext('') q = pyflwor.compile(query) self.queries.update({name.strip():(query,q)})
def save_exec(cmds, args): '''usage: query save_exec format filepath query . format = The output format. . filepath = The file to save the query's output in. . Path is relative to . $PROJECT_WORKING_DIR/analysis . query = Either the query text . or the name of a saved query Save the output of a query to a file. - If using the 'fvdl' or 'ofs' formats query must . return only objects of type Finding. - The other formats avaliable are 'text', and 'csv' - For a full list of formats type "formats"''' if args.count(' ') < 2: raise Exception, "Must supply both format, file location and a query." format, path, query = args.split(' ', 2) if '_' in format: i = format.index('_') version = format[i+1:] format = format[:i] else: version = '' if not os.path.isabs(path): path = os.path.join(ANALYSIS_DIR, path) if query in self.queries: q = self.queries[query][1] query = self.queries[query][0] else: q = pyflwor.compile(query) results = q(self.querydict()) if format in _formats: f = open(path, 'w') if format == 'text': f.write('Query:\n') f.write(query.strip()) f.write('\n\n\n') for r in results: if hasattr(r, '__iter__'): s = str(tuple(str(item) for item in r)) else: s = str(r) f.write(s) f.write('\n') elif format == 'csv': for r in results: if hasattr(r, '__iter__'): s = ', '.join(tuple(str(item) for item in r)) else: s = str(r) f.write(s) f.write('\n') f.close() else: raise Exception, "Format '%s' not supported" % format
def __init__(self, objects, queries=None): if not objects: raise Exception, "objects was empty, you must supply a query context (ie. objects to query...)" self.objects = objects self.queries = dict() if queries: for name in queries: query = queries[name] q = pyflwor.compile(query) self.queries.update({name.strip():(query,q)}) self.histfile = '.hist'
def test_flwr_orderby(self): pyflwor.compile('for x in <asdf> order by "adsf" desc return "adsf":x') pyflwor.compile('''for x in <asfd>, z in <asdf>, y in sdaf.asdf(asdf, asdf)[1] let y = <x/asdf>, y1 = <Afd>, y2 = <asdf>, y3 = <asdf> let x = <adf> where every x in <y> satisfies (q == z and (<y> is not <z>)) order by "asdf" desc return 'asdf':asdf, "one":1, "2.0":2.0''') pyflwor.compile('for x in <asdf> order by 0 ascd return x') pyflwor.compile('''for x in <asfd>, z in <asdf>, y in sdaf.asdf(asdf, asdf)[1] let y = <x/asdf>, y1 = <Afd>, y2 = <asdf>, y3 = <asdf> let x = <adf> where every x in <y> satisfies (q == z and (<y> is not <z>)) order by 1 ascd return asdf, 1, 2.0''')
def load(cmds, args): '''usage: query load filepath . filepath = path to the file relative to the current . working directory. Load queries from a file''' f = open(args, 'r') s = f.read() for line in s.split(QUERY_SEP): line = line.strip() if not line: continue name, query = line.split('=', 1) q = pyflwor.compile(query) self.queries.update({name.strip():(query.strip(),q)}) f.close()
def edit(cmds, args): '''usage: query edit str . str = name of a saved query Edit a saved query using the editor defined in the enviroment as $EDITOR''' def badquery(query): def bq(*args): return pyflwor.compile(query)(*args) return bq name = args name = name.strip() if name not in self.queries: raise Exception, "Query %s not defined" % name query = self.edittext(self.queries[name][0]) self.queries.update({name:(query,badquery(query))}) q = pyflwor.compile(query) self.queries.update({name:(query,q)})
def _exec(cmds, args): '''usage: query exec str . str = Either the query text . or the name of a saved query Execute a query, can either be a query text or a the name of a saved query.''' if args in self.queries: q = self.queries[args][1] else: print args q = pyflwor.compile(args) results = q(self.querydict()) for r in results: #if hasattr(r, '__iter__'): #print tuple(str(item) for item in r) #else: if isinstance(results, dict): print r, results[r] else: print r
def test_where_op(self): pyflwor.compile('hello[1 == 1]') pyflwor.compile('hello[1 != 1]') pyflwor.compile('hello[1 < 1]') pyflwor.compile('hello[1 <= 1]') pyflwor.compile('hello[1 > 1]') pyflwor.compile('hello[1 >= 1]')
def bq(*args): return pyflwor.compile(query)(*args)
def test_where_setcmp(self): pyflwor.compile('hello[a in <qs>]') pyflwor.compile('hello[a not in <qs>]') pyflwor.compile('hello[not a in <qs>]') pyflwor.compile('hello[<a> subset <qs>]') pyflwor.compile('hello[<a> superset <qq>]') pyflwor.compile('hello[<a> proper subset <aq>]') pyflwor.compile('hello[<a> proper superset <aq>]') pyflwor.compile('hello[<a> is <qs>]') pyflwor.compile('hello[<a> is not <qs>]')
def test_where_quantified(self): pyflwor.compile('hello[every x in <asdf> satisfies (x)]') self.assertRaises(SyntaxError, pyflwor.compile, 'hello[every x in <asdf> statisfies (x)]') self.assertRaises(SyntaxError, pyflwor.compile, 'hello[every x in <asdf> statisfies x]') pyflwor.compile('hello[some x in <asdf> satisfies (x)]') pyflwor.compile('hello[some x in <self/asdf> satisfies (x)]') pyflwor.compile('hello[some x in {for x in <asdf> return x} satisfies (x)]') pyflwor.compile('hello[some x in {for x in <asdf> return x} satisfies (x == y)]') pyflwor.compile('hello[some x in {for x in <asdf> return x} satisfies (x and not y(1,2))]')
def test_where_value_params(self): pyflwor.compile('hello[f(1)]') pyflwor.compile('hello[f(1,2,3)]') pyflwor.compile('hello[f(1,<asdf>,{for x in <asdf> return x})]') pyflwor.compile('hello[f(a,b,c)]') pyflwor.compile('hello[f(a(),b(),c())]') pyflwor.compile('hello[f(a[a],b[b],c[c])]')
def test_flwr(self): pyflwor.compile('for x in <asfd> return x') pyflwor.compile('for x in <asfd>, y in <adsf> return x') pyflwor.compile('for x in {for x in <asdf> return x} return x') pyflwor.compile('for x in <asfd> where x == y return x') pyflwor.compile('for x in <asfd> let y = <x/asdf> return x') pyflwor.compile('for x in <asfd> let y = {for x in <asdf> return x} return x') pyflwor.compile('for x in <asfd> let y = <x/asdf>, x = <adf> return x') pyflwor.compile('for x in <asfd> let y = <x/asdf> let x = <adf> return x') pyflwor.compile('for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> return x') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q) return x''') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q) return x,y,z''') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q) return x,y.sdf.asd,z''') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q) return x,y.sdf.asd,z()()()[asdf][asfd](1,2,3)''') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q) return 'asdf':asdf''') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q) return 'asdf':asdf, "hello":"hello World!"''') pyflwor.compile('''for x in <asfd>, z in <asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q == z and (<y> is not <z>)) return 'asdf':asdf, "one":1, "2.0":2.0''') pyflwor.compile('''for x in <asfd>, z in <asdf>, y in <asdf/asdf> let y = <x/asdf> let x = <adf> where every x in <y> satisfies (q == z and (<y> is not <z>)) return 'asdf':asdf, "one":1, "2.0":2.0''') pyflwor.compile('''for x in <asfd>, z in <asdf>, y in <asdf/asdf> let y = <x/asdf>, y1 = <Afd>, y2 = <asdf>, y3 = <asdf> let x = <adf> where every x in <y> satisfies (q == z and (<y> is not <z>)) return 'asdf':asdf, "one":1, "2.0":2.0''')
Order(customers[4], agents[1], products[1], randint(0,100)), Order(customers[2], agents[1], products[1], randint(0,100)), ] if __name__ == '__main__': import pyflwor print "all orders" for x in orders: print x print print print print "Orders where customer.name = Steve and agent.name = Ullman" q = pyflwor.compile('orders[self.customer.name == "Steve" and self.agent.name == "Ullman"]') for x in q(locals()): print x print print print print "1. Get names of products that are ordered by at least one customer three different times." q = pyflwor.compile(''' products [ some o1 in <orders> satisfies ( some o2 in <orders> satisfies ( some o3 in <orders> satisfies
def test_where_exists(self): pyflwor.compile('hello[wheRe]/hello[asdf]') pyflwor.compile('hello/hello[asdf]/asdf/wef') pyflwor.compile('hello/hello/wewe[asdf]/wef/waef/awef/weaf') pyflwor.compile('hello/hello[asdf] /wewe/ wef[asdf] /waef/awef[asdf]/weaf') self.assertRaises(SyntaxError, pyflwor.compile, 'hello/aef[asdf] hello[adsf]')
def test_slash(self): pyflwor.compile('hello/hello') pyflwor.compile('hello/hello/asdf/wef') pyflwor.compile('hello/hello/wewe/wef/waef/awef/weaf') pyflwor.compile('hello/hello /wewe/ wef /waef/awef/weaf') self.assertRaises(SyntaxError, pyflwor.compile, 'hello/awef/awef hello/awef')
def test_hello(self): pyflwor.compile('hello') self.assertRaises(SyntaxError, pyflwor.compile, 'hello hello')
def test_if(self): pyflwor.compile(''' for x in <asdf> return if (0) then 1 else 0 ''')
def test_flwr_function_args(self): pyflwor.compile(''' for x in <asdf> let f = function(q) { for y in q return y } return f ''')
def test_where_bool(self): pyflwor.compile('hello[a and b]') pyflwor.compile('hello[a or b]') pyflwor.compile('hello[not a or b]') pyflwor.compile('hello[a or not b]') pyflwor.compile('hello[not a and b]') pyflwor.compile('hello[a and not b]') pyflwor.compile('hello[not a or not b]') pyflwor.compile('hello[not a and not b]')
def test_where_parens_bool(self): pyflwor.compile('hello[((a and b) and not (a or b) or not (a and b)) and not (not a or b)]')
def test_hello(self): hello = 'hello world!' q = pyflwor.compile('hello') self.assertEquals(q(locals()), oset([hello]))
def test_where_value(self): pyflwor.compile('hello[a]') pyflwor.compile("hello[a[0]['hello']]") pyflwor.compile("hello[a[0][\"hello\"]]") pyflwor.compile("hello[a[0]['hello']()]") pyflwor.compile("hello[a[0]['hello'](0)]") pyflwor.compile("hello[a[0]['hello']('asdf')]") pyflwor.compile("hello[a[0]['hello'](asdf)]") pyflwor.compile("hello[a[0]['hello'](0, 'asdf', asdf)()()(1,2)]") pyflwor.compile('hello["sadf"]') pyflwor.compile('hello[123]') pyflwor.compile('hello[123.234]') pyflwor.compile('hello[asdf.asfd.asdf]') pyflwor.compile('hello[asdf[12].asfd().asdf]')
def test_hello(self): hello = "hello world!" q = pyflwor.compile("hello") self.assertEquals(q(locals()), oset([hello]))
def test_setops(self): pyflwor.compile('asdf - asdf') pyflwor.compile('asdf & asdf') pyflwor.compile('asdf | asdf') pyflwor.compile('(asdf | asdf) & asdf - (asdf & asdf) - (asdf & (afsd | asdf))') pyflwor.compile('asdf/asdf - asdf/asd[erw]')