Extract Triples From RDF File With Multiple Ontology - java

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.

Related

creating a model using JENA

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!

Parsing RDF items

I have a couple lines of (I think) RDF data
<http://www.test.com/meta#0001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>
<http://www.test.com/meta#0002> <http://www.test.com/meta#CONCEPT_hasType> "BEAR"^^<http://www.w3.org/2001/XMLSchema#string>
Each line has 3 items in it. I want to pull out the item before and after the URL. So that would result in:
0001, type, Class
0002, CONCEPT_hasType, (BEAR, string)
Is there a library out there (java or scala) that would do this split for me? Or do I just need to shove string.splits and assumptions in my code?
Most RDF libraries will have something to facilitate this. For example, if you parse your RDF data using Eclipse RDF4J's Rio parser, you will get back each line as a org.eclipse.rdf4j.model.Statement, with a subject, predicate and object value. The subject in both your lines will be an org.eclipse.rdf4j.model.IRI, which has a getLocalName() method you can use to get the part behind the last #. See the Javadocs for more details.
Assuming your data is in N-Triples syntax (which it seems to be given the example you showed us), here's a simple bit of code that does this and prints it out to STDOUT:
// parse the file into a Model object
InputStream in = new FileInputStream(new File("/path/to/rdf-data.nt"));
org.eclipse.rdf4j.model.Model model = Rio.parse(in, RDFFormat.NTRIPLES);
for (org.eclipse.rdf4j.model.Statement st: model) {
org.eclipse.rdf4j.model.Resource subject = st.getSubject();
if (subject instanceof org.eclipse.rdf4j.model.IRI) {
System.out.print(((IRI)subject).getLocalName());
}
else {
System.out.print(subject.stringValue());
}
// ... etc for predicate and object (the 2nd and 3rd elements in each RDF statement)
}
Update if you don't want to read data from a file but simply use a String, you could just use a java.io.StringReader instead of an InputStream:
StringReader r = new StringReader("<http://www.test.com/meta#0001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> .");
org.eclipse.rdf4j.model.Model model = Rio.parse(r, RDFFormat.NTRIPLES);
Alternatively, if you don't want to parse the data at all and just want to do String processing, there is a org.eclipse.rdf4j.model,URIUtil class which you can just feed a string and it can give you back the index of the local name part:
String uri = "http://www.test.com/meta#0001";
String localpart = uri.substring(URIUtil.getLocalNameIndex(uri)); // will be "0001"
(disclosure: I am on the RDF4J development team)

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 to retrieve all the elements name from a xml schema

I am having a problem with getting a name of a schema elements in java. I am creating a small xml editor which can load a xml schema and validate a xml file against xml schema. I want to parse a schema, get every elements name and then put it in my content assistant, so the user can see all the available elements.
I already read XSOM User's guide, but I didn't understand much...
Can someone help me to implement my addElementsFromSchema(File xsdfile) function, because I lost myself trying.
public static void addElementsFromSchema(File xsdfile){
}
It sounds like your primary need, at least for now, is to get the element names. You can get the element names with something like:
XSOMParser parser = new XSOMParser();
parser.parse(xsdfile);
XSSchemaSet schemas = parser.getResult();
Iterator<XSElementDecl> i = schemas.iterateElementDecls();
while (i.hasNext()) {
XSElementDecl element = i.next();
String name = element.getName();
// Add to editor
}
Showing element definitions is a lot more difficult, as element declarations in XML schemas can get quite complex.

Using JENA API - Getting all the classes from OWL file

Is it possible to get name of all the classes present in OWL file using JENA and store in the Array List?
OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, null);
String NS = "http://www.abc.com/abcportal/abc.owl" + "#";
base.read("file:abcInstance.owl");
ExtendedIterator<OntClass> iter = base.listClasses();
while ( iter.hasNext()){
System.out.println(iter.next());
}
Yes it is, see the Javadoc for OntModel.listClasses. You can easily copy the contents of the Iterator into an array or List. That you're asking the question suggests that you'll probably benefit from reading the Jena ontology API documentation

Categories

Resources