Ejemplo n.º 1
0
    def add_trees_to_handle(self,
                            handle,
                            trees=None,
                            tree_uri='',
                            context=None):
        """Add triples describing a set of trees to handle, which can be either 
        a file or a librdf model."""

        if tree_uri and not tree_uri.endswith('/'):
            tree_uri = tree_uri + '/'

        is_librdf_model = isinstance(handle, RDF.Model)

        if is_librdf_model and context:
            context = RDF.Node(RDF.Uri(context))
        else:
            context = None

        Uri = RDF.Uri
        prefixes = self.prefixes

        if trees is None:
            trees = self.trees

        if is_librdf_model:
            Redland.librdf_model_transaction_start(handle._model)
        else:
            for prefix, url in prefixes.items():
                handle.write('@prefix %s: <%s> .\n' % (prefix, url))

        for stmt in [(Uri(prefixes['cdao']), qUri('rdf:type'),
                      qUri('owl:Ontology'))]:
            self.add_stmt_to_handle(handle, RDF.Statement(*stmt), context)

        for tree in trees:
            self.tree_counter += 1
            self.tree_uri = node_uri(
                tree_uri, 'tree%s' % str(self.tree_counter).zfill(7))

            first_clade = tree.clade
            statements = self.process_clade(first_clade, root=tree_uri)
            for stmt in statements:
                self.add_stmt_to_handle(handle, stmt, context)

        if is_librdf_model:
            Redland.librdf_model_transaction_commit(handle._model)
            handle.sync()
Ejemplo n.º 2
0
    def add_trees_to_handle(self, handle, trees=None, tree_uri='', context=None):
        """Add triples describing a set of trees to handle, which can be either 
        a file or a librdf model."""

        if tree_uri and not tree_uri.endswith('/'):
            tree_uri = tree_uri + '/'

        is_librdf_model = isinstance(handle, RDF.Model)

        if is_librdf_model and context:
            context = RDF.Node(RDF.Uri(context))
        else:
            context = None

        Uri = RDF.Uri
        prefixes = self.prefixes
        
        if trees is None:
            trees = self.trees
        
        if is_librdf_model: 
            Redland.librdf_model_transaction_start(handle._model)
        else:
            for prefix, url in prefixes.items():
                handle.write('@prefix %s: <%s> .\n' % (prefix, url))
        
        for stmt in [(Uri(prefixes['cdao']), qUri('rdf:type'), qUri('owl:Ontology'))]:
            self.add_stmt_to_handle(handle, RDF.Statement(*stmt), context)
        
        for tree in trees:
            self.tree_counter += 1
            self.tree_uri = node_uri(tree_uri, 'tree%s' % str(self.tree_counter).zfill(7))

            first_clade = tree.clade
            statements = self.process_clade(first_clade, root=tree_uri)
            for stmt in statements:
                self.add_stmt_to_handle(handle, stmt, context)
                
        if is_librdf_model: 
            Redland.librdf_model_transaction_commit(handle._model)
            handle.sync()
Ejemplo n.º 3
0
    def add_trees_to_model(self, trees=None, storage=None, tree_uri='tree', context=None):
        """Add triples describing a set of trees to an RDF model."""

        if context: context = RDF.Node(RDF.Uri(context))
        self.tree_uri = tree_uri

        Uri = RDF.Uri
        urls = self.urls
        
        if trees is None:
            trees = self.trees
        
        if storage is None:
            # store RDF model in memory for now
            storage = new_storage()
        
        if self.model is None:
            self.model = RDF.Model(storage)
            if self.model is None:
                raise CDAOError("new RDF.model failed")
        model = self.model
                    
        Redland.librdf_model_transaction_start(model._model)
        
        for stmt in [(Uri(urls['cdao']), qUri('rdf:type'), qUri('owl:Ontology'))]:
            model.append(RDF.Statement(*stmt), context)
        
        for tree in trees:
            self.tree_counter += 1
            self.tree_uri = 'tree%s' % str(self.tree_counter).zfill(7)

            first_clade = tree.clade
            statements = self.process_clade(first_clade, root=tree_uri)
            for stmt in statements:
                model.append(stmt, context)
                
        Redland.librdf_model_transaction_commit(model._model)
            
        model.sync()