def produce_tables(version="sbol_2", lib_paths=None): ''' Method that is independant from the rdf/xml production, simply runs the parsing and evaluation process on the templates to produce the symbols and template tables. This process is just the parse_from file method and returns the tables. ''' if lib_paths is None: optpaths = [os.path.join(os.getcwd(), "templates")] else: optpaths = [lib_paths] to_run_fn = os.path.join(optpaths[0], "temp.shb") f = open(to_run_fn, "a") f.write("use <" + version + ">") f.close() parser = Parser(filename=to_run_fn, debug_lvl=1) with open(to_run_fn, 'r') as in_file: data = in_file.read() env = Env(filename=to_run_fn, serializer="sbolxml", paths=optpaths, version=version) forms = parser.parse(data) forms = pre_process(forms, version) env.interpret(forms) prefixes = [prefix for prefix in env._rdf._g.namespaces()] os.remove(to_run_fn) return env._symbol_table, env._template_table, prefixes
def setUp(self): self.env = Env() self.v_uri = Uri('http://test.triplepack/#variable') self.env.assign(self.v_uri, Value(42)) self.template = Template(Name('A'), [Name('x'), Name('y')], [ Property(Name('x'), Value(42)), Property(Uri('http://example.eg/predicate'), Name('y')) ]) self.expansion = Expansion(Name('e'), Name('A'), [Value(1), Value(2)], []) self.template.evaluate(self.env) def triple_eval(triple): (s, p, o) = triple s = s.evaluate(self.env) p = p.evaluate(self.env) o = o.evaluate(self.env) return (s, p, o) triples = self.expansion.as_triples(self.env) triples = [triple_eval(triple) for triple in triples] bindings = self.env._symbol_table templates = self.env._template_table self.pack = TriplePack(triples, bindings, templates, [])
def test_importer_test_files(self): env = Env(paths=["test/test_files/"]) parser = RDFScriptParser() with open("test/test_files/top.shb", 'r') as f: script = f.read() try: result = parser.parse(script) env.interpret(result) except FileNotFoundError as e: self.fail(e) self.assertEqual(len(result), 5)
def setUp(self): self.env = Env() self.parser = Parser() self.vuri = self.parser.parse( '<http://test.triplepack/#variable>')[0].parts[0] self.env.assign(self.vuri, Value(42)) self.template = self.parser.parse( ('A(x, y)(x = 42 ' '<http://example.eg/predicate> = y)'))[0] self.expansion = self.parser.parse('e is a A(1, 2)')[0] self.template.evaluate(self.env) triples = self.expansion.as_triples(self.env) triples = [triple_eval(triple, self.env) for triple in triples] bindings = self.env._symbol_table templates = self.env._template_table self.pack = TriplePack(triples, bindings, templates, self.env._paths)
def setUp(self): self.env = Env()
def setUp(self): self.env = Env() self.parser = Parser()
class LogicExtensionsTest(unittest.TestCase): def setUp(self): self.env = Env() self.v_uri = Uri('http://test.triplepack/#variable') self.env.assign(self.v_uri, Value(42)) self.template = Template(Name('A'), [Name('x'), Name('y')], [ Property(Name('x'), Value(42)), Property(Uri('http://example.eg/predicate'), Name('y')) ]) self.expansion = Expansion(Name('e'), Name('A'), [Value(1), Value(2)], []) self.template.evaluate(self.env) def triple_eval(triple): (s, p, o) = triple s = s.evaluate(self.env) p = p.evaluate(self.env) o = o.evaluate(self.env) return (s, p, o) triples = self.expansion.as_triples(self.env) triples = [triple_eval(triple) for triple in triples] bindings = self.env._symbol_table templates = self.env._template_table self.pack = TriplePack(triples, bindings, templates, []) def test_and_true_true(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) conjunction = And(true, true) triples = list(self.pack.triples) conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_and_true_false(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = And(true, false) triples = list(self.pack.triples) with self.assertRaises(ExtensionError): conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_and_false_true(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = And(false, true) triples = list(self.pack.triples) with self.assertRaises(ExtensionError): conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_and_false_false(self): false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = And(false, false) triples = list(self.pack.triples) with self.assertRaises(ExtensionError): conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_or_true_true(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) conjunction = Or(true, true) triples = list(self.pack.triples) conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_or_true_false(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = Or(true, false) triples = list(self.pack.triples) conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_or_false_true(self): true = AtLeastOne(Uri('http://example.eg/predicate', None)) false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = Or(false, true) triples = list(self.pack.triples) conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_or_false_false(self): false = AtLeastOne(Uri('http://test.eg/#notthere', None)) conjunction = Or(false, false) triples = list(self.pack.triples) with self.assertRaises(ExtensionError): conjunction.run(self.pack) self.assertEqual(triples, self.pack.triples)
class EnvTest(unittest.TestCase): def setUp(self): self.env = Env() self.parser = Parser() def tearDown(self): None def test_prefix_binding(self): prefix_uri = Uri('http://test.prefix.eg/') prefix = 'prefix' self.env.bind_prefix(prefix, prefix_uri) self.assertTrue('prefix' in [p for (p, n) in self.env._rdf._g.namespaces()]) def test_get_and_set_default_prefix(self): prefix = 'x' self.env.bind_prefix(prefix, Uri('http://eg/')) before = Uri(self.env._rdf._g.identifier.toPython()) self.assertEqual(before, self.env.uri) self.assertEqual(self.env.prefix, None) self.env.prefix = prefix self.env.uri = Uri('http://eg/') self.assertEqual(Uri('http://eg/'), self.env.uri) self.assertEqual('x', self.env.prefix) def test_self_uri_set(self): uri = Uri('setselfuri') self.env.self_uri = uri self.assertEqual(self.env.self_uri, uri) def test_assignment(self): uri = Uri('http://test.variable/#x') value = Value(12345) self.env.assign(uri, value) self.assertEqual(self.env._symbol_table.get(uri), value) def test_lookup(self): uri = Uri('http://test.variable/#x') value = Value(12345) self.env.assign(uri, value) self.assertEqual(self.env.lookup(uri), value) def test_template_binding(self): template = self.parser.parse('t()(x = 1 y = 2)')[0] uri = template.identifier.evaluate(self.env) with self.assertRaises(KeyError): self.env.lookup_template(uri) self.env.assign_template(uri, template.as_triples(self.env)) self.assertEqual(self.env._template_table.get(uri, False), template.as_triples(self.env)) def test_template_lookup(self): template = self.parser.parse('t()(x = 1 y = 2)')[0] uri = template.identifier.evaluate(self.env) with self.assertRaises(KeyError): self.env.lookup_template(uri) self.env._template_table[uri] = template.as_triples(self.env) self.assertEqual(self.env.lookup_template(uri), template.as_triples(self.env)) def test_extension_binding(self): t = self.parser.parse('t()(@extension E() @extension F())')[0] uri = t.identifier.evaluate(self.env) extensions = t.extensions self.assertFalse(self.env.lookup_extensions(uri)) self.env.assign_extensions(uri, extensions) self.assertEqual(extensions, self.env._extension_table.get(uri, None)) def test_extension_lookup(self): t = self.parser.parse('t()(@extension E() @extension F())')[0] uri = t.identifier.evaluate(self.env) extensions = t.extensions self.assertFalse(self.env.lookup_extensions(uri)) self.env._extension_table[uri] = extensions self.assertEqual(extensions, self.env.lookup_extensions(uri))
class CardinalityExtensionsTest(unittest.TestCase): def setUp(self): self.env = Env() self.v_uri = Uri('http://test.triplepack/#variable', None) self.env.assign(self.v_uri, Value(42, None)) self.template = Template(Name('A'), [Name('x'), Name('y')], [ Property(Name('x'), Value(42)), Property(Uri('http://example.eg/predicate'), Name('y')) ]) self.expansion = Expansion(Name('e'), Name('A'), [Value(1), Value(2)], []) self.template.evaluate(self.env) def triple_eval(triple): (s, p, o) = triple s = s.evaluate(self.env) p = p.evaluate(self.env) o = o.evaluate(self.env) return (s, p, o) triples = self.expansion.as_triples(self.env) triples = [triple_eval(triple) for triple in triples] bindings = self.env._symbol_table templates = self.env._template_table self.pack = TriplePack(triples, bindings, templates, []) def test_at_least_one(self): triples = list(self.pack.triples) with self.assertRaises(CardinalityError): ext = AtLeastOne(Uri('http://test.eg/#notthere')) ext.run(self.pack) ext = AtLeastOne(Uri('http://example.eg/predicate')) ext.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_exactly_one_fails(self): with self.assertRaises(CardinalityError): ext = ExactlyOne(Uri('http://test.eg/#notthere')) ext.run(self.pack) with self.assertRaises(CardinalityError): ext = ExactlyOne(Uri('http://example.eg/predicate')) add = self.pack.search( (None, Uri('http://example.eg/predicate'), None))[0] self.pack.add(add) ext.run(self.pack) def test_exactly_one_succeeds(self): triples = list(self.pack.triples) ext = ExactlyOne(Uri('http://example.eg/predicate')) ext.run(self.pack) self.assertEqual(triples, self.pack.triples) def test_exactly_N_fails(self): with self.assertRaises(CardinalityError): ext = ExactlyN(Uri('http://example.eg/predicate'), 2) ext.run(self.pack) with self.assertRaises(CardinalityError): ext = ExactlyN(Uri('http://example.eg/predicate'), 2) add = self.pack.search( (None, Uri('http://example.eg/predicate'), None))[0] self.pack.add(add) self.pack.add(add) ext.run(self.pack) def test_exactly_N_succeeds(self): with self.assertRaises(CardinalityError): ext = ExactlyN(Uri('http://example.eg/predicate'), 2) ext.run(self.pack) ext = ExactlyN(Uri('http://example.eg/predicate'), 2) add = self.pack.search( (None, Uri('http://example.eg/predicate'), None))[0] self.pack.add(add) ext.run(self.pack)
def setUp(self): self.env = Env() self.parser = Parser() self.maxDiff = None
class TemplateClassTest(unittest.TestCase): def setUp(self): self.env = Env() self.parser = Parser() self.maxDiff = None def tearDown(self): None def test_init(self): ''' Simplest check, does Templates Identifier get added to parts list. ''' template = Template(Identifier(Name('x')), [], []) self.assertEqual(template.identifier.parts[0], Name('x')) def test_as_triples_empty(self): ''' Tests if triples list is empty when no parser call. ''' template = Template(Identifier(Name('x')), [], []) self.assertCountEqual(template.as_triples(self.env), []) def test_as_triples_simple_triple(self): ''' Tests if triples list is not empty when parser call. ''' template = self.parser.parse('t()(x = z)')[0] expect = [(Identifier(Self()), Identifier(Name('x')), Identifier(Name('z')))] self.assertCountEqual(template.as_triples(self.env), expect) def test_as_triples_simple_triple_with_self(self): ''' Tests if triples list is not empty when parser call with self. ''' template = self.parser.parse('t()(x = self)')[0] expect = [(Identifier(Self()), Identifier(Name('x')), Identifier(Self()))] self.assertCountEqual(template.as_triples(self.env), expect) def test_as_triples_multiple_properties(self): ''' Tests if triples list is not empty when parser call with multiple properties. ''' template = self.parser.parse('t()(x = y z = 12345)')[0] expect = [(Identifier(Self()), Identifier(Name('x')), Identifier(Name('y'))), (Identifier(Self()), Identifier(Name('z')), Value(12345))] self.assertCountEqual(template.as_triples(self.env), expect) def test_as_triples_with_base(self): ''' Test for correctness when a base and specialised template are defined ''' forms = self.parser.parse('a()(x = 1) b()(a() y = 2)') base = forms[0] specialised = forms[1] base.evaluate(self.env) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')), Value(2))] self.assertCountEqual(specialised.as_triples(self.env), expect) def test_as_triples_with_base_chain(self): ''' Test for correctness when a base and multiple/chained specialised templates are defined # Calls Expansion() ?? ''' forms = self.parser.parse('a()(x = 1)' + 'b()(a() y = 2)' + 'c()(b() z = 3)') a = forms[0] b = forms[1] c = forms[2] a.evaluate(self.env) b.evaluate(self.env) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')), Value(3))] self.assertCountEqual(c.as_triples(self.env), expect) def test_as_triples_with_base_with_self(self): ''' Test for correctness when a base and specialised templates are defined and base has self as property. ''' forms = self.parser.parse('a()(x = self) b()(a() y = 2)') base = forms[0] specialised = forms[1] base.evaluate(self.env) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Identifier(Self())), (Identifier(Self()), Identifier(Name('y')), Value(2))] self.assertCountEqual(specialised.as_triples(self.env), expect) def test_as_triples_params(self): ''' Test for correctness when a template is defined with param and set to property ''' forms = self.parser.parse('a(x)(property = x)') a = forms[0] expect = [(Identifier(Self()), Identifier(Name('property')), Identifier(Parameter('x',0)))] for index,triple in enumerate(a.as_triples(self.env)): for tuple_index,element in enumerate(triple): self.assertEqual(element,expect[index][tuple_index]) self.assertCountEqual(a.as_triples(self.env), expect) def test_as_triples_with_base_with_params(self): ''' Test for correctness when a template is defined and property is set to value. ''' forms = self.parser.parse('a(x)(x = 12345) b(y)(a(y))') a = forms[0] b = forms[1] template_name = a.identifier.evaluate(self.env) self.env.assign_template(template_name, a.as_triples(self.env)) expect = [(Identifier(Self()), Identifier(Parameter('y', 0)), Value(12345))] self.assertCountEqual(expect, b.as_triples(self.env)) def test_as_triples_with_base_with_args(self): ''' Test for correctness when a template is defined and property is set to value. And specialised template contains arg ''' forms = self.parser.parse('a(x)(x = 12345) b()(a(12345))') a = forms[0] b = forms[1] a.evaluate(self.env) expect = [(Identifier(Self()), Value(12345), Value(12345))] self.assertCountEqual(expect, b.as_triples(self.env)) def test_as_triples_multiple_base_args_and_parameters(self): forms = self.parser.parse('a(x, y)(x = y)' + 'b(x, y)(a(x, "string") z=y)' + 'c(x, y)(b(1, 2) x=y)') a = forms[0] b = forms[1] c = forms[2] a.evaluate(self.env) b.evaluate(self.env) expect = [(Identifier(Self()), Value(1), Value("string")), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Parameter('x', 0)), Identifier(Parameter('y', 0)))] self.assertCountEqual(expect, c.as_triples(self.env)) @unittest.skip("There is no longer a current_self") def test_current_self_preserved(self): forms = self.parser.parse('a(x, y, z)(self=self)') a = forms[0] previous_self = self.env.uri a.as_triples(self.env) new_self = self.env.current_self self.assertEqual(previous_self, new_self) def test_as_triples_with_expansion_in_property(self): forms = self.parser.parse('a()(x = 1) b()(y = e is a a())') a = forms[0] b = forms[1] a.evaluate(self.env) expect = [(Identifier(Name('e')), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')), Identifier(Name('e')))] self.assertCountEqual(expect, b.as_triples(self.env)) def test_as_triples_with_expansion_in_property_with_self(self): forms = self.parser.parse('a()(x = 1) b()(self.y = e is a a())') a = forms[0] b = forms[1] a.evaluate(self.env) expect = [(Identifier(Name('e')), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Self(), Name('y')), Identifier(Name('e')))] self.assertCountEqual(expect, b.as_triples(self.env)) def test_as_triples_with_expansion_in_property_with_self_as_name(self): forms = self.parser.parse('a()(x = 1) b()(y = self.e is a a())') a = forms[0] b = forms[1] a.evaluate(self.env) expect = [(Identifier(Self(), Name('e')), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')), Identifier(Self(), Name('e')))] self.assertCountEqual(expect, b.as_triples(self.env)) def test_as_triples_with_base_with_self_named_expansion(self): forms = self.parser.parse('s()(z=self) t()(x=self.e is a s())') s = forms[0] t = forms[1] s.evaluate(self.env) e = Identifier(Self(), Name('e')) expect_s = [(Identifier(Self()), Identifier(Name('z')), Identifier(Self()))] self.assertCountEqual(expect_s, s.as_triples(self.env)) expect_t = [(e, Identifier(Name('z')).evaluate(self.env), e), (Identifier(Self()), Identifier(Name('x')), e)] self.assertCountEqual(expect_t, t.as_triples(self.env)) def test_as_triples_with_expansion_as_argument(self): forms = self.parser.parse('r()(y=1) s(exp)(x=exp)' + 't()(s(e is a r()))') r = forms[0] s = forms[1] t = forms[2] r.evaluate(self.env) s.evaluate(self.env) e = self.parser.parse('e is a r()')[0] expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), e)] self.assertCountEqual(expect, t.as_triples(self.env)) def test_evaluate_stores_triples(self): forms = self.parser.parse('t()(x=1 y=2)') t = forms[0] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) t.evaluate(self.env) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_of_base(self): forms = self.parser.parse('s()(z=3)t()(x=1 y=2 s())') s = forms[0] t = forms[1] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) s.evaluate(self.env) t.evaluate(self.env) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), Value(3))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_of_bases(self): forms = self.parser.parse('q()(a=4) s()(z=3)t()(x=1 y=2 s() q())') q = forms[0] s = forms[1] t = forms[2] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) q.evaluate(self.env) s.evaluate(self.env) t.evaluate(self.env) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), Value(3)), (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), Value(4))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_of_chained_bases(self): forms = self.parser.parse('q()(a=4) s()(z=3 q())t()(x=1 y=2 s())') q = forms[0] s = forms[1] t = forms[2] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) q.evaluate(self.env) s.evaluate(self.env) t.evaluate(self.env) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), Value(3)), (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), Value(4))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_parameterised_bases(self): forms = self.parser.parse('q(p)(a=p)' + 's(t)(z=t q("s"))' + 't()(x=1 y=2 s("t"))') q = forms[0] s = forms[1] t = forms[2] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) q.evaluate(self.env) s.evaluate(self.env) t.evaluate(self.env) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), Value("t")), (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), Value("s"))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_forward_parameterised_bases(self): forms = self.parser.parse('q(p)(a=p)' + 's(t)(z=t q(t))' + 't()(x=1 y=2 s("t"))') q = forms[0] s = forms[1] t = forms[2] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) q.evaluate(self.env) s.evaluate(self.env) t.evaluate(self.env) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), Value("t")), (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), Value("t"))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_unevaluated_parameter(self): forms = self.parser.parse('q(p)(a=p)' + 's(t)(z=t q(t))' + 't(t)(x=1 y=2 s(t))') q = forms[0] s = forms[1] t = forms[2] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) q.evaluate(self.env) s.evaluate(self.env) t.evaluate(self.env) param = Identifier(Parameter('t', 0)) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), param), (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), param)] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_unevaluated_parameters(self): forms = self.parser.parse('q(p)(a=p)' + 's(u,v)(z=u q(v))' + 't(a,b)(x=1 y=2 s(a,b))') q = forms[0] s = forms[1] t = forms[2] with self.assertRaises(KeyError): self.env.lookup_template(t.identifier.evaluate(self.env)) q.evaluate(self.env) s.evaluate(self.env) t.evaluate(self.env) a = Identifier(Parameter('a', 0)) b = Identifier(Parameter('b', 1)) found = self.env.lookup_template(t.identifier.evaluate(self.env)) expect = [(Identifier(Self()), Identifier(Name('x')).evaluate(self.env), Value(1)), (Identifier(Self()), Identifier(Name('y')).evaluate(self.env), Value(2)), (Identifier(Self()), Identifier(Name('z')).evaluate(self.env), a), (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), b)] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_expansion_as_property(self): forms = self.parser.parse('a(x)(p=x)' + 'b()(q = e is a a(2))') a = forms[0] b = forms[1] a.evaluate(self.env) b.evaluate(self.env) env = self.env me = Identifier(Self()) found = env.lookup_template(b.identifier.evaluate(env)) expect = [(Identifier(Name('e')).evaluate(env), Identifier(Name('p')).evaluate(env), Value(2)), (me, Identifier(Name('q')).evaluate(env), Identifier(Name('e')).evaluate(env))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_expansion_in_body(self): forms = self.parser.parse('a(x)(p=x)' + 'b()(e is a a(2))') a = forms[0] b = forms[1] a.evaluate(self.env) b.evaluate(self.env) env = self.env found = env.lookup_template(b.identifier.evaluate(env)) expect = [(Identifier(Name('e')).evaluate(env), Identifier(Name('p')).evaluate(env), Value(2))] self.assertCountEqual(found, expect) def test_evaluate_stores_triples_with_expansion_in_argument(self): forms = self.parser.parse('a(x)(p=x)' + 'b(x)(e=x)' + 'c()(b(f is a a(2)))') a = forms[0] b = forms[1] c = forms[2] a.evaluate(self.env) b.evaluate(self.env) c.evaluate(self.env) env = self.env me = Identifier(Self()) found = env.lookup_template(c.identifier.evaluate(env)) expect = [(me, Identifier(Name('e')).evaluate(env), Identifier(Name('f')).evaluate(env))] self.assertCountEqual(found, expect) expect = [(Identifier(Name('f')).evaluate(env), Identifier(Name('p')).evaluate(env), Value(2))] self.assertCountEqual(self.env._rdf.triples, expect) def test_evaluate_stores_triples_with_expansions(self): forms = self.parser.parse('a(x)(e=x)' + 'b(y)(p=y)' + 'c()(a(f is a b(1))' + ' q = g is a b(2)' + ' h is a b(3))') a = forms[0] b = forms[1] c = forms[2] a.evaluate(self.env) b.evaluate(self.env) c.evaluate(self.env) me = Identifier(Self()) env = self.env f = Identifier(Name('f')).evaluate(env) g = Identifier(Name('g')).evaluate(env) h = Identifier(Name('h')).evaluate(env) found = env.lookup_template(c.identifier.evaluate(env)) expect = [(me, Identifier(Name('e')).evaluate(env), f), (g, Identifier(Name('p')).evaluate(env), Value(2)), (me, Identifier(Name('q')).evaluate(env), g), (h, Identifier(Name('p')).evaluate(env), Value(3))] self.assertCountEqual(found, expect) expect = [(f, Identifier(Name('p')).evaluate(env), Value(1))] self.assertCountEqual(self.env._rdf.triples, expect) def test_init_extensions(self): forms = self.parser.parse('t()(@extension E() @extension F())') t = forms[0] expect = [ExtensionPragma('E', []), ExtensionPragma('F', [])] self.assertCountEqual(t.collect_extensions(self.env), expect) def test_evaluate_stores_extensions(self): forms = self.parser.parse('t()(@extension E() @extension F())') t = forms[0] t.evaluate(self.env) found = self.env.lookup_extensions(t.identifier.evaluate(self.env)) self.assertCountEqual(found, [ExtensionPragma( 'E', []), ExtensionPragma('F', [])]) def test_evaluate_stores_base_extensions(self): forms = self.parser.parse( 's()(@extension F()) t()(s() @extension E())') s = forms[0] t = forms[1] s.evaluate(self.env) t.evaluate(self.env) found = self.env.lookup_extensions(t.identifier.evaluate(self.env)) expect = [ExtensionPragma('F', []), ExtensionPragma('E', [])] self.assertCountEqual(found, expect) def test_evaluate_extension_arguments(self): forms = self.parser.parse('t()(@extension E(12345))') t = forms[0] t.evaluate(self.env) found = self.env.lookup_extensions(t.identifier.evaluate(self.env)) self.assertCountEqual(found, [ExtensionPragma('E', [Value(12345)])]) def test_evaluate_extension_arguments_name(self): forms = self.parser.parse('t()(@extension E(argument))') t = forms[0] t.evaluate(self.env) found = self.env.lookup_extensions(t.identifier.evaluate(self.env)) expect = [ExtensionPragma('E', [Identifier(Name('argument')).evaluate(self.env)])] self.assertCountEqual(found, expect) def test_evaluate_extension_self_name(self): forms = self.parser.parse('t()(@extension E(self.argument))') t = forms[0] self.env.current_self = Name(Self()) t.evaluate(self.env) found = self.env.lookup_extensions(t.identifier.evaluate(self.env)) expect = [ExtensionPragma('E', [Identifier(Self(), Name('argument'))])] self.assertCountEqual(found, expect) def test_evaluate_to_template_name(self): forms = self.parser.parse('t()(x=1 y=2)') t = forms[0] self.assertEqual(t.identifier.evaluate(self.env), t.evaluate(self.env)) def test_extension_parameters(self): forms = self.parser.parse('t(a)(@extension AtLeastOne(a))') t = forms[0] t.evaluate(self.env) atleastone = self.env.lookup_extensions(t.identifier.evaluate(self.env))[0] arg = Identifier(t.parameters[1]) self.assertEqual(arg, atleastone.args[0]) def test_extension_parameters_multiple(self): forms = self.parser.parse('t(a, b)(@extension AtLeastOne(a, b))') t = forms[0] t.evaluate(self.env) atleastone = self.env.lookup_extensions(t.identifier.evaluate(self.env))[0] args = [Identifier(p) for p in t.parameters[1:]] self.assertCountEqual(args, atleastone.args) def test_bodied_expansion_in_template(self): forms = self.parser.parse('s()(a = 1)' + 't()(self.e is a s()(b = 2))') s = forms[0] t = forms[1] s.evaluate(self.env) expect = [(Identifier(Self(), Name('e')), Identifier(Name('a')).evaluate(self.env), Value(1)), (Identifier(Self(), Name('e')), Identifier(Name('b')), Value(2))] self.assertCountEqual(expect, t.as_triples(self.env)) def test_bodied_expansion_in_template_property(self): forms = self.parser.parse('s()(a = 1)' + 't()(x = self.e is a s()(b = 2))') s = forms[0] t = forms[1] s.evaluate(self.env) expect = [(Identifier(Self(), Name('e')), Identifier(Name('a')).evaluate(self.env), Value(1)), (Identifier(Self(), Name('e')), Identifier(Name('b')), Value(2)), (Identifier(Self()), Identifier(Name('x')), Identifier(Self(), Name('e')))] self.assertCountEqual(expect, t.as_triples(self.env)) def test_as_triples_multiple_inheritance(self): forms = self.parser.parse('s()(a=123)' + 't()(b=456)' + 'u()(s() t())') s = forms[0] t = forms[1] u = forms[2] s.evaluate(self.env) t.evaluate(self.env) expect = [(Identifier(Self()), Identifier(Name('a')).evaluate(self.env), Value(123)), (Identifier(Self()), Identifier(Name('b')).evaluate(self.env), Value(456))] self.assertEqual(expect, u.as_triples(self.env)) def test_as_triples_nested_multiple_inheritance(self): forms = self.parser.parse('s()(a=123)' + 't()(s() b=456)' + 'u()(s() t() c=789)' + 'v()(s() t() u())') s = forms[0] t = forms[1] u = forms[2] v = forms[3] s.evaluate(self.env) t.evaluate(self.env) u.evaluate(self.env) a = (Identifier(Self()), Identifier(Name('a')).evaluate(self.env), Value(123)) b = (Identifier(Self()), Identifier(Name('b')).evaluate(self.env), Value(456)) c = (Identifier(Self()), Identifier(Name('c')).evaluate(self.env), Value(789)) expect = [a, a, b, a, a, b, c] self.assertEqual(expect, v.as_triples(self.env))
def parse_from_file(filepath, serializer='sbolxml', optpaths=[], out=None, extensions=[], debug_lvl=1, version="sbol_2", no_validation=None): if version == "sbol_3" and serializer == "sbolxml": serializer = "rdfxml" if len(optpaths) == 0: optpaths.append("templates") parser = Parser(filename=filepath, debug_lvl=debug_lvl) with open(filepath, 'r', encoding="utf8") as in_file: data = in_file.read() env = Env(filename=filepath, serializer=serializer, paths=optpaths, extensions=extensions, version=version) forms = parser.parse(data) forms = pre_process(forms, version) env.interpret(forms) sbol = str(env) ret_code = "" if not no_validation: if version == "sbol_3": ret_code = "No Validation Currently for SBOL3" print(ret_code) errors = [] else: errors = [] response = validate_sbol(sbol) try: if response['valid']: print('SBOL validator success.') ret_code = "SBOL validator success." else: print("SBOL validator failure.") for e in response['errors']: print(e) errors = response['errors'] ret_code = "SBOL validator failure." except TypeError: errors = ["Unable to Validate output."] else: ret_code = "No Validation." errors = ["No Validation."] if out is None: print(sbol) else: with open(out, 'w') as o: o.write(sbol) return {ret_code: errors}
def setUp(self): self.env = Env() self.env.current_self = Uri('') self.env.bind_prefix('p', Uri('prefix')) self.env.prefix = 'p'
class TestCoreIdentifier(unittest.TestCase): def setUp(self): self.env = Env() self.env.current_self = Uri('') self.env.bind_prefix('p', Uri('prefix')) self.env.prefix = 'p' def test_identifier_equal(self): i1 = Identifier(Name('n1'), Name('n2'), Name('n3')) i2 = Identifier(Name('n1'), Name('n2'), Name('n3')) self.assertEqual(i1, i2) i3 = Identifier(Name('n1'), Name('n2')) self.assertNotEqual(i1, i3) def test_identifier_evaluate_unbound_single_name(self): i = Identifier(Name('first')) expected = Uri('prefixfirst') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_unbound_two_names(self): i = Identifier(Name('first'), Name('second')) expected = Uri('prefixfirstsecond') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_unbound_self_as_prefix(self): i = Identifier(Self(), Name('n')) expected = Identifier(Self(), Name('n')) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_multiple_unbound(self): i = Identifier(Name('n1'), Name('n2'), Name('n3'), Name('n4')) expected = Uri('prefixn1n2n3n4') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_local(self): i = Identifier(Name('first')) expected = Value(1) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_prefixed(self): i = Identifier(Name('first'), Name('second')) expected = Value(1) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_multiple_parts(self): i = Identifier(Name('first'), Name('second'), Name('third'), Name('fourth')) expected = Value(1) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_uri_and_symbol_parts(self): i = Identifier(Uri('first'), Name('second')) expected = Value(1) uri = Uri('firstsecond') self.assertEqual(i.evaluate(self.env), uri) self.env.assign(i.evaluate(self.env), expected) actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_prefix(self): i = Identifier(Name('first'), Name('second')) prefix = Uri('http://first.org/#') self.env.assign(Identifier(Name('first')).evaluate(self.env), prefix) expected = Uri('http://first.org/#second') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_double_prefix(self): i = Identifier(Name('first'), Name('second'), Name('third')) prefix = Uri('http://first.org/#second#') uri = Identifier(Name('first'), Name('second')).evaluate(self.env) self.env.assign(uri, prefix) expected = Uri('http://first.org/#second#third') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_bound_prefix_not_uri(self): i = Identifier(Name('first'), Name('second')) value = Value(12345) self.env.assign(Identifier(Name('first')).evaluate(self.env), value) expected = Uri('prefixfirstsecond') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_evaluate_double_bound_prefix_not_uri(self): i = Identifier(Name('first'), Name('second'), Name('third')) value = Value(12345) prefix_part = Identifier(Name('first'), Name('second')) self.env.assign(prefix_part.evaluate(self.env), value) expected = Uri('prefixfirstsecondthird') actually = i.evaluate(self.env) self.assertEqual(expected, actually) def test_identifier_uri_first_unbound(self): i = Identifier(Uri('http://literal.eg/'), Name('literal')) expected = Uri('http://literal.eg/literal') actually = i.evaluate(self.env) self.assertEqual(expected, actually)
class TriplePackTest(unittest.TestCase): def setUp(self): self.env = Env() self.parser = Parser() self.vuri = self.parser.parse( '<http://test.triplepack/#variable>')[0].parts[0] self.env.assign(self.vuri, Value(42)) self.template = self.parser.parse( ('A(x, y)(x = 42 ' '<http://example.eg/predicate> = y)'))[0] self.expansion = self.parser.parse('e is a A(1, 2)')[0] self.template.evaluate(self.env) triples = self.expansion.as_triples(self.env) triples = [triple_eval(triple, self.env) for triple in triples] bindings = self.env._symbol_table templates = self.env._template_table self.pack = TriplePack(triples, bindings, templates, self.env._paths) def test_triples_init(self): exp_uri = Identifier(Name('e')).evaluate(self.env) triples = [(exp_uri, Value(1), Value(42)), (exp_uri, Uri('http://example.eg/predicate'), Value(2))] self.assertCountEqual(self.pack.triples, triples) self.assertCountEqual(self.pack.bindings, self.env._symbol_table) self.assertCountEqual(self.pack.templates, self.env._template_table) def test_triples_lookup(self): self.assertEqual(self.pack.lookup(self.vuri), Value(42)) self.assertEqual(self.pack.lookup(Uri('http://triplepack.org/#not')), None) def test_triples_lookup_template(self): self.assertCountEqual( self.pack.lookup_template( self.template.identifier.evaluate(self.env)), self.template.as_triples(self.env)) self.assertCountEqual( self.pack.lookup_template(Uri('http://triplepack.org/#not')), None) def test_triples_get_subjects_1(self): self.assertEqual(self.pack.subjects, set([self.expansion.identifier.evaluate(self.env)])) def test_triples_get_subjects_2(self): expansion = Expansion(Identifier(Name('f')), Identifier(Name('A')), [Value(1), Value(2)], []) triples = expansion.as_triples(self.env) triples = [triple_eval(triple, self.env) for triple in triples] doublePack = TriplePack(self.pack.triples + triples, self.pack.bindings, self.pack.templates, self.pack._paths) self.assertEqual( doublePack.subjects, set([ self.expansion.identifier.evaluate(self.env), expansion.identifier.evaluate(self.env) ])) def test_triples_get_subjects_empty(self): emptyPack = TriplePack([], {}, {}, []) self.assertEqual(emptyPack.subjects, set()) def test_triples_get_predicates(self): self.assertEqual(self.pack.predicates, set([Value(1), Uri('http://example.eg/predicate')])) def test_triples_get_predicates_empty(self): emptyPack = TriplePack([], {}, {}, []) self.assertEqual(emptyPack.predicates, set()) def test_triples_get_objects(self): self.assertEqual(self.pack.objects, set([Value(2), Value(42)])) def test_triples_get_objects_empty(self): emptyPack = TriplePack([], {}, {}, []) self.assertEqual(emptyPack.objects, set()) def test_triples_get_triples_by_subject(self): expected_result = [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42)), (Identifier(Name('e')).evaluate(self.env), Uri('http://example.eg/predicate'), Value(2))] self.assertEqual( self.pack.search( (Identifier(Name('e')).evaluate(self.env), None, None)), expected_result) self.assertEqual( self.pack.search( (Identifier(Name('f')).evaluate(self.env), None, None)), []) def test_triples_get_triples_by_predicate(self): self.assertEqual( self.pack.search((None, Value(1), None)), [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))]) self.assertEqual( self.pack.search((None, Uri('http://example.eg/predicate'), None)), [(Identifier(Name('e')).evaluate( self.env), Uri('http://example.eg/predicate'), Value(2))]) self.assertEqual(self.pack.search((None, Value(3), None)), []) def test_triples_get_triples_by_object(self): self.assertEqual( self.pack.search((None, None, Value(42))), [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))]) self.assertEqual(self.pack.search( (None, None, Value(2))), [(Identifier(Name('e')).evaluate( self.env), Uri('http://example.eg/predicate'), Value(2))]) self.assertEqual(self.pack.search((None, None, Value(3))), []) def test_triples_get_triples_exact(self): self.assertEqual( self.pack.search((Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))), [(Identifier(Name('e')).evaluate(self.env), Value(1), Value(42))]) self.assertEqual( self.pack.search((Value(1), Value(2, None), Value(3, None))), []) def test_triples_get_triples_subject_predicate(self): self.assertEqual( self.pack.search( (Identifier(Name('e')).evaluate(self.env), Value(1, None), None)), [(Identifier(Name('e')).evaluate(self.env), Value( 1, None), Value(42, None))]) self.assertEqual( self.pack.search((Identifier(Name('f')).evaluate(self.env), Value(1, None), None)), []) self.assertEqual( self.pack.search((Identifier(Name('e')).evaluate(self.env), Value(2, None), None)), []) def test_triples_get_triples_subject_object(self): self.assertEqual( self.pack.search((Identifier(Name('e')).evaluate(self.env), None, Value(42, None))), [(Identifier(Name('e')).evaluate(self.env), Value( 1, None), Value(42, None))]) self.assertEqual( self.pack.search((Identifier(Name('f')).evaluate(self.env), None, Value(42, None))), []) self.assertEqual( self.pack.search((Identifier(Name('e')).evaluate(self.env), None, Value(41, None))), []) def test_triples_get_triples_predicate_object(self): self.assertEqual( self.pack.search((None, Value(1, None), Value(42, None))), [(Identifier(Name('e')).evaluate(self.env), Value( 1, None), Value(42, None))]) self.assertEqual( self.pack.search((None, Value(2, None), Value(42, None))), []) self.assertEqual( self.pack.search((None, Value(1, None), Value(41, None))), []) def test_triples_subject_has_property(self): self.assertTrue( self.pack.has( Identifier(Name('e')).evaluate(self.env), Value(1, None))) self.assertFalse( self.pack.has( Identifier(Name('e')).evaluate(self.env), Value(2, None))) self.assertFalse( self.pack.has( Identifier(Name('f')).evaluate(self.env), Value(1, None))) def test_triples_subject_has_unique_property(self): self.assertTrue( self.pack.has_unique( Identifier(Name('e')).evaluate(self.env), Value(1, None))) self.assertFalse( self.pack.has_unique( Identifier(Name('e')).evaluate(self.env), Value(2, None))) self.assertFalse( self.pack.has_unique( Identifier(Name('f')).evaluate(self.env), Value(1, None))) duplicatePack = TriplePack( self.pack.triples + [(Identifier(Name('e')).evaluate( self.env), Value(1, None), Value(42, None))], self.pack.bindings, self.pack.templates, self.pack._paths) self.assertFalse( duplicatePack.has_unique( Identifier(Name('e')).evaluate(self.env), Value(1, None))) def test_triples_get_values_for(self): self.assertEqual( self.pack.value( Identifier(Name('e')).evaluate(self.env), Value(1, None)), Value(42, None)) self.assertEqual( self.pack.value( Identifier(Name('e')).evaluate(self.env), Value(2, None)), None) self.assertEqual( self.pack.value( Identifier(Name('f')).evaluate(self.env), Value(1, None)), None) duplicatePack = TriplePack( self.pack.triples + [(Identifier(Name('e')).evaluate( self.env), Value(1, None), Value(41, None))], self.pack.bindings, self.pack.templates, self.pack._paths) self.assertEqual( duplicatePack.value( Identifier(Name('e')).evaluate(self.env), Value(1, None)), [Value(42, None), Value(41, None)]) def test_triples_add(self): self.assertFalse( self.pack.has( Identifier(Name('e')).evaluate(self.env), Value('fake', None))) self.pack.add((Identifier(Name('e')).evaluate(self.env), Value('fake', None), Value('added', None))) self.assertTrue( self.pack.has( Identifier(Name('e')).evaluate(self.env), Value('fake', None))) self.assertEqual( self.pack.value( Identifier(Name('e')).evaluate(self.env), Value('fake', None)), Value('added', None)) def test_triples_set(self): self.assertEqual( self.pack.value( Identifier(Name('e')).evaluate(self.env), Value(1, None)), Value(42, None)) self.pack.set( Identifier(Name('e')).evaluate(self.env), Value(1, None), Value('set', None)) self.assertTrue( self.pack.has_unique( Identifier(Name('e')).evaluate(self.env), Value(1, None))) self.assertEqual( self.pack.value( Identifier(Name('e')).evaluate(self.env), Value(1, None)), Value('set', None)) self.pack.set( Identifier(Name('e')).evaluate(self.env), Value('fake', None), Value('set', None)) self.assertTrue( self.pack.has( Identifier(Name('e')).evaluate(self.env), Value('fake', None)))