summaryrefslogtreecommitdiff
path: root/gstudio/management/commands/dump_all_rdf.py
blob: 0b0097e469c4be6923691931b23d31917d64c7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

import rdflib
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 *
import settings
import os.path
from django.core.management.base import NoArgsCommand
from django.core.management.base import BaseCommand


def rdf_all(notation='xml'):
    """
    Funtion 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 IOMemory 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:
    
        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
    
    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"]

    for node in NID.objects.all():
        node_dict=node.ref.__dict__
        node_type = node.reftype
        try:
            if (node_type=='Gbobject'):
    	    	node=Gbobject.objects.get(title=node)
    		rdflib=link(node)
    	    elif (node_type=='None'):
    		node=Gbobject.objects.get(title=node)
    		rdflib=link(node)  
		   
            elif (node_type=='Process'):
    		node=Gbobject.objects.get(title=node)
    		rdflib=link(node)
		  
            elif (node_type=='System'):
    		node=Gbobject.objects.get(title=node)
    		rdflib=link(node)
		
            elif (node_type=='Objecttype'):
    		node=Objecttype.objects.get(title=node)
    		rdflib=link(node)

    	    elif (node_type=='Attributetype'):
    		node=Attributetype.objects.get(title=node)
    	        rdflib=link(node)
    	
    	    elif (node_type=='Complement'):
    		node=Complement.objects.get(title=node)
    		rdflib=link(node)
    
            elif (node_type=='Union'):
    	   	node=Union.objects.get(title=node)
    		rdflib=link(node)
    	
            elif (node_type=='Intersection'):
    		node=Intersection.objects.get(title=node)
    		rdflib=link(node)
    
            elif (node_type=='Expression'):
    		node=Expression.objects.get(title=node)
    		rdflib=link(node)
    
            elif (node_type=='Processtype'):
    		node=Processtype.objects.get(title=node)
    		rdflib=link(node)
    
            elif (node_type=='Systemtype'):
    	 	node=Systemtype.objects.get(title=node)
    		rdflib=link(node)
    
            elif (node_type=='AttributeSpecification'):
    		node=AttributeSpecification.objects.get(title=node)
    		rdflib=link(node) 	 
              
            elif (node_type=='RelationSpecification'):
    		node=RelationSpecification.objects.get(title=node)
    		rdflib=link(node) 	 
    
            elif(node_type=='Attribute'):
    		node=Attribute.objects.get(title=node)
    		rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
    
            elif(node_type=='Relationtype' ):
    		node=Relationtype.objects.get(title=node)
    		rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
    
    	    elif(node_type=='Metatype'):
    		node=Metatype.objects.get(title=node)
    		rdflib = Namespace('http://sbox.gnowledge.org/gstudio/')
        except:
            if(node_type=='Attribute'):
                node=Attribute.objects.get(title=node) 
                rdflib= Namespace('http://sbox.gnowledge.org/gstudio/')
    
            if(node_type=='Relationtype' ):
                node=Attribute.objects.get(title=node)
                rdflib= Namespace('http://sbox.gnowledge.org/gstudio/')
                    
            if(node_type=='Metatype'):
                node=Attribute.objects.get(title=node)
                rdflib= Namespace('http://sbox.gnowledge.org/gstudio/')

        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)
               #path to store the rdf in a file
    
    x = os.path.join(os.path.dirname(__file__), 'rdffiles.rdf')
    temp_path=str(x)
    file = open(temp_path, 'w')
    file.write(rdf_code)
    file.close()
    graph.commit()
    print rdf_code
    graph.close()
    


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

    	url_addr=link+host_name+node_url
    	rdflib=Namespace(url_addr) 
        return rdflib


    	
	
	
	
       



class Command(BaseCommand):
	def handle(self,*args,**options):		
                # verify the type of the node and pass the node to display the rdf accordingly.		
	
    		rdf_all(*args)