Skip to content

Introduce relationship queries.#246

Merged
waynemwashuma merged 14 commits into
wimaengine:devfrom
waynemwashuma:relationships
Sep 10, 2025
Merged

Introduce relationship queries.#246
waynemwashuma merged 14 commits into
wimaengine:devfrom
waynemwashuma:relationships

Conversation

@waynemwashuma
Copy link
Copy Markdown
Collaborator

@waynemwashuma waynemwashuma commented Sep 10, 2025

Objective

Introduce a relationship query system that enables traversing entity relationships (e.g. parent–child trees or arbitrary graphs) in both breadth-first and depth-first order. This provides a generic and reusable foundation for hierarchical and graph-based entity systems, with strong type safety.

Solution

  • Add VisitEntities interface: Provides a consistent contract for traversal across relationship types through the visit method.Must be implemented by relationship components e.g (e.g. Children, Parent, Neighbour)
  • Added RelationshipQuery : Accepts a World, a relationship type (e.g. Children), an optional target type (e.g. Parent), and optional Data/Filter parameters.Can be used to traverse relationship edges in breadth first or depth first order.
  • Added test suite for RelationshipQuery which verifies traversal correctness across multiple topologies:
    • Tree with nested children and grandchildren.
    • Grid-like graph of interconnected neighbours.

Tests assert that traversal order and coverage match expectations for BFS and DFS.

Showcase

A RelationshipQuery can be used to walk hierarchies and a graphs consistently:

// Traverse a parent–child tree
const query = new RelationshipQuery(world, Children, Parent, [Entity])
query.treebfs(rootEntity, ([child], [parent]) => {
  // Do something with (parent, child)
})

// Traverse a directed graph of neighbours
const graphQuery = new RelationshipQuery(world, Neighbour)
graphQuery.graphdfs(startEntity, ([neighbour], [self]) => {
  // Visit edges in depth-first order
})

This makes hierarchical propagation (e.g. transforms, constraints) and graph traversals (e.g. connectivity, influence spreading) straightforward and safe across entities.

Migration guide

These changes do not break existing code however:

  • Relationship components (like Children and Parent) should now implement VisitEntities.
  • For tree traversals, replace manual loops over queries with RelationshipQuery.treebfs or RelationshipQuery.treedfs.
  • For graph traversals, replace ad-hoc neighbour iteration with RelationshipQuery.graphbfs or RelationshipQuery.graphdfs.

Checklist

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@waynemwashuma waynemwashuma self-assigned this Sep 10, 2025
@waynemwashuma waynemwashuma added type:enhancement New feature or request mod:relationship This PR/issue involves relationship package labels Sep 10, 2025
@waynemwashuma waynemwashuma marked this pull request as ready for review September 10, 2025 19:33
@waynemwashuma waynemwashuma merged commit 4e4e873 into wimaengine:dev Sep 10, 2025
5 checks passed
@waynemwashuma waynemwashuma deleted the relationships branch September 10, 2025 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:relationship This PR/issue involves relationship package type:enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant