Skip to content

Commit

Permalink
Added two extension methods for CMAbstractModel
Browse files Browse the repository at this point in the history
  • Loading branch information
olekscode committed Jul 11, 2023
1 parent 21c8ff9 commit a64f624
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/Cormas-OpenMole-Tests/CMOpenMoleBridgeTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,49 @@ CMOpenMoleBridgeTest >> testLastDataOfModel [
data := CMOpenMoleBridge lastDataOfModel: model.

expectedData := {
#numberOfAliveAgents -> model class defaultNumberOfAgents .
#numberOfAliveAgents -> model class defaultInitialNumberOfAgents .
#numberOfDeadAgents -> 0 } asDictionary.

self assert: data equals: expectedData.
]

{ #category : #tests }
CMOpenMoleBridgeTest >> testLoadInputParametersFromJson [

| contents file |

contents := '{
"numberOfRows": 10,
"numberOfColumns": 13,
"initialNumberOfAgents": 2
}'.

file := FileSystem memory / 'input.json'.
file writeStreamDo: [ :stream | stream nextPutAll: contents ].

model loadInputParametersFromJson: file.

self assert: model numberOfRows equals: 10.
self assert: model numberOfColumns equals: 13.
self assert: model initialNumberOfAgents equals: 2.
]

{ #category : #tests }
CMOpenMoleBridgeTest >> testSaveProbesToJson [

| expectedDictionary actualDictionary file |

expectedDictionary := {
'numberOfAliveAgents' -> 5 .
'numberOfDeadAgents' -> 0
} asDictionary.

file := FileSystem memory / 'output.json'.

model initializeSimulation.
model saveProbesToJson: file.

actualDictionary := STONJSON fromString: file contents.

self assert: actualDictionary equals: expectedDictionary.
]
33 changes: 33 additions & 0 deletions src/Cormas-OpenMole/CMAbstractModel.extension.st
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
Extension { #name : #CMAbstractModel }

{ #category : #'*Cormas-OpenMole' }
CMAbstractModel >> loadInputParametersFromJson: aFileReference [

| jsonString parameters selector |

jsonString := aFileReference
readStreamDo: [ :stream | stream contents ].

parameters := STONJSON fromString: jsonString.

parameters keysAndValuesDo: [ :parameterName :value |
selector := (parameterName, ':') asSymbol.
self perform: selector with: value ].


]

{ #category : #'*Cormas-OpenMole' }
CMAbstractModel >> runOpenMoleSimulation [

"TODO"
]

{ #category : #'*Cormas-OpenMole' }
CMAbstractModel >> saveProbesToJson: aFileReference [

| probesDictionary jsonString |

probesDictionary := (self class probes collect: [ :selector |
selector asString -> (self perform: selector)
]) asDictionary.

jsonString := STONJSON toString: probesDictionary.

aFileReference writeStreamDo: [ :stream |
stream nextPutAll: jsonString ].

]

0 comments on commit a64f624

Please sign in to comment.