Using topological sort to order rules in rules engines