মঙ্গলবার, ২৭ ডিসেম্বর, ২০১১

Jena Example for VCARD concept


figure 1Tutorial01: Creating a simple RDF model
package tutorial;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
public class Tutorial01 extends Object {
    // some definitions
    static String personURI    = "http://somewhere/JohnSmith";
    static String fullName     = "John Smith";
    
      public static void main (String args[]) {
     
     // create an empty model
     Model model = ModelFactory.createDefaultModel();
     // create the resource
     /*Resource johnSmith = model.createResource(personURI);
       // add the property
       johnSmith.addProperty(VCARD.FN, fullName);*/
     //cascading ways
          Resource johnSmith =
         model.createResource(personURI)
              .addProperty(VCARD.FN, fullName);
          model.write(System.out,"Turtle");
      }
}


figure 2Tutorial02: vcard:N property takes a resource as its value. The ellipse representing the compound name has no URI. It is known as an blank Node.

package tutorial;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.VCARD;
public class Tutorial02 extends Object{
public static void main(String[] args) {
String personURI    = "http://somewhere/JohnSmith";
String givenName    = "John";
String familyName   = "Smith";
String fullName     = givenName + " " + familyName;
// create an empty Model
Model model = ModelFactory.createDefaultModel();
// create the resource
Resource johnSmith
 = model.createResource(personURI)
        .addProperty(VCARD.FN, fullName)
        .addProperty(VCARD.N,
                     model.createResource() //for empty model
                          .addProperty(VCARD.Given, givenName)
                          .addProperty(VCARD.Family, familyName));
//model.write(System.out,"Turtle");
model.write(System.out);
//model.write(System.out,"RDF/XML-ABBREV");
//model.write(System.out,"N-TRIPLE");
}
}
Output: For model.write(System.out)

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#" > 
  <rdf:Description rdf:about="http://somewhere/JohnSmith">
    <vcard:N rdf:nodeID="A0"/>
    <vcard:FN>John Smith</vcard:FN>
  </rdf:Description>
  <rdf:Description rdf:nodeID="A0">
    <vcard:Family>Smith</vcard:Family>
    <vcard:Given>John</vcard:Given>
  </rdf:Description>
</rdf:RDF>

Output: For model.write(System.out,"RDF/XML-ABBREV")
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#">
  <rdf:Description rdf:about="http://somewhere/JohnSmith">
    <vcard:N rdf:parseType="Resource">
      <vcard:Family>Smith</vcard:Family>
      <vcard:Given>John</vcard:Given>
    </vcard:N>
    <vcard:FN>John Smith</vcard:FN>
  </rdf:Description>
</rdf:RDF>
Output: For model.write(System.out,"N-TRIPLE")

<http://somewhere/JohnSmith> <http://www.w3.org/2001/vcard-rdf/3.0#N> _:A492ebc41X3aX13480bf1cd8X3aXX2dX7fff .
<http://somewhere/JohnSmith> <http://www.w3.org/2001/vcard-rdf/3.0#FN> "John Smith" .
_:A492ebc41X3aX13480bf1cd8X3aXX2dX7fff <http://www.w3.org/2001/vcard-rdf/3.0#Family> "Smith".
_:A492ebc41X3aX13480bf1cd8X3aXX2dX7fff <http://www.w3.org/2001/vcard-rdf/3.0#Given> "John" .
Tutorial03: Print out the predicate, subject and object of each statement using StmtIterator

package tutorial;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
// Tutorial 3 Statement attribute accessor methods
public class Tutorial03 extends Object {
    public static void main (String args[]) {  
        // some definitions
        String personURI    = "http://somewhere/JohnSmith";
        String givenName    = "John";
        String familyName   = "Smith";
        String fullName     = givenName + " " + familyName;
        // create an empty model
        Model model = ModelFactory.createDefaultModel();
        // create the resource
        Resource johnSmith 
          = model.createResource(personURI)
                 .addProperty(VCARD.FN, fullName)
                 .addProperty(VCARD.N, 
                              model.createResource()
                                   .addProperty(VCARD.Given, givenName)
                                   .addProperty(VCARD.Family, familyName));
       
        // list the statements in the graph
        StmtIterator iter = model.listStatements(); 
        // print out the predicate, subject and object of each statement
        while (iter.hasNext()) {
            Statement stmt      = iter.nextStatement();         // get next statement
            Resource  subject   = stmt.getSubject();   // get the subject
            Property  predicate = stmt.getPredicate(); // get the predicate
            RDFNode   object    = stmt.getObject();    // get the object            
            System.out.print(subject.toString());
            System.out.print(" " + predicate.toString() + " ");
            if (object instanceof Resource) {
                System.out.print(object.toString());
            } else {
                // object is a literal
                System.out.print(" \"" + object.toString() + "\"");
            }
            System.out.println(" .");
        }
    }
}
Output:
 http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#N 5fbceadd:13480c6bb62:-7fff .
http://somewhere/JohnSmith http://www.w3.org/2001/vcard-rdf/3.0#FN  "John Smith" .
5fbceadd:13480c6bb62:-7fff http://www.w3.org/2001/vcard-rdf/3.0#Family  "Smith" .
5fbceadd:13480c6bb62:-7fff http://www.w3.org/2001/vcard-rdf/3.0#Given  "John" .

RDF Representation in Jena Ai

English: RDF graph for Eric Miller provided as...Image via jena.sourceforge
RDF N-triple can represent with RDF model. RDF is best thought of in the form of node and arc diagrams. A simple vcard might look like this in RDF:


Ellipse represents the Resources of the concept.
Arc represents the Properties of the concept.
Rectangle represents the literal/Resources of the concept.
Source Code:

package tutorial;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
//Tutorial 1 creating a simple model
public class Tutorial01 extends Object {
    // some definitions
    static String personURI    = "http://somewhere/JohnSmith";
    static String fullName     = "John Smith";

    public static void main (String args[]) {
     
     // create an empty model
     Model model = ModelFactory.createDefaultModel();
     // create the resource
      Resource johnSmith = model.createResource(personURI);
       // add the property
       johnSmith.addProperty(VCARD.FN, fullName);
          model.write(System.out,"Turtle");    
      }
}


Enhanced by Zemanta

Jena Api

Jena is a Java framework for building Semantic Web applications. It provides a programmatic environment for RDFRDFS and OWLSPARQL and includes a rule-based inference engine. Jena is open source and grown out of work with the HP Labs Semantic Web Programme.

The Jena Framework includes:
  • A RDF API
  • Reading and writing RDF in RDF/XML, N3 and N-Triples
  • An OWL API
  • In-memory and persistent storage
  • SPARQL query engine
Support is provided by the jena-users mailing list.

Enhanced by Zemanta

রবিবার, ২৫ ডিসেম্বর, ২০১১

What is RDF

English: RDF graph for Eric Miller provided as...Image via Wikipedia
RDF is a standard model for data interchange on the Web. RDF has features that facilitate data merging even if the underlying schemas differ, and it specifically supports the evolution of schemas over time without requiring all the data consumers to be changed.
RDF extends the linking structure of the Web to use URIs to name the relationship between things as well as the two ends of the link (this is usually referred to as a “triple”). Using this simple model, it allows structured and semi-structured data to be mixed, exposed, and shared across different applications.
This linking structure forms a directed, labeled graph, where the edges represent the named link between two resources, represented by the graph nodes. This graph view is the easiest possible mental model for RDF and is often used in easy-to-understand visual explanations.

Enhanced by Zemanta

What is Semantic Web

Semantic Web "Layercake" (2006)Image via WikipediaThe Semantic Web provides a common framework that allows data to be shared and reused across application, enterprise, and community boundaries. It is a collaborative effort led by W3C with participation from a large number of researchers and industrial partners. It is based on the Resource Description Framework ( RDF). See also the separateFAQ for further information.
Enhanced by Zemanta