A Rewriting Technique for the Analysis and the Optimization of Active Databases