JENA example 1

source code:

	// Create an empty ontology model
	OntModel ontModel = ModelFactory.createOntologyModel();
	String ns = new String("http://www.example.com/onto1#");
	String baseURI = new String("http://www.example.com/onto1");
	Ontology onto = ontModel.createOntology(baseURI);

	// Create ‘Person’, ‘MalePerson’ and ‘FemalePerson’ classes
	OntClass person = ontModel.createClass(ns + "Person");
	OntClass malePerson = ontModel.createClass(ns + "MalePerson");
	OntClass femalePerson = ontModel.createClass(ns + "FemalePerson");

	// FemalePerson and MalePerson are subclasses of Person
	person.addSubClass(malePerson);
	person.addSubClass(femalePerson);

	// FemalePerson and MalePerson are disjoint
	malePerson.addDisjointWith(femalePerson);
	femalePerson.addDisjointWith(malePerson);

	
	
	
	// Create datatype property 'hasAge'
	DatatypeProperty hasAge = ontModel.createDatatypeProperty(ns + "hasAge");
	// 'hasAge' takes integer values, so its range is 'integer'
	// Basic datatypes are defined in the ‘vocabulary’ package
	hasAge.setDomain(person);
	hasAge.setRange(XSD.integer); // com.hp.hpl.jena.vocabulary.XSD

	// Create individuals
	Individual john = malePerson.createIndividual(ns + "John");
	Individual jane = femalePerson.createIndividual(ns + "Jane");
	Individual bob = malePerson.createIndividual(ns + "Bob");

	// Create statement 'John hasAge 20'
	Literal age20 = ontModel.createTypedLiteral("20", XSDDatatype.XSDint);
	Statement johnIs20 = ontModel.createStatement(john, hasAge, age20);
	ontModel.add(johnIs20);


result:

			
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:j.0="http://www.example.com/onto1#" > 
  <rdf:Description rdf:about="http://www.example.com/onto1#Jane">
    <rdf:type rdf:resource="http://www.example.com/onto1#FemalePerson"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1#John">
    <j.0:hasAge rdf:datatype="http://www.w3.org/2001/XMLSchema#int">20</j.0:hasAge>
    <rdf:type rdf:resource="http://www.example.com/onto1#MalePerson"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1#FemalePerson">
    <owl:disjointWith rdf:resource="http://www.example.com/onto1#MalePerson"/>
    <rdfs:subClassOf rdf:resource="http://www.example.com/onto1#Person"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1#Person">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1">
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Ontology"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1#hasAge">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#integer"/>
    <rdfs:domain rdf:resource="http://www.example.com/onto1#Person"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#DatatypeProperty"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1#Bob">
    <rdf:type rdf:resource="http://www.example.com/onto1#MalePerson"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.com/onto1#MalePerson">
    <owl:disjointWith rdf:resource="http://www.example.com/onto1#FemalePerson"/>
    <rdfs:subClassOf rdf:resource="http://www.example.com/onto1#Person"/>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>