Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introducing Semantic Web Programming (Part I)

DZone's Guide to

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.

· Web Dev Zone
Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

What 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:

  • Java Software Development Kit (SDK
  • Eclipse Integrated Development Environment (IDE)
  • Apache Jena

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:

Image title

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:

Image title

Click Finish.

Step 2: Create the first class by selecting the src folder and right-click to get the context menu to show:

Image title

Image title

Step 3: Import the Jena library into Eclipse by starting from the Window menu and choosing Preferences:

Image title

In the Preferences window, we choose Java > Build Path > User Libraries:

Image title

Click the New button to create a new user library:

Image title

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:

Image title

Result:

Image title

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):

Image title

Choosing the Libraries tab and click the Add Library button:

Image title

Choose User Library and Next:

Image title

Check JenaLibs and click the Finish button:

Image title

Click OK. JenaLibs library will occur in my project explorer

Image title

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:

Image title


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:

Image title

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



 import org.apache.jena.rdf.model.*; 



RDF graph (g)



 Model g = ModelFactory.createDefaultModel(); 



Blank node (blank)



 Resource blank = g.createResource(); 



Resource node (resource)



 Resource uri = g.createResource("https://dzone.com"); 



Literal node (literal)



 Literal literal = g.createLiteral("Ngoc Minh Tran"); 



Triple



Resource urisubject  = g.createResource("https://dzone.com");

Resource uriobject  = g.createResource("https://dzone.com/member/ngocminh");

Property member = g.createProperty("https://dzone.com" + "/member");

urisubject.addProperty(member, uriobject); 





Code With dotNetRDF (in Visual Studio)

Namespace


 using VDS.RDF.*; 

RDF graph (g)

 IGraph g = new Graph(); 

Blank node (blank)

 IBlankNode blank = g.CreateBlankNode(); 

Resource node (resource)

 IUriNode uri = g.CreateUriNode(UriFactory.Create("https://dzone.com")); 

Literal node (literal)

 ILiteralNode literal = g.CreateLiteralNode("Ngoc Minh Tran"); 

Triple


IUriNode urisubject  = g.CreateUriNode(UriFactory.Create

("https://dzone.com "));

IUriNode member = g.CreateUriNode(UriFactory.Create

("https://dzone.com /member"));

IUriNode uriobject = g.CreateUriNode(UriFactory.Create

("https://dzone.com /member/ngocminh"));

g.Assert(new Triple(urisubject, member, uriobject)); 



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!

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
open source ,semantic web ,web dev ,java ,.net

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}