1 | package genius.core.logging;
|
---|
2 |
|
---|
3 | import java.io.Closeable;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileNotFoundException;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.PrintStream;
|
---|
8 |
|
---|
9 | import genius.core.Bid;
|
---|
10 | import genius.core.actions.Action;
|
---|
11 | import genius.core.events.MultipartyNegoActionEvent;
|
---|
12 | import genius.core.events.NegotiationEvent;
|
---|
13 | import genius.core.events.SessionEndedNormallyEvent;
|
---|
14 | import genius.core.listener.Listener;
|
---|
15 | import genius.core.parties.NegotiationPartyInternal;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Creates a file logger which will log the nego events to a csv file
|
---|
19 | */
|
---|
20 | public class FileLogger implements Listener<NegotiationEvent>, Closeable {
|
---|
21 | // The internal print stream used for file writing
|
---|
22 | PrintStream ps;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | *
|
---|
26 | * @param fileName
|
---|
27 | * the log file without the .csv extension. If parent
|
---|
28 | * directory(s) of file don't exist, they are created
|
---|
29 | * @throws FileNotFoundException
|
---|
30 | */
|
---|
31 | public FileLogger(String fileName) throws FileNotFoundException {
|
---|
32 | new File(fileName).getParentFile().mkdirs();
|
---|
33 | ps = new PrintStream(fileName + ".csv");
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public void close() throws IOException {
|
---|
38 | ps.close();
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public void notifyChange(NegotiationEvent e) {
|
---|
43 | if (e instanceof MultipartyNegoActionEvent) {
|
---|
44 | MultipartyNegoActionEvent ea = (MultipartyNegoActionEvent) e;
|
---|
45 | Action action = ea.getAction();
|
---|
46 | if (action == null)
|
---|
47 | return; // error?
|
---|
48 | ps.println(
|
---|
49 | ea.getRound() + "," + ea.getTurn() + "," + ea.getTime() + "," + action.getAgent() + "," + action);
|
---|
50 | } else if (e instanceof SessionEndedNormallyEvent) {
|
---|
51 | SessionEndedNormallyEvent ee = (SessionEndedNormallyEvent) e;
|
---|
52 | Bid bid = ee.getAgreement();
|
---|
53 | if (bid == null) {
|
---|
54 | ps.print("ended-no-agreement");
|
---|
55 | } else {
|
---|
56 | ps.print("agreement," + bid);
|
---|
57 | for (NegotiationPartyInternal party : ((SessionEndedNormallyEvent) e).getParties()) {
|
---|
58 | ps.print("," + party.getUtility(bid));
|
---|
59 | }
|
---|
60 | }
|
---|
61 | ps.println();
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|
65 | }
|
---|