How can I query Object Properties in an OWL ontology - owl

I am working with the FoodOn ontology and need to figure out if a certain class is somehow related to another class. Typical use case: Can a vegan person eat honey? No, because honey is a subsub class of "invertebrate animal food product"!
I am using the python owlready2 library which allows me to run SPARQL queries and query subclass relations like this:
SELECT ?class
WHERE
{
# honey
<http://purl.obolibrary.org/obo/UBERON_0036016> rdfs:subClassOf* ?class .
# animal food product
?class rdfs:subClassOf* <http://purl.obolibrary.org/obo/FOODON_00001176>
}
This code gives me the full subclass path between honey and animal food product - great.
My problem is, that the relationship is not always that of a subclass. Let's look at "vegetarian food product" using the Protege editor:
We can see that "vegetarian food product" is a subclass of "food product by organism" but at the same time it is also equivalent to 'food product'
and (not ('derives from' some
('invertebrate animal' or 'vertebrate animal'))).
If I look at all triples using SPARQL I get the subclass relationship but the equivalentClass is just a bnode:
(rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'),
rdflib.term.URIRef('http://purl.obolibrary.org/obo/FOODON_00002381')),
(rdflib.term.URIRef('http://www.w3.org/2002/07/owl#equivalentClass'),
rdflib.term.BNode('5846'))
[stripped some output]
Why does SPARQL not return all relationships?
How can I query for all classes that are either a subclass of 'vegetarian food product' or related to it by some other object property?
Also, how can I query for the 'AND' and 'OR' component in the example above?
I do accept non-python solutions as long as it can be automated. Protege has a DL Query tab but I think that I cannot export the results using the UI...
Thank you!

Related

Can I use a recursive ParentalKey in Wagtail?

Say I have the following model in a Wagtail app:
# models.py
from django.db import models
from django.db.models.deletion import CASCADE
from wagtail.admin.edit_handlers import InlinePanel, FieldPanel
from modelcluster.models import ParentalKey, ClusterableModel
class Person(ClusterableModel):
name = models.CharField(max_length=300)
contact_for = ParentalKey(
'self', on_delete=CASCADE, null=True, related_name='contacts'
)
panels = [
FieldPanel('name'),
FieldPanel('contact_for'),
InlinePanel('contacts')
]
And the following hooks:
# wagtail_hooks.py
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
from home.models import Person
class PersonAdmin(ModelAdmin):
model = Person
menu_label = 'People'
menu_icon = 'list-ul'
menu_order = 200
add_to_settings_menu = False
exclude_from_explorer = False
list_display = ('name',)
search_fields = ('name',)
list_filter = ('contact_for',)
modeladmin_register(PersonAdmin)
When running the above and navigating to Admin > People > Add Person I will get a RecursionError exception (maximum recursion depth exceeded).
I'm guessing this is a Wagtail related issue, because I can use the class as intended in IPython:
./manage.py shell -i ipython
In [1]: from home.models import Person
In [2]: mike = Person(name='Mike')
In [3]: mike.contacts = [Person(name='Sam'), Person(name='John')]
In [4]: mike.contacts
Out[4]: <modelcluster.fields.create_deferring_foreign_related_manager.<locals>.DeferringRelatedManager at 0x7f36e9548af0>
In [5]: mike.save()
In [6]: [person.name for person in Person.objects.all()]
Out[6]: ['Mike', 'Sam', 'John']
In [7]: Person.objects.all()[1].contact_for.name
Out[7]: 'Mike'
So, is there a way I can make use of a recursive ParentalKey in Wagtail? What am I doing wrong/missing?
Edit: I just found this answer. So I'm wondering if I should even be trying to use ParentalKey (and ParentalManyToManyField) for non Page models.
Edit 2: For anyone interested, I ended up splitting my Model to avoid a recursive key. InlinePanels do work with ModelAdmin classes, it's just Wagtail doesn't seem to support recursive keys. I have also opted to use Snippets and SnippetChooserPanel where it felt appropriate.
A ParentalKey means that the child model is notionally treated as 'part of' the parent model and doesn't exist as an independent entity - for example, an image gallery being part of a page - for purposes such as versioning, and moderation workflow. (In Wagtail, non-page models handled through snippets or ModelAdmin don't have these features, but in order for them to share the same InlinePanel mechanism as pages, they also use ParentalKey.)
In this case, a Person is not part of another Person, and needs to be editable independently of the 'parent', so a ParentalKey isn't appropriate here. Instead, you should use a ForeignKey, which just indicates some relation between the models. This does mean that you can't use an InlinePanel to manage multiple Person records within the same view - you'll have to edit them separately, and use something like SnippetChooserPanel or a simple dropdown to set up the relations between them.

Sequelize 2 Many to Many relationship

I have videos that can be liked by users and commented by users so I need 2 many to many associations.
User model
User.belongsToMany(models.Video,{ through: 'user_like_video' });
User.belongsToMany(models.Video, { through: 'user_comment_video' });
Video model
Video.belongsToMany(models.User, {through: 'user_like_video'});
Video.belongsToMany(models.User, {through: 'user_comment_video'});
My resulting schema in DB is :
The many to many relationship between video a tags work well I can use the method getTags() to retrieve all the tags from a video but how are created methods when you have 2 (many to many relationship) ?
When I use user.getVideos(), I only get the last relation registered which is here user_comment_video.
I finally find a way :
Video.belongsToMany(models.User, {through: 'user_like_video', as: 'Like'});
Video.belongsToMany(models.User, {through: 'user_comment_video',as: 'Comment'});
this exposes methods getLike() and getComment()

Cakephp not loading belongsToMany association beetween two Models

I just started my project with cakephp3.0. I startet with the Users. I put a function isAuthorized in the UsersController which should validate if the User is permitted to view other users, to edit other users and so on.
I created a table "userprivileges" where all possible privileges, which can be assigned to a user are listed.
The table is "userprivileges" is:
id | controller | function
Next I want to group the priviliges into roles.
I created following table: "userprivilegeroles"
id | role
Next I created the BTM-Table: "userprivileges_userprivilegeroles"
id | userprivilegerole_id | userprivilegerole_id
The User is linked to the userprivilegeroles table, so I need to search if the requested controller/function is view-able for the user.
I tried like this:
$this->loadModel('Userprivilegeroles');
$userprivilegeroles = $this->Userprivilegeroles->find('all', [
'conditions' => [
'Userprivileges.controller' => $this->request->controller,
'Userprivileges.function' => $this->request->action],
'contain' => ['Userprivileges']
]);
Now this fails with following error:
Userprivilegeroles is not associated with Userprivileges.
There is a little more description, where I can search for the problem prompted to the 500 page.
The class for the specified table does not exist.
The Table was created with a typo: TableRegistry::get('Atricles');
The class file has a typo in the name or incorrect namespace: class Atricles extends Table
The file containing the class has a typo or incorrect casing: Atricles.php
The Table was used using associations but the association has a typo: $this->belongsTo('Atricles')
The table class resides in a Plugin but no plugin notation was used in the association definition.
Lets go for it one by one:
The class for the specified table does not exist.
It does:
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class UserprivilegerolesTable extends Table
{
public function initialize(array $config){
$this->belongsToMany('Userprivileges', [
'joinTable' => 'userprivileges_userprivilegeroles',
'className' => 'Userprivileges'
]);
}
}
Next
The Table was created with a typo: TableRegistry::get('Atricles');
No.
The class file has a typo in the name or incorrect namespace: class Atricles extends Table
no.
The file containing the class has a typo or incorrect casing: Atricles.php
no. its in src/Model/Table/Userprivilegeroles.php
The Table was used using associations but the association has a typo: $this->belongsTo('Atricles')
See above. I don't see a typo.
The table class resides in a Plugin but no plugin notation was used in the association definition.
As described, I called it from the UsersController.
Can anyone help me finding, why the association doesn't work as expected? Maybe I am to much stuck in the cakephp 2.0 ORM, but I am almost sure that I did the association like described in the cookbook.
I've searched already and found some other issues with the belongstomany association. But they all handle problems with saving data. I want to search data, so I see no connection there.
Actually the filename is incorrect, filenames must match classnames (PSR-4), so it should be UserprivilegerolesTable.php. The warning notes may need some fixing or further explanation, as they are a little misleading by not using the Table suffix.
Once you've fixed that you'll stumble over the next problem, contained belongsToMany and hasMany associations cannot be used in conditions, as they are retrieved in a separate query, you'll have to use matching() instead.
Cookbook > Database Access & ORM > Query Builder > Filtering by Associated Data
How to filter by conditions for associated models?

Case insensitive Charfield in django models

I am trying to achieve a category model where name has unique=True,
but practically I can still add same category name with different cases.
i.e. I have a category called Food
I am still able to add food, FOOD, fOod, FOOd
Is their any philosophy behind this? or it is a work in progress.
Cause in real world if I think of Category Food, it will always be food, no matter what case it has used to mention itself.
Thank you in advance to look at this.
To answer my own question:
I have found I can have clean method on my model. So I added
class Category(models.Model):
name = models.CharField(max_length=200, unique=True)
def clean(self):
self.name = self.name.capitalize()
It is capitalising the first letter, which is then handled by the save method, which calls the validate_unique method to raise error.
You can use Postgre specific model field called Citext fields (case insensitive fields).
There are three option at the moment:
class CICharField(**options), class CIEmailField(**options) and class CITextField(**options)
Example:
from django.db import models
from django.contrib.postgres.fields import CICharField
class Category(models.Model):
name = CICharField(verbose_name="Name", max_length=255)
But don't forget to create an extension for the citext fields.
See here.
Basically, you have to add the extension class in the migration file, inside the operations array, before the first CreateModel operation.
# migration file
operations = [
CITextExtension(), # <------ here
migrations.CreateModel(
...
),
...,
]
Setting the column to case-insensitive collation should fix this. You may need to do it at the SQL level.

AppEngine Datastore get entities that have ALL items in list property

I want to implement some kind of tagging functionality to my app. I want to do something like...
class Item(db.Model):
name = db.StringProperty()
tags = db.ListProperty(str)
Suppose I get a search that have 2 or more tags. Eg. "restaurant" and "mexican".
Now, I want to get Items that have ALL, in this case 2, given tags.
How do I do that? Or is there a better way to implement what I want?
I believe you want tags to be stored as 'db.ListProperty(db.Category)' and then query them with something like:
return db.Query(Item)\
.filter('tags = ', expected_tag1)\
.filter('tags = ', expected_tag2)\
.order('name')\
.fetch(256)
(Unfortunately I can't find any good documentation for the db.Category type. So I cannot definitively say this is the right way to go.) Also note, that in order to create a db.Category you need to use:
new_item.tags.append(db.Category(unicode(new_tag_text)))
use db.ListProperty(db.Key) instead,which stores a list of entity's keys.
models:
class Profile(db.Model):
data_list=db.ListProperty(db.Key)
class Data(db.Model):
name=db.StringProperty()
views:
prof=Profile()
data=Data.gql("")#The Data entities you want to fetch
for data in data:
prof.data_list.append(data)
/// Here data_list stores the keys of Data entity
Data.get(prof.data_list) will get all the Data entities whose key are in the data_list attribute

Resources