1 | package de.ugoe.cs.eventbench.efg.commands;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Random;
|
---|
9 |
|
---|
10 | import de.ugoe.cs.eventbench.data.Event;
|
---|
11 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
12 | import de.ugoe.cs.eventbench.efg.data.EFGEvent;
|
---|
13 | import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel;
|
---|
14 | import de.ugoe.cs.util.console.Command;
|
---|
15 | import de.ugoe.cs.util.console.Console;
|
---|
16 | import edu.umd.cs.guitar.model.GUITARConstants;
|
---|
17 | import edu.umd.cs.guitar.model.IO;
|
---|
18 | import edu.umd.cs.guitar.model.data.EFG;
|
---|
19 | import edu.umd.cs.guitar.model.data.EventGraphType;
|
---|
20 | import edu.umd.cs.guitar.model.data.EventType;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * <p>
|
---|
24 | * Command to that loads an EFG and creates a first-order Markov model with the
|
---|
25 | * same structure.
|
---|
26 | * </p>
|
---|
27 | *
|
---|
28 | * @author Steffen Herbold
|
---|
29 | * @version 1.0
|
---|
30 | */
|
---|
31 | public class CMDefgToMM implements Command {
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * (non-Javadoc)
|
---|
35 | *
|
---|
36 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
37 | */
|
---|
38 | @Override
|
---|
39 | public void run(List<Object> parameters) {
|
---|
40 | String filename;
|
---|
41 | String modelname;
|
---|
42 | try {
|
---|
43 | filename = (String) parameters.get(0);
|
---|
44 | modelname = (String) parameters.get(1);
|
---|
45 | } catch (Exception e) {
|
---|
46 | throw new InvalidParameterException();
|
---|
47 | }
|
---|
48 |
|
---|
49 | EFG efg = (EFG) IO.readObjFromFile(filename, EFG.class);
|
---|
50 |
|
---|
51 | // extracting the events from the EFG
|
---|
52 | List<EventType> efgEvents = efg.getEvents().getEvent();
|
---|
53 | List<Event<?>> myEvents = new ArrayList<Event<?>>(efgEvents.size());
|
---|
54 | for (EventType event : efgEvents) {
|
---|
55 | /*
|
---|
56 | * the widgetId and eventId are only hash values,
|
---|
57 | * the "interpretation" is found in the GUI file.
|
---|
58 | */
|
---|
59 | String eventTarget = event.getWidgetId();
|
---|
60 | String eventId = event.getEventId();
|
---|
61 |
|
---|
62 | /*
|
---|
63 | * Not sure what these are used for.
|
---|
64 | String eventType = event.getType();
|
---|
65 | String eventAction = event.getAction();
|
---|
66 | */
|
---|
67 |
|
---|
68 | Event<?> myEvent = new EFGEvent(eventId);
|
---|
69 | myEvent.setTarget(eventTarget);
|
---|
70 | myEvents.add(myEvent);
|
---|
71 | }
|
---|
72 |
|
---|
73 | // extracting graph structure from the EFG
|
---|
74 | /*
|
---|
75 | * getEventGraph returns an adjacency matrix, i.e., a square matrix of
|
---|
76 | * efgEvents.size(), where a 1 in row i, column j means an edge
|
---|
77 | * efgEvents.get(i)->efgEvents.get(j) exists.
|
---|
78 | */
|
---|
79 | EventGraphType efgGraph = efg.getEventGraph();
|
---|
80 |
|
---|
81 | FirstOrderMarkovModel model = new FirstOrderMarkovModel(new Random());
|
---|
82 | Collection<List<Event<?>>> subsequences = new LinkedList<List<Event<?>>>();
|
---|
83 |
|
---|
84 | int efgSize = efgEvents.size();
|
---|
85 | for (int row = 0; row < efgSize; row++) {
|
---|
86 | for (int col = 0; col < efgSize; col++) {
|
---|
87 | int relation = efgGraph.getRow().get(row).getE().get(col);
|
---|
88 | // otherEvent is followed by currentEvent
|
---|
89 | if (relation != GUITARConstants.NO_EDGE) {
|
---|
90 | List<Event<?>> edge = new LinkedList<Event<?>>();
|
---|
91 | edge.add(myEvents.get(row));
|
---|
92 | edge.add(myEvents.get(col));
|
---|
93 | subsequences.add(edge);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | model.train(subsequences);
|
---|
98 | GlobalDataContainer.getInstance().addData(modelname, model);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * (non-Javadoc)
|
---|
103 | *
|
---|
104 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
105 | */
|
---|
106 | @Override
|
---|
107 | public void help() {
|
---|
108 | Console.println("Usage: efgToMM <filename> <modelname>");
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|