Search code examples
rdftriples

How do you express that some subject relates to anything in RDF semantic triples?


Most examples of triples in RDF/TTL are of the very specific sort, relating terms:

  • Dogs eat steak

And where sometimes alternatives are allowed, like this:

  • Goats eat grass, and Goats eat brambles, and Goats eat cardboard, and [...]

But I would like to express something like this in RDF:

  • Dogs eat (any kind of meat)

Or even more general statements

  • Goats eat anything

So the object is restricted only by a category of some sort, or perhaps not restricted at all.

What is the right way to do this?


Solution

  • How to do this is very dependent on the domain you're trying to model, what you want to use the model for, and what exactly you're trying to convey. Unfortunately, your simple examples are not actually as simple as they appear. For example:

    Dogs eat steak

    Do you mean that there is a particular dog that eats steak, or is it meant to generally state that all dogs eat steak? If the latter: do you mean to say that dogs never eat anything but steak, or do you mean to say that steak is one of the things that dogs eat? Do all dogs always eat something, or do some dogs not eat anything at all? Being precise matters in conceptual modelling.

    So, assuming that what we actually want to say is "it is true for every dog that if it eats something, it's steak", we could express that as follows using RDF Schema:

      my:Dog a rdfs:Class .
      my:Steak a rdfs:Class .
    
      my:eats a rdf:Property ;
            rdfs:domain my:Dog ;
            rdfs:range my:Steak .
    

    Actually, we have said even more here: we've not only said that if a dog eats something, that something is steak, we have also said that if somebody eats anything, that somebody is a Dog. In other words, only dogs can eat anything, and all they can eat is steak.

    So, that might be too simplistic, right?

    To model the more complex "if a dog eats something, it must be steak" without automatically implying that anybody who eats anything is a dog, we can use the Web Ontology Language, OWL, and use a property restriction:

      my:Dog a owl:Class ;
           rdfs:subClassOf [ a owl:Restriction ;
                             owl:onProperty my:eats ;
                             owl:allValuesFrom my:Steak ].
    

    This literally says "the class of dogs is a subclass of the class of all things that eat only steak".

    ...and of course, there's other, more complex ways to model other kinds of statements and restrictions. If you want to know more about how to do this kind of thing, I suggest you look through one of the many tutorials on RDF / OWL ontology modelling that are available online.

    Of course, like I said in the beginning, whether you need this kind of modelling depends on your application, and what you are trying to use this model for. In some cases, it's perfectly fine to just say something like this in RDF:

    my:Dog a my:Animal .
    my:Meat a my:Food .
    my:Dog my:eats my:Meat .
    my:Goat my:eats my:Food .    
    

    However, using the more expressive RDFS/OWL vocabulary to model classes and relations has many benefits, particularly in reuse of your dataset, and in how existing tools (such as reasoners and query engines) can make use of it.