Skip to content
Snippets Groups Projects
Commit 6ff8845b authored by Evan Eames's avatar Evan Eames Committed by Ugo Albanese
Browse files

Merged in NRRPLT-7271 (pull request #130)

[NRRPLT-7271] Add brain mode info to ./brain endpoint response

Approved-by: Ugo Albanese
parent 497b54a7
No related branches found
No related tags found
No related merge requests found
...@@ -215,44 +215,36 @@ abstract class BaseExperimentService { ...@@ -215,44 +215,36 @@ abstract class BaseExperimentService {
} }
async getBrain() { async getBrain() {
const bibi = (await this.getBibi())[0].bibi;
if (!bibi.brainModel) return null;
let brainModelFile = bibi.brainModel.file.toString();
if (bibi.brainModel._model) {
brainModelFile = path.join(bibi.brainModel._model, brainModelFile);
}
const brain = (await this.getFile(
brainModelFile.toString(),
FILE_TYPE.BRAIN
)).toString();
if (!bibi.brainModel.populations) bibi.brainModel.populations = []; const response: any = {};
else if (!Array.isArray(bibi.brainModel.populations))
bibi.brainModel.populations = [bibi.brainModel.populations];
const robots: string[] = []; const bibi = (await this.getBibi())[0].bibi;
if (!bibi.bodyModel) bibi.bodyModel = [];
else if (!Array.isArray(bibi.bodyModel)) bibi.bodyModel = [bibi.bodyModel];
if (bibi.bodyModel.length && !bibi.bodyModel[0]._robotId) { let brainMode: string;
robots.push('robot'); // legacy config if (!bibi.mode) {
} else if (bibi.bodyModel.length) { brainMode = 'SynchronousPynnNestSimulation';
bibi.bodyModel.forEach(model => { } else {
if (!model._robotId) { brainMode = bibi.mode.__text;
console.error(
'Multiple bodyModels has been defined with same or no names.' +
'Please check bibi config file.'
);
}
robots.push(model._robotId);
});
} }
response.mode = brainMode;
return Promise.resolve({ if (bibi.brainModel) {
brain, let brainModelFile = bibi.brainModel.file.toString();
brainType: path.extname(brainModelFile).substr(1), if (bibi.brainModel._model) {
robots, brainModelFile = path.join(bibi.brainModel._model, brainModelFile);
populations: _.reduce( }
const brain = (await this.getFile(
brainModelFile.toString(),
FILE_TYPE.BRAIN
)).toString();
if (!bibi.brainModel.populations) bibi.brainModel.populations = [];
else if (!Array.isArray(bibi.brainModel.populations))
bibi.brainModel.populations = [bibi.brainModel.populations];
response.brain = brain;
response.brainType = path.extname(brainModelFile).substr(1);
response.populations = _.reduce(
bibi.brainModel.populations, bibi.brainModel.populations,
(acc, pop) => { (acc, pop) => {
let popObj; let popObj;
...@@ -272,8 +264,29 @@ abstract class BaseExperimentService { ...@@ -272,8 +264,29 @@ abstract class BaseExperimentService {
return { ...acc, [pop._population]: popObj }; return { ...acc, [pop._population]: popObj };
}, },
{} {}
) );
}); }
const robots: string[] = [];
if (!bibi.bodyModel) bibi.bodyModel = [];
else if (!Array.isArray(bibi.bodyModel)) bibi.bodyModel = [bibi.bodyModel];
if (bibi.bodyModel.length && !bibi.bodyModel[0]._robotId) {
robots.push('robot'); // legacy config
} else if (bibi.bodyModel.length) {
bibi.bodyModel.forEach(model => {
if (!model._robotId) {
console.error(
'Multiple bodyModels has been defined with same or no names.' +
'Please check bibi config file.'
);
}
robots.push(model._robotId);
});
}
response.robots = robots;
return Promise.resolve(response);
} }
async setBrain(brain, populations, removePopulations = false, newBrain?) { async setBrain(brain, populations, removePopulations = false, newBrain?) {
......
...@@ -380,7 +380,7 @@ describe('ExperimentServiceFactory', function() { ...@@ -380,7 +380,7 @@ describe('ExperimentServiceFactory', function() {
return { csvFiles, profilerFiles }; return { csvFiles, profilerFiles };
}); });
it('should return return null if bibi has no brainModel', async () => { it('should only return default mode and robots if bibi has no brainModel', async () => {
const mockedBrainFileContent = 'brain_file_content'; const mockedBrainFileContent = 'brain_file_content';
let mockedBibiJSWithoutBrain = JSON.parse(JSON.stringify(mockedBibiJS)); let mockedBibiJSWithoutBrain = JSON.parse(JSON.stringify(mockedBibiJS));
delete mockedBibiJSWithoutBrain.bibi.brainModel; delete mockedBibiJSWithoutBrain.bibi.brainModel;
...@@ -390,7 +390,10 @@ describe('ExperimentServiceFactory', function() { ...@@ -390,7 +390,10 @@ describe('ExperimentServiceFactory', function() {
.stub(es, 'getBibi') .stub(es, 'getBibi')
.returns(Promise.resolve([mockedBibiJSWithoutBrain])); .returns(Promise.resolve([mockedBibiJSWithoutBrain]));
sinon.stub(es, 'getFile').returns(Promise.resolve(mockedBrainFileContent)); sinon.stub(es, 'getFile').returns(Promise.resolve(mockedBrainFileContent));
return es.getBrain().should.eventually.be.null; return es.getBrain().should.eventually.deep.equal({
mode: 'SynchronousPynnNestSimulation',
robots: ['robot']
});
}); });
it('should return the brain from the bibi', async () => { it('should return the brain from the bibi', async () => {
...@@ -402,6 +405,7 @@ describe('ExperimentServiceFactory', function() { ...@@ -402,6 +405,7 @@ describe('ExperimentServiceFactory', function() {
return es.getBrain().should.eventually.deep.equal({ return es.getBrain().should.eventually.deep.equal({
brain: mockedBrainFileContent, brain: mockedBrainFileContent,
brainType: 'py', brainType: 'py',
mode: 'SynchronousPynnNestSimulation',
populations: { populations: {
sensors: { from: 0, to: 5, step: NaN }, sensors: { from: 0, to: 5, step: NaN },
actors: { from: 5, to: 8, step: NaN } actors: { from: 5, to: 8, step: NaN }
...@@ -423,6 +427,7 @@ describe('ExperimentServiceFactory', function() { ...@@ -423,6 +427,7 @@ describe('ExperimentServiceFactory', function() {
return es.getBrain().should.eventually.deep.equal({ return es.getBrain().should.eventually.deep.equal({
brain: mockedBrainFileContent, brain: mockedBrainFileContent,
brainType: 'py', brainType: 'py',
mode: 'SynchronousPynnNestSimulation',
populations: {}, populations: {},
robots: ['robot'] robots: ['robot']
}); });
...@@ -441,6 +446,7 @@ describe('ExperimentServiceFactory', function() { ...@@ -441,6 +446,7 @@ describe('ExperimentServiceFactory', function() {
return es.getBrain().should.eventually.deep.equal({ return es.getBrain().should.eventually.deep.equal({
brain: mockedBrainFileContent, brain: mockedBrainFileContent,
brainType: 'py', brainType: 'py',
mode: 'SynchronousPynnNestSimulation',
populations: { populations: {
sensors: { from: 0, to: 5, step: NaN }, sensors: { from: 0, to: 5, step: NaN },
actors: { from: 5, to: 8, step: NaN } actors: { from: 5, to: 8, step: NaN }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment