Semantic web(2py)

The idea of the semantic web, embodied by Linked Data, is that of labeling relations between data using the Web Ontology Language (OWL) and post the data online using an XML based protocol (RDF). Now you can do it in web2py by simply labeling fields and tables in the Database Abstraction Layer (DAL) with .rdf={...} and installing this plugin. The plugin exposes a web service that publishes your database as Linked Data (only tables labeled with rdf). This system works with new and with most legacy databases. It transparently supports SQLite, MySQL, PostgreSQL, MSSQL, Oracle, FireBird, Informix, DB2, Ingres, and the Google App Engine. [read more]




Sample Linked Data pages created by web2py

click here for RDF tables
click here for RDF table:post
click here for RDF table:post record 1
click here for RDF table:post record 2
click here for RDF table:comment
click here for RDF table:comment record 1
click here for RDF table:comment record 2
click here for RDF table:tag
click here for RDF table:tag record 1
click here for RDF table:tag record 2


Code to generate the above pages

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.
#############################################
# define tables: blog post, comment, tag
# a comment references a post (one2many)
# a post can have many tags (many2many)
#############################################

db.define_table('post',
Field('author', 'string'),
Field('title', 'string'),
Field('body', 'text'))

db.define_table('comment',
Field('post_id', db.post),
Field('author', 'string'),
Field('body', 'text'))

db.define_table('tag',
Field('name', 'string'))


db.define_table('post_tag_link',
Field('post_id', db.post),
Field('tag_id', db.tag),
Field('ranking', 'integer'))


#############################################
# label tables, fields and relations with OWL
#############################################

db.rdf_namespaces = {'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}

db.post.title.rdf = 'dc:title'
db.post.body.rdf = 'sioc:content'
db.post.author.rdf = {
'name': 'sioc:has_creator',
'children': [ {'name': 'sioc:User',
'_rdf:about': 'http://chrisbaron.com',
'_rdfs:label': '$VALUE',
'children': [ {'name': 'rdfs:seeAlso',
'_rdf:resource': 'http://massimo.com'
} ]
} ]
}

db.post.rdf = {
'tables': {'_rdf:Id': 'Post','name': 'rdf:Class'},
'individual': {'name': 'rdf:Post' },
'select': {'name': 'rdf:Individual', '_rdf:name': 'post' },
'namespaces': {
'select': {'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'},
'read': {'_xmlns:owl': 'http://www.w3.org/2002/07/owl#',
'_xmlns:dc': 'http://purl.org/dc/elements/1.1/',
'_xmlns:sioc': 'http://rdfs.org/sioc/ns#',
'_xmlns:rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' }
}
}

db.comment.author.rdf = 'sioc:has_creator'
db.comment.body.rdf = 'sioc:content'

db.comment.rdf = {
'tables': {'_rdf:Id': 'Comment','name': 'rdf:Class'},
'individual': {'name': 'rdf:Comment' },
'select': {'name': 'rdf:Individual', '_rdf:name': 'comment' },
'namespaces': {
'select': {'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'},
'read': {'_xmlns:sioc': 'http://rdfs.org/sioc/ns#',
'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' }
}
}

db.tag.name.rdf = 'sioc:content'

db.tag.rdf = {
'tables': {'_rdf:Id': 'Tag','name': 'rdf:Class'},
'individual': {'name': 'rdf:Tag' },
'select': {'name': 'rdf:Individual', '_rdf:name': 'tag' },
'namespaces': {
'select': {'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'},
'read': {'_xmlns:sioc': 'http://rdfs.org/sioc/ns#',
'_xmlns:rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' }
}
}


db.post_tag_link.rdf_mapping = {'_rank': 'ranking' }

#############################################
# insert some sample data
#############################################

if not db(db.post.id>0).count():
post_id_1 = db.post.insert(author = 'Chris Baron',
title = 'How To Publish RDF Using web2py',
body = 'How To Publish RDF Using web2py, really')

post_id_2 = db.post.insert(author = 'Massimo Di Pierro',
title = 'How Not To Publish RDF Using web2py',
body = 'bla bla bla.')

db.comment.insert(post_id = post_id_1,
author = 'Jon Doe',
body = 'I like your post')

db.comment.insert(post_id = post_id_2,
author = 'Jane Doe',
body = "I don't like your post")

tag_id_1 = db.tag.insert(name = 'For Real')
tag_id_2 = db.tag.insert(name = 'Joke')

db.post_tag_link.insert(post_id = post_id_1,
tag_id = tag_id_1,
ranking = 10)

db.post_tag_link.insert(post_id = post_id_2,
tag_id = tag_id_2,
ranking = 5)