Skip to content

Commit

Permalink
Adapt Random node to new introspector
Browse files Browse the repository at this point in the history
  • Loading branch information
Calamari committed May 1, 2021
1 parent 3aa242b commit fbad504
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/Introspector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import InvertDecorator from './decorators/InvertDecorator'
import Introspector from './Introspector'
import Selector from './Selector'
import Sequence from './Sequence'
import Random from './Random'

describe('Introspector', () => {
let bTree
Expand Down Expand Up @@ -333,6 +334,33 @@ describe('Introspector', () => {
})
})

describe('with a Random node', () => {
beforeEach(() => {
blackboard = {
start: 0,
run: 0,
end: 0,
result: SUCCESS
}
bTree = new BehaviorTree({ tree: new Random({ nodes: ['simpleTask', 'failingTask'] }), blackboard })
})

it('cleans the results', () => {
for (let i = 10; i--; ) {
bTree.step({ introspector })
}
expect(introspector.lastResult).not.toEqual(null)
expect(introspector.results.length).toEqual(10)

expect(introspector.lastResult).toEqual(
expect.objectContaining({
children: [{ name: expect.any(String), result: expect.any(Boolean) }],
result: expect.any(Boolean)
})
)
})
})

describe('.reset method', () => {
it('cleans the results', () => {
bTree = new BehaviorTree({ tree: 'simpleTask', blackboard: {} })
Expand Down
22 changes: 15 additions & 7 deletions src/Random.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ import BranchNode from './BranchNode'
export default class Random extends BranchNode {
nodeType = 'Random'

run(blackboard = null, { indexes = [], rerun, registryLookUp = (x) => x } = {}) {
this.blueprint.start(blackboard)
run(blackboard = null, { indexes = [], introspector, rerun, registryLookUp = (x) => x } = {}) {
if (!rerun) this.blueprint.start(blackboard)
let currentIndex = indexes.shift() || 0
if (!rerun) {
currentIndex = Math.floor(Math.random() * this.numNodes)
}
const node = registryLookUp(this.blueprint.nodes[currentIndex])
const result = node.run(blackboard, { indexes, rerun, registryLookUp })
const result = node.run(blackboard, { indexes, introspector, rerun, registryLookUp })
let overallResult = result
if (result === RUNNING) {
return [currentIndex, ...indexes]
overallResult = [currentIndex, ...indexes]
} else if (typeof result === 'object') {
// array
return [...indexes, currentIndex, ...result]
overallResult = [...indexes, currentIndex, ...result]
}
this.blueprint.end(blackboard)
return result
const isRunning = overallResult === RUNNING || typeof overallResult === 'object'
if (!isRunning) {
this.blueprint.end(blackboard)
}
if (introspector) {
const debugResult = isRunning ? RUNNING : overallResult
introspector.wrapLast(1, this, debugResult, blackboard)
}
return overallResult
}
}
32 changes: 32 additions & 0 deletions src/Random.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,36 @@ describe('Random', () => {
expect(blackboard.callStack[1]).toEqual('task3')
expect(blackboard.callStack[2]).toEqual('task3')
})

it('does not call start on rerunning running task', () => {
blackboard.start = 0
blackboard.end = 0

const random = new Random({
start: function (blackboard) {
++blackboard.start
},
end: function (blackboard) {
++blackboard.end
},
nodes: [task1, task2, task3]
})

blackboard.result = RUNNING
random.run(blackboard)

expect(blackboard.start).toEqual(1)
expect(blackboard.end).toEqual(0)

random.run(blackboard, { rerun: true })

expect(blackboard.start).toEqual(1)
expect(blackboard.end).toEqual(0)

blackboard.result = FAILURE
random.run(blackboard, { rerun: true })

expect(blackboard.start).toEqual(1)
expect(blackboard.end).toEqual(1)
})
})

0 comments on commit fbad504

Please sign in to comment.