Skip to content

Commit

Permalink
Merge pull request cormas#745 from olekscode/744-moveTowardsconstrain…
Browse files Browse the repository at this point in the history
…tOnPath-is-throwing-SubscriptOutOfBounds-exception

744 move towardsconstraint on path is throwing subscript out of bounds exception
  • Loading branch information
olekscode committed Feb 11, 2024
2 parents e1847a9 + f5f67d1 commit e02cb01
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 20 deletions.
9 changes: 8 additions & 1 deletion repository/Cormas-Core/TCMLocated.trait.st
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,17 @@ TCMLocated >> moveTowards: aCell constraintOnPath: aBlock [
"Purpose: moves one cell in direction of a given location, taking a specified constraint on each cell of the path to destination.
Argument: aCell = <SpatialEntityElement>
Example: self moveTowards: self house patch constraintOnPath: [:c | c isClosed not]"

| path |

self isSituated ifTrue: [
self patch = aCell ifFalse: [
self moveTo: (self patch wayTo: aCell constraint: aBlock) first ] ]
path := self patch wayTo: aCell constraint: aBlock.

path size < 2
ifTrue: [ self error: 'Could not find a valid path' ].

self moveTo: path second ] ]
]

{ #category : #'star moving' }
Expand Down
3 changes: 2 additions & 1 deletion repository/Cormas-Mocks/CMMockAnimal.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Class {
CMMockAnimal >> initialize [

super initialize.
energy := Cormas randomFloatFrom: 0 to: 1
energy := 0.5
]

{ #category : #pov }
Expand All @@ -25,4 +25,5 @@ CMMockAnimal >> sizeOfPOV [
{ #category : #control }
CMMockAnimal >> step [

self randomWalk
]
14 changes: 13 additions & 1 deletion repository/Cormas-Mocks/CMMockCell.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ CMMockCell >> initRandom [
ifFalse: [ #dead ])
]

{ #category : #testing }
CMMockCell >> isAliveCell [

^ state = #alive
]

{ #category : #testing }
CMMockCell >> isDeadCell [

^ state = #dead
]

{ #category : #control }
CMMockCell >> newState [
"Purpose: the specific transition function of the automaton should be written here
Example: self bufferState: (test ifTrue: [value1] ifFalse: [value2])"

self state = #dead
self isDeadCell
ifTrue: [ ^ self bufferState: #alive ].
^ self bufferState: #dead
]
Expand Down
89 changes: 88 additions & 1 deletion repository/Cormas-Mocks/CMMockModel.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ CMMockModel class >> friendlyName [
^ 'MockModel'
]

{ #category : #examples }
CMMockModel class >> showEmpty [
<example>
self showModelWithInit: #initEmpty
]

{ #category : #examples }
CMMockModel class >> showFixed [
<example>
self showModelWithInit: #initFixed
]

{ #category : #examples }
CMMockModel class >> showInit [
<example>
self showModelWithInit: #init
]

{ #category : #examples }
CMMockModel class >> showModelWithInit: aSymbol [

| model builder |

model := self new
activeInit: aSymbol;
initializeSimulation;
yourself.

builder := CMR3SpaceDiagramBuilder new
cormasModel: model;
asPresenter.

builder openWithSpec.
]

{ #category : #'as yet unclassified' }
CMMockModel >> cellMatrix [
| matrix |

matrix := #(
(0 0 1 1)
(1 0 1 1)
(1 0 1 1)
(1 1 1 0)).

(matrix size = numberOfRows and: [ matrix allSatisfy: [ :row | row size = numberOfColumns ] ])
ifFalse: [ self error: 'Matrix has the wrong dimensions' ].

^ matrix
]

{ #category : #init }
CMMockModel >> init [

Expand All @@ -82,6 +133,7 @@ CMMockModel >> initAlive [

{ #category : #init }
CMMockModel >> initDead [

self theCells do: #initDead
]

Expand All @@ -92,11 +144,26 @@ CMMockModel >> initEmpty [
createGridLines: numberOfRows
columns: numberOfColumns
neighbourhood: 8
closed: false.
closed: true.

self initRandom
]

{ #category : #init }
CMMockModel >> initFixed [

| cow goat |

self initEmpty.
self initializeCellsFromMatrix.

cow := self newEntity: CMMockCow.
goat := self newEntity: CMMockGoat.

cow moveTo: (self pickCellAt: 1 @ 2).
goat moveTo: (self pickCellAt: 3 @ 3).
]

{ #category : #init }
CMMockModel >> initRandom [

Expand Down Expand Up @@ -143,6 +210,20 @@ CMMockModel >> initialize [
theCells := OrderedCollection new.
]

{ #category : #initialization }
CMMockModel >> initializeCellsFromMatrix [

| matrixCellValue modelCell |

1 to: numberOfRows do: [ :i |
1 to: numberOfColumns do: [ :j |
matrixCellValue := (self cellMatrix at: i) at: j.
modelCell := self pickCellAt: j @ i.

matrixCellValue = 0 ifTrue: [ modelCell initDead ].
matrixCellValue = 1 ifTrue: [ modelCell initAlive ] ] ]
]

{ #category : #probes }
CMMockModel >> numberOfAliveAgents [
"return the population size of the alive"
Expand Down Expand Up @@ -189,6 +270,12 @@ CMMockModel >> randomSeed: aNumber [
Cormas seed: aNumber
]

{ #category : #control }
CMMockModel >> step: t [

self askRandom: CMMockAnimal toDo: #step.
]

{ #category : #accessing }
CMMockModel >> theCells [

Expand Down
4 changes: 2 additions & 2 deletions repository/Cormas-Tests/CMProjectManagerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ CMProjectManagerTest >> testIsValidProjectName [
CMProjectManagerTest >> testModelControlInitMethods [

self assert: (self projectManager modelControlInitMethods isKindOf: Array).
self assert: self projectManager modelControlInitMethods isEmpty.
self assert: self projectManager modelControlInitMethods size equals: 1.
]

{ #category : #test }
CMProjectManagerTest >> testModelInitMethods [

self assert: (self projectManager modelInitMethods isKindOf: Array).
self assert: self projectManager modelInitMethods size equals: 5.
self assert: self projectManager modelInitMethods size equals: 6.
]

{ #category : #test }
Expand Down
51 changes: 51 additions & 0 deletions repository/Cormas-Tests/CMSpatialEntityElementTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Class {
#name : #CMSpatialEntityElementTest,
#superclass : #TestCase,
#instVars : [
'model'
],
#category : #'Cormas-Tests-Model'
}

{ #category : #initialization }
CMSpatialEntityElementTest >> setUp [

super setUp.

model := CMMockModel new
activeInit: #initFixed;
initializeSimulation;
yourself.
]

{ #category : #tests }
CMSpatialEntityElementTest >> testMoveTowardsConstraintOnPath [

| agent destination expectedPathOfIds |

agent := model theCows anyOne.
destination := model pickCellAt: 3 @ 2.

expectedPathOfIds := #(5 9 14 11 7) asOrderedCollection.

expectedPathOfIds do: [ :expectedCellId |
self assert: agent patch id equals: expectedCellId.
agent moveTowards: destination constraintOnPath: [ :cell | cell isAliveCell ] ].

self assert: agent patch id equals: destination id.
]

{ #category : #tests }
CMSpatialEntityElementTest >> testWayToConstrained [

| origin destination path pathOfIds expectedPathOfIds |

origin := model pickCellAt: 1 @ 2.
destination := model pickCellAt: 3 @ 2.

path := origin wayTo: destination constraint: [ :cell | cell isAliveCell ].
pathOfIds := path collect: [ :cell | cell id ].

expectedPathOfIds := #(5 9 14 11 7) asOrderedCollection.
self assert: pathOfIds equals: expectedPathOfIds.
]
26 changes: 12 additions & 14 deletions repository/Cormas-UI-Tests/CMR3SpaceTestFixture.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CMR3SpaceTestFixture >> builder [
{ #category : #states }
CMR3SpaceTestFixture >> initialCellColors [

"Shape colors of a grid for the cells of the initial mock model with a fixed random seed"
"Shape colors of a grid for the cells of the initial mock model"

^ self initialCellStates collect: [ :each |
each = #alive
Expand All @@ -29,13 +29,12 @@ CMR3SpaceTestFixture >> initialCellColors [
{ #category : #states }
CMR3SpaceTestFixture >> initialCellStates [

"Initial cell states of a mock model with a fixed random seed"

^ OrderedCollection withAll: #(
alive alive alive dead
dead dead alive dead
alive dead dead alive
dead dead dead dead).
"Initial cell states of a mock model"

^ model cellMatrix flattened asOrderedCollection collect: [ :each |
each = 1
ifTrue: [ #alive ]
ifFalse: [ #dead ] ].
]

{ #category : #initialization }
Expand All @@ -44,7 +43,7 @@ CMR3SpaceTestFixture >> initialize [
super initialize.

model := CMMockModel new
randomSeed: 42;
activeInit: #initFixed;
initializeSimulation;
yourself.

Expand Down Expand Up @@ -83,9 +82,8 @@ CMR3SpaceTestFixture >> updatedCellStates [

"Cell states of a mock model with a fixed random seed after one simulation step"

^ OrderedCollection withAll: #(
dead dead dead alive
alive alive dead alive
dead alive alive dead
alive alive alive alive).
^ self initialCellStates collect: [ :each |
each = #alive
ifTrue: [ #dead ]
ifFalse: [ #alive ] ]
]

0 comments on commit e02cb01

Please sign in to comment.