If you have trs19 or TANE then i would look at the hump yard module on the dls made my escd84. It has all the automatic decoupling and sorting to different tracks randomly. I'd say if you look in surveyor then that might tell you how it works.
I have assets made by trev999 and updated by me which I have made a PDF tutorial on how to use the assets. The PDF available is 95% done, but it will explain almost all you need to know.