LightVoting
 All Classes Namespaces Files Functions Variables Pages
CEnvironment.java
Go to the documentation of this file.
1 
24 package org.lightvoting.simulation.environment;
25 
26 import org.lightjason.agentspeak.language.CLiteral;
27 import org.lightjason.agentspeak.language.CRawTerm;
28 import org.lightjason.agentspeak.language.ILiteral;
31 
32 import java.util.Collections;
33 import java.util.LinkedList;
34 import java.util.List;
35 
36 
41 public final class CEnvironment
42 {
43  private final List<CGroup> m_groups;
44 
45  private final List<CVotingAgent> m_agentList;
46 
47  // Index of the last activated agent
48  private int m_currentIndex;
49 
50  private boolean m_firstActivated;
51  private final String m_fileName;
52 
58  public CEnvironment( final int p_size, final String p_fileName )
59  {
60  m_fileName = p_fileName;
61  m_groups = Collections.synchronizedList( new LinkedList<>() );
62  m_agentList = new LinkedList<>();
63  }
64 
71  public final void initialset( final CVotingAgent p_votingAgent )
72  {
73  m_agentList.add( p_votingAgent );
74 
75  if ( !m_firstActivated )
76  {
77 
78  final CVotingAgent l_firstAgent = m_agentList.get( 0 );
79 
80  l_firstAgent.sleep( 0 );
81  l_firstAgent.getChair().sleep( 0 );
82  m_firstActivated = true;
83  }
84  }
85 
91  public ILiteral literal( final CVotingAgent p_votingAgent )
92  {
93  return CLiteral.from( "groups", CRawTerm.from( m_groups ) );
94  }
95 
101  public CGroup openNewGroupRandom( final CVotingAgent p_votingAgent )
102  {
103  final CGroup l_group = new CGroup( p_votingAgent, "RANDOM" );
104  m_groups.add( l_group );
105  System.out.println( "Created Group " + l_group );
106  this.wakeUpAgent();
107 
108  return l_group;
109  }
110 
116  public CGroup openNewGroupCoordinated( final CVotingAgent p_votingAgent )
117  {
118  final CGroup l_group = new CGroup( p_votingAgent, "COORDINATED" );
119  m_groups.add( l_group );
120  System.out.println( "Created Group " + l_group );
121 
122  return l_group;
123  }
124 
131  public void addAgentRandom( final CGroup p_randomGroup, final CVotingAgent p_votingAgent )
132  {
133  p_randomGroup.addRandom( p_votingAgent );
134  this.wakeUpAgent();
135  }
136 
137 
145  public void addAgentCoordinated( final CGroup p_group, final CVotingAgent p_votingAgent )
146  {
147  p_group.addCoordinated( p_votingAgent );
148  this.wakeUpAgent();
149  }
150 
156  public ILiteral detectGroup( final CChairAgent p_chairAgent )
157  {
158  for ( final CGroup l_group : m_groups )
159  {
160  if ( !( ( l_group.literal( p_chairAgent ) ) == null ) )
161  return l_group.literal( p_chairAgent );
162  }
163  return null;
164  }
165 
166  // open group for further elections, unless the capacity is reached
167  // also, wake up the next agent
168 
173  public void reopen( final CGroup p_group )
174  {
175  p_group.reopen();
176  this.wakeUpAgent();
177  }
178 
179  private void wakeUpAgent()
180  {
181  m_currentIndex++;
182  final CVotingAgent l_wakingAgent = m_agentList.get( m_currentIndex );
183  l_wakingAgent.sleep( 0 );
184  l_wakingAgent.getChair().sleep( 0 );
185  System.out.println( "Waking up agent " + l_wakingAgent.name() );
186  }
187 }
CGroup openNewGroupCoordinated(final CVotingAgent p_votingAgent)
open new group (for coordinated grouping)
void addAgentCoordinated(final CGroup p_group, final CVotingAgent p_votingAgent)
add agent to group (for coordinated grouping)
ILiteral detectGroup(final CChairAgent p_chairAgent)
detect group of chair agent
CGroup openNewGroupRandom(final CVotingAgent p_votingAgent)
open new group (for random grouping)
ILiteral literal(final CVotingAgent p_votingAgent)
returns literal representation of existing groups
void addAgentRandom(final CGroup p_randomGroup, final CVotingAgent p_votingAgent)
add agent to group (for random grouping)
Created by sophie on 21.02.17.
final void initialset(final CVotingAgent p_votingAgent)
initialize groups
void reopen(final CGroup p_group)
open group for further elections unless the capacity is reached.
Created by sophie on 24.04.17.
Definition: CGroup.java:42
BDI agent with voting capabilities.
CEnvironment(final int p_size, final String p_fileName)
constructor