LightVoting
 All Classes Namespaces Files Functions Variables Pages
CSend.java
Go to the documentation of this file.
1 
24 package org.lightvoting.simulation.action.message;
25 
26 import org.lightjason.agentspeak.action.IBaseAction;
27 import org.lightjason.agentspeak.agent.IAgent;
28 import org.lightjason.agentspeak.common.CPath;
29 import org.lightjason.agentspeak.common.IPath;
30 import org.lightjason.agentspeak.language.CLiteral;
31 import org.lightjason.agentspeak.language.CRawTerm;
32 import org.lightjason.agentspeak.language.ITerm;
33 import org.lightjason.agentspeak.language.execution.IContext;
34 import org.lightjason.agentspeak.language.execution.fuzzy.CFuzzyValue;
35 import org.lightjason.agentspeak.language.execution.fuzzy.IFuzzyValue;
36 import org.lightjason.agentspeak.language.instantiable.plan.trigger.CTrigger;
37 import org.lightjason.agentspeak.language.instantiable.plan.trigger.ITrigger;
39 
40 import java.util.List;
41 import java.util.Map;
42 import java.util.concurrent.ConcurrentHashMap;
43 
44 
49 public final class CSend extends IBaseAction
50 {
54  private final Map<String, CVotingAgent> m_agents = new ConcurrentHashMap<>();
55 
60  public final CVotingAgent register( final CVotingAgent p_agent )
61  {
62  m_agents.put( p_agent.name(), p_agent );
63  return p_agent;
64  }
65 
71  public final CVotingAgent unregister( final CVotingAgent p_agent )
72  {
73  m_agents.remove( p_agent.name() );
74  return p_agent;
75  }
76 
77  @Override
78  public final IPath name()
79  {
80  return CPath.from( "message/send" );
81  }
82 
83  @Override
84  public final int minimalArgumentNumber()
85  {
86  return 2;
87  }
88 
89  @Override
90  public final IFuzzyValue<Boolean> execute( final IContext p_context, final boolean p_parallel, final List<ITerm> p_argument,
91  final List<ITerm> p_return )
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  }
131 }
final IFuzzyValue< Boolean > execute(final IContext p_context, final boolean p_parallel, final List< ITerm > p_argument, final List< ITerm > p_return)
Definition: CSend.java:90
final CVotingAgent unregister(final CVotingAgent p_agent)
Unregisters agent Removes agent from map.
Definition: CSend.java:71
final Map< String, CVotingAgent > m_agents
thread-safe map for storing name and agent object
Definition: CSend.java:54
Action to send messages for communication.
Definition: CSend.java:49
BDI agent with voting capabilities.