Skip to main content

Querying Inferred Data

This page demonstrates how to query both asserted facts and inferred facts using Fluree's reasoning capabilities.

Find All Mammals

With the class hierarchy in place, query for all Mammals:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?organism": ["schema:name", "bio:scientificName"]
},
"where": { "@id": "?organism", "@type": "bio:classes/Mammal" },
"opts": {
"reasoner": "owl2rl"
}
}

Result includes:

  • Lion (typed as Feline → inferred as Mammal)
  • Tiger (typed as Feline → inferred as Mammal)
  • Wolf (typed as Canine → inferred as Mammal)
  • Elephant (typed as Herbivore → inferred as Mammal)
  • Chimpanzee (typed as Primate → inferred as Mammal)
  • ... and all other mammals

Without reasoning, only organisms explicitly typed as bio:classes/Mammal would be returned.

Find All Animals

Go further up the hierarchy:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?organism": ["schema:name", "type"]
},
"where": { "@id": "?organism", "@type": "bio:classes/Animal" },
"opts": {
"reasoner": "owl2rl"
}
}

This returns ALL organisms in the database, since every class is a subclass of Animal.

Find All Carnivores

Query for meat-eaters:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?organism": ["schema:name", "bio:habitat"]
},
"where": { "@id": "?organism", "@type": "bio:classes/Carnivore" },
"opts": {
"reasoner": "owl2rl"
}
}

Result:

  • Lion (Feline → Carnivore)
  • Tiger (Feline → Carnivore)
  • Domestic Cat (Feline → Carnivore)
  • Wolf (Canine → Carnivore)
  • Arctic Fox (Canine → Carnivore)

Query Inverse Relationships

Find what each organism is eaten by:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": ["?preyName", "?predatorName"],
"where": [
{ "@id": "?prey", "bio:properties/eatenBy": "?predator", "schema:name": "?preyName" },
{ "@id": "?predator", "schema:name": "?predatorName" }
],
"opts": {
"reasoner": "owl2rl"
}
}

Even though we only asserted eats relationships, the inverse eatenBy is inferred:

PreyEaten By
DeerLion
DeerWolf
MouseEagle
MouseHawk
MouseCobra

Find All Predators of an Organism

What eats the mouse?


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?predator": ["schema:name", "type"]
},
"where": { "@id": "bio:organisms/mouse", "bio:properties/eatenBy": "?predator" },
"opts": {
"reasoner": "owl2rl"
}
}

Result: Bald Eagle, Red-tailed Hawk, King Cobra

Query Symmetric Relationships

Find organisms related to the Lion:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?related": ["schema:name"]
},
"where": { "@id": "bio:organisms/lion", "bio:properties/relatedTo": "?related" },
"opts": {
"reasoner": "owl2rl"
}
}

Or query from the Tiger's perspective:


{
"select": {
"?related": ["schema:name"]
},
"where": { "@id": "bio:organisms/tiger", "bio:properties/relatedTo": "?related" }
}

Both queries return the same result due to symmetry.

Endangered Species Analysis

Find endangered mammals:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?organism": ["schema:name", "bio:conservationStatus", "bio:habitat"]
},
"where": { "@id": "?organism", "@type": "bio:classes/Mammal", "bio:conservationStatus": "Endangered" },
"opts": {
"reasoner": "owl2rl"
}
}

Result:

  • Tiger (Feline → Mammal, Endangered)
  • Chimpanzee (Primate → Mammal, Endangered)
  • Gorilla (Primate → Mammal, Endangered)

Compare Inferred vs Asserted Types

See all types (both asserted and inferred) for an organism:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": ["?type"],
"where": { "@id": "bio:organisms/tiger", "@type": "?type" },
"opts": {
"reasoner": "owl2rl"
}
}

Result:

  • bio:Organism (asserted)
  • bio:classes/Feline (asserted)
  • bio:classes/Carnivore (inferred)
  • bio:classes/Mammal (inferred)
  • bio:classes/Animal (inferred)

Habitat Analysis with Inference

Find all animals in a specific habitat:


{
"@context": {
"bio": "https://biology-example.com/ns/",
"schema": "http://schema.org/"
},
"select": {
"?organism": ["schema:name", "type"]
},
"where": { "@id": "?organism", "@type": "bio:classes/Animal", "bio:habitat": {"@id": "bio:habitats/Forest"} },
"opts": {
"reasoner": "owl2rl"
}
}

Returns all forest-dwelling animals across the entire taxonomy.

tip

Reasoning enables you to write simpler queries—ask for "Mammals" and get all organisms that are mammals, regardless of how specifically they were typed.

Performance Considerations

Reasoning adds computational overhead. Strategies for optimization:

  1. Materialize inferences: Pre-compute and store inferred facts
  2. Limit scope: Use reasoner only when needed
  3. Optimize hierarchy depth: Deeper hierarchies = more inference work

Summary

Query PatternResult
type AnimalAll organisms (everything is an animal)
type MammalAll mammals including Felines, Canines, etc.
eatenBy ?predatorInverse of asserted eats relationships
relatedTo ?xBoth directions of symmetric relationships
All types for entityShows asserted + all inferred supertypes

Reasoning transforms your data model from explicit to expressive—store minimal facts, query rich knowledge.