LightVoting
 All Classes Namespaces Files Functions Variables Pages
CMain.java
Go to the documentation of this file.
1 
24 package org.lightvoting;
25 
26 import com.google.common.collect.Sets;
31 import org.yaml.snakeyaml.Yaml;
32 
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.util.Collection;
36 import java.util.Iterator;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.stream.Collectors;
40 import java.util.stream.IntStream;
41 
45 public final class CMain
46 {
47  private static CEnvironment s_environment;
48  private static int s_altnum;
49  private static String s_grouping;
50  private static String s_protocol;
51 
55  private CMain()
56  {
57  }
58 
64  public static void main( final String[] p_args ) throws Exception
65  {
66  // Example code taken from
67  // https://lightjason.github.io/tutorial/tutorial-agentspeak-in-fifteen-minutes/
68  //
69  // parameter of the command-line arguments:
70  // 1. ASL file
71  // 2. number of agents
72  // 3. number of iterations (if not set maximum)
73 
74  readYaml();
75 
76  final Set<CVotingAgent> l_agents;
77  final CVotingAgent.CVotingAgentGenerator l_votingagentgenerator;
78 
79  // final CDataWriter l_writer = new CDataWriter();
80  final String l_name = "results.h5";
81  CDataWriter.createHDF5( l_name );
82  CDataWriter.test( l_name );
83 
84  try
85  {
86  final FileInputStream l_stream = new FileInputStream( p_args[0] );
87  final FileInputStream l_chairstream = new FileInputStream( p_args[1] );
88 
89  s_environment = new CEnvironment( Integer.parseInt( p_args[2] ), l_name );
90 
91  l_votingagentgenerator = new CVotingAgent.CVotingAgentGenerator( new CSend(), l_stream, s_environment, s_altnum, s_grouping, l_name );
92  l_agents = l_votingagentgenerator
93  .generatemultiple( Integer.parseInt( p_args[2] ), new CChairAgent.CChairAgentGenerator( l_chairstream, s_environment, s_grouping, s_protocol, l_name ) )
94  .collect( Collectors.toSet() );
95 
96 
97  System.out.println( " Numbers of agents: " + l_agents.size() );
98 
99 
100  }
101  catch ( final Exception l_exception )
102  {
103  l_exception.printStackTrace();
104  throw new RuntimeException();
105  }
106 
107  // generate empty set of active agents
108 
109  final Set<CVotingAgent> l_activeAgents = Sets.newConcurrentHashSet();
110 
111  System.out.println( " Numbers of active agents: " + l_activeAgents.size() );
112 
113  System.out.println( " Numbers of active agents: " + l_activeAgents.size() );
114  System.out.println( " Will run " + p_args[3] + " cycles." );
115 
116  IntStream
117  // define cycle range, i.e. number of cycles to run sequentially
118  .range( 0,
119  p_args.length < 4
120  ? Integer.MAX_VALUE
121  : Integer.parseInt( p_args[3] ) )
122  .forEach( j ->
123  {
124  System.out.println( "Global cycle: " + j );
125  l_agents.parallelStream().forEach( i ->
126  {
127  try
128  {
129  // check if the conditions for triggering a new cycle are fulfilled in the environment
130  // call each agent, i.e. trigger a new agent cycle
131  i.call();
132  // i.getChair().sleep( 0 );
133  i.getChair().call();
134  }
135  catch ( final Exception l_exception )
136  {
137  l_exception.printStackTrace();
138  throw new RuntimeException();
139  }
140  } );
141  } );
142  }
143 
144  @SuppressWarnings( "unchecked" )
145  private static void readYaml() throws FileNotFoundException
146  {
147  final Yaml l_yaml = new Yaml();
148 
149  System.out.println( l_yaml.dump( l_yaml.load( new FileInputStream( "src/main/resources/org/lightvoting/configuration.yaml" ) ) ) );
150 
151  final Map<String, Map<String, String>> l_values = (Map<String, Map<String, String>>) l_yaml
152  .load( new FileInputStream( "src/main/resources/org/lightvoting/configuration.yaml" ) );
153 
154  for ( final String l_key : l_values.keySet() )
155  {
156  final Map<String, String> l_subValues = l_values.get( l_key );
157  System.out.println( l_key );
158 
159  for ( final String l_subValueKey : l_subValues.keySet() )
160  {
161  System.out.println( String.format( "\t%s = %s",
162  l_subValueKey, l_subValues.get( l_subValueKey ) ) );
163 
164  // parse input
165  if ( "grouping".equals( l_subValueKey ) )
166  s_grouping = l_subValues.get( l_subValueKey );
167  if ( "protocol".equals( l_subValueKey ) )
168  s_protocol = l_subValues.get( l_subValueKey );
169  if ( "altnum".equals( l_subValueKey ) )
170  s_altnum = Integer.parseInt( l_subValues.get( l_subValueKey ) );
171  }
172  }
173  }
174 
175  private static void addAgents( final Collection<CVotingAgent> p_activeAgents, final int p_newAgNum, final Iterator<CVotingAgent> p_agentIterator )
176  {
177 
178  for ( int i = 0; i < p_newAgNum; i++ )
179  {
180  if ( p_agentIterator.hasNext() )
181  {
182  final CVotingAgent l_curAg = p_agentIterator.next();
183  p_activeAgents.add( l_curAg );
184  System.out.println( "added Agent " + l_curAg.name() );
185  p_agentIterator.remove();
186 
187  }
188  }
189 
190  }
191 
192 
193 }
static int s_altnum
Definition: CMain.java:48
static void addAgents(final Collection< CVotingAgent > p_activeAgents, final int p_newAgNum, final Iterator< CVotingAgent > p_agentIterator)
Definition: CMain.java:175
static CEnvironment s_environment
Definition: CMain.java:47
static void main(final String[] p_args)
Main.
Definition: CMain.java:64
Main, providing runtime of LightVoting.
Definition: CMain.java:45
CMain()
Hidden constructor.
Definition: CMain.java:55
static String s_protocol
Definition: CMain.java:50
Created by sophie on 21.02.17.
Action to send messages for communication.
Definition: CSend.java:49
static void readYaml()
Definition: CMain.java:145
static String s_grouping
Definition: CMain.java:49
BDI agent with voting capabilities.