public function validate()
    {
        $cardinalityMap = $this->getCardinalityMap();
        $typeCount = [];

        foreach (array_keys($cardinalityMap) as $type) {
            $typeCount[$type] = 0;
        }

        foreach ($this->children as $type => $children) {
            $typeCount[$type] += count($children);
        }

        foreach ($typeCount as $type => $count) {
            list($min, $max) = $cardinalityMap[$type];
            if (null !== $min && $count < $min) {
                throw new OutOfBoundsException(sprintf(
                    'QueryBuilder node "%s" must have at least "%s" '.
                    'child nodes of type "%s". "%s" given.',
                    $this->getName(),
                    $min,
                    $type,
                    $count
                ));
            }
        }
    }