def make_grid():
  """Create a grid, with relations for going n, s, e, w."""
  result = nql.NeuralQueryContext()
  result.declare_relation('n', 'place_t', 'place_t')
  result.declare_relation('s', 'place_t', 'place_t')
  result.declare_relation('e', 'place_t', 'place_t')
  result.declare_relation('w', 'place_t', 'place_t')
  result.declare_relation('color', 'place_t', 'color_t')
  result.declare_relation('distance_to', 'place_t', 'corner_t')

  kg_lines = []
  dij = {'n': (-1, 0), 's': (+1, 0), 'e': (0, +1), 'w': (0, -1)}
  for i in range(0, 4):
    for j in range(0, 4):
      cell_color = 'black' if (i % 2) == (j % 2) else 'white'
      kg_lines.append('\t'.join(['color', cell(i, j), cell_color]) + '\n')
      kg_lines.append(
          '\t'.join(['distance_to', cell(i, j), 'ul',
                     str(i + j)]) + '\n')
      for direction, (di, dj) in dij.items():
        if (0 <= i + di < 4) and (0 <= j + dj < 4):
          kg_lines.append(
              '\t'.join([direction, cell(i, j),
                         cell(i + di, j + dj)]) + '\n')
  result.load_kg(lines=kg_lines, freeze=True)
  return result
Create a grid, with relations for going n, s, e, w.
language/nql/nql/nql_test_lib.py