DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Introducing Semantic Web Programming (Part I)

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.

Ngoc Minh Tran user avatar by
Ngoc Minh Tran
CORE ·
May. 02, 17 · Tutorial
Like (5)
Save
Tweet
Share
8.97K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Semantic Web Semantics (computer science) Library Java (programming language) Resource Description Framework Jena (framework) Integrated development environment Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes-Native Development With Quarkus and Eclipse JKube
  • A First Look at Neon
  • Introduction To OpenSSH
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: