creating a model using JENA - java

I am new in the semantic web field, and i'm trying to create a java model using JENA to extract classes, subclass and/or comments from an OWL file..
any help/guidance on how to do such thing would be appreciated.
Thank you

You can do so with the Jena Ontology API. This API allows you to create an ontology model from owl file and then provides you access to all the information stored in the ontology as Java Classes.
Here is a quick introduction to Jena ontology. This introduction contains useful information on getting started with Jena Ontology.
The code generally looks like this:
String owlFile = "path_to_owl_file"; // the file can be on RDF or TTL format
/* We create the OntModel and specify what kind of reasoner we want to use
Depending on the reasoner you can acces different kind of information, so please read the introduction. */
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
/* Now we read the ontology file
The second parameter is the ontology base uri.
The third parameter can be TTL or N3 it represents the file format*/
model.read(owlFile, null, "RDF/XML");
/* Then you can acces the information using the OntModel methods
Let's access the ontology properties */
System.out.println("Listing the properties");
model.listOntProperties().forEachRemaining(System.out::println);
// let's access the classes local names and their subclasses
try {
base.listClasses().toSet().forEach(c -> {
System.out.println(c.getLocalName());
System.out.println("Listing subclasses of " + c.getLocalName());
c.listSubClasses().forEachRemaining(System.out::println);
});
} catch (Exception e) {
e.printStackTrace();
}
// Note that depending on the classes types, accessing some information might throw an exception.
Here is the Jena Ontology API JavaDoc.
I hope it was useful!

Related

How to combine weka and LibSVM in java code

I have successfully integrated LibSVM API to mu java code. I need to transfer large document collection to numerical representation and give it to LibSVM classifier. As far as I know weka has the ability to transfer documents to feature vectors. Can any one please tell me how to do that?
U can do it like this
DataSource source = new DataSource(new File("mycsvinputfile"));
System.out.println(source.getStructure());
Instances data = source.getDataSet();
// setting class attribute if the data format does not provide this information
// For example, the XRFF format saves the class attribute information as well
if (data.classIndex() == -1)
data.setClassIndex(data.numAttributes() - 1);
//initialize svm classifier
LibSVM svm = new LibSVM();
svm.buildClassifier(data);
Don't forget to use weka.jar, libsvm.jar, and wlsvm.jar (the libsvm wrapper) in your project. So just include all 3 jars in your build path or class path or whatever.

How to build an rdf file using an xml file as input

I have an input file in xml format and I need to convert it into a .rdf file that is based on an ontology model created.
Can anyone let me know what the suitable method is to do this using jena api in java?
Is your input file in some arbitrary XML format, or is it already serialized as RDF/XML? (ie: is the root tag of your document <rdf:RDF>?)
If it is in some arbitrary format, then you will need to define some rdf-based schema for representing your data. This is purely project-specific, and will require work on your part to define a way for a graph to apply to your data.
Once you have done that, then basic document construction is a topic for the Jena Tutorials. There is far too much material to cover here, but the basics of creating a statement should suffice:
final Model m = ModelFactory.createDefaultModel();
final Resource s = m.createResource("urn:ex:subject");
final Property p = m.createProperty("urn:ex:predicate");
final Resource o = m.createResource("urn:ex:object");
m.add(s,p,o);
try( final OutputStream out = Files.newOutputStream(Paths.createTempFile("tmp","rdf"), StandardOpenOptions.CREATE_NEW) ){
m.write(out, null, "RDF/XML");
}
The exercise of iterating over your XML and constructing the proper set of statements is left as an exercise for the reader.
If your data is already in RDF/XML, then you can directly read it in to a model:
// Assume you have an InputStream called 'in' pointing at your input data
final Model m = ModelFactory.createDefaultModel();
m.read(in, null, "RDF/XML"); // Assumed that there is no base

How do I parse and save all the classes with their properties from an RDF file using Jena API in Java?

I have a project where I need to parse an RDF file and record all the data on which I will have to search for specific data afterwards. I searched the web and all I could find was how to query the said RDF file but I want to parse it and save all the classes in objects.
This is how a class looks in my file:
<BFO rdf:about="BFO:0000007">
<rdfs:label>process</rdfs:label>
<Definition>
<rdf:Description>
<def>A process is an entity that exists in time by occurring or happening, has temporal parts and always involves and depends on some entity during the time it occurs.</def>
</rdf:Description>
</Definition>
<is_a rdf:resource="EFO:0000001"/>
</BFO>
Update:
yea all u said worked, thank you very much for that but one last question:) the namespaces i have are:
<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns="http://www.ebi.ac.uk/efo/">
</rdf>
My question is for the
<Definition>
<rdf:Description>
<def>smth here</def>
</rdf:Description>
</Definition>
i couldn't find a way to get the def tag, any idea how i can access it? sry to bother again:)
Jena uses the Model object to store RDF that has been parsed from an external source, such as a file or web URL. Depending on your project's needs, and the size of the data, you have a basic choice of whether to read the file into a memory Model, or a persistent Model. Memory is generally faster to access, but of course once your program exits the contents of the memory model are lost (well, unless you write them to a file again).
You should read the Jena tutorials to get a better understanding of the processes involved, but reading a file into a Jena memory model is very easy:
String source = ".. your file location ..";
Model m = FileManager.get().loadModel( source, "RDF/XML" );
If a persistent model would better suit your project, the best choice is Jena TDB. Again, it's very easy to get a model that you can begin querying.
You also asked:
and save all the classes in objects
I'm not quite sure what you mean here. Possibly you're thinking of some sort of ORM tool, similar to ActiveRecord? There's nothing built-in to Jena to do that, though there have been various other projects that have looked at doing something like an ORM layer. I'm not sure which of them is currently active though. But you don't need an ORM to access the properties of your resources, you can just use the Jena API:
String namespace = ".. your namespace here ..";
Resource bfo = m.getResource( namespace + "BFO:0000007" );
Property definition = m.getProperty( namespace + "Definition" );
RDFNode def = bfo.getProperty( definition ).getObject();
Updated
OK, in response to the comments: if you don't know which properties to expect on a resource, you can list them:
String namespace = ".. your namespace here ..";
Resource bfo = m.getResource( namespace + "BFO:0000007" );
for (StmtIterator i = bfo.listProperties(); i.hasNext(); ) {
Statement smt = i.next();
System.out.println( "Resource " + stmt.getSubject().getURI() +
" has property " + stmt.getPredicate().getURI() +
" with value " + stmt.getObject() );
}
This is just a very simple loop, you will want to do something much more useful, but hopefully that shows you that you can still process the resources in the model without knowing their properties in advance. Of course, at some point in your code you will have to connect a particular property to whatever your application wants to do with that particular value of the resource. So somewhere your code will have to have some knowledge of which properties to expect.
Re-reading your comment, I notice you refer to "concepts in an ontology" (even though there are no classes in the RDF fragment you quoted in the question). You might find the capabilities of the Jena ontology API useful, which can list the ontology classes in a model, the properties that have been defined, etc.

Extracting data from OWL file

<!-- http://www.semanticweb.org/vaio/ontologies/2013/0/untitled-ontology-113#hasDegree -->
<owl:ObjectProperty rdf:about="http://www.semanticweb.org/vaio/ontologies/2013/0/untitled-ontology-113#hasDegree">
<rdfs:range rdf:resource="http://www.semanticweb.org/vaio/ontologies/2013/0/untitled-ontology-113#degree"/>
<rdfs:domain rdf:resource="http://www.semanticweb.org/vaio/ontologies/2013/0/untitled-ontology-113#student"/>
</owl:ObjectProperty>
Using java api i need to check in <owl:ObjectProperty ,
if rdf:about then how I take hasDegree after the # and also .. similarly if rdfs:range then degree after #
on the 6th line?
Or how can i extract these value using java api?
I'm not entirely sure what you're trying to get out of the file, your question is not clear. But if you want to parse OWL, particularly OWL in RDF/XML format as you've shown, you should look at using either Jena or Sesame as they are the defacto standard Java APIs for working with RDF. I would recommend the Sesame API because it's simpler and easier to get the hang of, but both are very good libraries.
Each have good documentation on the website on how to use the API and active user & developer forums where you can seek help.
Good luck.
Since your input is in OWL, consider using the OWL-API, or any other OWL API, rather than a less OWL-specific tool like XPath, XSLT, an RDF library, etc.
I am assuming you are using the OWL-API and that your questions is: "How do I get the ranges or domains of an object property in my ontology?" In that case:
/*Load the ontology from a local file and do the initialisations*/
File inputfile = new File("ontologyPath");
OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); ;
OWLDataFactory dataFactory = manager.getOWLDataFactory();
OWLOntology yourOntology = manager.loadOntologyFromOntologyDocument(inputfile);
IRI ontologyIRI = yourOntology.getOntologyID().getOntologyIRI();
/*Get an object property and its ranges*/
OWLObjectProperty o_p_about = dataFactory.getOWLObjectProperty(IRI.create(ontologyIRI + "#"+"about"));
Set<OWLClassExpression> ranges_of_about = about.getRanges(LUCADAOntology);
To pick it up from here, you can check the documentation and example codes on the OWL-API webpage, they are very usfeul.
Use Jena API to call the OWL file,then Turtle to write/output it.

Extract Triples From RDF File With Multiple Ontology

I need a source code with java and jena (or other languages) that ables to extract triples from a RDF file that has multiple ontology.
There is a source code in This Page but this code needs to determine an ontology in source code.
I need a source code that itself read ontology from rdf files and print Sujects,Predicate and Objects as string in URL format.
This is my rdf file: My Files
Can anybody help me to solve this problem?
You just need to have the same code as in here:
How to extract RDF triples from xml file using existing Ontology in java?
But without the predicate that filters the statements. For example ...
FileManager fManager = FileManager.get();
Model model = fManager.loadModel("some_file.rdf");
SimpleSelector selector = new SimpleSelector(null, null, (RDFNode)null) {
public boolean selects(Statement s)
{ return true; }
}
StmtIterator iter = model.listStatements(selector);
while(it.hasNext()) {
Statement stmt = iter.nextStatement();
System.out.print(stmt.getSubject().toString());
System.out.print(stmt.getPredicate().toString());
System.out.println(stmt.getObject().toString());
}
If this is not what you need, please explain your question in further detail. I do not fully understand it.

Categories

Resources