source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/efg/commands/CMDefgToMM.java @ 196

Last change on this file since 196 was 196, checked in by sherbold, 13 years ago
  • added command efgToMM for parsing an EFG file (see guitar.sourceforge.net) and create a first-order Markov model with the same edges as the EFG file and each transition equally probable
  • Property svn:mime-type set to text/plain
File size: 4.4 KB
Line 
1package de.ugoe.cs.eventbench.efg.commands;
2
3import java.security.InvalidParameterException;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Random;
9
10import de.ugoe.cs.eventbench.data.Event;
11import de.ugoe.cs.eventbench.data.GlobalDataContainer;
12import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel;
13import de.ugoe.cs.util.console.Command;
14import de.ugoe.cs.util.console.Console;
15import edu.umd.cs.guitar.model.GUITARConstants;
16import edu.umd.cs.guitar.model.IO;
17import edu.umd.cs.guitar.model.data.EFG;
18import edu.umd.cs.guitar.model.data.EventGraphType;
19import edu.umd.cs.guitar.model.data.EventType;
20
21/**
22 * <p>
23 * Command to that loads an EFG and creates a first-order Markov model with the
24 * same structure.
25 * </p>
26 *
27 * @author Steffen Herbold
28 * @version 1.0
29 */
30public class CMDefgToMM implements Command {
31
32        /*
33         * (non-Javadoc)
34         *
35         * @see de.ugoe.cs.util.console.Command#run(java.util.List)
36         */
37        @Override
38        public void run(List<Object> parameters) {
39                String filename;
40                String modelname;
41                try {
42                        filename = (String) parameters.get(0);
43                        modelname = (String) parameters.get(1);
44                } catch (Exception e) {
45                        throw new InvalidParameterException();
46                }
47
48                EFG efg = (EFG) IO.readObjFromFile(filename, EFG.class);
49
50                // extracting the events from the EFG
51                List<EventType> efgEvents = efg.getEvents().getEvent();
52                List<Event<?>> myEvents = new ArrayList<Event<?>>(efgEvents.size());
53                for (EventType event : efgEvents) {
54                        /*
55                         * Steffen (Question): I think the widgetId is only a hash value
56                         * identifying the target Is it sufficient as a target or should it
57                         * be somehow resolved into something more meaningful? If so, how?
58                         * Where is it available? There is some kind of GUI file with this
59                         * information, right? Should information of the GUI file be
60                         * resolved or the GUI file simply be associated with the EFG
61                         * internally. The best solution for this probably depends on the
62                         * test-case structure, which I have not analyzed yet.
63                         */
64
65                        // both widget ID and eventType are always null in the sample EFGs
66                        // ...
67                        String eventTarget = event.getWidgetId();
68                        //String eventType = event.getType();
69
70                        /*
71                         * What is the Action? What is the difference between the Action and
72                         * the Type?
73                         */
74                        String eventAction = event.getAction();
75
76                        Event<?> myEvent = new Event<Object>(eventAction);
77                        myEvent.setTarget(eventTarget);
78                        myEvents.add(myEvent);
79                }
80
81                // extracting graph structure from the EFG
82                /*
83                 * getEventGraph returns an adjacency matrix, i.e., a square matrix of
84                 * efgEvents.size(), where a 1 in row i, column j means an edge
85                 * efgEvents.get(i)->efgEvents.get(j) exists.
86                 */
87                EventGraphType efgGraph = efg.getEventGraph();
88
89                FirstOrderMarkovModel model = new FirstOrderMarkovModel(new Random());
90                Collection<List<Event<?>>> subsequences = new LinkedList<List<Event<?>>>();
91
92                /*
93                 * Code adapted from package
94                 * edu.umd.cs.guitar.testcase.plugin.TCPlugin#parseFollowRelations()
95                 * (part of testcase-generator-core)
96                 */
97                int efgSize = efgEvents.size();
98                for (int row = 0; row < efgSize; row++) {
99                        for (int col = 0; col < efgSize; col++) {
100                                int relation = efgGraph.getRow().get(row).getE().get(col);
101
102                                // otherEvent is followed by currentEvent
103                                if (relation != GUITARConstants.NO_EDGE) {
104                                        List<Event<?>> edge = new LinkedList<Event<?>>();
105                                        edge.add(myEvents.get(row));
106                                        edge.add(myEvents.get(col));
107                                        subsequences.add(edge);
108
109                                        /*
110                                         * Steffen (Question): What is the purpose of this if? What
111                                         * is the difference between a normal and a reaching edge?
112                                         * if (relation == GUITARConstants.REACHING_EDGE &&
113                                         * !otherEvent.getEventId().equals(
114                                         * currentEvent.getEventId())) { I probably don't need this
115                                         * anyways, since for usage analysis only successors are
116                                         * relevant Vector<EventType> p = null;//
117                                         * preds.get(otherEvent); if (p == null) { p = new
118                                         * Vector<EventType>(); }
119                                         *
120                                         * p.add(currentEvent); preds.put(otherEvent, p); }
121                                         */
122                                }
123                        }
124                }
125                model.train(subsequences);
126                GlobalDataContainer.getInstance().addData(modelname, model);
127        }
128
129        /*
130         * (non-Javadoc)
131         *
132         * @see de.ugoe.cs.util.console.Command#help()
133         */
134        @Override
135        public void help() {
136                Console.println("Usage: efgToMM <filename> <modelname>");
137        }
138
139}
Note: See TracBrowser for help on using the repository browser.