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_expansion_one_arg_with_body(self): forms = self.parser.parse('e is a a(12345)(x=true)') expect = Expansion(Identifier(Name('e')), Identifier(Name('a')), [Value(12345)], [Property(Identifier(Name('x')), Value(True))]) self.assertEqual(expect, forms[0])
def test_expansion_one_arg_no_body(self): forms = self.parser.parse('e is a a(12345)') expect = Expansion(Identifier(Name('e')), Identifier(Name('a')), [Value(12345)], []) self.assertEqual(expect, forms[0])
def test_expansion_no_args_no_body(self): forms = self.parser.parse('e is a a()') expect = Expansion(Identifier(Name('e')), Identifier(Name('a')), [], []) self.assertEqual(expect, forms[0])
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_expansion_multiple_properties(self): forms = self.parser.parse('e is a a()(x=true y=false)') expect = Expansion(Identifier(Name('e')), Identifier(Name('a')), [], [Property(Identifier(Name('x')), Value(True)), Property(Identifier(Name('y')), Value(False))]) self.assertEqual(expect, forms[0])
def test_expansion_expansion_as_arg(self): forms = self.parser.parse('e is a a(f is a b(12345))') f = self.parser.parse('f is a b(12345)')[0] expect = Expansion(Identifier(Name('e')), Identifier(Name('a')), [f], []) self.assertEqual(expect, forms[0])
def test_empty_template_noargs(self): script = 'DNASequence()(Other())' forms = self.parser.parse(script) expected_template = Template( Identifier(Name('DNASequence')), [], [Expansion(None, Identifier(Name('Other')), [], [])]) self.assertCountEqual(forms, [expected_template])
def test_assignment_uri_expansion(self): script = '<expansion> = e is a t()' expected = [ Assignment( Identifier(Uri('expansion')), Expansion(Identifier(Name('e')), Identifier(Name('t')), [], [])) ] actually = self.parser.parse(script) self.assertEqual(expected, actually)
def test_template_onearg_base(self): script = 'B(x)(x = 42)\nA(x)(B(x) encoding = <SBOL:IUPACDNA>)' forms = self.parser.parse(script) expected_template = Template(Identifier(Name('A')), ['x'], [ Expansion(None, Identifier(Name('B')), [Identifier(Name('x'))], []), Property(Identifier(Name('encoding')), Identifier(Uri('SBOL:IUPACDNA'))) ]) self.assertCountEqual([forms[1]], [expected_template])
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 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)