@SuppressWarnings({"unchecked", "rawtypes"})
 private static Generator<Object[]> createSourceGeneratorFromObject(
     ArrayTypeDescriptor descriptor,
     BeneratorContext context,
     Generator<Object[]> generator,
     Object sourceObject) {
   if (sourceObject instanceof StorageSystem) {
     StorageSystem storage = (StorageSystem) sourceObject;
     String selector = descriptor.getSelector();
     String subSelector = descriptor.getSubSelector();
     if (!StringUtil.isEmpty(subSelector))
       generator =
           WrapperFactory.applyHeadCycler(
               new DataSourceGenerator(storage.query(subSelector, false, context)));
     else generator = new DataSourceGenerator(storage.query(selector, false, context));
   } else if (sourceObject instanceof EntitySource) {
     DataSourceGenerator<Entity> entityGenerator =
         new DataSourceGenerator<Entity>((EntitySource) sourceObject);
     generator = WrapperFactory.applyConverter(entityGenerator, new Entity2ArrayConverter());
   } else if (sourceObject instanceof Generator) {
     generator = (Generator<Object[]>) sourceObject;
   } else throw new ConfigurationError("Source type not supported: " + sourceObject.getClass());
   return generator;
 }
  @SuppressWarnings({"unchecked", "rawtypes"})
  static ComponentBuilder<?> createReferenceBuilder(
      ReferenceDescriptor descriptor, BeneratorContext context) {
    SimpleTypeDescriptor typeDescriptor = (SimpleTypeDescriptor) descriptor.getTypeDescriptor();

    // check uniqueness
    boolean unique = DescriptorUtil.isUnique(descriptor, context);
    Uniqueness uniqueness = (unique ? Uniqueness.SIMPLE : Uniqueness.NONE);

    // do I only need to generate nulls?
    if (DescriptorUtil.isNullable(descriptor, context)
        && DescriptorUtil.shouldNullifyEachNullable(descriptor, context))
      return builderFromGenerator(createNullGenerator(descriptor, context), descriptor, context);

    // TODO use SimpleTypeGeneratorFactory?
    Generator<?> generator = null;
    generator = DescriptorUtil.getGeneratorByName(typeDescriptor, context);
    if (generator == null)
      generator = SimpleTypeGeneratorFactory.createScriptGenerator(typeDescriptor);
    if (generator == null)
      generator = SimpleTypeGeneratorFactory.createConstantGenerator(typeDescriptor, context);
    if (generator == null)
      generator =
          SimpleTypeGeneratorFactory.createValuesGenerator(typeDescriptor, uniqueness, context);

    // get distribution
    Distribution distribution =
        FactoryUtil.getDistribution(
            typeDescriptor.getDistribution(), descriptor.getUniqueness(), false, context);

    // check source
    if (generator == null) {
      // check target type
      String targetTypeName = descriptor.getTargetType();
      ComplexTypeDescriptor targetType =
          (ComplexTypeDescriptor) context.getDataModel().getTypeDescriptor(targetTypeName);
      if (targetType == null) throw new ConfigurationError("Type not defined: " + targetTypeName);

      // check targetComponent
      String targetComponent = descriptor.getTargetComponent();

      // check source
      String sourceName = typeDescriptor.getSource();
      if (sourceName == null) throw new ConfigurationError("'source' is not set for " + descriptor);
      Object sourceObject = context.get(sourceName);
      if (sourceObject instanceof StorageSystem) {
        StorageSystem sourceSystem = (StorageSystem) sourceObject;
        String selector = typeDescriptor.getSelector();
        String subSelector = typeDescriptor.getSubSelector();
        boolean subSelect = !StringUtil.isEmpty(subSelector);
        String selectorToUse = (subSelect ? subSelector : selector);
        if (isIndividualSelector(selectorToUse)) {
          generator = new DataSourceGenerator(sourceSystem.query(selectorToUse, true, context));
        } else {
          generator =
              new DataSourceGenerator(
                  sourceSystem.queryEntityIds(
                      targetTypeName,
                      selectorToUse,
                      context)); // TODO v0.7.2 query by targetComponent
          if (selectorToUse == null && distribution == null)
            if (context.isDefaultOneToOne()) distribution = new ExpandSequence();
            else distribution = SequenceManager.RANDOM_SEQUENCE;
        }
        if (subSelect) generator = WrapperFactory.applyHeadCycler(generator);
      } else throw new ConfigurationError("Not a supported source type: " + sourceName);
    }

    // apply distribution if necessary
    if (distribution != null)
      generator = new DistributingGenerator(generator, distribution, unique);

    // check multiplicity
    generator = ComponentBuilderFactory.createMultiplicityWrapper(descriptor, generator, context);
    if (LOGGER.isDebugEnabled()) LOGGER.debug("Created " + generator);

    // check 'cyclic' config
    generator = DescriptorUtil.wrapWithProxy(generator, typeDescriptor);
    return builderFromGenerator(generator, descriptor, context);
  }