Introducing Semantic Web Programming (Part I)
In this article, we take a quick introductory look at Semantic Web and RDF programming, and how to set up your dev environment.
Join the DZone community and get the full member experience.
Join For FreeWhat is the Semantic Web?
The Semantic Web is an extension of the current web in which information is given well-defined meaning, better enabling computers and people to work in cooperation. (W3C)
In the above definition, we need to pay attention to some important things:
The first, the Semantic Web is an extension of the current web that means the Semantic Web builds upon the principles and the technologies of the Web (standard Web).
The second, Semantic means meaning.
And finally, through ontologies and ontology - related technologies, meaning or semantic, can be shared (like resources of Web) and can be understood easily by software.
The three most important aspects of the Semantic Web to define are the concepts and relationships between them, store and display information, and query data. The Semantic Web technologies support for these aspects are OWL (Web Ontology Language), RDF (Resource Description Framework), and SPARQL (SPARQL Protocol and RDF Query Language), respectively.
In this article, I will introduce a bit about RDF and I also introduce how to program with the Jena API (for Java programmers) and dotNetRDF (for .NET programmers). However, first, we need to set up our development environment.
Environment for Java Programmers
If you are a Java programmer, here is an overview of the environment:
Apache Jena
This distribution contains the APIs and SPARQL engine for our Java semantic web applications. You can access the link here and download the zip file to your computer:
Using Jena With Eclipse
I am using Eclipse as my Java development environment and I imported the Jena library into Eclipse using the following steps:
Step 1: Open up my IDE (Eclipse) and create a new Java project by choosing File > New > Project > Java Project:
Click Finish.
Step 2: Create the first class by selecting the src folder and right-click to get the context menu to show:
Step 3: Import the Jena library into Eclipse by starting from the Window menu and choosing Preferences:
In the Preferences window, we choose Java > Build Path > User Libraries:
Click the New button to create a new user library:
Click the Add External JARs button to add the .jar files from Jena. These files contain the compiled Java libraries that Jena uses, together with the Jena code itself in Jena .jar. I have selected all of the .jar files in the lib directory in my Jena install directory:
Result:
Step 4: Go back to my project and configure the Java build path to use the library I just created. To start, I right-click on the project node in the Project Explorer window to bring up the project properties menu, navigate to the Build Path menu option, and choose Configure Build Path (or Add Libraries):
Choosing the Libraries tab and click the Add Library button:
Choose User Library and Next:
Check JenaLibs and click the Finish button:
Click OK. JenaLibs library will occur in my project explorer
Environment for .NET Programmers (C#)
If you are a .NET programmer, then here is an overview of the environment:
Visual Studio (I am using VS 2012).
dotNetRDF library.
Adding dotNetRDF Library into Visual Studio
You can add dotNetRDF library into your .NET project by using NuGet, or manually doing it yourself. I used NuGet by selecting the PROJECT menu and choosing Manage NuGet Packages. In the Manage NuGet Packages window, I found dotNetRDF and clicked the Install button:
Learning a Bit About RDF and Coding
In this article, I will introduce a bit about RDF, which is an important standard in the Semantic Web. RDF and other standards will be explained more thoroughly in the subsequent articles.
RDF (Resource Description Framework)
In the Semantic Web approach, information is represented as statements. A statement includes three parts: subject, predicate, and object. Because of these three parts, statements are also sometimes referred to as triples. Let's look at the following examples:
Ex 1: https://dzone.com has a member whose username is ngocminh (subject:https://dzone.com, predicate:has a member, object: ngocminh)
Ex 2: ngocminh's full name is Ngoc Minh Tran (subject: ngocminh, predicate: full name, object: Ngoc Minh Tran)
Subjects and objects are also called resources; predicates are also called properties. A resource is an object, a 'thing' we want to talk about. Every resource has a URI (Universal Resource Identifier), which can be a URL (Unified Resource Locator) or some other kind of unique identifier. Properties are a special kind of resource; they describe relations between resources. In the above Ex 1, the resources are "https://dzone.com" and "ngocminh", and the property is "has a member"; in the above Ex 2, resources are "ngocminh" and "Ngoc Minh Tran", and the property is "full name."
RDF is a standard for describing information or statements. The best form of the RDF is a graph. The nodes and the arrows make up the graph. The nodes are subjects and objects (or resources), the arrows are predicates (or properties).
There are three kinds of nodes:
Blank node: is used when we don't know the identity of the resource. A blank node is represented by an empty oval.
Resource node: is represented by an oval which contains a URI (ex: https://dzone.com).
Literal node: a literal is a concrete data value, like a number or a string, and it can not be the subject, only the object, of a statement (Ex: Ngoc Minh Tran). Literal node is represented by a rectangle.
The direction of the arrows always points from the subject to the object.
The statements of Ex 1 and Ex 2 (and I also added a new resource, address) can be shown as the following RDF graph:
Coding
There are many libraries which are created to support Semantic Web programming. I chose the Jena library and the dotNetRDF library because they are used with Java and C#, the two most popular languages today. We can use these libraries to create RDF graphs.
The following tables represent some important things to create a simple RDF graph with Jena and dotNetRDF libraries:
Code With Jena API (in Eclipse) |
|
Package |
|
RDF graph (g) |
|
Blank node (blank) |
|
Resource node (resource) |
|
Literal node (literal) |
|
Triple |
|
Code With dotNetRDF (in Visual Studio) |
|
Namespace |
|
RDF graph (g) |
|
Blank node (blank) |
|
Resource node (resource) |
|
Literal node (literal) |
|
Triple |
|
Conclusion
In this article, I have introduced a bit about RDF and Semantic Web programming. In the subsequent articles, I will introduce more about the semantic web and how to program in the semantic web with Jena, dotNetRDF, etc. I hope you find my article helpful!
Opinions expressed by DZone contributors are their own.
Comments