Refactor foreach in AllRule and AnyRule