class_docstr: A class that represents a collection/database of rewrites.
  
  These databases are used to logically organize collections of rewrites
  (i.e. `GraphRewriter`\s and `NodeRewriter`).
class_docstr_tok: ["A","class","that","represents","a","collection\/database","of","rewrites",".","These","databases","are","used","to","logically","organize","collections","of","rewrites","(","i.e",".","`","GraphRewriter","`","\\s","and","`","NodeRewriter","`",")","."]
code: def __query__(self, q):
      # The ordered set is needed for deterministic rewriting.
      variables = OrderedSet()
      for tag in q.include:
          variables.update(self.__db__[tag])
      for tag in q.require:
          variables.intersection_update(self.__db__[tag])
      for tag in q.exclude:
          variables.difference_update(self.__db__[tag])
      remove = OrderedSet()
      add = OrderedSet()
      for obj in variables:
          if isinstance(obj, RewriteDatabase):
              def_sub_query = q
              if q.extra_rewrites:
                  def_sub_query = copy.copy(q)
                  def_sub_query.extra_rewrites = []
              sq = q.subquery.get(obj.name, def_sub_query)
  
              replacement = obj.query(sq)
              replacement.name = obj.name
              remove.add(obj)
              add.add(replacement)
      variables.difference_update(remove)
      variables.update(add)
      return variables
  
imports: import copy
  import math
  import sys
  from collections.abc import Iterable, Sequence
  from functools import cmp_to_key
  from io import StringIO
  from typing import Optional, Union
  from pytensor.configdefaults import config
  from pytensor.graph.rewriting import basic
  from pytensor.misc.ordered_set import OrderedSet
  from pytensor.utils import DefaultOrderedDict
label_desc: Use image node_id 5 for calling the RewriteDatabase obj's underlying member method code with example usage: obj.__query__(q) and returns: variables