2

I want to get the class which an individual belongs to. In more detail, I want to pass a parameter to a method which is an individual using python (using rdflib), method should return class of an individual which it belongs to.

For example: I have a AlcoholicBeverage class, it has subclass Beer and Beer class has a subclass Brands. In Brands class there are many individuals which represents beer brands. I want to pass a parameter to a method, run SPARQL query to find, xyz is an individual of Brands class.

Ideally I also want to get, xyz is a Beer (because Brands class is subclass of Beer) and Beer is a AlcoholicBeverage.

Thanks for your help.

DNA
  • 42,007
  • 12
  • 107
  • 146
alan turing
  • 463
  • 4
  • 20

1 Answers1

3

How about:

SELECT ?class WHERE { xyz a ?class . }

Note that a is a shorthand for rdf:type - see the SPARQL specification section 4.2.4

You will only get the superclasses (AlcoholicBeverage, Beer) from this query if they have either been explicitly added to your RDF model, or if you have inference enabled (and the schema loaded into the model), so that they are added implicitly.

Your schema is a bit strange - Beer is clearly a subclass of AlcoholicBeverage, but what is Brands? Why not either make each brand a subclass of Beer (implying that the individuals are actual physical drinks: "This drink is an Old Peculier, which is a type of Beer"), or make each brand an individual of Beer: "Old Peculier is a Beer", for example. Brands seems to be a sort of informal placeholder that doesn't make sense in a formal schema? Why is Brands plural when your other class names are singular?

Stanislav Kralin
  • 11,070
  • 4
  • 35
  • 58
DNA
  • 42,007
  • 12
  • 107
  • 146
  • Thanks for your reply and suggestion. I believe my problem is, I can not refer to the xyz in correct way. Without filter section, following query works fine, but with the FILTER part, it returns nothing. How can I refer to a class or individual in .owl file? PREFIX owl: [link] (http://goo.gl/ZwwgT) SELECT ?class WHERE { ?sub rdfs:subClassOf ?class . FILTER(?sub=owl:Brands)} – alan turing Nov 12 '12 at 22:33