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

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" .

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন