LightVoting
 All Classes Namespaces Files Functions Variables Pages
CGroup.java
Go to the documentation of this file.
1 
24 package org.lightvoting.simulation.environment;
25 
26 import cern.colt.bitvector.BitVector;
27 import org.lightjason.agentspeak.language.CLiteral;
28 import org.lightjason.agentspeak.language.CRawTerm;
29 import org.lightjason.agentspeak.language.ILiteral;
30 import org.lightjason.agentspeak.language.ITerm;
31 import org.lightjason.agentspeak.language.instantiable.plan.trigger.CTrigger;
32 import org.lightjason.agentspeak.language.instantiable.plan.trigger.ITrigger;
35 
36 import java.util.LinkedList;
37 import java.util.List;
38 
42 public class CGroup
43 {
44  private final List<CVotingAgent> m_agentList;
45 
46  private final int m_capacity = 3;
47 
48  private final CChairAgent m_chair;
49 
50  private boolean m_open;
51 
52  private BitVector m_result;
53 
54  private boolean m_readyForElection;
55  private boolean m_inProgress;
56 
62  public CGroup( final CVotingAgent p_votingAgent, final String p_grouping )
63  {
64  m_agentList = new LinkedList<>();
65  m_agentList.add( p_votingAgent );
66  m_chair = p_votingAgent.getChair();
67  m_open = true;
68  m_result = null;
69  if ( "RANDOM".equals( p_grouping ) )
70  m_readyForElection = false;
71  else
72  m_readyForElection = true;
73  m_inProgress = false;
74  }
75 
82  public ILiteral literal( final CVotingAgent p_votingAgent )
83  {
84  return CLiteral.from( "group", CRawTerm.from( m_chair ), CRawTerm.from( this.open() ), CRawTerm.from( m_result ),
85  CRawTerm.from( m_agentList.contains( p_votingAgent ) ) );
86  }
87 
93  public ILiteral literal( final CChairAgent p_chairAgent )
94  {
95  final List<ITerm> l_terms = new LinkedList<>();
96 
97  for ( int i = 0; i < m_agentList.size(); i++ )
98  l_terms.add( CRawTerm.from( m_agentList.get( i ) ) );
99 
100 
101  if ( ( this.m_chair ).equals( p_chairAgent ) )
102  return CLiteral.from( "group", CRawTerm.from( this ) );
103  else return null;
104  }
105 
106  public boolean readyForElection()
107  {
108  return m_readyForElection;
109  }
110 
115  public void addRandom( final CVotingAgent p_votingAgent )
116  {
117  System.out.println( "Adding agent, old size is " + m_agentList.size() );
118  m_agentList.add( p_votingAgent );
119  if ( m_agentList.size() >= m_capacity )
120  {
121  m_open = false;
122  m_readyForElection = true;
123  }
124  }
125 
131  public void addCoordinated( final CVotingAgent p_votingAgent )
132  {
133 
134  System.out.println( "Adding agent, old size is " + m_agentList.size() );
135  m_agentList.add( p_votingAgent );
136  m_open = false;
137  m_readyForElection = true;
138  }
139 
140  public void remove( final CVotingAgent p_votingAgent )
141  {
142  m_agentList.remove( p_votingAgent );
143  }
144 
145  public boolean open()
146  {
147  return m_open;
148  }
149 
150  public boolean electionInProgress()
151  {
152  return m_inProgress;
153  }
154 
155  public void startProgress()
156  {
157  m_inProgress = true;
158  }
159 
164  public void triggerAgents( final CChairAgent p_chairAgent )
165  {
166  m_agentList.forEach( i ->
167  i.trigger(
168  CTrigger.from(
169  ITrigger.EType.ADDGOAL,
170  CLiteral.from(
171  "submit/vote",
172  CRawTerm.from( p_chairAgent ) )
173  )
174  )
175  );
176  }
177 
182  public int size()
183  {
184  return m_agentList.size();
185  }
186 
194  public ILiteral updateBasic( final CChairAgent p_chairAgent, final BitVector p_result )
195  {
196  // send result of election to all agents in the group
197  m_agentList.stream().forEach( i ->
198  i.trigger( CTrigger.from(
199  ITrigger.EType.ADDGOAL,
200  CLiteral.from( "election/result",
201  CRawTerm.from( p_chairAgent ),
202  CRawTerm.from( p_result ) )
203  )
204  )
205  );
206 
207  m_result = p_result;
208  return this.literal( p_chairAgent );
209  }
210 
219  public ILiteral updateIterative( final CChairAgent p_chairAgent, final BitVector p_result, final int p_iteration )
220  {
221  // send result of election to all agents in the group
222  m_agentList.stream().forEach( i ->
223  {
224  i.trigger( CTrigger.from(
225  ITrigger.EType.ADDGOAL,
226  CLiteral.from( "election/result",
227  CRawTerm.from( p_chairAgent ),
228  CRawTerm.from( p_result ),
229  CRawTerm.from( p_iteration ) )
230  )
231  );
232  System.out.println( "triggering agent " + i.name() );
233  } );
234 
235  m_result = p_result;
236  return this.literal( p_chairAgent );
237 
238  }
239 
243  public void reset()
244  {
245  m_inProgress = false;
246  m_readyForElection = false;
247  }
248 
252  public void reopen()
253  {
254  if ( m_agentList.size() < m_capacity )
255  {
256  m_open = true;
257  }
258  }
259 
265  public BitVector result()
266  {
267  return m_result;
268  }
269 
270 
271  public boolean finale()
272  {
273  return m_agentList.size() >= m_capacity;
274  }
275 
276  public void makeReady()
277  {
278  m_readyForElection = true;
279  }
280 
286  public CVotingAgent determineAgent( final String p_agentName )
287  {
288  for ( int i = 0; i < m_agentList.size(); i++ )
289  if ( p_agentName.equals( m_agentList.get( i ).name() ) )
290  return m_agentList.get( i );
291  return null;
292  }
293 }
BitVector result()
return current result
Definition: CGroup.java:265
ILiteral literal(final CChairAgent p_chairAgent)
returns literal representation for chair agent, is null if the chair is not the chair of the group ...
Definition: CGroup.java:93
void reopen()
reopen group unless capacity is reached
Definition: CGroup.java:252
final List< CVotingAgent > m_agentList
Definition: CGroup.java:44
Created by sophie on 21.02.17.
void addCoordinated(final CVotingAgent p_votingAgent)
add voting agent (for coordinated grouping)
Definition: CGroup.java:131
CVotingAgent determineAgent(final String p_agentName)
determine agent for given name
Definition: CGroup.java:286
CGroup(final CVotingAgent p_votingAgent, final String p_grouping)
constructor
Definition: CGroup.java:62
ILiteral updateIterative(final CChairAgent p_chairAgent, final BitVector p_result, final int p_iteration)
update group literal for chair agent ( for random grouping )
Definition: CGroup.java:219
void addRandom(final CVotingAgent p_votingAgent)
add voting agent (for random grouping)
Definition: CGroup.java:115
ILiteral literal(final CVotingAgent p_votingAgent)
returns literal representation for voting agent
Definition: CGroup.java:82
Created by sophie on 24.04.17.
Definition: CGroup.java:42
void triggerAgents(final CChairAgent p_chairAgent)
trigger agents in group
Definition: CGroup.java:164
BDI agent with voting capabilities.
ILiteral updateBasic(final CChairAgent p_chairAgent, final BitVector p_result)
update group literal for chair agent ( for random grouping )
Definition: CGroup.java:194