caps_lock

Untitled

Mar 23rd, 2015
261
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lab2;
  2.  
  3. import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
  4. import com.hp.hpl.jena.rdf.model.*;
  5. import com.hp.hpl.jena.vocabulary.VCARD;
  6.  
  7. public class Main {
  8.  
  9.     static String URI    = "http://example.com/";
  10.    
  11.     public static void main(String[] args) {
  12.         org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.OFF);  
  13.         Model model = ModelFactory.createDefaultModel();
  14.         Model model2 = ModelFactory.createDefaultModel();
  15.         model2.read("http://centria.di.fct.unl.pt/~cd/foaf.nt", "Turtle");
  16.        
  17.         Model res = model2.union(model);
  18.         Resource cDamasio = addCarlos(model);
  19.         addMe(model);
  20.         model.write(System.out,"Turtle");
  21. //      model.write(System.out,"RDF/XML"); // to ask
  22. ////        iterateover(model);
  23.         iterateoverres(res, cDamasio);
  24.  
  25.     }
  26.    
  27.     public static void iterateoverres(Model model, Resource cDamasioResource){
  28.        
  29.         StmtIterator iter = model.listStatements(null, model.getProperty("http://xmlns.com/foaf/0.1/interest"), (RDFNode)null);
  30.         while (iter.hasNext()) {
  31.             Statement stmt      = iter.nextStatement();
  32.             RDFNode   object    = stmt.getObject();
  33.            
  34.             model.read(object.toString(),"Turtle");
  35.         }
  36.         model.write(System.out,"Turtle");
  37.     }
  38.    
  39.     public static Resource addCarlos(Model model){
  40.         String cdName = "CarlosDamasio";
  41.         String cdURI =  "http://centria.di.fct.unl.pt/cd";
  42.         Resource CD = model.createResource(URI + cdName);
  43.         Resource cdSplittedNameResource = model.createResource();
  44.         Resource cdTel = model.createResource();
  45.        
  46.        
  47.         Property www = model.createProperty(cdURI); // to ask
  48.        
  49.         CD.addProperty(VCARD.FN, cdName);
  50.         CD.addProperty(VCARD.N, cdSplittedNameResource);
  51.         CD.addProperty(VCARD.TEL, cdTel);
  52.         CD.addProperty(www, "website");
  53.         CD.addProperty(VCARD.EMAIL, "cd@fct.unl.pt");      
  54.         CD.addProperty(VCARD.GEO, "()");
  55.         CD.addProperty(VCARD.ADR,"Faculdade de Ciências e Tecnologia da Universidade Nova de Lisboa");
  56.        
  57.         cdSplittedNameResource.addProperty(VCARD.Given, "Carlos");
  58.         cdSplittedNameResource.addProperty(VCARD.Family, "Damasio");
  59.        
  60.         Property extension = model.createProperty(cdURI+"/myconcepts/extension/e10758");
  61.         Property number = model.createProperty(cdURI+"/myconcepts/number/e21948536");
  62.         cdTel.addProperty(extension, "extension");
  63.         cdTel.addProperty(number, "number");
  64.         return CD;
  65.     }
  66.    
  67.     public static void addMe(Model model){
  68.         String pkName = "PawelKrysiak";
  69.         Resource PK = model.createResource(URI + pkName);
  70.         Resource pkSplittedNameResource = model.createResource();
  71.         Property www = model.createProperty("http://w-master.cba.pl/index"); // to ask
  72.        
  73.         PK.addProperty(VCARD.FN, pkName);
  74.         PK.addProperty(VCARD.N, pkSplittedNameResource);
  75.         PK.addProperty(VCARD.TEL, "663571174");
  76.         PK.addProperty(www, "website");
  77.         PK.addProperty(VCARD.EMAIL, "p.krysiak@campus.fct.unl.pt");    
  78.         PK.addProperty(VCARD.ADR,"Residencia frausto da silva");
  79.        
  80.         pkSplittedNameResource.addProperty(VCARD.Given, "Paweł");
  81.         pkSplittedNameResource.addProperty(VCARD.Family, "Krysiak");
  82.     }
  83.    
  84.     public static void iterateover(Model model){
  85.         StmtIterator iter = model.listStatements();
  86.  
  87.         // print out the predicate, subject and object of each statement
  88.         while (iter.hasNext()) {
  89.             Statement stmt      = iter.nextStatement();  // get next statement
  90.             Resource  subject   = stmt.getSubject();     // get the subject
  91.             Property  predicate = stmt.getPredicate();   // get the predicate
  92.             RDFNode   object    = stmt.getObject();      // get the object
  93.  
  94.             System.out.print(subject.toString());
  95.             System.out.print(" " + predicate.toString() + " ");
  96.             if (object instanceof Resource) {
  97.                System.out.print(object.toString());
  98.             } else {
  99.                 // object is a literal
  100.                 System.out.print(" \"" + object.toString() + "\"");
  101.             }
  102.  
  103.             System.out.println(" .");
  104.         }
  105.     }
  106.  
  107. }
RAW Paste Data