Skip to content

Commit

Permalink
Cleaning management of weak and strong repository as well as Space wi…
Browse files Browse the repository at this point in the history
…th participants
  • Loading branch information
ngaud committed Mar 25, 2020
1 parent 0afefb7 commit a17f520
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 100 deletions.
Expand Up @@ -93,6 +93,16 @@ default boolean isPseudoEmpty() {
@Pure
int getNumberOfStrongParticipants();

/**
* Replies the number of strong participants to the space.
* This function ignores the weak participants.
*
* @return the number of participants.
* @since 0.11
*/
@Pure
int getNumberOfWeakParticipants();

/** Apply the given procedure to each of the strong participants.
* This function ignores the weak participants.
*
Expand All @@ -101,4 +111,12 @@ default boolean isPseudoEmpty() {
*/
void forEachStrongParticipant(Procedure1<? super UUID> callback);

/** Apply the given procedure to each of the weak participants.
* This function ignores the strong participants.
*
* @param callback the lambda to invoke.
* @since 0.11
*/
void forEachWeakParticipant(Procedure1<? super UUID> callback);

}
Expand Up @@ -234,16 +234,16 @@ class Kernel {
val context = iter.next
val ^space = context.defaultSpace as SpaceWithParticipants

for (participant : ^space.internalStrongParticipantStructure.values) {
val listener = participant.participant
^space.forEachStrongParticipant [ id, p |
val listener = p.participant
if (listener instanceof InformedEventListener) {
val ag = listener.ownerInstance
assert ag !== null
if (encounteredAgents += ag.ID) {
agents += ag
}
}
}
]

}

Expand Down
Expand Up @@ -229,6 +229,7 @@ final class AgentLife {
protected def attachAgentToPlatform(spawningContext : Context) : InternalEventBusCapacity {
// Set up the internal bus
val eb = getEventBus
assert(eb !== null)
//
// Register the agent on its parent default space.
var defaultSpace = spawningContext.defaultSpace
Expand Down
@@ -1,17 +1,17 @@
/*
/*
* $Id$
*
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
*
* Copyright (C) 2014-2020 the original authors or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -83,13 +83,9 @@ abstract class AbstractNamespaceFinder<N extends SarlName, O> implements INamesp
}
}
if (^space instanceof SpaceWithParticipants) {
var participantMap = ^space.internalStrongParticipantStructure
var participant = participantMap.get(agentId)
if (participant !== null) {
var listener = participant.participant
if (listener instanceof InformedEventListener) {
return listener.ownerInstance
}
var listener = ^space.getListenerFromStrongParticipant(agentId)
if ((listener !== null) && (listener instanceof InformedEventListener)) {
return (listener as InformedEventListener).ownerInstance
}
}
}
Expand Down
Expand Up @@ -30,6 +30,7 @@ import io.sarl.lang.core.SpaceID
import io.sarl.sre.services.logging.LoggingService
import java.text.MessageFormat
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.logging.Level
import javax.inject.Inject
Expand Down Expand Up @@ -67,6 +68,10 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
@Accessors(PUBLIC_GETTER)
var spaceParticipantListener : SpaceParticipantListener

val strongRepository = new ConcurrentHashMap<UUID, Participant>

val weakRepository = new ConcurrentHashMap<UUID, Participant>

/** Constructor.
*
* @param id identifier of the space.
Expand Down Expand Up @@ -103,11 +108,12 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
var id = entity.ID
var address = new Address(this.spaceID, id)
var participant = Participant::createAndInit(address, entity)

assert(id !== null)
assert(participant !== null)
if (weakParticipant) {
ensureInternalWeakParticipantStructure.put(id, participant)
this.weakRepository.putIfAbsent(id, participant)
} else {
internalStrongParticipantStructure.put(id, participant)
this.strongRepository.putIfAbsent(id, participant)
}

getSpaceParticipantListener?.participantJoined(participant)
Expand All @@ -119,15 +125,13 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
var participant : Participant = null
var becomesEmpty : boolean

var structure = this.internalStrongParticipantStructure
var structure = this.strongRepository
participant = structure.remove(entity.ID)
becomesEmpty = structure.empty
if (participant === null) {
// Try a weak listener
structure = this.internalWeakParticipantStructure
if (structure !== null) {
participant = structure.remove(entity.ID)
}
structure = this.weakRepository
participant = structure.remove(entity.ID)
}

if (participant !== null) {
Expand All @@ -144,12 +148,12 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
assert id !== null
var participant : Participant = null

participant = this.internalStrongParticipantStructure.get(id)
participant = this.strongRepository.get(id)
if (participant !== null) {
return participant.address
}

participant = this.internalWeakParticipantStructure?.get(id)
participant = this.weakRepository?.get(id)
if (participant !== null) {
return participant.address
}
Expand Down Expand Up @@ -196,29 +200,15 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
protected def getScopedParticipants(scope : Scope<? super Address>) : ConcurrentLinkedDeque<Participant> {
val scopedParticipants = new ConcurrentLinkedDeque
if (scope === null) {
for (p : internalStrongParticipantStructure.values) {
scopedParticipants += p
}
val struct = internalWeakParticipantStructure
if (struct !== null) {
for (p : struct.values) {
scopedParticipants += p
}
}
strongRepository.forEach[id, participant|scopedParticipants += participant]
weakRepository.forEach[id, participant|scopedParticipants += participant]
} else {
for (p : internalStrongParticipantStructure.values) {
if (scope.matches(p.address)) {
scopedParticipants += p
}
}
val struct = internalWeakParticipantStructure
if (struct !== null) {
for (p : struct.values) {
if (scope.matches(p.address)) {
scopedParticipants += p
}
}
}
strongRepository.filter[id, p|scope.matches(p.address)].forEach [ id, participant |
scopedParticipants += participant
]
weakRepository.filter[id, p|scope.matches(p.address)].forEach [ id, participant |
scopedParticipants += participant
]
}
return scopedParticipants
}
Expand Down Expand Up @@ -249,12 +239,18 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S

@Pure
override getNumberOfStrongParticipants : int {
internalStrongParticipantStructure.size
strongRepository.size
}

@Pure
def getNumberOfWeakParticipants : int {
weakRepository.size
}


@Pure
override isPseudoEmpty(id : UUID) : boolean {
val struct = internalStrongParticipantStructure
val struct = strongRepository
val sz = struct.size
if (sz <= 0) {
return true
Expand All @@ -266,9 +262,24 @@ abstract class AbstractEventSpace extends AbstractSpace implements EventSpace, S
}

override forEachStrongParticipant(callback : (UUID)=>void) {
for (participant : internalStrongParticipantStructure.values) {
callback.apply(participant.address.UUID)
}
strongRepository.forEach[id, participant|callback.apply(id)] // see if participant.address.UUID is better
}

override forEachWeakParticipant(callback : (UUID)=>void) {
weakRepository.forEach[id, participant|callback.apply(id)] // see if participant.address.UUID is better
}

override forEachStrongParticipant(callback : (UUID, Participant)=>void) {
strongRepository.forEach[id, participant|callback.apply(id, participant)]
}

override forEachWeakParticipant(callback : (UUID, Participant)=>void) {
weakRepository.forEach[id, participant|callback.apply(id, participant)]
}

override getListenerFromStrongParticipant(target : UUID) : EventListener {
var participant = this.strongRepository?.get(target)
return participant.participant
}

}
Expand Up @@ -47,26 +47,15 @@ import java.util.concurrent.ConcurrentHashMap
*/
class OpenLocalEventSpace extends AbstractEventSpace implements OpenEventSpace {

val strongRepository = new ConcurrentHashMap<UUID, Participant>

volatile var weakRepository : ConcurrentHashMap<UUID, Participant>

override getInternalStrongParticipantStructure : ConcurrentHashMap<UUID, Participant> {
/*override getInternalStrongParticipantStructure : ConcurrentHashMap<UUID, Participant> {
this.strongRepository
}

override getInternalWeakParticipantStructure : ConcurrentHashMap<UUID, Participant> {
this.weakRepository
}

override ensureInternalWeakParticipantStructure : ConcurrentHashMap<UUID, Participant> {
var r = this.weakRepository
if (r === null) {
this.weakRepository = new ConcurrentHashMap
r = this.weakRepository
}
return this.weakRepository
}
}*/

def register(entity : EventListener, weakParticipant : boolean) : Address {
entity.registerToSpace(weakParticipant)
Expand Down Expand Up @@ -117,17 +106,14 @@ class RestrictedAccessLocalEventSpace extends AbstractEventSpace implements Rest
this.accessPermission = accessPermission
}

override getInternalStrongParticipantStructure : ConcurrentHashMap<UUID, Participant> {
/*override getInternalStrongParticipantStructure : ConcurrentHashMap<UUID, Participant> {
this.repository
}

override getInternalWeakParticipantStructure : ConcurrentHashMap<UUID, Participant> {
null
}
}*/

override ensureInternalWeakParticipantStructure : ConcurrentHashMap<UUID, Participant> {
null
}

/**
* Replies the Access Control List.
Expand Down
@@ -1,30 +1,29 @@
/*
/*
* $Id$
*
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
*
* Copyright (C) 2014-2020 the original authors or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sarl.sre.spaces

import io.sarl.lang.annotation.PrivateAPI
import io.sarl.lang.core.EventListener
import io.sarl.lang.core.Space
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap

/**
* Space with participants that exhibits the participants' list.
Expand All @@ -39,29 +38,21 @@ import java.util.concurrent.ConcurrentHashMap
@PrivateAPI
interface SpaceWithParticipants extends Space {

/** Replies the internal data structure for storing the strong space's participants.
* A Weak participant will not prevent the space from begin destroyed if it is the only one staying in it, a Strong participant will prevent the space destruction.
* A space containing only Weak participants will be destroyed by the SRE Kernel
* @return the entire strong participant structure, never {@code null}
/** Apply the given procedure to each of the strong participants.
* This function ignores the weak participants.
*
* @param callback the lambda to invoke.
* @since 0.11
*/
def getInternalStrongParticipantStructure : ConcurrentHashMap<UUID, Participant>
def forEachStrongParticipant(callback : (UUID, Participant) => void);

/** Replies the internal data structure for storing the weak space's participants.
* A Weak participant will not prevent the space from begin destroyed if it is the only one staying in it, a Strong participant will prevent the space destruction.
* A space containing only Weak participants will be destroyed by the SRE Kernel
* @return the entire weak participant structure, or {@code null} if none.
/** Apply the given procedure to each of the weak participants.
* This function ignores the strong participants.
*
* @param callback the lambda to invoke.
* @since 0.11
*/
def getInternalWeakParticipantStructure : ConcurrentHashMap<UUID, Participant>

/** Replies the internal data structure for storing the weak space's participants.
* A Weak participant will not prevent the space from begin destroyed if it is the only one staying in it, a Strong participant will prevent the space destruction.
* A space containing only Weak participants will be destroyed by the SRE Kernel
* @return the entire weak participant structure, never {@code null}.
* @since 0.11
*/
def ensureInternalWeakParticipantStructure : ConcurrentHashMap<UUID, Participant>
def forEachWeakParticipant(callback : (UUID, Participant)=>void);

def getListenerFromStrongParticipant(target : UUID):EventListener;
}

0 comments on commit a17f520

Please sign in to comment.