Exemple #1
0
        def creator(label_uri):
            if not label_uri:
                return
            label, uri = label_uri
            if not label:
                label = None

            types = [type_uri]

            if not uri:
                uri = uri_gen = self.doc.infGenerateUri(label)
            else:
                uri_gen = None

            props = []
            if self.doc.annotations_by_name.get('defaultRdsId'):
                props.append(LiteralTriple.of(uri, self.doc.annotations_by_name.get('defaultRdsId'), self.doc.infDefaultRdsId(uri_gen)))

            if data.get('template'):
                types.append(kb.ns_owl+'Thing')
                tpl = self.doc._findTemplate(type_uri)
                if tpl:
                    for v in tpl['roles'].itervalues():
                        if v.get('restricted_by_value'):
                            props.append(ObjectTriple.of(uri, v['uri'], v['type_uri']))
                        if v['is_literal']:
                            props.append(DatatypeQuad.of(uri, v['uri'], '', v['type_uri']))
            self.doc << actions.AddEntity(self.doc, uri, types, label, props)
Exemple #2
0
 def creator(prop_obj):
     if not prop_obj:
         return
     prop, obj = prop_obj
     if not prop or not obj:
         return
     p = props[prop]['uri']
     self.doc << actions.AddProperty(self.doc, ObjectTriple.of(self.parent.uri, p, uri))
Exemple #3
0
    def build_main(self, objprops, dtprops, untranslated):
        itemid = untranslated.pop('id', None)

        objprops, dtprops, untranslated = self.doc.translate_props(untranslated, objprops, dtprops)

        if itemid is None:
            itemid = self.doc.infGenerateUri()

        # undo/redo in the future?

        for k, v in objprops.iteritems():
            ObjectTriple.insert(self.doc, itemid, k, v)

        for k, v in dtprops.iteritems():
            LiteralTriple.insert(self.doc, itemid, k, v)

        self.doc.AsyncChangeState(self.doc.state_changed)

        return itemid
Exemple #4
0
    def build(self, doc, roles, items=None, current=None, done=None):
        if self.base:
            if hasattr(self.base, 'build'):
                roles = self.base.build(doc, self.prepare_base_props(roles),
                                        items, current, done)
                return self.translate_base(roles, True)
            else:
                return

        if self.value:
            return {self.ownrole if self.ownrole else name: self.value}

        if self.function:
            value = eval(self.function, dict(roles))
            return {self.ownrole if self.ownrole else name: value}

        itemid = None

        if self.name in roles and not self.ownrole:
            itemid = roles[self.name]
        else:
            if self.uri:
                if len(self.uri) == 1:
                    itemid = iter(self.uri).next()
                else:
                    return
            elif self.ownrole:
                if self.ownrole in roles:
                    itemid = roles[self.ownrole]
                    if not itemid:
                        return

            if not itemid:
                found = self.scan_by_roles(doc, roles)
                if len(found) == 1:
                    itemid = iter(found).next()
                    # if items != None:
                    #     items.add(itemid)
                    # return {self.ownrole if self.ownrole else name: itemid}
            elif itemid != 'new':
                if not IsUri(itemid):
                    itemid = doc.basens + itemid
            else:
                itemid = doc.infGenerateUri()

        for k, v in self.roles.iteritems():
            if v == 'self':
                continue
            value = roles.get(k)
            if not value and (not itemid or not v.get('is_optional')):
                return

        if not itemid:
            itemid = doc.infGenerateUri()

        result = {}
        result[self.ownrole if self.ownrole else self.name] = itemid

        triples = doc.grTriplesForSubj(itemid)

        if self.types and len(self.types) == 1:
            if (itemid, kb.rdf_type) not in done:
                if (itemid, kb.rdf_type) not in current:
                    for t in triples:
                        if t.p == kb.rdf_type:
                            t.deletefrom(doc)
                current.add((itemid, kb.rdf_type))
                for v in self.types:
                    ObjectTriple.insert(doc, itemid, kb.rdf_type, v)

        for k, v in self.roles.iteritems():
            if v == 'self':
                continue
            value = roles.get(k)
            if not value:
                continue

            if (itemid, v['uri']) in done:
                continue

            if value == 'new':
                if not v['is_literal']:
                    value = doc.infGenerateUri()
                result[k] = value

            if (itemid, v['uri']) not in current:
                for t in triples:
                    if t.p == v['uri']:
                        t.deletefrom(doc)

            current.add((itemid, v['uri']))

            if v['is_literal']:
                LiteralTriple.insert(doc, itemid, v['uri'], value)
            else:
                ObjectTriple.insert(doc, itemid, v['uri'], value)

        doc.AsyncChangeState(doc.state_changed)
        if items != None:
            items.add(itemid)
        return result
Exemple #5
0
    def __call__(self, **props):
        itemid   = props.pop('id', None)
        edit     = props.pop('edit', False)
        itemtype = None

        objprops, dtprops, untranslated = ({}, {}, props)
        if 'type' in props:
            itemtype = props.pop('type')
            if getattr(itemtype, 'build', None):
                return itemtype.build(self.doc, props)
            if getattr(itemtype, 'translate_props', None):
                objprops, dtprops, untranslated = itemtype.translate_props(untranslated, for_build = True)
            else:
                if itemtype:
                    objprops, dtprops, untranslated = self.doc.translate_type_props(set([itemtype]), untranslated)
                objprops[kb.rdf_type] = itemtype
        
        objprops, dtprops, untranslated = self.doc.translate_props(untranslated, objprops, dtprops)

        if itemid is None:
            itemid = self.doc.infGenerateUri()

            for k, v in objprops.iteritems():
                ObjectTriple.insert(self.doc, itemid, k, v)

            for k, v in dtprops.iteritems():
                LiteralTriple.insert(self.doc, itemid, k, v)

        else:
            triples = self.doc.grTriplesForSubj(itemid)

            types = set()
            for t in triples:
                if t.p==kb.rdf_type:
                    types.add(t.o)

            todelete = set()
            if edit and kb.rdf_type in objprops:
                skiproles = set()
                if objprops[kb.rdf_type] != None:
                    for v in self.doc.infGetAvailableRoles([objprops[kb.rdf_type]]).values():
                        skiproles.add(v['uri'])
                for v in self.doc.infGetAvailableRoles(types).values():
                    if v['uri'] not in skiproles:
                        todelete.add(v['uri'])
                types.clear()
                
            if types:
                objprops, dtprops, untranslated = self.doc.translate_type_props(types, untranslated, objprops, dtprops)
            
            if edit:
                for t in triples:
                    if t.p in objprops or t.p in dtprops or t.p in todelete:
                        t.deletefrom(self.doc)

            for k, v in objprops.iteritems():
                if v:
                    ObjectTriple.insert(self.doc, itemid, k, v)
    
            for k, v in dtprops.iteritems():
                if v:
                    LiteralTriple.insert(self.doc, itemid, k, v)

        if getattr(itemtype, 'template', None):
            ObjectTriple.insert(self.doc, itemid, kb.rdf_type, kb.ns_owl+'Thing')

        self.doc.AsyncChangeState(self.doc.state_changed)

        return itemid
Exemple #6
0
    def build(self, doc, roles, items = None, current = None, done = None):
        if self.base:
            if hasattr(self.base, 'build'):
                roles = self.base.build(doc, self.prepare_base_props(roles), items, current, done)
                return self.translate_base(roles, True)
            else:
                return

        if self.value:
            return {self.ownrole if self.ownrole else name: self.value}

        if self.function:
            value = eval(self.function, dict(roles))
            return {self.ownrole if self.ownrole else name: value}

        itemid = None

        if self.name in roles and not self.ownrole:
            itemid = roles[self.name]
        else:
            if self.uri:
                if len(self.uri) == 1:
                    itemid = iter(self.uri).next()
                else:
                    return
            elif self.ownrole:
                if self.ownrole in roles:
                    itemid = roles[self.ownrole]
                    if not itemid:
                        return
                        
            if not itemid:
                found = self.scan_by_roles(doc, roles)
                if len(found) == 1:
                    itemid = iter(found).next()
                    # if items != None:
                    #     items.add(itemid)
                    # return {self.ownrole if self.ownrole else name: itemid}
            elif itemid != 'new':
                if not IsUri(itemid):
                    itemid = doc.basens + itemid
            else:
                itemid = doc.infGenerateUri()

        for k, v in self.roles.iteritems():
            if v == 'self':
                continue
            value = roles.get(k)
            if not value and (not itemid or not v.get('is_optional')):
                return

        if not itemid:
            itemid = doc.infGenerateUri()

        result = {}
        result[self.ownrole if self.ownrole else self.name]  = itemid

        triples = doc.grTriplesForSubj(itemid)

        if self.types and len(self.types) == 1:
            if (itemid, kb.rdf_type) not in done:
                if (itemid, kb.rdf_type) not in current:
                    for t in triples:
                        if t.p == kb.rdf_type:
                            t.deletefrom(doc)
                current.add((itemid, kb.rdf_type))
                for v in self.types:
                    ObjectTriple.insert(doc, itemid, kb.rdf_type, v)

        for k, v in self.roles.iteritems():
            if v == 'self':
                continue
            value = roles.get(k)
            if not value:
                continue

            if (itemid, v['uri']) in done:
                continue

            if value == 'new':
                if not v['is_literal']:
                    value = doc.infGenerateUri()
                result[k] = value

            if (itemid, v['uri']) not in current:
                for t in triples:
                    if t.p == v['uri']:
                        t.deletefrom(doc)

            current.add((itemid, v['uri']))

            if v['is_literal']:
                LiteralTriple.insert(doc, itemid, v['uri'], value)
            else:
                ObjectTriple.insert(doc, itemid, v['uri'], value)

        doc.AsyncChangeState(doc.state_changed)
        if items != None:
            items.add(itemid)
        return result
Exemple #7
0
    def __call__(self, **props):
        itemid = props.pop('id', None)
        edit = props.pop('edit', False)
        itemtype = None

        objprops, dtprops, untranslated = ({}, {}, props)
        if 'type' in props:
            itemtype = props.pop('type')
            if getattr(itemtype, 'build', None):
                return itemtype.build(self.doc, props)
            if getattr(itemtype, 'translate_props', None):
                objprops, dtprops, untranslated = itemtype.translate_props(
                    untranslated, for_build=True)
            else:
                if itemtype:
                    objprops, dtprops, untranslated = self.doc.translate_type_props(
                        set([itemtype]), untranslated)
                objprops[kb.rdf_type] = itemtype

        objprops, dtprops, untranslated = self.doc.translate_props(
            untranslated, objprops, dtprops)

        if itemid is None:
            itemid = self.doc.infGenerateUri()

            for k, v in objprops.iteritems():
                ObjectTriple.insert(self.doc, itemid, k, v)

            for k, v in dtprops.iteritems():
                LiteralTriple.insert(self.doc, itemid, k, v)

        else:
            triples = self.doc.grTriplesForSubj(itemid)

            types = set()
            for t in triples:
                if t.p == kb.rdf_type:
                    types.add(t.o)

            todelete = set()
            if edit and kb.rdf_type in objprops:
                skiproles = set()
                if objprops[kb.rdf_type] != None:
                    for v in self.doc.infGetAvailableRoles(
                        [objprops[kb.rdf_type]]).values():
                        skiproles.add(v['uri'])
                for v in self.doc.infGetAvailableRoles(types).values():
                    if v['uri'] not in skiproles:
                        todelete.add(v['uri'])
                types.clear()

            if types:
                objprops, dtprops, untranslated = self.doc.translate_type_props(
                    types, untranslated, objprops, dtprops)

            if edit:
                for t in triples:
                    if t.p in objprops or t.p in dtprops or t.p in todelete:
                        t.deletefrom(self.doc)

            for k, v in objprops.iteritems():
                if v:
                    ObjectTriple.insert(self.doc, itemid, k, v)

            for k, v in dtprops.iteritems():
                if v:
                    LiteralTriple.insert(self.doc, itemid, k, v)

        if getattr(itemtype, 'template', None):
            ObjectTriple.insert(self.doc, itemid, kb.rdf_type,
                                kb.ns_owl + 'Thing')

        self.doc.AsyncChangeState(self.doc.state_changed)

        return itemid