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 | Event<?> myEvent = new EFGEvent(event.getEventId());
|
---|
60 | /*
|
---|
61 | * The target is currently not set to event.widgetId() because the
|
---|
62 | * WidgetId is not available when loading sequences in form of test
|
---|
63 | * cases.
|
---|
64 | */
|
---|
65 | //myEvent.setTarget(event.getWidgetId(););
|
---|
66 | myEvents.add(myEvent);
|
---|
67 | }
|
---|
68 |
|
---|
69 | // extracting graph structure from the EFG
|
---|
70 | /*
|
---|
71 | * getEventGraph returns an adjacency matrix, i.e., a square matrix of
|
---|
72 | * efgEvents.size(), where a 1 in row i, column j means an edge
|
---|
73 | * efgEvents.get(i)->efgEvents.get(j) exists.
|
---|
74 | */
|
---|
75 | EventGraphType efgGraph = efg.getEventGraph();
|
---|
76 |
|
---|
77 | FirstOrderMarkovModel model = new FirstOrderMarkovModel(new Random());
|
---|
78 | Collection<List<Event<?>>> subsequences = new LinkedList<List<Event<?>>>();
|
---|
79 |
|
---|
80 | int efgSize = efgEvents.size();
|
---|
81 | for (int row = 0; row < efgSize; row++) {
|
---|
82 | for (int col = 0; col < efgSize; col++) {
|
---|
83 | int relation = efgGraph.getRow().get(row).getE().get(col);
|
---|
84 | // otherEvent is followed by currentEvent
|
---|
85 | if (relation != GUITARConstants.NO_EDGE) {
|
---|
86 | List<Event<?>> edge = new LinkedList<Event<?>>();
|
---|
87 | edge.add(myEvents.get(row));
|
---|
88 | edge.add(myEvents.get(col));
|
---|
89 | subsequences.add(edge);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | model.train(subsequences);
|
---|
94 | GlobalDataContainer.getInstance().addData(modelname, model);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * (non-Javadoc)
|
---|
99 | *
|
---|
100 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
101 | */
|
---|
102 | @Override
|
---|
103 | public void help() {
|
---|
104 | Console.println("Usage: efgToMM <filename> <modelname>");
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|