@SuppressWarnings({"unchecked", "rawtypes"})
 public static GeneratorComponent<?> createGeneratorComponent(
     InstanceDescriptor descriptor, Uniqueness ownerUniqueness, BeneratorContext context) {
   if (descriptor instanceof ComponentDescriptor)
     return ComponentBuilderFactory.createComponentBuilder(
         (ComponentDescriptor) descriptor, ownerUniqueness, context);
   else if (descriptor instanceof VariableDescriptor)
     return new Variable(
         descriptor.getName(),
         VariableGeneratorFactory.createGenerator((VariableDescriptor) descriptor, context),
         descriptor.getTypeDescriptor().getScope());
   else if (descriptor instanceof ArrayElementDescriptor)
     return ComponentBuilderFactory.createComponentBuilder(
         (ArrayElementDescriptor) descriptor, ownerUniqueness, context);
   else
     throw new UnsupportedOperationException(
         "Not a supported generator compnent type: " + descriptor.getClass());
 }
  @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);
  }
 @SuppressWarnings("unchecked")
 protected ComponentBuilder<Entity> createComponentBuilder(
     ComponentDescriptor component, BeneratorContext context) {
   return (ComponentBuilder<Entity>)
       ComponentBuilderFactory.createComponentBuilder(component, Uniqueness.NONE, context);
 }