How to convert a string to OWL Object Property in Java? - java

I am writing a DL query parser in Java. I need to use the method getObjectPropertyValues(). how to convert a string to OWL Object Property expression in Java , please give me a sample code.

If you are using the OWLAPI you can reuse the code from here for parsing a DL query:
https://github.com/owlcs/owlapi/wiki/DL-Queries-with-a-real-reasoner

You create an OWLObjectProperty using the aptly named OWLDataFactory.getOWLObjectProperty(IRI iri), and IRI has a constructor IRI(String). E.g., from the examples in the documentation:
OWLObjectProperty prop
= factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#propA"));

Related

Interpolate JSON values into a string

I am writing an application/class that will take in a template text file and a JSON value and return interpolated text back to the caller.
The format of the input template text file needs to be determined. For example: my name is ${fullName}
Example of the JSON:
{"fullName": "Elon Musk"}
Expected output:
"my name is Elon Musk"
I am looking for a widely used library/formats that can accomplish this.
What format should the template text file be?
What library would support the template text file format defined above and accept JSON values?
Its easy to build my own parser but there are many edge cases that needs to be taken care of and I do not want to reinvent the wheel.
For example, if we have a slightly complex JSON object with lists, nested values etc. then I will have to think about those as well and implement it.
I have always used org.json library. Found at http://www.json.org/.
It makes it really easy to go through JSON Objects.
For example if you want to make a new object:
JSONObject person = new JSONObject();
person.put("fullName", "Elon Musk");
person.put("phoneNumber", 3811111111);
The JSON Object would look like:
{
"fullName": "Elon Musk",
"phoneNumber": 3811111111
}
It's similar to retrieving from the Object
String name = person.getString("fullName");
You can read out the file with BufferedReader and parse it as you wish.
Hopefully I helped out. :)
This is how we do it.
Map inputMap = ["fullName": "Elon Musk"]
String finalText = StrSubstitutor.replace("my name is \${fullName}", inputMap)
You can try this:
https://github.com/alibaba/fastjson
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.

How to Pre Process Json String in Java :: Convert Capitalised Field names to lowerCase Camel case names

My current Android project consumes many Json web services.
I have no control over the Json content.
I wish to persist this Json directly into my applications local Realm database.
The issue is the Json Field Names Are All Capitalised.
I do not wish my Realm DTO objects to have capitalised field names as thats just WRONG.
How can I transform the Capitalised field names to acceptable Java field name format?
Is there any Json pre processing libraries that will perform the required transformation of Capitalised field names?
I realise I can use Jackson/GSON type libraries to solve this issue, however that means transforming Json to Java Pojo before I can persist the data.
The json Field names are "ThisIsAFieldName".
What I want is to transform them to "thisIsAFieldName".
I think you should really consider letting your JSON deserializer handle this, but if this really isn't a possibility you can always use good old string manipulation :
String input; // your JSON input
Pattern p = Pattern.compile("\"([A-Z])([^\"]*\"\\s*:)"); // matches '"Xxxx" :'
Matcher m = p.matcher(input);
StringBuffer output = new StringBuffer();
while (m.find()) {
m.appendReplacement(output, String.format("\"%s$2", m.group(1).toLowerCase());
}
m.appendTail(output);
Ideone test.

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.

Invalid character while converting from JSON to XML using jsonlib

I'm trying to convert a JSON string to XML using jsonlib in Java.
JSONObject json = JSONObject.fromObject(jsonString);
XMLSerializer serializer = new XMLSerializer();
String xml = serializer.write( json );
System.out.println(xml);
The error that I get is
nu.xom.IllegalNameException: 0x24 is not a legal NCName character
The problem here is that I have some properties in my JSON that are invalid XML characters. eg. I have a property named "$t". The XMLSerializer throws the exception while trying to create a XML tag in this name because $ is not allowed in XML tag names. Is there any way in which I can override this XML well formedness check done by the serializer?
First I'd suggest to add the language you are using (it is Java, right?).
You could override the method where it checks your XML tag name to do nothing.
I took a look at the spec for the json-lib XMLSerializer and to my surprise it seems to have no option for serialising a JSON object whose keys are not valid XML names. If that's the case then I think you will need to find a different library.
You could loop over json.keySet (recursively if necessary) and replace invalid keys with valid ones (using remove and add).

Need to parse String in XML format to obtain Request Information

I have got a String request in the XML format , which i need to parse it to obtain the Request data from it .
The XML String would be conssiting of a lot of subtags within it , and data is appended in it in the form of CDATA as well as there are >&lt , ini t.
I want to use STAX approach for this .
Please sugesst if theer are any cons wtth this ??
i need this inside a Java Webservice
You can parse your XML file with native PHP function.
EX :
$path = '/my/file/xml.xml';
$file = file_get_contents($path);
$xml = new SimpleXMLElement($file);
And get nodes with :
$value1 = $xml->node[0]->value;
$value2 = $xml->node[1]->value;
There would not be any issues in using DOM parser based STAX API.

Categories

Resources