Mopteh icon

Mopteh

Mopteh | PRO | 02/14/11 11:33:28 AM UTC | 0 ⭐ | 306 👁️ | Never ⏰ | []
Java |

3.59 KB

|

None

|

0 👍

/

0 👎

import java.io.*;
import java.util.*;
import java.lang.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.FileManager;
 
class Simpsons{
 
    public String rdfname = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
    public String xsdname = "http://www.w3.org/2001/XMLSchema#";
    public static String famname = "http://www.ifi.uio.no/INF3580/v11/family#";
    public static String simname = "http://www.ifi.uio.no/INF3580/v11/simpsons#";
    static String testfil = "http://sws.ifi.uio.no/inf3580/v11/oblig/2/simpsons.ttl";
    private static Model simpsonsmodel = null;
    
    public static void main(String[] args){
 
        String filnavn = testfil;
        String output = args[1];
        int lengde = filnavn.length();
        char[] filarray = filnavn.toCharArray();
        char filtype = filarray[filarray.length-1];
        reader(filnavn, filtype, output);
 
    }
    
    static void reader (String filnavn, char type, String output){
    
        String lang = "";
    
        if (type == 'l'){
            lang = "TTL";
        } else if (type == '3') {
            lang = "N3";
        } else if (type == 't') {
            lang = "N-TRIPLE";
        } else if (type == 'f') {
            lang = "RDF/XML";
        } else {
            System.out.println("Not a valid file");
            lang = "";
        }
 
        Model simpsonsmodel = ModelFactory.createDefaultModel();
        Model inFoafInstance = FileManager.get().loadModel(filnavn, lang);
        simpsonsmodel = inFoafInstance;
 
        addSimpsons(simpsonsmodel, output);
 
 
    }
    
    static void addSimpsons(Model simpsonsmodel, String output){
    
        Resource homer = simpsonsmodel.createResource(simname+"Homer");
        Resource herb = simpsonsmodel.createResource(simname+"Herb");
        Resource abraham = simpsonsmodel.createResource(simname+"Abraham");
        Resource mona = simpsonsmodel.createResource(simname+"Mona");
        Resource BLANK = simpsonsmodel.createResource(simname+" ");
        
        Property ageint = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("^^xsd:int"));
        Property a = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("rdf:type"));
        Property person = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:Person"));
        Property name = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:hasName"));
        Property age = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:hasAge"));
        Property spouse = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:hasSpouse"));
        Property father = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:hasFather"));
        Property old = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:Old"));
        Property minor = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:Minor"));
        Property infant = simpsonsmodel.createProperty(simpsonsmodel.expandPrefix("fam:Infant"));
 
        simpsonsmodel.add(herb, a, person);
        simpsonsmodel.add(abraham, a, person);
        simpsonsmodel.add(mona, a, person);
        simpsonsmodel.add(mona, spouse, abraham);
        simpsonsmodel.add(abraham, spouse, mona);
        simpsonsmodel.add(herb, father, "");
        simpsonsmodel.add(homer, father, abraham);
        simpsonsmodel.add(abraham, age, "78"+ageint);
        simpsonsmodel.add(mona, age, "70"+ageint);
 
//      System.out.println(simpsonsmodel);
 
        ResIterator it = simpsonsmodel.listResourcesWithProperty(age);
        while (it.hasNext()){
            System.out.println(it.next());
        //  int alder = it.getProperty(age).getInt;
        //  System.out.println(alder);
            simpsonsmodel.listObjectsOfProperty(it.next(), age);
        }
 
        try {
            simpsonsmodel.write(new FileOutputStream("output.rdf"));
            simpsonsmodel.write(new FileOutputStream(output));
        } catch (IOException e) {
            System.err.println("Skriving til fil feilet!");
        }
    }
}

Comments