Exemplo n.º 1
0
    def parameterize(self, *args, **kwargs): # IGNORE:R0201
        """
        A filter for constructing keyword arguments for passing to the model constructor
        or the create or get_or_create methods of this manager.

        The arguments are processed and new keyword arguments added to the keyword
        arguments received. The resulting keyword arguments dictionary is returned.

        The keyword arguments received must not conflict with the new keyword
        arguments formed.

        The argument list should be formed as follows:

            0 arguments: No keyword arguments added.

            1 argument:  Added as the 'name' element of the returned dict.

            2 arguments: Raises Exception.

            3 arguments:
                'resource': a Resource formed from arguments 0, 1 using get_or_create.
                'name': argument 1 becomes the predicate name.
                XXX Cleanup?

        If 2 or more than 3 arguments are passed an exception is raised.
        """
        from rdf.models import Namespace, Resource, Concept
        from rdf.shortcuts import get_or_create
        l = len(args)
        if 0 == l:
            pass
        elif 1 == l:
            kwargs['resource'] = args[0]
        elif 2 == l:
            PROPERTY = Concept.objects.get(
                resource__namespace=Namespace.objects.RDF, resource__name='Property')
            kwargs['resource'], _ = get_or_create(
                Resource, namespace=args[0], name=args[1], type=PROPERTY)
        elif 3 == l:
            PROPERTY = Concept.objects.get(
                resource__namespace=Namespace.objects.RDF, resource__name='Property')
            kwargs['resource'], _ = get_or_create(
                Resource, namespace=args[0], name=args[1], type=PROPERTY)
            kwargs.update(args[2]) 
        else:
            raise Exception('unexpected arguments (%s, %s)' \
                % (unicode(args), unicode(kwargs)))
        return kwargs
Exemplo n.º 2
0
 def test_get_or_create_from_values_twice(self):
     N = create(Namespace, 'n', 'http://example.com/namespace/')
     T = create(Concept, N, 't')
     r = create(Resource, N, 'r', T)
     V = get(Namespace, code='xs')['string']
     one_none = Cardinality.objects.get(domain='1', range='?') # IGNORE:E1101
     p = create(Predicate, N, 'p', domain=r.type, range=V, cardinality=one_none)
     v = {'value': u'something clever'}
     s, _ = get_or_create(Statement, r, p, v)
     self.assertEqual(r, s.subject)
     self.assertEqual(p, s.predicate)
     self.assertEqual(v['value'], s.object)
     self.assertEqual(String, type(s.object))
     self.assertEqual(None, s.reified)
     t, _ = get_or_create(Statement, r, p)
     self.assertEqual(r, t.subject)
     self.assertEqual(p, t.predicate)
     self.assertEqual(v['value'], t.object)
     self.assertEqual(String, type(t.object))
Exemplo n.º 3
0
 def __init__(self, app, app_path, mapping):
     self.app, self.app_path, self.mapping = app, app_path, mapping
     try: 
         self.namespace = Namespace.objects.get(resource__name=mapping['namespace'])
     except Namespace.DoesNotExist:
         app_label = self.app_path.split('.')[-1]
         self.namespace, _ = get_or_create(Namespace, app_label, mapping['namespace'])
     self.concepts, self.predicates = {}, {} 
     self._concept_cache = None
     self._object_names = [_.strip() for _ in mapping['models'].split(',')] \
         if mapping.has_key('models') else None
Exemplo n.º 4
0
    def parameterize(self, *args, **kwargs): # IGNORE:R0201
        """
        A filter for constructing keyword arguments for passing to the model constructor
        or the create or get_or_create methods of this manager.

        The arguments are processed and new keyword arguments added to the keyword
        arguments received. The resulting keyword arguments dictionary is returned.

        The keyword arguments received must not conflict with the new keyword
        arguments formed.

        The argument list should be formed as follows:

            0 arguments: No keyword arguments added.

            1 argument:  Added as the 'code' element of the returned dict.

            2 arguments:
                Argument 0: Added as the 'code' element.
                Argument 1: Added as the 'resource' element.

        If more than 2 arguments are passed an exception is raised.
        """
        from rdf.models import Namespace, Resource
        from rdf.shortcuts import get_or_create
        l = len(args)
        if 0 == l:
            pass
        elif 1 == l:
            kwargs['code'] = args[0]
        elif 2 == l:
            r = args[1]
            r, _ = (r, False) if not isinstance(r, basestring) else \
                get_or_create(Resource, r, type=Namespace.objects.RDFS['Namespace'])
            kwargs['code'], kwargs['resource'] = args[0], r
        else:
            raise Exception(
                '''expected namespace code and resource, got '%s' ''' % unicode(args))
        return kwargs
Exemplo n.º 5
0
def pre():
    '''
    Invoked during the ontology load process, before any ontology definitions 
    are parsed. Establishes the core RDF, RDFS and OWL constructs necessary for 
    successfully parsing RDF ontologies.
    '''

    for d in CARDINALITIES:
        for r in CARDINALITIES:
            Cardinality.objects.get_or_create(domain=d[0], range=r[0])

    # Shortcuts require the RDF, RDFS namespaces and the type and namespace types:
    
    # The resource behind the RDF namespace:
    RDF, _ = Resource.objects.get_or_create(name='http://www.w3.org/1999/02/22-rdf-syntax-ns#')
    
    # The RDF namespace itself: 
    RDF, _ = Namespace.objects.get_or_create(
        code='rdf',
        defaults=dict( 
            resource=RDF,
            title='RDF Vocabulary',
            description='''The RDF vocabulary  published by the W3C as http://www.w3c.org/1999/02/22-rdf-syntax-ns#.'''))
    
    # The resource behind the RDFS namespace: 
    RDFS, _ = Resource.objects.get_or_create(name=u'http://www.w3.org/2000/01/rdf-schema#')
    
    # The RDFS namespace itself: 
    RDFS, _ = Namespace.objects.get_or_create(
        code='rdfs',
        defaults=dict( 
            resource=RDFS,
            title='RDF Schema Vocabulary',
            description='''The RDF Schema vocabulary published by the W3C as http://www.w3.org/2000/01/rdf-schema#.'''))
    
    # The resource behind the namespace type: 
    nsr, _ = Resource.objects.get_or_create(namespace=RDFS, name='Namespace')
    
    # The namespace type: 
    ns, _ = Concept.objects.get_or_create(
        resource=nsr,
        defaults=dict( 
            name='Namespace',
            model_name='rdf.models.Namespace',
            title='RDF Namespace',
            description='The basic RDF namespace concept. All namespaces are examples of this concept.'))
    
    # The resource behind the type type: 
    tr, _ = Resource.objects.get_or_create(namespace=RDFS, name='Class')
    
    # The type itself:
    t, _ = Concept.objects.get_or_create(
        resource=tr,
        defaults=dict( 
            name='Class',
            model_name='rdf.models.Concept',
            title='RDFS Class',
            description='The basic RDFS Class used to form vocabularies.'))
    
    # The resource behind the literal type: 
    lr, _ = Resource.objects.get_or_create(namespace=RDFS, name='Literal')
    
    # The literal itself:
    l, _ = Concept.objects.get_or_create(
        resource=lr,
        defaults=dict(
            model_name='rdf.models.Concept',
            title='RDFS Literal',
            description='The basic RDFS Literal used to form vocabularies.'))
    
    # Assign the namespace type to the namespaces: 
    RDF.resource.type = ns; RDF.resource.save()
    RDFS.resource.type = ns; RDFS.resource.save()
    
    # Assign the type type to the types...:
    tr.type = t; tr.save()
    lr.type = l; lr.save()
    nsr.type = t; nsr.save()
    
    # Shortcuts now work...
    
    get_or_create(
        Concept, RDFS, 'Resource', 
        defaults=dict(
            model_name='rdf.models.Resource',
            title='RDF Resource',
            description='The basic RDF resource concept that all other concepts are derived from.'))
    
    get_or_create(
        (Namespace, 'xml', 'http://www.w3.org/xml/1998/namespace'),
        (Namespace, 'xs', 'http://www.w3.org/2001/XMLSchema#'), # Check out that hash ;)
        (Namespace, 'drdfs', 'http://.../django/schema#'),
        (Namespace, 'owl', 'http://www.w3.org/2002/07/owl#'),
        (Namespace, 'dc', 'http://purl.org/dc/elements/1.1/'))

    get_or_create(
        Concept, RDF, 'Property', 
        defaults=dict(
            model_name='rdf.models.Predicate',
            title='RDF Property',
            description='The RDF property concept. All properties belong to this concept.'))
    
    get_or_create(
        Concept, RDF, 'Statement', 
        defaults=dict(
            model_name='rdf.models.Statement',
            title='RDF Statement',
            description='The RDF statement concept. All statements belong to this concept.'))
Exemplo n.º 6
0
def post():
    '''
    Invoked during the ontology load process, after all core ontology definitions 
    have been parsed but before any additional fragments are loaded. 
    
    Carries out any necessary post-processing for the coreontology elements, 
    including but not limited to: 
    
        * Specifying predicate cardinalities
        * Establishing model mappings for concepts and literals
        * Synthesizing value predicates for literals
        
    '''
    RDF, RDFS, OWL, XS, DC, DRDFS = get(
        (Namespace, 'rdf'), (Namespace, 'rdfs'), (Namespace, 'owl'),
        (Namespace, 'xs'), (Namespace, 'dc'), (Namespace, 'drdfs'))
    
    one_to_one = Cardinality.objects.get(domain='1', range='1')
    any_to_one = Cardinality.objects.get(domain='*', range='1')

    # Ontologies...
    
    ONTOLOGY = OWL['Ontology']
    ONTOLOGY.model_name = 'rdf.models.Ontology'
    ONTOLOGY.save()
    
    _mark_internal_ontologies()
    
    # First arrange support for accessing resource identifier strings, e.g. rdf:about:
    
    URI, _ = get_or_create(
        # URI literal stored in the resources table, in the name field. 
        # This unfortunately leaves off the namespace prefix but will do for now.
        Concept, DRDFS, 'uri', 
        defaults=dict(
            literal=True, 
            model_name='rdf.models.Resource', 
            title='Uniform Resource Identifier', 
            description='A string that uniquely identifies the associated resource.'))
    URI.resource.type = RDFS['Literal']
    URI.resource.save()
        
    get_or_create(
        # Predicate for accessing the URI for a resource.
        Predicate, RDF, 'about', 
        defaults=dict(
            domain=RDFS['Resource'],
            range=URI, 
            cardinality=one_to_one, 
            field_name='rdf.models.Resource.name', # Not quite right... missing prefix
            title='RDF About',
            description='Associates a resource with its URI'))
    
    # Now map some built-in predicates to the right database columns, assign 
    # cardinalities, etc.:

    TYPE = RDF['type']
    TYPE.field_name = 'rdf.models.Resource.type'
    TYPE.cardinality = one_to_one
    TYPE.save()
    
    SUBJECT = RDF['subject']
    SUBJECT.field_name = 'rdf.models.Statement.subject'
    SUBJECT.cardinality = any_to_one
    SUBJECT.save()
    
    PREDICATE = RDF['predicate']
    PREDICATE.field_name = 'rdf.models.Statement.predicate'
    PREDICATE.cardinality = any_to_one
    PREDICATE.save()

    OBJECT = RDF['predicate']
    OBJECT.cardinality = any_to_one
    OBJECT.save()
    
    # RDF['object'] field is more complicated... it is special-cased in the compiler.
    
    b = Concept.objects.get(resource__namespace=XS, resource__name='boolean')
    b.title='Boolean Literal'
    b.description='The literal type used to store Boolean truth values.'
    b.save()
    _recursive_map_model(b, 'rdf.models.Boolean')

    d = Concept.objects.get(resource__namespace=XS, resource__name='date')
    d.title = 'Date Literal'
    d.description = 'The literal type used to store date values.'
    d.save()
    _recursive_map_model(d, 'rdf.models.Date')

    t = Concept.objects.get(resource__namespace=XS, resource__name='time')
    t.title = 'Time literal'
    t.description = 'The literal type used to store timestamps.'
    t.save()
    _recursive_map_model(t, 'rdf.models.Time')

    t = Concept.objects.get(resource__namespace=XS, resource__name='dateTime')
    t.title = 'Date-Time literal'
    t.description = 'Another literal type used to store timestamps.'
    t.save()
    _recursive_map_model(t, 'rdf.models.Time')

    d = Concept.objects.get(resource__namespace=XS, resource__name='duration')
    d.title='Duration Literal'
    d.description='The literal type used to store durations.'
    d.save()
    _recursive_map_model(d, 'rdf.models.Duration')

    d = Concept.objects.get(resource__namespace=XS, resource__name='decimal')
    d.title='Decimal Literal'
    d.description='The literal type used to store decimal numbers.'
    d.save()
    _recursive_map_model(d, 'rdf.models.Decimal')

    d = Concept.objects.get(resource__namespace=XS, resource__name='double')
    d.title='Double Literal'
    d.description='The literal type used to store double size floating point numbers.'
    d.save()
    _recursive_map_model(d, 'rdf.models.Float')

    d = Concept.objects.get(resource__namespace=XS, resource__name='float')
    d.title='Float Literal'
    d.description='The literal type used to store floating point numbers.'
    d.save()
    _recursive_map_model(d, 'rdf.models.Float')

    s = Concept.objects.get(resource__namespace=XS, resource__name='string')
    s.title = 'String Literal'
    s.description='The literal type used to store text values.'
    s.save()
    _recursive_map_model(s, 'rdf.models.String')

    p = Predicate.objects.get(resource__namespace=DC, resource__name='title')
    p.range = XS['string']
    p.save()

    p = Predicate.objects.get(resource__namespace=DC, resource__name='description')
    p.range = XS['string']
    p.save()