Java owlapi extract - java

I have an ontology
<owl:ObjectProperty rdf:about="http://purl.obolibrary.org/obo/BFO_0000050">
<owl:inverseOf rdf:resource="http://purl.obolibrary.org/obo/BFO_0000051"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#TransitiveProperty"/>
<oboInOwl:hasDbXref rdf:datatype="http://www.w3.org/2001/XMLSchema#string">BFO:0000050</oboInOwl:hasDbXref>
<oboInOwl:hasOBONamespace rdf:datatype="http://www.w3.org/2001/XMLSchema#string">external</oboInOwl:hasOBONamespace>
<oboInOwl:id rdf:datatype="http://www.w3.org/2001/XMLSchema#string">part_of</oboInOwl:id>
<oboInOwl:shorthand rdf:datatype="http://www.w3.org/2001/XMLSchema#string">part_of</oboInOwl:shorthand>
<rdfs:label rdf:datatype="http://www.w3.org/2001/XMLSchema#string">part of</rdfs:label>
</owl:ObjectProperty>
I'm trying to extract all the ObjectProperties,
for (OWLObjectProperty obp : ont.getObjectPropertiesInSignature()){
System.out.println(obp.toString());
}
this will print the name of ObjectProperty, e.g. http://purl.obolibrary.org/obo/BFO_0000050.
I wonder how to get the rdfs:label, e.g. part of

The rdfs:label in OWL is an annotation.
To get the label you must query for the annotation of the objectProperty you want.
To display all annotations of an ontology you can do something like that :
final OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(my_file));
final List<OWLAnnotation> annotations = ontology.objectPropertiesInSignature()//
.filter(objectProperty -> objectProperty.equals(the_object_property_I_want))//
.flatMap(objectProperty -> ontology.annotationAssertionAxioms(objectProperty.getIRI()))//
.map(OWLAnnotationAssertionAxiom::getAnnotation)//
.collect(Collectors.toList());
for (final OWLAnnotation annotation : annotations)
System.out.println(annotation.getProperty() + "\t" + annotation.getValue());
getObjectPropertiesInSignature() is deprecated in the modern (more than one year) version of owlapi (5). So please considere using the stream version objectPropertiesInSignature of java-8 . java-9 have been release few days ago, so it is a good time to learn the stream functionnality.
NB: the annotations are almost free, but OWL2 have put some more standardisation on it, so there is annotations with 'predefined semantics'.

Related

ElasticSearch - define custom letter order for sorting

I'm using ElasticSearch 2.4.2 (via HibernateSearch 5.7.1.Final from Java).
I have a problem with string sorting.
The language of my application has diacritics, which have a specific alphabetic
ordering. For example Ł goes directly after L, Ó goes after O, etc.
So you are supposed to sort the strings like this:
Dla
Dła
Doa
Dóa
Dza
Eza
ElasticSearch sorts by typical letters first, and moves all strange
letters to at the end:
Dla
Doa
Dza
Dła
Dóa
Eza
Can I add a custom letter ordering for ElasticSearch?
Maybe there are some plugins for this?
Do I need to write my own plugin? How do I start?
I found a plugin for Polish language for ElasticSearch,
but as I understand it is for analysing, and analysing is not a solution
in my case, because it will ignore diacritics and leave words with L and Ł mixed:
Dla
Dłb
Dlc
This would sometimes be acceptable, but is not acceptable in my specific usecase.
I will be grateful for any remarks on this.
I've never used it, but there is a plugin that could fit your needs: the ICU collation plugin.
You will have to use the icu_collation token filter, which will turns the tokens into collation keys. For that reason you will need to use a separate #Field (e.g. myField_sort) in Hibernate Search.
You can assign a specific analyzer to your field with #Field(name = "myField_sort", analyzer = #Analyzer(definition = "myCollationAnalyzer")), and define this analyzer (type, parameters) with something like that on one of your entities:
#Entity
#Indexed
#AnalyzerDef(
name = "myCollationAnalyzer",
filters = {
#TokenFilterDef(
name = "polish_collation",
factory = ElasticsearchTokenFilterFactory.class,
params = {
#Parameter(name = "type", value = "'icu_collation'"),
#Parameter(name = "language", value = "'pl'")
}
)
}
)
public class MyEntity {
See the documentation for more information: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#_custom_analyzers
It's admittedly a bit clumsy right now, but analyzer configuration will get a bit cleaner in the next Hibernate Search version with normalizers and analyzer definition providers.
Note: as usual, your field will need to be declared as sortable (#SortableField(forField = "myField_sort")).

Eclipse formatter: how do I format arrays in annotations

I'm using Hibernate Validation (JSR 303) and I'm trying to tame the Eclipse formatter to have nested annotations on seperate lines. Example:
#DefinedParametersMatchesResultPresence.List( {
#DefinedParametersMatchesResultPresence( measurement = Measurement.penetrationLength ),
#DefinedParametersMatchesResultPresence( measurement = Measurement.coneResistance ), #DefinedParametersMatchesResultPresence( measurement = Measurement.depth ),
#DefinedParametersMatchesResultPresence( measurement = Measurement.electricalConductivity ),
} )
However.. I can't get the annotations in the DefinedParametersMatchesResultPresence.List on a new line when running format. Additionally, the formatter does not comply to my max line length and wrap to a new line.
I'm using:
Version: Neon.2 Release (4.6.2)
Build id: 20161208-0600
Although the formatting concerns annotations, the array formatting also applies. After setting array formatting correctly it works as expected.
Next: its also possible to use the #Repeatable annotation when using your own annotations which lead to a nicer

Java approach for XML Validation

I want to validate the xml tag values in java.
Scenario:
Suppose I have data as follow
ELements: element1, element2, element3, element4, element5
Values: value1, value2, value3, value4, value5, value6, value7, value8
Now following are the possible combinations which I have to validate:
1. Element1 can have value1/value2 ( element1->value1/value2 )
2. Element2 can have value3/value4 ( element2->value3/value4 )
4. Element3 can have value5 if element1 has value1 ( element3->value5 if element1->value1 )
else Element3 can have value6 if element1 has value2 ( element3->value6 if element1->value2 )
5. Element4 can have value7 if element1->value1 and element2->value4
I can have hard-coding of the requirement in one java file but I want a flexible approach wherein if in future any new condition comes into picture then it can be easily added.
I thought of Hibernate Validation but later on came to know that it is supported for Java 6 and above. My constraint is that I have to use Java 1.5
Please suggest an appropriate approach to fulfill the above requirement. Any link suggestion would also work.
Note: Schema validation is already being carried out.
Explore Schematron for applying business rules on XML data
Schematron Project site - http://www.schematron.com
Introduction tutorial - http://www.dpawson.co.uk/schematron/introduction.html,
see for element to element constraints in the tutorial
One more tutorial - http://www.xml.com/lpt/a/1318
Check this out http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html. This has some basic validation techniques i guess assuming you have the schema
use DOMParser class in java.
It will help you a lot.

Configure checkstyle for method chaining?

Our project contains many statements in the method chaining fluent style:
int totalCount = ((Number) em
.createQuery("select count(up) from UserPermission up where " +
"up.database.id = :dbId and " +
"up.user.id <> :currentUserId ")
.setParameter("dbId", cmd.getDatabaseId())
.setParameter("currentUserId", currentUser.getId())
.getSingleResult())
.intValue();
I've got checkstyle mostly configured to match our existing code style, but now it's failing on these snippets, preferring instead:
int totalCount = ((Number) em
.createQuery("select count(up) from UserPermission up where " +
"up.database.id = :dbId and " +
"up.user.id <> :currentUserId ")
.setParameter("dbId", cmd.getDatabaseId())
.setParameter("currentUserId", currentUser.getId())
.getSingleResult())
.intValue();
Which is totally inappropriate. Is there anyway to configure checkstyle to accept the method chaining style? Is there an alternate tool I can run from maven to enforce this kind of indentation?
I never made this work in Eclipse so we barely use Format Source. In the end it is often best to extend. We tried hard and failed. It was one and half year ago. In the end we use formatting text only in Eclipse by Selecting the line or to preformat before we format by hand.
Usually the formating done by a engineer carries a certain meaning. And so automatic format will never work. Especially if you do something like
public static void myMethod(
int value, String value2, String value3)
If you autoformat this it fails similar to your example.
So feel free to join the club of not using automatic formatting beside as a step before you format it the human way.
with intellij , it can be done by selecting "align when multiline" in case of "method chain calls" so i guess this property is misconfigured in the configurations.

MongoDB can't find by national regex or query

Looks like I miss something important.
I have some records inserted into mongoDB that contains fields with national characters. There are no problem to insert it to DB or find them and all values looks pretty good.
But if I try to find particular one with "find()" or "regex()" they return nothing. For example:
DBObject query = new BasicDBObject();
query.put("position", Pattern.compile(".*forsøg.*"));
--or--
query.put("position","forsøg");
System.out.println(collection.find(query).count()); // prints 0
in log
query={ "position" : { "$regex" : ".*������.*"}}
--or---
query={ "position" : "������"}
Field value for "position" is equal "forsøg" ofc. Pattern.matches(".*forsøg.*", "forsøg") returns true.
If I replace pattern with one containing only ASCII characters (".abc." for example ) all methods work as expected. Collection.findAll() return all saved instances with readable and correct values.
Versions: MongoDB 2.0.6 64bit, mongo-java-driver 2.8.0, Java 7. I tried the same with spring-data-mongodb 1.0.2.RELEASE but removed it.
Looks like I meet a strange bug related with a maven + testng. The same code executed from .war and from testsuit provides totally different result in database.
The difference may be easily found if point your browser to
http://127.0.0.1:28017/baseName/collectionName/
and look at the values after each execution.

Categories