Skip to content

Commit

Permalink
Merge pull request #55 from TimKam/54-refactoring
Browse files Browse the repository at this point in the history
54 refactoring
  • Loading branch information
TimKam authored Mar 12, 2019
2 parents a2c8fb8 + 4b28906 commit ac1952b
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 46 deletions.
7 changes: 1 addition & 6 deletions examples/node/beliefPlan.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
We import js-son and assign Belief, Plan, Agent, and Environment to sepearate consts for the sake of
convenience:
*/
const JSson = require('js-son-agent')

const Belief = JSson.Belief
const Plan = JSson.Plan
const Agent = JSson.Agent
const Environment = JSson.Environment
const { Belief, Plan, Agent, Environment } = require('js-son-agent')

/*
Basic example of using the basic belief-plan approach with JSson
Expand Down
7 changes: 4 additions & 3 deletions examples/node/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ We import the JS-son dependencies:
const {
Belief,
Desire,
Intentions, // eslint-disable-line no-unused-vars
Plan,
Agent,
Environment
Expand Down Expand Up @@ -196,8 +195,10 @@ const stateFilter = (state, agentKey, agentBeliefs) => {
() => 0.5 - Math.random()
).slice(0, 2)
// add some noise
let noise = Object.keys(state).filter(agentId => state[agentId].keyBelief).length < 50 * Math.random() ? [true] : []
noise = Object.keys(state).filter(agentId => state[agentId].keyBelief).length < 29 * Math.random() ? [false] : noise
let noise = Object.keys(state).filter(
agentId => state[agentId].keyBelief).length < 50 * Math.random() ? [true] : []
noise = Object.keys(state).filter(agentId => state[agentId].keyBelief).length < 29 * Math.random()
? [false] : noise
// combine announcements
const pastReceivedAnnouncements =
recentVolatileAnnouncements.concat(
Expand Down
9 changes: 2 additions & 7 deletions examples/web/src/js/GameOfLife.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// import DOM helper for render function
import $$ from 'dom7'
// import js-son
const JSson = require('js-son-agent')
// assign Belief, Plan, Agent, and Environment to separate consts
const Belief = JSson.Belief
const Plan = JSson.Plan
const Agent = JSson.Agent
const Environment = JSson.Environment
// import js-son and assign Belief, Plan, Agent, and Environment to separate consts
import { Belief, Plan, Agent, Environment } from 'js-son-agent'

/*
Note: beliefs will be created dynamically at a later stage
Expand Down
12 changes: 8 additions & 4 deletions spec/src/agent/Belief.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ describe('belief()', () => {
...Belief('null', null),
...Belief('array', [])
}
expect(console.warn).not.toHaveBeenCalledWith('JS-son: Created belief with non-JSON object, non-JSON data type value')
expect(console.warn).not.toHaveBeenCalledWith(
'JS-son: Created belief with non-JSON object, non-JSON data type value'
)
})

it('should not throw warning if belief is JSON.stringify-able', () => {
console.warn.calls.reset()
// eslint-disable-next-line no-unused-vars
const belief = Belief('object', {})
expect(console.warn).not.toHaveBeenCalledWith('JS-son: Created belief with non-JSON object, non-JSON data type value')
expect(console.warn).not.toHaveBeenCalledWith(
'JS-son: Created belief with non-JSON object, non-JSON data type value')
})

it('should throw a warning if belief is not JSON.stringify-able and not of a JSON data type', () => {
it('should throw warning if belief isn\'t JSON.stringify-able & not of a JSON data type', () => {
console.warn.calls.reset()
// eslint-disable-next-line no-unused-vars
const belief = Belief('function', () => {})
expect(console.warn).toHaveBeenCalledWith('JS-son: Created belief with non-JSON object, non-JSON data type value')
expect(console.warn).toHaveBeenCalledWith(
'JS-son: Created belief with non-JSON object, non-JSON data type value')
})
})
6 changes: 3 additions & 3 deletions spec/src/environment/Environment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ describe('Environment / run()', () => {
dogHungry: true
}

const update = actions => {
return actions.some(action => action.actions.includes('Here, take some food!')) ? { dogHungry: false } : {}
}
const update = actions => (actions.some(
action => action.actions.includes('Here, take some food!')) ? { dogHungry: false } : {}
)

it('Should process agent actions', () => {
const environment = new Environment([human], state, update)
Expand Down
7 changes: 0 additions & 7 deletions spec/src/js-son.spec.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/agent/Intentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* JS-son agent intentions generator
* @param {object} beliefs the agent's current beliefs
* @param {object} desires the agent's desires (from which the intentions are filtered)
* @param {function} preferenceFunctionGen
* @param {function} preferenceFunctionGenerator
* @returns {array} JS-son agent intentions
*/
const Intentions = (beliefs, desires, preferenceFunctionGen) => {
const Intentions = (beliefs, desires, preferenceFunctionGenerator) => {
const intentions = {}
const intentionKeys = Object.keys(desires).filter(preferenceFunctionGen(beliefs, desires))
const intentionKeys = Object.keys(desires).filter(preferenceFunctionGenerator(beliefs, desires))
intentionKeys.forEach(intentionKey => (intentions[intentionKey] = desires[intentionKey](beliefs)))
return intentions
}
Expand Down
19 changes: 6 additions & 13 deletions src/js-son.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
const Belief = require('./agent/Belief')
const Desire = require('./agent/Desire')
const Intentions = require('./agent/Intentions')
const Plan = require('./agent/Plan')
const Agent = require('./agent/Agent')
const Environment = require('./environment/Environment')

const JSson = {
Belief,
Desire,
Intentions,
Plan,
Agent,
Environment
Belief: require('./agent/Belief'),
Desire: require('./agent/Desire'),
Intentions: require('./agent/Intentions'),
Plan: require('./agent/Plan'),
Agent: require('./agent/Agent'),
Environment: require('./environment/Environment')
}

module.exports = JSson

0 comments on commit ac1952b

Please sign in to comment.