Database-assisted state machine learning