2012-03-26 58 views
1

好的......其实我有一个名为rdf.py的代码生成rdf代码..我想要做的是直接在4store中移动该文件..我已经将整个代码存储在一个变量并想直接将该变量传递给4store ..是否有可能?转rdf到4store

rdf.py的代码如下。 rdf_code包含生成的整个rdf代码

`好吧......其实我有一个名为rdf.py的代码生成rdf代码..我想要做的是直接在4store中移动该文件..我已将整个代码存储在一个变量中,并希望直接将该变量传递给4store ..是否有可能?

rdf.py的代码如下。 rdf_code包含生成

import rdflib 
from rdflib.events import Dispatcher, Event 
from rdflib.graph import ConjunctiveGraph as Graph 
from rdflib import plugin 
from rdflib.store import Store, NO_STORE, VALID_STORE 
from rdflib.namespace import Namespace 
from rdflib.term import Literal 
from rdflib.term import URIRef 
from tempfile import mkdtemp 
from gstudio.models import * 
from objectapp.models import * 
from reversion.models import Version 
from optparse import make_option 





def get_nodetype(name): 
    """ 
    returns the model the id belongs to. 
    """  
    try: 
     """ 
     ALGO:  get object id, go to version model, return for the given id. 
     """ 
     node = NID.objects.get(title=str(name)) 
     # Retrieving only the relevant tupleset for the versioned objects 
     vrs = Version.objects.filter(type=0 , object_id=node.id) 
     # Returned value is a list, so splice it . 
     vrs = vrs[0] 

    except Error: 
     return "The item was not found." 

    return vrs.object._meta.module_name 


def rdf_description(name, notation='xml'): 
    """ 
    Function takes title of node, and rdf notation. 
    """ 
    valid_formats = ["xml", "n3", "ntriples", "trix"] 
    default_graph_uri = "http://gstudio.gnowledge.org/rdfstore" 
    configString = "/var/tmp/rdfstore" 

    # Get the Sleepycat plugin. 
    store = plugin.get('IOMemory', Store)('rdfstore') 



    # Open previously created store, or create it if it doesn't exist yet 
    graph = Graph(store="IOMemory", 
       identifier = URIRef(default_graph_uri)) 
    path = mkdtemp() 
    rt = graph.open(path, create=False) 
    if rt == NO_STORE: 
    #There is no underlying Sleepycat infrastructure, create it 
     graph.open(path, create=True) 
    else: 
     assert rt == VALID_STORE, "The underlying store is corrupt" 


    # Now we'll add some triples to the graph & commit the changes 
    # rdflib = Namespace('http://sbox.gnowledge.org/gstudio/') 
    graph.bind("gstudio", "http://gnowledge.org/") 
    exclusion_fields = ["id", "rght", "node_ptr_id", "image", "lft", "_state", "_altnames_cache", "_tags_cache", "nid_ptr_id", "_mptt_cached_fields"] 
    node_type=get_nodetype(name) 
    if (node_type=='gbobject'): 
     node=Gbobject.objects.get(title=name) 
    elif (node_type=='objecttype'): 
     node=Objecttype.objects.get(title=name) 
    elif (node_type=='metatype'): 
     node=Metatype.objects.get(title=name) 
    elif (node_type=='attributetype'): 
     node=Attributetype.objects.get(title=name) 
    elif (node_type=='relationtype'): 
     node=Relationtype.objects.get(title=name) 
    elif (node_type=='attribute'): 
     node=Attribute.objects.get(title=name) 
    elif (node_type=='complement'): 
     node=Complement.objects.get(title=name) 
    elif (node_type=='union'): 
     node=Union.objects.get(title=name) 
    elif (node_type=='intersection'): 
     node=Intersection.objects.get(title=name) 
    elif (node_type=='expression'): 
     node=Expression.objects.get(title=name) 
    elif (node_type=='processtype'): 
     node=Processtype.objects.get(title=name) 
    elif (node_type=='systemtype'): 
     node=Systemtype.objects.get(title=name) 





    node_url=node.get_absolute_url() 
    site_add= node.sites.all() 
    a = site_add[0] 
    host_name =a.name 
    #host_name=name 
    link='http://' 
    #Concatenating the above variables will give the url address. 

    url_add=link+host_name+node_url 
    rdflib = Namespace(url_add) 
# node=Objecttype.objects.get(title=name) 

    node_dict=node.__dict__ 

    subject=str(node_dict['id']) 
    for key in node_dict: 
     if key not in exclusion_fields: 
      predicate=str(key) 
      pobject=str(node_dict[predicate]) 
      graph.add((rdflib[subject], rdflib[predicate], Literal(pobject))) 


    rdf_code= graph.serialize(format=notation) 



    # print out all the triples in the graph 
    for subject, predicate, object in graph: 
     print subject, predicate, object 



    graph.commit() 
    print rdf_code 
    graph.close() 

我可以直接通过rdf_code到4store ...如果是,那么如何在整个RDF代码?

回答

2

最简单的方法是将该图转换为ntriples并将其发送到http://yourhost:port/data/GRAPH_URI。如果您执行HTTP POST那么三元组将被附加到由GRAPH_URI表示的现有图。如果你做了一个HTTP PUT,那么当前的图将被替换。如果图形不存在,那么无论您是POST还是PUT,它都会创建。

以这个功能为例:

def assert4s(data,epr,graph,contenttype,flush=False): 
    try: 
     params = urllib.urlencode({'graph': graph, 
            'data': data, 
            'mime-type' : contenttype }) 
     opener = urllib2.build_opener(urllib2.HTTPHandler) 
     request = urllib2.Request(epr,params) 
     request.get_method = lambda: ('PUT' if flush else 'POST') 
     url = opener.open(request) 
     return url.read() 
    except Exception, e: 
     raise e 

如果你有以下数据:

triples = """<a> <b> <c> . 
<d> <e> <f> . 
""" 

你可以做以下电话:

assert4s(triples, 
     "http://yourhost:port/data/", 
     "http://some.org/graph/id", 
     "application/x-turtle") 

编辑

我以前的答案假设你使用4s-httpd服务器。您可以使用以下命令4s-httpd -p PORT kb_name在4store中启动SPARQL服务器。一旦你有了这个运行,您可以使用以下服务:

4store SPARQLServer文档相当齐全。

+0

嘿非常感谢! 可以请指定这个更多 'assert4s(三元组, [“http:// yourhost:port/data /”], [“http://some.org/graph/id”],' - 这是RDF图的主域?? /我尝试过“http://example.org”,也是我的RDF的主页URI,以及与“http:// yourhost:port /数据“,我在这里丢失了什么?? 4store上的文档太神秘了 – 2012-03-29 10:50:44

+0

'http:// yourhost:port/data /'它不是RDF数据的主域,它是4store http服务器倾听。'http:// some。org/graph/id'是图表的名称,你基本可以放任何你想要的东西。请参阅我的答案上的新编辑以获取更多解释。 – 2012-03-29 17:42:11

+0

在我执行代码时运行4Store服务器时出现以下错误:**错误:由同级**重置连接,如果我运行4store服务器然后执行程序,则出现以下错误: ** URLError: **' – 2012-03-30 10:03:11