Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@
"dist/*"
],
"dependencies": {
"vifaa": "^0.1.0"
"vifaa": "^0.2.0"
}
}
160 changes: 140 additions & 20 deletions src/schedule/core/systembuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class SchedulerBuilder {
const group = {
id: context.groups.length,
config,
parentId: undefined,
systems: []
}

Expand Down Expand Up @@ -101,9 +102,6 @@ export class SchedulerBuilder {
}

context.systems.push(system)
context.systemIdsByFunc.set(systemLabel, system.id)

// context.systemIdsByFunc.set(systemLabel, system.id) // Removed as it's no longer used

if (systemLabel !== '') {
const existing = context.nodesByLabel.get(systemLabel)
Expand All @@ -117,6 +115,8 @@ export class SchedulerBuilder {
}

for (const [scheduleLabel, context] of schedules) {
this.resolveGroupParents(context, scheduleLabel)

for (let i = 0; i < context.systems.length; i++) {
const system = context.systems[i]
const groupLabel = system.config.systemGroup
Expand All @@ -136,6 +136,72 @@ export class SchedulerBuilder {
return schedules
}

/**
* @private
* @param {ScheduleContext} context
* @param {string} scheduleLabel
*/
resolveGroupParents(context, scheduleLabel) {
for (let i = 0; i < context.groups.length; i++) {
const group = context.groups[i]
const parentLabel = group.config.parent

if (!parentLabel) continue

const parentId = context.groupIdsByTypeId.get(typeid(parentLabel))

if (parentId === undefined) {
throws(`The parent system group "${parentLabel.name}" must be registered explicitly before it can be used on schedule "${scheduleLabel}".`)
}

group.parentId = parentId
}

this.assertNoGroupCycles(context)
}

/**
* @private
* @param {ScheduleContext} context
*/
assertNoGroupCycles(context) {

/** @type {GroupVisitState[]} */
const state = new Array(context.groups.length).fill(GroupVisitState.Unvisited)

for (let i = 0; i < context.groups.length; i++) {
this.visitGroupHierarchy(context, i, state)
}
}

/**
* @private
* @param {ScheduleContext} context
* @param {number} groupId
* @param {GroupVisitState[]} state
*/
visitGroupHierarchy(context, groupId, state) {
const visitState = state[groupId]

if (visitState === GroupVisitState.Visiting) {
const group = context.groups[groupId]

throws(`Schedule "${context.label}" contains cyclic system group nesting involving "${group.config.label.name}".`)
}

if (visitState === GroupVisitState.Visited) return

state[groupId] = GroupVisitState.Visiting

const { parentId } = context.groups[groupId]

if (parentId !== undefined) {
this.visitGroupHierarchy(context, parentId, state)
}

state[groupId] = GroupVisitState.Visited
}

/**
* @private
* @param {ScheduleContext} context
Expand Down Expand Up @@ -173,6 +239,9 @@ export class SchedulerBuilder {
/** @type {Map<number, number>} */
const graphIdsBySystemId = new Map()

/** @type {Map<number, number[]>} */
const groupSystemsCache = new Map()

/** @type {Set<string>} */
const edges = new Set()

Expand All @@ -189,7 +258,7 @@ export class SchedulerBuilder {
this.addNodeOrdering(context, graph, graphIdsBySystemId, edges, {
kind: ScheduleNodeKind.System,
id: system.id
}, system.config.before, system.config.after)
}, system.config.before, system.config.after, groupSystemsCache)
}

for (let i = 0; i < context.groups.length; i++) {
Expand All @@ -198,7 +267,7 @@ export class SchedulerBuilder {
this.addNodeOrdering(context, graph, graphIdsBySystemId, edges, {
kind: ScheduleNodeKind.Group,
id: group.id
}, group.config.before, group.config.after)
}, group.config.before, group.config.after, groupSystemsCache)
}

return graph
Expand All @@ -213,24 +282,25 @@ export class SchedulerBuilder {
* @param {ScheduleNodeRef} source
* @param {(SystemFunc | Constructor | string)[] | undefined} before
* @param {(SystemFunc | Constructor | string)[] | undefined} after
* @param {Map<number, number[]>} groupSystemsCache
*/
addNodeOrdering(context, graph, graphIdsBySystemId, edges, source, before, after) {
addNodeOrdering(context, graph, graphIdsBySystemId, edges, source, before, after, groupSystemsCache) {
if (before) {

for (let i = 0; i < before.length; i++) {
const label = describeReference(before[i])

this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, source, this.resolveNode(context, label), before[i])
this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, source, this.resolveNode(context, label), label)
this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, source, this.resolveNode(context, label), before[i], groupSystemsCache)
this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, source, this.resolveNode(context, label), label, groupSystemsCache)
}
}

if (after) {
for (let i = 0; i < after.length; i++) {
const label = describeReference(after[i])

this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, this.resolveNode(context, label), source, after[i])
this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, this.resolveNode(context, label), source, label)
this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, this.resolveNode(context, label), source, after[i], groupSystemsCache)
this.addExpandedEdge(context, graph, graphIdsBySystemId, edges, this.resolveNode(context, label), source, label, groupSystemsCache)
}
}
}
Expand Down Expand Up @@ -261,10 +331,11 @@ export class SchedulerBuilder {
* @param {ScheduleNodeRef} from
* @param {ScheduleNodeRef} to
* @param {SystemFunc | Constructor | string} targetLabel
* @param {Map<number, number[]>} groupSystemsCache
*/
addExpandedEdge(context, graph, graphIdsBySystemId, edges, from, to, targetLabel) {
const fromSystems = this.expandNodeToSystems(context, from)
const toSystems = this.expandNodeToSystems(context, to)
addExpandedEdge(context, graph, graphIdsBySystemId, edges, from, to, targetLabel, groupSystemsCache) {
const fromSystems = this.expandNodeToSystems(context, from, groupSystemsCache)
const toSystems = this.expandNodeToSystems(context, to, groupSystemsCache)

for (let i = 0; i < fromSystems.length; i++) {
for (let j = 0; j < toSystems.length; j++) {
Expand Down Expand Up @@ -295,12 +366,55 @@ export class SchedulerBuilder {
* @private
* @param {ScheduleContext} context
* @param {ScheduleNodeRef} node
* @param {Map<number, number[]>} groupSystemsCache
* @returns {number[]}
*/
expandNodeToSystems(context, node) {
expandNodeToSystems(context, node, groupSystemsCache) {
if (node.kind === ScheduleNodeKind.System) return [node.id]

return context.groups[node.id].systems
return this.expandGroupToSystems(context, node.id, groupSystemsCache, new Set())
}

/**
* @private
* @param {ScheduleContext} context
* @param {number} groupId
* @param {Map<number, number[]>} cache
* @param {Set<number>} visiting
* @returns {number[]}
*/
expandGroupToSystems(context, groupId, cache, visiting) {
const cached = cache.get(groupId)

if (cached) return cached

if (visiting.has(groupId)) {
const group = context.groups[groupId]

throws(`Schedule "${context.label}" contains cyclic system group nesting involving "${group.config.label.name}".`)
}

visiting.add(groupId)

/** @type {number[]} */
const systems = [...context.groups[groupId].systems]

for (let i = 0; i < context.groups.length; i++) {
const child = context.groups[i]

if (child.parentId !== groupId) continue

const childSystems = this.expandGroupToSystems(context, child.id, cache, visiting)

for (let j = 0; j < childSystems.length; j++) {
systems.push(childSystems[j])
}
}

visiting.delete(groupId)
cache.set(groupId, systems)

return systems
}
}

Expand All @@ -319,10 +433,7 @@ function getOrCreateScheduleContext(schedules, label) {
systems: [],
groups: [],
nodesByLabel: new Map(),
groupIdsByTypeId: new Map(),
systemIdsByFunc: new Map()

// systemIdsByFunc: new Map() // Removed as it's no longer used
groupIdsByTypeId: new Map()
})

schedules.set(label, created)
Expand Down Expand Up @@ -352,7 +463,6 @@ const ScheduleNodeKind = Object.freeze({
* @property {SystemGroupRegistration[]} groups
* @property {Map<string, ScheduleNodeRef>} nodesByLabel
* @property {Map<TypeId, number>} groupIdsByTypeId
* @property {Map<string, number>} systemIdsByFunc
*/

/**
Expand All @@ -365,6 +475,7 @@ const ScheduleNodeKind = Object.freeze({
* @typedef SystemGroupRegistration
* @property {number} id
* @property {SystemGroupConfig} config
* @property {number | undefined} parentId
* @property {number[]} systems
*/

Expand All @@ -373,3 +484,12 @@ const ScheduleNodeKind = Object.freeze({
* @property {ScheduleNodeKind} kind
* @property {number} id
*/

/**
* @enum {number}
*/
const GroupVisitState = Object.freeze({
Unvisited: 0,
Visiting: 1,
Visited: 2
})
1 change: 1 addition & 0 deletions src/schedule/core/systemconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @typedef SystemGroupConfig
* @property {Constructor} label
* @property {string} schedule
* @property {Constructor | undefined} [parent]
* @property {SystemOrderReference[] | undefined} [before]
* @property {SystemOrderReference[] | undefined} [after]
*/
Expand Down
Loading
Loading