package de.ugoe.cs.eventbench.efg; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; import java.util.List; import java.util.Random; import de.ugoe.cs.eventbench.commands.CMDupdateModel; import de.ugoe.cs.eventbench.data.Event; import de.ugoe.cs.eventbench.efg.data.EFGEvent; import de.ugoe.cs.eventbench.models.DeterministicFiniteAutomaton; import de.ugoe.cs.eventbench.models.FirstOrderMarkovModel; import edu.umd.cs.guitar.model.GUITARConstants; import edu.umd.cs.guitar.model.IO; import edu.umd.cs.guitar.model.data.EFG; import edu.umd.cs.guitar.model.data.EventGraphType; import edu.umd.cs.guitar.model.data.EventType; /** *

* Provides functionality to generates models defined in the package * de.ugoe.cs.eventbench.models from EFGs. *

* * @author Steffen Herbold * @version 1.0 */ public class EFGModelGenerator { /** *

* Generates a {@link FirstOrderMarkovModel} from an EFG. In the generated * model, all following events are equally possible, i.e., the model is * equal to a {@link DeterministicFiniteAutomaton}. However, through further * training (e.g., {@link CMDupdateModel}) this can be changed. *

* * @param efgFileName * name of the EFG file * @return model generated from the EFG */ public FirstOrderMarkovModel efgToFirstOrderMarkovModel(String efgFileName) { EFG efg = (EFG) IO.readObjFromFile(efgFileName, EFG.class); Collection>> subsequences = generateEdgeSequences(efg); FirstOrderMarkovModel model = new FirstOrderMarkovModel(new Random()); model.train(subsequences); return model; } /** *

* Generates a {@link DeterministicFiniteAutomaton} from an EFG. *

* * @param efgFileName * name of the EFG file * @return model generated from the EFG */ public DeterministicFiniteAutomaton efgToDeterministicFiniteAutomaton( String efgFileName) { EFG efg = (EFG) IO.readObjFromFile(efgFileName, EFG.class); Collection>> subsequences = generateEdgeSequences(efg); DeterministicFiniteAutomaton model = new DeterministicFiniteAutomaton( new Random()); model.train(subsequences); return model; } /** *

* Extracts the graph structure from the EFG. The result is a set of * sequences, where each sequence has length two and represents an edge in * the EFG. *

* * @param efg * EFG for which the edge sequence set is generated * @return edge sequence set */ private Collection>> generateEdgeSequences(EFG efg) { List> events = createEvents(efg); /* * getEventGraph returns an adjacency matrix, i.e., a square matrix of * efgEvents.size(), where a 1 in row i, column j means an edge * efgEvents.get(i)->efgEvents.get(j) exists. */ EventGraphType efgGraph = efg.getEventGraph(); Collection>> subsequences = new LinkedList>>(); int efgSize = events.size(); for (int row = 0; row < efgSize; row++) { for (int col = 0; col < efgSize; col++) { int relation = efgGraph.getRow().get(row).getE().get(col); // otherEvent is followed by currentEvent if (relation != GUITARConstants.NO_EDGE) { List> edge = new LinkedList>(); edge.add(events.get(row)); edge.add(events.get(col)); subsequences.add(edge); } } } return subsequences; } /** *

* Extracts creates {@link EFGEvent} for every event contained in the EFG. *

* * @param efg * EFG for which the events are created * @return list of events */ private List> createEvents(EFG efg) { List efgEvents = efg.getEvents().getEvent(); List> myEvents = new ArrayList>(efgEvents.size()); for (EventType event : efgEvents) { /* * the widgetId and eventId are only hash values, the * "interpretation" is found in the GUI file. */ Event myEvent = new EFGEvent(event.getEventId()); myEvent.setTarget(event.getWidgetId()); myEvents.add(myEvent); } return myEvents; } }