-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #241 from TimKam/239-beliefs-as-functions
Support functional beliefs
- Loading branch information
Showing
8 changed files
with
313 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const FunctionalBelief = require('../../../src/agent/FunctionalBelief') | ||
|
||
const beliefWarning = 'JS-son: Created belief with non-JSON object, non-JSON data type value' | ||
const functionalBeliefWarning = 'JS-son: functional belief without proper two-argument function' | ||
|
||
describe('FunctionalBelief()', () => { | ||
console.warn = jasmine.createSpy('warn') | ||
|
||
it('should create a new functional belief with the specified key, value, rule, and order', () => { | ||
const functionBelief = FunctionalBelief('isSlippery', false, (_, newBeliefs) => newBeliefs.isRaining.value, 0) | ||
expect(functionBelief.isSlippery).toEqual(false) | ||
expect(functionBelief.value).toEqual(false) | ||
expect(functionBelief.rule.toString()).toEqual(((_, newBeliefs) => newBeliefs.isRaining.value).toString()) | ||
expect(functionBelief.order).toEqual(0) | ||
}) | ||
|
||
it('should create a new functional belief with the specified key, value, rule, order, priority, and priority update spec', () => { | ||
const functionBelief = FunctionalBelief('isSlippery', false, (_, newBeliefs) => newBeliefs.isRaining.value, 0, 2) | ||
expect(functionBelief.isSlippery).toEqual(false) | ||
expect(functionBelief.value).toEqual(false) | ||
expect(functionBelief.rule.toString()).toEqual(((_, newBeliefs) => newBeliefs.isRaining.value).toString()) | ||
expect(functionBelief.order).toEqual(0) | ||
expect(functionBelief.priority).toEqual(2) | ||
}) | ||
|
||
it('should throw warning if base belief is not JSON.stringify-able & not of a JSON data type', () => { | ||
console.warn.calls.reset() | ||
// eslint-disable-next-line no-unused-vars | ||
const functionBelief = FunctionalBelief('function', () => {}, (_, newBeliefs) => newBeliefs.isRaining, 0, 2) | ||
expect(console.warn).toHaveBeenCalledWith(beliefWarning) | ||
}) | ||
|
||
it('should throw warning if rule is not a function', () => { | ||
console.warn.calls.reset() | ||
// eslint-disable-next-line no-unused-vars | ||
const functionBelief = FunctionalBelief('isSlippery', false, true, 0, 2) | ||
expect(console.warn).toHaveBeenCalledWith(functionalBeliefWarning) | ||
}) | ||
|
||
it('should throw warning if rule is a function that does not take exactly two arguments', () => { | ||
console.warn.calls.reset() | ||
// eslint-disable-next-line no-unused-vars | ||
const functionBelief = FunctionalBelief('isSlippery', false, newBeliefs => newBeliefs.isRaining, 0) | ||
expect(console.warn).toHaveBeenCalledWith(functionalBeliefWarning) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const Belief = require('./Belief') | ||
|
||
const warning = 'JS-son: functional belief without proper two-argument function' | ||
|
||
/** | ||
* JS-son agent belief generator | ||
* @param {string} id the belief's unique identifier | ||
* @param {any} value the belief's default value | ||
* @param {function} rule the function that is used to infer the belief, given the agent's current beliefs and the belief update | ||
* @param {number} order the belief's order when belief functions are evaluated | ||
* @param {number} priority the belief's priority in case of belief revision; optional | ||
* @param {boolean} updatePriority whether in case of a belief update, the priority of the defeating belief should be adopted; optional, defaults to true | ||
* @returns {object} JS-son agent functional belief | ||
*/ | ||
const FunctionalBelief = (id, value, rule, order, priority, updatePriority=false) => { | ||
if (typeof rule !== 'function' || rule.length !== 2) console.warn(warning) | ||
const baseBelief = Belief(id, value, priority, updatePriority=false) | ||
return { ...baseBelief, rule, order, value } | ||
} | ||
|
||
module.exports = FunctionalBelief |
Oops, something went wrong.