Skip to content

Commit

Permalink
Merge 7f70144 into 864eac4
Browse files Browse the repository at this point in the history
  • Loading branch information
olekscode committed Mar 3, 2024
2 parents 864eac4 + 7f70144 commit 308ba90
Show file tree
Hide file tree
Showing 13 changed files with 1,193 additions and 74 deletions.
6 changes: 6 additions & 0 deletions repository/Cormas-Core/CMAbstractModel.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,12 @@ Examples: self averageOfAttribute: #age of: Person
collect: [:e | e perform: att])
]

{ #category : #accessing }
CMAbstractModel >> cellAt: aRowIndex at: aColumnIndex [

^ self spaceModel cellAt: aRowIndex at: aColumnIndex
]

{ #category : #'accessing - entities' }
CMAbstractModel >> centralCell [
"Purpose: returns the cell located at the center of the spatial grid. Number of lines and number of columns of the spatial grid have to be odd"
Expand Down
2 changes: 1 addition & 1 deletion repository/Cormas-Core/CMSpace4Neighbourhood.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ CMSpace4Neighbourhood >> initializeOrderedNeighbourhood: k [
v at: 3 put: (self elementaryEntitiesAt: i) ].
(i := self eastCell: k) > 0 ifTrue: [
v at: 4 put: (self elementaryEntitiesAt: i) ].
^ (self elementaryEntitiesAt: k) neighbourhood: v
^ (self elementaryEntitiesAt: k) neighbourhoodWithNils: v
]
2 changes: 1 addition & 1 deletion repository/Cormas-Core/CMSpace6Neighbourhood.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ CMSpace6Neighbourhood >> initializeOrderedNeighbourhood: k [
v at: 5 put: (self elementaryEntitiesAt: i) ].
(i := self eastCellHexa: (self northCell: k)) > 0 ifTrue: [
v at: 6 put: (self elementaryEntitiesAt: i) ].
^ (self elementaryEntitiesAt: k) neighbourhood: v
^ (self elementaryEntitiesAt: k) neighbourhoodWithNils: v
]

{ #category : #initialization }
Expand Down
2 changes: 1 addition & 1 deletion repository/Cormas-Core/CMSpace8Neighbourhood.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ CMSpace8Neighbourhood >> initializeOrderedNeighbourhood: k [
v at: 7 put: (self elementaryEntitiesAt: i) ].
(i := self northCell: (self eastCell: k)) > 0 ifTrue: [
v at: 8 put: (self elementaryEntitiesAt: i) ].
^ (self elementaryEntitiesAt: k) neighbourhood: v
^ (self elementaryEntitiesAt: k) neighbourhoodWithNils: v
]

{ #category : #initialization }
Expand Down
12 changes: 6 additions & 6 deletions repository/Cormas-Core/CMSpaceModel.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ CMSpaceModel >> aggregate: listPolygons [
perform:
(self cormasModel getterForEntityClass: listPolygons first class).
allPolygons removeAll: theOthers. "Reconstruction of the neighbourhood of the new polygon"
fusion neighbourhood: nil.
fusion neighbourhoodWithNils: nil.
self voisinsPolygonesNoeuds: (Array with: fusion). "Reconstruction of the neighbourhood of the neighbours of the new polygon"
fusion neighbourhood do: [ :p | p neighbourhood: nil ].
fusion neighbourhood do: [ :p | p neighbourhoodWithNils: nil ].
self
voisinsPolygonesNoeuds: fusion neighbourhood asOrderedCollection. "Updating the components of the new spatial entity, in case it is composed of"
(fusion class inheritsFrom: CMSpatialEntityElement)
Expand All @@ -143,9 +143,9 @@ CMSpaceModel >> aggregate: listPolygons new: entityC [
ifFalse: [ newEntityC := entityC new init. "Calcul des coordonnees d'origine"
newEntityC outline: (self outlineAggregatedFrom: listPolygons). "Updating the coordinates of the polygon image in the drawing window"
self setImagesPolygons: (Array with: newEntityC). "Construction of the neighbourhood of the new polygon"
newEntityC neighbourhood: nil.
newEntityC neighbourhoodWithNils: nil.
self voisinsPolygonesNoeuds: (Array with: newEntityC). "Reconstruction of the neighbourhood of the neighbours of the new polygon"
newEntityC neighbourhood do: [ :p | p neighbourhood: nil ].
newEntityC neighbourhood do: [ :p | p neighbourhoodWithNils: nil ].
self
voisinsPolygonesNoeuds: newEntityC neighbourhood asOrderedCollection. "Updating the components of the new spatial entity"
newEntityC addComponents: listPolygons.
Expand Down Expand Up @@ -1716,7 +1716,7 @@ CMSpaceModel >> loadNeighboorsClass: class separator: aChar [
ligne close.
id := dataVector first.
anEntity := self getInstanceOfClass: class fromId: id.
anEntity neighbourhood: OrderedCollection new.
anEntity neighbourhoodWithNils: OrderedCollection new.
dataVector remove: id.
dataVector
do: [ :anID |
Expand Down Expand Up @@ -2771,7 +2771,7 @@ CMSpaceModel >> voisinsPolygonesNoeuds: lesPolygones [
lesPolygones
do: [ :b |
b neighbourhood isNil
ifTrue: [ b neighbourhood: Set new ].
ifTrue: [ b neighbourhoodWithNils: Set new ].
b nodes: Dictionary new.
boundingRectangle := Rectangle
origin: b bounds origin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ CMSpacePolygonNeighbourhood >> initializePolygone: b in: allPolygones [
| boundingRectangle p pPrec pCour |

b neighbourhood isNil
ifTrue: [ b neighbourhood: Set new ].
ifTrue: [ b neighbourhoodWithNils: Set new ].
b nodes: Dictionary new.
boundingRectangle := self boundingRectangleFor: b.

Expand Down
24 changes: 14 additions & 10 deletions repository/Cormas-Core/CMSpatialEntity.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ Class {
#superclass : #CMEntity,
#instVars : [
'spaceModel',
'neighbourhood',
'destroyed',
'outline',
'edge',
'theOccupants',
'theAggregates'
'theAggregates',
'neighbourhoodWithNils'
],
#classInstVars : [
'bounds'
Expand Down Expand Up @@ -335,7 +335,9 @@ CMSpatialEntity >> initialize [
" Private - Set the receiver's internal state. Initally a spatial entity is not dead "

super initialize.

destroyed := false.
neighbourhoodWithNils := OrderedCollection new: 8.
]

{ #category : #printing }
Expand Down Expand Up @@ -672,14 +674,9 @@ Return value: <ByteString>"

{ #category : #'environment - sensing - space' }
CMSpatialEntity >> neighbourhood [
"If the cell is located at the edge or in the corner of a closed (non-torroidal) envionment, the missing neighbours are represented as nils. Here we remove them to get only the list of neighbours"

^ neighbourhood ifNil: [ neighbourhood := OrderedCollection new: 8]
]

{ #category : #'environment - sensing - space' }
CMSpatialEntity >> neighbourhood: aCollection [

neighbourhood := aCollection
^ neighbourhoodWithNils reject: [ :each | each isNil ]
]

{ #category : #'environment - sensing - space' }
Expand All @@ -692,7 +689,14 @@ Return value: <Collection(SpatialEntity)>"
| collec |
collec := self neighbourhood asOrderedCollection.
collec addFirst: self.
^collec
^collec asArray
]

{ #category : #'environment - sensing - space' }
CMSpatialEntity >> neighbourhoodWithNils: aCollection [
"If the cell is located at the edge or in the corner of a closed (non-torroidal) envionment, the missing neighbours are represented as nils"

neighbourhoodWithNils := aCollection
]

{ #category : #deprecated }
Expand Down
4 changes: 2 additions & 2 deletions repository/Cormas-Core/CMSpatialEntityAggregate.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ CMSpatialEntityAggregate >> initNeighbourhood [

self edge: (self components contains: [:c | c edge]).
self setSurround.
neighbourhood := (surround
neighbourhoodWithNils := (surround
collect:
[:a |
a theAggregates
at: self class name
ifAbsent: [nil]]) asSet. "To get rid of duplicates, a Set is used..."
neighbourhood := neighbourhood asOrderedCollection "...and then made as an OrderedCollection once the job is done..."
neighbourhoodWithNils := neighbourhoodWithNils asOrderedCollection "...and then made as an OrderedCollection once the job is done..."
]

{ #category : #displaying }
Expand Down
121 changes: 77 additions & 44 deletions repository/Cormas-Core/CMSpatialEntityElement.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ Return value: <Number>"
{ #category : #'initialize-release' }
CMSpatialEntityElement >> initNeighbourhood [

self neighbourhood ifEmpty: [ ^ nil ].
self neighbourhood: (self neighbourhood select: [ : c | c notNil ])
"do nothing"
]

{ #category : #testing }
Expand Down Expand Up @@ -686,14 +685,16 @@ CMSpatialEntityElement >> neighbourE [
"Purpose: returns the neighbour to the East
Not nil only for square and hexagonal cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 4
ifTrue: [^self neighbourhood at: 4].
self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 7].
self spaceModel nbNeighbours = 6
ifTrue: [^self neighbourhood at: 5].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [ index := 4 ].
self spaceModel nbNeighbours = 8 ifTrue: [ index := 7 ].
self spaceModel nbNeighbours = 6 ifTrue: [ index := 5 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the East' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
Expand All @@ -702,89 +703,121 @@ CMSpatialEntityElement >> neighbourN [
Not nil only for square cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

self spaceModel nbNeighbours = 4 | (self spaceModel nbNeighbours = 8)
ifTrue: [ ^ self neighbourhood at: 1 ].
^ nil
| index neighbour |

index := 1.
neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the North' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
CMSpatialEntityElement >> neighbourNE [
"Purpose: returns the neighbour to the NorthEast
Not nil only for square (8 neighbors) and hexagonal cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 8].
self spaceModel nbNeighbours = 6
ifTrue: [^self neighbourhood at: 6].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [
self error: 'The number of neighbours was set to 4. There is no neighbour to the North-East' ].

self spaceModel nbNeighbours = 8 ifTrue: [ index := 8 ].
self spaceModel nbNeighbours = 6 ifTrue: [ index := 6 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the North-East' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
CMSpatialEntityElement >> neighbourNW [
"Purpose: returns the neighbour to the NorthWest
Not nil only for square (8 neighbors) and hexagonal cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 2].
self spaceModel nbNeighbours = 6
ifTrue: [^self neighbourhood at: 1].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [
self error: 'The number of neighbours was set to 4. There is no neighbour to the North-West' ].

self spaceModel nbNeighbours = 8 ifTrue: [ index := 2 ].
self spaceModel nbNeighbours = 6 ifTrue: [ index := 1 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the North-West' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
CMSpatialEntityElement >> neighbourS [
"Purpose: returns the neighbour to the South
Not nil only fore square cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 4
ifTrue: [^self neighbourhood at: 3].
self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 5].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [ index := 3 ].
self spaceModel nbNeighbours = 8 ifTrue: [ index := 5 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the South' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
CMSpatialEntityElement >> neighbourSE [
"Purpose: returns the neighbour to the SouthEast
Not nil only for square (8 neighbors) and hexagonal cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 6].
self spaceModel nbNeighbours = 6
ifTrue: [^self neighbourhood at: 4].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [
self error: 'The number of neighbours was set to 4. There is no neighbour to the South-East' ].

self spaceModel nbNeighbours = 8 ifTrue: [ index := 6 ].
self spaceModel nbNeighbours = 6 ifTrue: [ index := 4 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the South-East' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
CMSpatialEntityElement >> neighbourSW [
"Purpose: returns the neighbour to the SouthWest
Not nil only for square (8 neighbors) and hexagonal cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 4].
self spaceModel nbNeighbours = 6
ifTrue: [^self neighbourhood at: 3].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [
self error: 'The number of neighbours was set to 4. There is no neighbour to the South-West' ].

self spaceModel nbNeighbours = 8 ifTrue: [ index := 4 ].
self spaceModel nbNeighbours = 6 ifTrue: [ index := 3 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the South-West' ].
^ neighbour
]

{ #category : #'environment - sensing - space' }
CMSpatialEntityElement >> neighbourW [
"Purpose: returns the neighbour to the West
Not nil only for square and hexagonal cells (regular spatial grid).
Return value: <SpatialEntityElement | nil>"

| index neighbour |

self spaceModel nbNeighbours = 4
ifTrue: [^self neighbourhood at: 2].
self spaceModel nbNeighbours = 8
ifTrue: [^self neighbourhood at: 3].
self spaceModel nbNeighbours = 6
ifTrue: [^self neighbourhood at: 2].
^nil
self spaceModel nbNeighbours = 4 ifTrue: [ index := 2 ].
self spaceModel nbNeighbours = 8 ifTrue: [ index := 3 ].
self spaceModel nbNeighbours = 6 ifTrue: [ index := 2 ].

neighbour := neighbourhoodWithNils at: index.
neighbour ifNil: [ self error: 'This cell does not have a neighbour to the West' ].
^ neighbour
]

{ #category : #'displaying - obsolete' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ CMSpatialEntityElement >> orderedNeighbourhood: aCollection [
orderedNeighbourhood: is now deprecated and will be removed in the future releases"

self deprecated: 'Use #neighbourhood: instead'.
^ self neighbourhood: aCollection
^ self neighbourhoodWithNils: aCollection
]
14 changes: 14 additions & 0 deletions repository/Cormas-Tests/CMAbstractModelTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ CMAbstractModelTest >> setUp [
model := self newTestModel.
]

{ #category : #tests }
CMAbstractModelTest >> testCellAtAt [

| expectedCell expectedId actualCell |

expectedCell := model spaceModel cellAt: 3 at: 2.
expectedId := 10.

actualCell := model cellAt: 3 at: 2.

self assert: actualCell equals: expectedCell.
self assert: actualCell id equals: expectedId.
]

{ #category : #'tests-accessing-entities' }
CMAbstractModelTest >> testCentralCell [
| m |
Expand Down
Loading

0 comments on commit 308ba90

Please sign in to comment.