SPARQL: Get all entities from a P279+ query - sparql

Example: Q11862829 is subclass of Q1047113, which is subclass of Q9081. Can I build a query that returns all entities of which Q11862829 is subclass of between it and Q9081?
wd:Q11862829 wdt:P279+ wd:Q9081 is "close to what I want.

Related

simple SPARQL with OR operator

I am trying to learn SPARQL to use with WikiData and I can't figure out how to perform an 'OR' statement, for instance find all taxons which are a subclasses of mammals OR a subclass of a subclass of mammals. I don't see how to use the VALUES method and if I use operator P171* with filter it takes too long. The following code provides an 'AND' statement I would like the equivalent 'OR' statement.
SELECT ?taxon
WHERE
{
?taxon wdt:P171 wd:Q7377. #taxon is a subclass of mammals
?taxonn wdt:P171/wdt:P171 wd:Q7377. #taxon is a subclass of a subclass of mammals
}
There are multiple ways to perform an 'OR' statement in SPARQL. A good overview is provided in the follwing answer: https://stackoverflow.com/a/30502737/12780418.
For your query the easiest way is to use |, i.e.
SELECT ?taxon WHERE {
?taxon wdt:P171|wdt:P171/wdt:P171 wd:Q7377.
}

HQL Fetch Join without knowing object implementation

I'm having performance issues and I'm trying to add some fetch join to get some data in only one request.
My problem is :
I got a mapped object AbstractObject.
And I also got other mapped object like ObjectA, ObjectB etc... all are extending AbstractObject.
In my ObjectA I got a list of foos.
In my ObjectB I got a list of otherFoos.
What I'm trying to do :
I want to write a request like this :
entityManager.createQuery("SELECT ao FROM AbstractObject ao LEFT JOIN FETCH ao.foos LEFT JOIN FETCH ao.otherFoos WHERE ao.id = ?1", AbstractObject.class)
Problem: I got a nullPointerException because, I think, hibernate does not know foos or otherFoos for AbstractObject.
In my point of view, I do not know if I will have a ObjectA or ObjectB.
Do someone know a solution for this issue ?
Hibernate version : 5.0.12.Final
In general, in order to execute an HQL query and return properties of multiple entities on the same result, you need a projection to a DTO.
So you will create a DTO for example ObjectAB, which will not be mapped, it will probably extending both Object A and B and then you will create your query and set a result transformer to this DTO.
Alternatively you could use a Tuple instead of a DTO, but this doesn't change the main idea. For more details of this methodology you may read this article.

Spring Data Neo4j Cypher get entity type or class name

In my SDN 4 project I have a following Cypher query(a part of query):
(entity)<-[:COMMENTED_ON]-(comg:CommentGroup)
for example I can get id of entity with a following Cypher function id(entity)
How to get entity name or class name ?
Use the labels function
match (entity)<-[:COMMENTED_ON]-(comg:CommentGroup) return id(entity), labels(entity)
For each row returned, you'll get the Neo4j id and array of labels.
Assuming your NodeEntity class labels match at least one of these labels, you can then iterate and load the appropriate class instance yourself.
Generally speaking you shouldn't need to do this however.
If (entity) is polymorphic, SDN/OGM will hydrate the correct objects for you. It pretty much does under the hood what I described above, but it also handles matching on interfaces, subclasses, etc.

Adding multiple domains to objectProperty in Protege 5

I have created an ontology using Protege 5-beta-17. In my ontology I have some classes:
Mountain, Lake, Location etc...
I also have an object property:
hasLocation.
For this object property I have set the range the "Location" class, and the domain
the "Mountain" and "Lake" classes.
When I try to view the ontology using the CMap tool it shows that only the
"Mountain" "hasLocation" "Location".
The "Lake" class is presented without the "hasLocation" object property.
Did I do something wrong? Ore do I have do something else in Protege?
I found out what the problem was.
When adding a domain/range to object property in protege you have to click the following buttons and select one of your classes:
If you want to add another domain/range you simply click one of the buttons again and add another class. If you are doing it like this your telling Protege that the domain/range of your object property is an INTERSECTION of two classes. This means that the individual that will take the domains/ranges place is an INSTANCE OF BOTH CLASSES and NOT EXCLUSIVELY OF ONE OF THEM.
This was my mistake. I was adding the classes to the domain in the wrong way.
So... The correct way for adding multiple distinct domains for an object property is the following:
Simply click the domain/range button again and select the "Class expression editor" tab:
And in the "Class expression editor" type in your classes like this: "ClassA or ClassB or ClassC or ...".
In my case it was "Mountain or Lake".
After that click "ok" and thats it.

NSFetchedResultsController - KVO, UITableView and a "Tree"

I'm using a NSFetchedResultsController to implement KVO for my UITableView (which is obvious). What I can't figure out is, how to use multiple Entities - sort of a tree structure - to be present(ed) simultaneously.
Here is my setup:
Entity1
DisplayName
RelationToEntity2
Entity2
DisplayName
Now I can fetch the data of either to be presented - so far so good. What I want is to have a one-sectioned TableView (like a flattened view) with the following structure:
Entity1 (Entry 1)
Entity2 (Entry 1)
Entity2 (Entry 2)
…
Entity1 (Entry 2)
…
Though it might look like a thing to be done via sections, it's not. Both Entities should be a UITableViewCells. Can someone please point me the right direction to flatten the without loosing the actual hierarchy.
It sounds like you need to maintain your own 'flattened' datasource. Perhaps the following will work:
When NSFetchedResultController tells you a new Entity1 has been inserted, you insert Entity1 and its associated Entity2s into say, _flattenedArray so it looks like this:
[<Entity1>, <related Entity2>, <related Entity2>...]
Where you insert them is up to you - it pretty much comes down to:
construct a subarray containing the new Entity1 and associated Entity2 objects
decide where in _flattenedArray to insert the new subarray.
call reloadData or some other means to inform the tableView of the new data
When an Entity1 object is removed, remove it and all subsequent Entity2 objects until you encounter the end of _flattenedArray or run into another Entity1 object. This assumes Entity2 is never a "top level" object. If it is you will need to only remove those Entity2 objects in the relation.
When an Entity1 object gains or loses an Entity2 object, you can first delete the Entity1 object from _flattenedArray then reinserting it. If this is too efficient, do a merge instead.
This is exactly the situation to use entity inheritance. When fetching a parent abstract entity with a fetched results controller all of the child entities can be displayed by the table. Use a section order property to display the sections in the order you prefer. And use a second sort property to order within the sections. The Notes app does this by fetching an abstract Container entity and displays Accounts and Folders which are child entities. It sections by Account and the first folder "All iCloud" cell is actually an Account. Folders have a relation to an account even though they are equals in the entity inheritance tree.