LightVoting
 All Classes Namespaces Files Functions Variables Pages
org.lightvoting.simulation.action.message.CSend Class Reference

Action to send messages for communication. More...

Inherits IBaseAction.

+ Collaboration diagram for org.lightvoting.simulation.action.message.CSend:

Public Member Functions

final IFuzzyValue< Boolean > execute (final IContext p_context, final boolean p_parallel, final List< ITerm > p_argument, final List< ITerm > p_return)
 
final int minimalArgumentNumber ()
 
final IPath name ()
 
final CVotingAgent register (final CVotingAgent p_agent)
 Register a new agent. More...
 
final CVotingAgent unregister (final CVotingAgent p_agent)
 Unregisters agent Removes agent from map. More...
 

Private Attributes

final Map< String, CVotingAgentm_agents = new ConcurrentHashMap<>()
 thread-safe map for storing name and agent object More...
 

Detailed Description

Based on/credits to https://lightjason.github.io/tutorials/communication/

Definition at line 49 of file CSend.java.

Member Function Documentation

final IFuzzyValue<Boolean> org.lightvoting.simulation.action.message.CSend.execute ( final IContext  p_context,
final boolean  p_parallel,
final List< ITerm >  p_argument,
final List< ITerm >  p_return 
)

first parameter of the action is the name of the receiving agent

Definition at line 90 of file CSend.java.

92  {
96  final IAgent<?> l_receiver = m_agents.get( p_argument.get( 0 ).<String>raw() );
97 
98  // if the agent is it not found, action fails
99  if ( l_receiver == null )
100  return CFuzzyValue.from( false );
101 
102  // create the receiving goal-trigger of the message
103  l_receiver.trigger(
104  CTrigger.from(
105  ITrigger.EType.ADDGOAL,
106 
107  // create the goal literal "message/receive(M,S)" with M is the message literal
108  // and S the sending agent name
109  CLiteral.from(
110  "message/receive",
111 
112  // message literal
113  CLiteral.from(
114  "message",
115 
116  // first argument is the agent name so copy all other arguments to the message literal
117  p_argument.subList( 1, p_argument.size() ).stream().map( i -> CRawTerm.from( i.raw() ) )
118  ),
119 
120  // name of the sending agent in this the agent which calls the send action is read from
121  // context and translate in the communication agent, the communication agent has got the
122  // method name() to read the agent name
123  CLiteral.from( "from", CRawTerm.from( p_context.agent().<CVotingAgent>raw().name() ) )
124  )
125 
126  )
127  );
128 
129  return CFuzzyValue.from( true );
130  }
final int org.lightvoting.simulation.action.message.CSend.minimalArgumentNumber ( )

Definition at line 84 of file CSend.java.

85  {
86  return 2;
87  }
final IPath org.lightvoting.simulation.action.message.CSend.name ( )

Definition at line 78 of file CSend.java.

79  {
80  return CPath.from( "message/send" );
81  }
final CVotingAgent org.lightvoting.simulation.action.message.CSend.register ( final CVotingAgent  p_agent)
Parameters
p_agentagent object
Returns
object of registered agent

Definition at line 60 of file CSend.java.

61  {
62  m_agents.put( p_agent.name(), p_agent );
63  return p_agent;
64  }
final CVotingAgent org.lightvoting.simulation.action.message.CSend.unregister ( final CVotingAgent  p_agent)
Parameters
p_agentagent object
Returns
object of unregistered agent

Definition at line 71 of file CSend.java.

72  {
73  m_agents.remove( p_agent.name() );
74  return p_agent;
75  }

Member Data Documentation

final Map<String, CVotingAgent> org.lightvoting.simulation.action.message.CSend.m_agents = new ConcurrentHashMap<>()
private

Definition at line 54 of file CSend.java.