[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.plugin.guitar;
|
---|
[227] | 16 |
|
---|
| 17 | import java.util.ArrayList;
|
---|
| 18 | import java.util.Collection;
|
---|
| 19 | import java.util.LinkedList;
|
---|
| 20 | import java.util.List;
|
---|
| 21 | import java.util.Random;
|
---|
| 22 |
|
---|
[922] | 23 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
| 24 | import de.ugoe.cs.autoquest.plugin.guitar.eventcore.GUITAREventTarget;
|
---|
| 25 | import de.ugoe.cs.autoquest.plugin.guitar.eventcore.GUITAREventType;
|
---|
| 26 | import de.ugoe.cs.autoquest.plugin.guitar.eventcore.GUITARReplayable;
|
---|
| 27 | import de.ugoe.cs.autoquest.usageprofiles.DeterministicFiniteAutomaton;
|
---|
| 28 | import de.ugoe.cs.autoquest.usageprofiles.FirstOrderMarkovModel;
|
---|
[227] | 29 | import edu.umd.cs.guitar.model.GUITARConstants;
|
---|
| 30 | import edu.umd.cs.guitar.model.IO;
|
---|
| 31 | import edu.umd.cs.guitar.model.data.EFG;
|
---|
| 32 | import edu.umd.cs.guitar.model.data.EventGraphType;
|
---|
| 33 | import edu.umd.cs.guitar.model.data.EventType;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * <p>
|
---|
| 37 | * Provides functionality to generates models defined in the package
|
---|
[922] | 38 | * de.ugoe.cs.autoquest.usageprofiles from EFGs.
|
---|
[227] | 39 | * </p>
|
---|
| 40 | *
|
---|
| 41 | * @author Steffen Herbold
|
---|
| 42 | * @version 1.0
|
---|
| 43 | */
|
---|
| 44 | public class EFGModelGenerator {
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 47 | * <p>
|
---|
| 48 | * Generates a {@link FirstOrderMarkovModel} from an EFG. In the generated
|
---|
| 49 | * model, all following events are equally possible, i.e., the model is
|
---|
[849] | 50 | * equal to a {@link DeterministicFiniteAutomaton}.
|
---|
[227] | 51 | * </p>
|
---|
| 52 | *
|
---|
| 53 | * @param efgFileName
|
---|
| 54 | * name of the EFG file
|
---|
| 55 | * @return model generated from the EFG
|
---|
| 56 | */
|
---|
| 57 | public FirstOrderMarkovModel efgToFirstOrderMarkovModel(String efgFileName) {
|
---|
| 58 | EFG efg = (EFG) IO.readObjFromFile(efgFileName, EFG.class);
|
---|
| 59 |
|
---|
[556] | 60 | Collection<List<Event>> subsequences = generateEdgeSequences(efg);
|
---|
[227] | 61 | FirstOrderMarkovModel model = new FirstOrderMarkovModel(new Random());
|
---|
| 62 | model.train(subsequences);
|
---|
| 63 | return model;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * <p>
|
---|
| 68 | * Generates a {@link DeterministicFiniteAutomaton} from an EFG.
|
---|
| 69 | * </p>
|
---|
| 70 | *
|
---|
| 71 | * @param efgFileName
|
---|
| 72 | * name of the EFG file
|
---|
| 73 | * @return model generated from the EFG
|
---|
| 74 | */
|
---|
| 75 | public DeterministicFiniteAutomaton efgToDeterministicFiniteAutomaton(
|
---|
| 76 | String efgFileName) {
|
---|
| 77 | EFG efg = (EFG) IO.readObjFromFile(efgFileName, EFG.class);
|
---|
| 78 |
|
---|
[556] | 79 | Collection<List<Event>> subsequences = generateEdgeSequences(efg);
|
---|
[227] | 80 | DeterministicFiniteAutomaton model = new DeterministicFiniteAutomaton(
|
---|
| 81 | new Random());
|
---|
| 82 | model.train(subsequences);
|
---|
| 83 | return model;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * <p>
|
---|
| 88 | * Extracts the graph structure from the EFG. The result is a set of
|
---|
| 89 | * sequences, where each sequence has length two and represents an edge in
|
---|
| 90 | * the EFG.
|
---|
| 91 | * </p>
|
---|
| 92 | *
|
---|
| 93 | * @param efg
|
---|
| 94 | * EFG for which the edge sequence set is generated
|
---|
| 95 | * @return edge sequence set
|
---|
| 96 | */
|
---|
[556] | 97 | private Collection<List<Event>> generateEdgeSequences(EFG efg) {
|
---|
| 98 | List<Event> events = createEvents(efg);
|
---|
[227] | 99 | /*
|
---|
| 100 | * getEventGraph returns an adjacency matrix, i.e., a square matrix of
|
---|
| 101 | * efgEvents.size(), where a 1 in row i, column j means an edge
|
---|
| 102 | * efgEvents.get(i)->efgEvents.get(j) exists.
|
---|
| 103 | */
|
---|
| 104 | EventGraphType efgGraph = efg.getEventGraph();
|
---|
[556] | 105 | Collection<List<Event>> subsequences = new LinkedList<List<Event>>();
|
---|
[227] | 106 |
|
---|
| 107 | int efgSize = events.size();
|
---|
| 108 | for (int row = 0; row < efgSize; row++) {
|
---|
| 109 | for (int col = 0; col < efgSize; col++) {
|
---|
| 110 | int relation = efgGraph.getRow().get(row).getE().get(col);
|
---|
| 111 | // otherEvent is followed by currentEvent
|
---|
| 112 | if (relation != GUITARConstants.NO_EDGE) {
|
---|
[556] | 113 | List<Event> edge = new LinkedList<Event>();
|
---|
[227] | 114 | edge.add(events.get(row));
|
---|
| 115 | edge.add(events.get(col));
|
---|
| 116 | subsequences.add(edge);
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | return subsequences;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * <p>
|
---|
| 125 | * Extracts creates {@link EFGEvent} for every event contained in the EFG.
|
---|
| 126 | * </p>
|
---|
| 127 | *
|
---|
| 128 | * @param efg
|
---|
| 129 | * EFG for which the events are created
|
---|
| 130 | * @return list of events
|
---|
| 131 | */
|
---|
[556] | 132 | private List<Event> createEvents(EFG efg) {
|
---|
[227] | 133 | List<EventType> efgEvents = efg.getEvents().getEvent();
|
---|
[556] | 134 | List<Event> myEvents = new ArrayList<Event>(efgEvents.size());
|
---|
[227] | 135 | for (EventType event : efgEvents) {
|
---|
| 136 | /*
|
---|
| 137 | * the widgetId and eventId are only hash values, the
|
---|
| 138 | * "interpretation" is found in the GUI file.
|
---|
| 139 | */
|
---|
[556] | 140 | GUITAREventType type = new GUITAREventType(event.getEventId());
|
---|
| 141 | GUITAREventTarget target = new GUITAREventTarget(event.getWidgetId());
|
---|
| 142 | Event myEvent = new Event(type, target);
|
---|
| 143 | myEvent.addReplayable(new GUITARReplayable(event.getEventId()));
|
---|
[227] | 144 | }
|
---|
| 145 | return myEvents;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|