RelationshipQuery

View source

Queries and traverses relationships while reading data from both sides of each connection.

The relationship constructor identifies the component on the entity being expanded. That component must implement TraverseEntities; its TraverseEntities#visit result supplies the connected entity handles. The target constructor identifies the component required on each connected entity. The query uses one ECS Query for each side, so the same data tuple and filters can be applied to both the relationship and target entities.

Use RelationshipQuery#treebfs or RelationshipQuery#treedfs when the relationship is a hierarchy and repeated visits should follow its structure. Use RelationshipQuery#graphbfs or RelationshipQuery#graphdfs when connections can meet or form cycles and entity IDs should be tracked while walking.

Examples

import { EntityHandle, World } from '@wimaengine/ecs'
import { Children, Parent } from '@wimaengine/hierarchy'
import { RelationshipQuery } from '@wimaengine/relationship'

const world = new World()
const root = world.spawn([new Children()])
const child = world.spawn([new Parent(root)])
world.get(root, Children).list.push(child)

const query = new RelationshipQuery(world, Children, Parent, [EntityHandle])
query.treebfs(root, ([childHandle], [parentHandle]) => {
  console.log(parentHandle.id(), childHandle.id())
})

Constructor


constructor<Relationship extends TraverseEntities, Target extends TraverseEntities = Relationship, Data extends unknown[] = [EntityHandle], Filter extends QueryFilter<FilterTransform>[] = []>(world: World, relationship: Constructor<Relationship>, target?: Constructor<Target>, data?: [...TupleConstructor<Data>[]], filter?: [...Filter[]])

View source

Creates relationship and target queries for a world.

The relationship-side query matches [relationship, ...data] on the entity currently being expanded. The target-side query matches [target, ...data] on each handle returned by the relationship. In both result tuples, the leading relationship or target component is consumed by the traversal and the remaining data values are passed to visit. Both queries use the supplied filters; see Query for query-cache and filter behavior.

Type Parameters

Relationship extends TraverseEntities

Target extends TraverseEntities = Relationship

Data extends unknown[] = [EntityHandle]

Filter extends QueryFilter<FilterTransform>[] = []

Parameters

NameDescriptionDefault
world

World whose entities the query reads.

—
relationship

Relationship component on entities being expanded. It must implement TraverseEntities.

—
target?

Target component required on connected entities. Defaults to the relationship constructor.

...
data?

Additional component constructors returned to the callback on both sides. Defaults to EntityHandle.

...
filter?

Filters applied to both the relationship-side and target-side queries.

...

Properties


Methods


treebfs(entity: EntityHandle, visit: object): void

View source

Traverses relationship edges breadth-first without tracking visited IDs.

Every relationship result is queued in the order returned by TraverseEntities#visit, so the traversal follows the relationship structure level by level and may revisit an entity when the structure reaches it again. For each edge whose target query matches, visit receives (descendantData, ancestorData) in that order. A missing target match skips the callback, but the handle is still queued and can be expanded if its relationship query matches. If the starting entity has no relationship match, the traversal finishes without invoking visit.

Parameters

NameDescriptionDefault
entity

Entity from which to begin traversal.

—
visit

Called with the descendant data first and ancestor data second.

—

treedfs(entity: EntityHandle, visit: object): void

View source

Traverses relationship edges depth-first without tracking visited IDs.

Children are visited in the order returned by TraverseEntities#visit, descending before the next sibling. Every structural occurrence is eligible to be visited again. The callback receives (descendantData, ancestorData) in that order. If the starting entity has no relationship match, the method returns immediately. For an edge, a missing ancestor or target query match skips the callback and does not expand that descendant further.

Parameters

NameDescriptionDefault
entity

Entity from which to begin traversal.

—
visit

Called with the descendant data first and ancestor data second.

—

graphbfs(entity: EntityHandle, visit: object): void

View source

Traverses relationship edges breadth-first while tracking visited IDs.

An entity whose ID has already been visited is skipped when it is removed from the queue. Descendant handles whose IDs are already visited are filtered before a relationship result is queued; duplicate edges can therefore still be reported when they are queued before the target ID is marked visited. For each matching target, visit receives (descendantData, ancestorData) in that order. A missing ancestor match skips that queue entry. A missing target match skips only the callback; the handle remains eligible for expansion if it has relationship data.

Parameters

NameDescriptionDefault
entity

Entity from which to begin traversal.

—
visit

Called with the descendant data first and ancestor data second.

—

graphdfs(entity: EntityHandle, visit: object): void

View source

Traverses relationship edges depth-first while tracking visited IDs.

The starting entity must match the relationship-side query; otherwise the method returns immediately. Before each edge is processed, a visited descendant ID skips that edge. When both query sides match, visit receives (descendantData, ancestorData) in that order, then the edge’s ancestor ID is recorded as visited before its relationship is expanded. A missing ancestor or target match skips the edge and does not expand it. The traversal therefore follows graph IDs rather than treating every structural occurrence as a new tree node.

Parameters

NameDescriptionDefault
entity

Entity from which to begin traversal.

—
visit

Called with the descendant data first and ancestor data second.

—