Skip to content
73 changes: 73 additions & 0 deletions Assignment3/Raul Ortega Gonzalez - u120175/Task06.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package semanticweb.assignment3;

import java.io.InputStream;

import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.util.FileManager;
import org.apache.jena.vocabulary.VCARD;

/**
* Task 07: Querying ontologies (RDFs)
* @author elozano
* @author isantana
*
*/
public class Task06
{
public static String ns = "http://somewhere#";
public static String foafNS = "http://xmlns.com/foaf/0.1/";
public static String foafEmailURI = foafNS+"email";
public static String foafKnowsURI = foafNS+"knows";
public static String stringTypeURI = "http://www.w3.org/2001/XMLSchema#string";

public static void main(String args[])
{
String filename = "resources/example5.rdf";

// Create an empty model
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);

// Use the FileManager to find the input file
InputStream in = FileManager.get().open(filename);

if (in == null)
throw new IllegalArgumentException("File: "+filename+" not found");

// Read the RDF/XML file
model.read(in, null);

// Create a new class named "Researcher"
OntClass researcher = model.createClass(ns+"Researcher");

// ** TASK 6.1: Create a new class named "University" **
OntClass university = model.createClass(ns+"University");

// ** TASK 6.2: Add "Researcher" as a subclass of "Person" **
model.getOntClass(ns+"Person").addSubClass(researcher);

// ** TASK 6.3: Create a new property named "worksIn" **
Property worksIn = model.createProperty(ns+"worksIn");

// ** TASK 6.4: Create a new individual of Researcher named "Jane Smith" **
Individual janeSmith = researcher.createIndividual(ns+"Jane Smith");


// ** TASK 6.5: Add to the individual JaneSmith the fullName, given and family names **
janeSmith.addProperty(VCARD.FN, janeSmith);
janeSmith.addProperty(VCARD.Given, janeSmith);
janeSmith.addProperty(VCARD.Family, janeSmith);

// ** TASK 6.6: Add UPM as the university where John Smith works **
Individual johnSmith = model.getIndividual(ns + "John Smith");
Individual UPM = university.createIndividual(ns + "UPM");
johnSmith.addProperty(worksIn, UPM);


model.write(System.out, "RDF/XML-ABBREV");
}
}
66 changes: 66 additions & 0 deletions Assignment3/Raul Ortega Gonzalez - u120175/Task07.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package semanticweb.assignment3;

import java.io.InputStream;

import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.iterator.ExtendedIterator;

/**
* Task 07: Querying ontologies (RDFs)
* @author elozano
* @author isantana
*
*/
public class Task07
{
public static String ns = "http://somewhere#";

public static void main(String args[])
{
String filename = "resources/example6.rdf";

// Create an empty model
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);

// Use the FileManager to find the input file
InputStream in = FileManager.get().open(filename);

if (in == null)
throw new IllegalArgumentException("File: "+filename+" not found");

// Read the RDF/XML file
model.read(in, null);


// ** TASK 7.1: List all individuals of "Person" **
OntClass person = model.getOntClass(ns+"Person");
ExtendedIterator<Individual> it1 = model.listIndividuals(person);
while(it1.hasNext()){
System.out.println(it1.next());
}

// ** TASK 7.2: List all subclasses of "Person" **
ExtendedIterator<OntClass> it2 = person.listSubClasses();
while(it2.hasNext()){
System.out.println(it2.next());
}

// ** TASK 7.3: Make the necessary changes to get as well indirect instances and subclasses. TIP: you need some inference... **
OntModel model2 = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF,model);
OntClass persona = model2.getOntClass(ns+"Person");
ExtendedIterator<Individual> it3 = model2.listIndividuals(persona);
ExtendedIterator<OntClass> it4 = persona.listSubClasses();
while(it3.hasNext()){
Individual inf = it3.next();
System.out.println(inf.getURI());
}
while(it4.hasNext()){
System.out.println(it4.next());
}
}
}