source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/efg/EFGModelGenerator.java @ 297

Last change on this file since 297 was 227, checked in by sherbold, 13 years ago
  • extracted class de.ugoe.cs.eventbench.efg.EFGModelGenerator from de.ugoe.cs.eventbench.efg.commands.CMDefgToMM

+ added command efgToDFA

  • extracted class de.ugoe.cs.eventbench.efg.GUITARTestCaseParser from de.ugoe.cs.eventbench.efg.commands.CMDefgTestCasesToSequences
  • Property svn:mime-type set to text/plain
File size: 4.1 KB
Line 
1package de.ugoe.cs.eventbench.efg;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.LinkedList;
6import java.util.List;
7import java.util.Random;
8
9import de.ugoe.cs.eventbench.commands.CMDupdateModel;
10import de.ugoe.cs.eventbench.data.Event;
11import de.ugoe.cs.eventbench.efg.data.EFGEvent;
12import de.ugoe.cs.eventbench.models.DeterministicFiniteAutomaton;
13import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel;
14import edu.umd.cs.guitar.model.GUITARConstants;
15import edu.umd.cs.guitar.model.IO;
16import edu.umd.cs.guitar.model.data.EFG;
17import edu.umd.cs.guitar.model.data.EventGraphType;
18import edu.umd.cs.guitar.model.data.EventType;
19
20/**
21 * <p>
22 * Provides functionality to generates models defined in the package
23 * de.ugoe.cs.eventbench.models from EFGs.
24 * </p>
25 *
26 * @author Steffen Herbold
27 * @version 1.0
28 */
29public class EFGModelGenerator {
30
31        /**
32         * <p>
33         * Generates a {@link FirstOrderMarkovModel} from an EFG. In the generated
34         * model, all following events are equally possible, i.e., the model is
35         * equal to a {@link DeterministicFiniteAutomaton}. However, through further
36         * training (e.g., {@link CMDupdateModel}) this can be changed.
37         * </p>
38         *
39         * @param efgFileName
40         *            name of the EFG file
41         * @return model generated from the EFG
42         */
43        public FirstOrderMarkovModel efgToFirstOrderMarkovModel(String efgFileName) {
44                EFG efg = (EFG) IO.readObjFromFile(efgFileName, EFG.class);
45
46                Collection<List<Event<?>>> subsequences = generateEdgeSequences(efg);
47                FirstOrderMarkovModel model = new FirstOrderMarkovModel(new Random());
48                model.train(subsequences);
49                return model;
50        }
51
52        /**
53         * <p>
54         * Generates a {@link DeterministicFiniteAutomaton} from an EFG.
55         * </p>
56         *
57         * @param efgFileName
58         *            name of the EFG file
59         * @return model generated from the EFG
60         */
61        public DeterministicFiniteAutomaton efgToDeterministicFiniteAutomaton(
62                        String efgFileName) {
63                EFG efg = (EFG) IO.readObjFromFile(efgFileName, EFG.class);
64
65                Collection<List<Event<?>>> subsequences = generateEdgeSequences(efg);
66                DeterministicFiniteAutomaton model = new DeterministicFiniteAutomaton(
67                                new Random());
68                model.train(subsequences);
69                return model;
70        }
71
72        /**
73         * <p>
74         * Extracts the graph structure from the EFG. The result is a set of
75         * sequences, where each sequence has length two and represents an edge in
76         * the EFG.
77         * </p>
78         *
79         * @param efg
80         *            EFG for which the edge sequence set is generated
81         * @return edge sequence set
82         */
83        private Collection<List<Event<?>>> generateEdgeSequences(EFG efg) {
84                List<Event<?>> events = createEvents(efg);
85                /*
86                 * getEventGraph returns an adjacency matrix, i.e., a square matrix of
87                 * efgEvents.size(), where a 1 in row i, column j means an edge
88                 * efgEvents.get(i)->efgEvents.get(j) exists.
89                 */
90                EventGraphType efgGraph = efg.getEventGraph();
91                Collection<List<Event<?>>> subsequences = new LinkedList<List<Event<?>>>();
92
93                int efgSize = events.size();
94                for (int row = 0; row < efgSize; row++) {
95                        for (int col = 0; col < efgSize; col++) {
96                                int relation = efgGraph.getRow().get(row).getE().get(col);
97                                // otherEvent is followed by currentEvent
98                                if (relation != GUITARConstants.NO_EDGE) {
99                                        List<Event<?>> edge = new LinkedList<Event<?>>();
100                                        edge.add(events.get(row));
101                                        edge.add(events.get(col));
102                                        subsequences.add(edge);
103                                }
104                        }
105                }
106                return subsequences;
107        }
108
109        /**
110         * <p>
111         * Extracts creates {@link EFGEvent} for every event contained in the EFG.
112         * </p>
113         *
114         * @param efg
115         *            EFG for which the events are created
116         * @return list of events
117         */
118        private List<Event<?>> createEvents(EFG efg) {
119                List<EventType> efgEvents = efg.getEvents().getEvent();
120                List<Event<?>> myEvents = new ArrayList<Event<?>>(efgEvents.size());
121                for (EventType event : efgEvents) {
122                        /*
123                         * the widgetId and eventId are only hash values, the
124                         * "interpretation" is found in the GUI file.
125                         */
126                        Event<?> myEvent = new EFGEvent(event.getEventId());
127                        myEvent.setTarget(event.getWidgetId());
128                        myEvents.add(myEvent);
129                }
130                return myEvents;
131        }
132}
Note: See TracBrowser for help on using the repository browser.