class ParserLiteralTest(unittest.TestCase):
    def setUp(self):
        self.parser = Parser()

    def tearDown(self):
        None

    def test_parser_literal_boolean(self):
        forms = self.parser.parse('true false')
        self.assertEqual(forms, [Value(True, None), Value(False, None)])

    def test_parser_literal_double(self):
        forms = self.parser.parse('0.12345')
        self.assertEqual(forms, [Value(0.12345, None)])

    def test_parser_literal_integer(self):
        forms = self.parser.parse('12345')
        self.assertEqual(forms, [Value(12345, None)])

    def test_parser_literal_negative_integer(self):
        forms = self.parser.parse('-12345')
        self.assertEqual(forms, [Value(-12345, None)])

    def test_parser_literal_string(self):
        script = '"string with whitespace"'
        forms = self.parser.parse(script)
        self.assertEqual(forms, [Value("string with whitespace", None)])
class TestIdentifierParsing(unittest.TestCase):
    def setUp(self):
        self.parser = Parser()

    def tearDown(self):
        None

    def test_identifier_single_part(self):
        script = 'n'
        expected = [Identifier(Name('n'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_two_parts(self):
        script = 'p.n'
        expected = [Identifier(Name('p'), Name('n'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_three_parts(self):
        script = 'p.n.n1'
        expected = [Identifier(Name('p'), Name('n'), Name('n1'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_starts_with_uri(self):
        script = '<http://prefix/>.n'
        expected = [Identifier(Uri('http://prefix/'), Name('n'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_ends_with_uri(self):
        script = 'p.<n>'
        expected = [Identifier(Name('p'), Uri('n'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_uri_in_middle(self):
        script = 'p.<n>.n1'
        expected = [Identifier(Name('p'), Uri('n'), Name('n1'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_uri_only(self):
        script = '<http://name.eg/#test>'
        expected = [Identifier(Uri('http://name.eg/#test'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_identifier_self_as_prefix(self):
        script = 'self.n'
        expected = [Identifier(Self(), Name('n'))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_name_self_as_suffix(self):
        script = 'p.self'
        expected = [Identifier(Name('p'), Self())]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)
Example #3
0
class TestPropertyClass(unittest.TestCase):
    def setUp(self):
        self.env = Env()
        self.parser = Parser()

    def tearDown(self):
        None

    def test_as_triples_simple(self):
        p = Property(Identifier(Name('x')), Identifier(Name('y')))
        expected = [(Identifier(Self()), Identifier(Name('x')),
                     Identifier(Name('y')))]
        actually = p.as_triples(self.env)
        self.assertEqual(expected, actually)

    def test_as_triples_expansion(self):
        forms = self.parser.parse('t()(x=1) e is a t()')
        t = forms[0]
        e = forms[1]
        p = Property(Identifier(Name('y')), e)

        t.evaluate(self.env)

        expected = [(Identifier(Name('e')),
                     Identifier(Name('x')).evaluate(self.env), Value(1)),
                    (Identifier(Self()), Identifier(Name('y')),
                     Identifier(Name('e')))]
        actually = p.as_triples(self.env)
        self.assertEqual(expected, actually)

    def test_as_triples_self_in_name(self):
        p = Property(Identifier(Self()), Value(1))
        expected = [(Identifier(Self()), Identifier(Self()), Value(1))]
        actually = p.as_triples(self.env)
        self.assertEqual(expected, actually)
Example #4
0
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
Example #5
0
class ParserPragmaTest(unittest.TestCase):
    def setUp(self):
        self.parser = Parser()

    def tearDown(self):
        None

    def test_prefix_pragma_uri(self):
        script = "@prefix Prefix = <http://example.eg/>"
        expected = [
            PrefixPragma('Prefix', Identifier(Uri('http://example.eg/')))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_prefix_pragma_name(self):
        script = "@prefix Prefix = name"
        expected = [PrefixPragma('Prefix', Identifier(Name('name')))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_default_prefix_pragma(self):
        script = "@prefix Prefix"
        expected = [DefaultPrefixPragma('Prefix')]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_import_pragma_uri(self):
        script = "@use <import>"
        expected = [ImportPragma(Identifier(Uri('import')))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

        script = "use <import>"
        expected = [ImportPragma(Identifier(Uri('import')))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_import_pragma_name(self):
        script = "@use this.target"
        expected = [ImportPragma(Identifier(Name('this'), Name('target')))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

        script = "use this.target"
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_extension_pragma(self):
        expected = [ExtensionPragma('E', [])]
        actually = self.parser.parse('@extension E()')
        self.assertEqual(expected, actually)
class ParserAssignmentTest(unittest.TestCase):
    def setUp(self):
        self.parser = Parser()
        self.maxDiff = None
        self.logger = logging.getLogger(__name__)

    def tearDown(self):
        None

    def test_assignment_name_string(self):
        script = 'Name = "hello"'
        expected = [Assignment(Identifier(Name('Name')), Value("hello"))]
        actually = self.parser.parse(script)
        self.assertCountEqual(expected, actually)

    def test_assignment_name_integer(self):
        script = 'Name = 12345'
        expected = [Assignment(Identifier(Name('Name')), Value(12345))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_name_boolean(self):
        script = 'Name = true'
        expected = [Assignment(Identifier(Name('Name')), Value(True))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

        script = 'Name = false'
        expected = [Assignment(Identifier(Name('Name')), Value(False))]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_name_double(self):
        script = 'Name = 0.12345'
        actually = self.parser.parse(script)
        expected = [Assignment(Identifier(Name('Name')), Value(0.12345))]
        self.assertEqual(expected, actually)

    def test_assignment_name_uri(self):
        script = 'Name = <http://uri.org/>'
        expected = [
            Assignment(Identifier(Name('Name')),
                       Identifier(Uri('http://uri.org/')))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_name_name(self):
        script = 'Name = Name'
        expected = [
            Assignment(Identifier(Name('Name')), Identifier(Name('Name')))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_uri_string(self):
        script = '<http://uri.org/> = "hello"'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')), Value("hello"))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_uri_integer(self):
        script = '<http://uri.org/> = 12345'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')), Value(12345))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_uri_boolean(self):
        script = '<http://uri.org/> = true'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')), Value(True))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

        script = '<http://uri.org/> = false'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')), Value(False))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_uri_double(self):
        script = '<http://uri.org/> = 0.12345'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')), Value(0.12345))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_uri_uri(self):
        script = '<http://uri.org/> = <http://value.org>'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')),
                       Identifier(Uri('http://value.org')))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_uri_name(self):
        script = '<http://uri.org/> = Name'
        expected = [
            Assignment(Identifier(Uri('http://uri.org/')),
                       Identifier(Name('Name')))
        ]
        actually = self.parser.parse(script)

        self.assertEqual(expected, actually)

    def test_assignment_self_name(self):
        script = 'self.v = Name'
        expected = [
            Assignment(Identifier(Self(), Name('v')), Identifier(Name('Name')))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    def test_assignment_name_expansion(self):
        script = 'expansion = e is a t()'
        expected = [
            Assignment(
                Identifier(Name('expansion')),
                Expansion(Identifier(Name('e')), Identifier(Name('t')), [],
                          []))
        ]
        actually = self.parser.parse(script)
        self.assertEqual(expected, actually)

    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)
class ParserExpansionTest(unittest.TestCase):

    def setUp(self):
        self.parser = Parser()
        self.maxDiff = None

    def tearDown(self):
        None

    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_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_multi_args_no_body(self):
        forms = self.parser.parse('e is a a(12345, 54321)')
        expect = Expansion(Identifier(Name('e')),
                           Identifier(Name('a')),
                           [Value(12345),
                            Value(54321)],
                           [])

        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_expansion_no_args_with_body(self):
        forms = self.parser.parse('e is a a()(x=true)')
        expect = Expansion(Identifier(Name('e')),
                           Identifier(Name('a')),
                           [],
                           [Property(Identifier(Name('x')), Value(True))])
        self.assertEqual(expect, forms[0])

    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_multi_args_with_body(self):
        forms = self.parser.parse('e is a a(12345, 54321)(x=true)')
        expect = Expansion(Identifier(Name('e')),
                           Identifier(Name('a')),
                           [Value(12345),
                            Value(54321)],
                           [Property(Identifier(Name('x')), Value(True))])

        self.assertEqual(expect, forms[0])

    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])
Example #8
0
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))
Example #9
0
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))
Example #10
0
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}
Example #11
0
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)))
class ParserTemplateTest(unittest.TestCase):
    def setUp(self):
        self.parser = Parser()
        self.maxDiff = None
        self.logger = logging.getLogger(__name__)

    def tearDown(self):
        None

    def test_template_noargs_nobase(self):
        script = 'DNASequence()(encoding = <SBOL:IUPACDNA>)'
        forms = self.parser.parse(script)
        expected_template = Template(Identifier(Name('DNASequence')), [], [
            Property(Identifier(Name('encoding')),
                     Identifier(Uri('SBOL:IUPACDNA')))
        ])
        self.assertCountEqual(forms, [expected_template])

    def test_empty_template_noargs_nobase(self):
        script = 'DNASequence()'
        forms = self.parser.parse(script)
        expected_template = Template(Identifier(Name('DNASequence')), [], [])
        self.assertCountEqual(forms, [expected_template])

    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_empty_template_args(self):
        script = 'DNASequence(x, y, z)(Other(x))'
        forms = self.parser.parse(script)

        expected_template = Template(Identifier(
            Name('DNASequence')), ['x', 'y', 'z'], [
                Expansion(None, Identifier(Name('Other')),
                          [Identifier(Name('x'))], [])
            ])
        self.assertCountEqual(forms, [expected_template])

    def test_empty_template_args_nobase(self):
        script = 'DNASequence(x, y, z)'
        forms = self.parser.parse(script)
        expected_template = Template(Identifier(Name('DNASequence')),
                                     ['x', 'y', 'z'], [])
        self.assertCountEqual(forms, [expected_template])

    def test_template_onearg_nobase(self):
        script = 'DNASequence(x)(encoding = <SBOL:IUPACDNA>)'
        forms = self.parser.parse(script)
        expected_template = Template(Identifier(Name('DNASequence')), ['x'], [
            Property(Identifier(Name('encoding')),
                     Identifier(Uri('SBOL:IUPACDNA')))
        ])
        self.assertCountEqual(forms, [expected_template])

    def test_template_multiargs_nobase(self):
        script = 'DNASequence(x, y, z)(encoding = <SBOL:IUPACDNA>)'
        forms = self.parser.parse(script)

        expected_template = Template(Identifier(
            Name('DNASequence')), ['x', 'y', 'z'], [
                Property(Identifier(Name('encoding')),
                         Identifier(Uri('SBOL:IUPACDNA')))
            ])
        self.assertCountEqual(forms, [expected_template])

    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])

    def test_expansion_in_property(self):
        script = 'A()(x = e is a B())'
        forms = self.parser.parse(script)
        e = self.parser.parse('e is a B()')[0]

        expected_template = Template(Identifier(Name('A')), [],
                                     [Property(Identifier(Name('x')), e)])
        self.assertEqual(expected_template, forms[0])

    def test_expansion_in_property_with_body(self):
        script = 'A()(x = e is a B()(y = 12345))'
        forms = self.parser.parse(script)
        e = self.parser.parse('e is a B()(y = 12345)')[0]
        expected_template = Template(Identifier(Name('A')), [],
                                     [Property(Identifier(Name('x')), e)])
        self.assertEqual(expected_template, forms[0])

    def test_expansion_in_body_with_body(self):
        script = 'A()(e is a B()(y = 12345))'
        forms = self.parser.parse(script)
        e = self.parser.parse('e is a B()(y = 12345)')[0]
        expected_template = Template(Identifier(Name('A')), [], [e])
        self.assertEqual(expected_template, forms[0])

    def test_expansion_in_body(self):
        script = 'A()(e is a B())'
        forms = self.parser.parse(script)
        e = self.parser.parse('e is a B()')[0]
        expected_template = Template(Identifier(Name('A')), [], [e])
        self.assertEqual(expected_template, forms[0])

    def test_expansion_in_property_with_args(self):
        script = 'A()(x = e is a B(12345))'
        forms = self.parser.parse(script)
        e = self.parser.parse('e is a B(12345)')[0]
        expected_template = Template(Identifier(Name('A')), [],
                                     [Property(Identifier(Name('x')), e)])
        self.assertEqual(expected_template, forms[0])

    def test_expansion_in_body_with_args(self):
        script = 'A()(e is a B(12345))'
        forms = self.parser.parse(script)
        e = self.parser.parse('e is a B(12345)')[0]
        expected_template = Template(Identifier(Name('A')), [], [e])
        self.assertEqual(expected_template, forms[0])

    def test_extension_in_body(self):
        script = 'A()(@extension ExtensionName())'
        forms = self.parser.parse(script)
        a = forms[0]
        expected_template = Template(Identifier(Name('A')), [],
                                     [ExtensionPragma('ExtensionName', [])])
        self.assertEqual(expected_template, a)

    def test_extension_in_body_with_arg(self):
        script = 'A()(@extension ExtensionName(12345))'
        forms = self.parser.parse(script)
        a = forms[0]
        expected_template = Template(Identifier(
            Name('A')), [], [ExtensionPragma('ExtensionName', [Value(12345)])])
        self.assertEqual(expected_template, a)

    def test_extension_in_body_with_multi_args(self):
        script = 'A()(@extension ExtensionName(12345, 67890))'
        forms = self.parser.parse(script)
        a = forms[0]
        expected_template = Template(
            Identifier(Name('A')), [],
            [ExtensionPragma('ExtensionName',
                             [Value(12345), Value(67890)])])
        self.assertEqual(expected_template, a)