A Generalized Control-Flow-Aware Pattern Recognition Algorithm for Behavior Synthesis