private static String toMessage(
     NodeInitializerContext context,
     ModelSchemaStore schemaStore,
     Iterable<ModelType<?>> scalarTypes,
     Iterable<ModelType<?>> constructableTypes) {
   Optional<ModelProperty<?>> modelPropertyOptional = context.getModelProperty();
   StringBuffer s = new StringBuffer();
   if (modelPropertyOptional.isPresent()) {
     s.append(
         String.format(
             "A model element of type: '%s' can not be constructed.%n",
             context.getContainingType().get().getName()));
     ModelProperty<?> modelProperty = modelPropertyOptional.get();
     if (isManagedCollection(modelProperty.getType())) {
       s.append(
           String.format(
               "Its property '%s %s' is not a valid managed collection%n",
               modelProperty.getType().getName(), modelProperty.getName()));
       ModelCollectionSchema<?, ?> schema =
           (ModelCollectionSchema) schemaStore.getSchema(modelProperty.getType());
       s.append(
           String.format("A managed collection can not contain '%s's%n", schema.getElementType()));
       appendManagedCollections(s, 1, constructableTypes);
     } else if (isScalarCollection(modelProperty.getType(), schemaStore)) {
       ModelCollectionSchema<?, ?> schema =
           (ModelCollectionSchema) schemaStore.getSchema(modelProperty.getType());
       s.append(
           String.format(
               "Its property '%s %s' is not a valid scalar collection%n",
               modelProperty.getType().getName(), modelProperty.getName()));
       s.append(
           String.format("A scalar collection can not contain '%s's%n", schema.getElementType()));
       s.append(explainScalarCollections(scalarTypes));
     } else {
       s.append(
           String.format(
               "Its property '%s %s' can not be constructed%n",
               modelProperty.getType().getName(), modelProperty.getName()));
       s.append(String.format("It must be one of:%n"));
       s.append(String.format("    - %s%n", MANAGED_TYPE_DESCRIPTION));
       s.append("    - A managed collection. ");
       appendManagedCollections(s, 1, constructableTypes);
       s.append(
           String.format(
               "%n    - A scalar collection. %s%n    - %s",
               explainScalarCollections(scalarTypes), UNMANAGED_PROPERTY_DESCRIPTION));
       maybeAppendConstructables(s, constructableTypes, 1);
     }
   } else {
     s.append(
         String.format(
             "A model element of type: '%s' can not be constructed.%n",
             context.getModelType().getName()));
     s.append(String.format("It must be one of:%n"));
     s.append(String.format("    - %s", MANAGED_TYPE_DESCRIPTION));
     maybeAppendConstructables(s, constructableTypes, 1);
   }
   return s.toString();
 }
 private <T> NodeInitializer extractNodeInitializer(NodeInitializerContext<T> context) {
   ModelSchema<T> schema = schemaStore.getSchema(context.getModelType());
   for (NodeInitializerExtractionStrategy extractor : allStrategies) {
     NodeInitializer nodeInitializer = extractor.extractNodeInitializer(schema, context);
     if (nodeInitializer != null) {
       return nodeInitializer;
     }
   }
   throw canNotConstructTypeException(context);
 }
  private <T> StructSchema<T> getSchema(
      Class<T> source, DefaultMethodModelRuleExtractionContext context) {
    if (!RuleSource.class.isAssignableFrom(source)
        || !source.getSuperclass().equals(RuleSource.class)) {
      context.add("Rule source classes must directly extend " + RuleSource.class.getName());
    }

    ModelSchema<T> schema = schemaStore.getSchema(source);
    if (!(schema instanceof StructSchema)) {
      return null;
    }

    validateClass(source, context);
    return (StructSchema<T>) schema;
  }
  private <P> void addPropertyLink(
      MutableModelNode modelNode,
      ManagedProperty<P> property,
      ModelSchemaStore schemaStore,
      NodeInitializerRegistry nodeInitializerRegistry) {
    ModelType<P> propertyType = property.getType();
    ModelSchema<P> propertySchema = schemaStore.getSchema(propertyType);
    ModelType<T> publicType = bindings.getPublicSchema().getType();

    validateProperty(propertySchema, property, nodeInitializerRegistry);

    ModelPath childPath = modelNode.getPath().child(property.getName());
    if (propertySchema instanceof ManagedImplSchema) {
      if (!property.isWritable()) {
        ModelRegistrations.Builder builder =
            managedRegistrationBuilder(childPath, property, nodeInitializerRegistry, publicType);
        addLink(modelNode, builder, property.isInternal());
      } else {
        if (propertySchema instanceof ScalarCollectionSchema) {
          ModelRegistrations.Builder builder =
              managedRegistrationBuilder(childPath, property, nodeInitializerRegistry, publicType);
          addLink(modelNode, builder, property.isInternal());
        } else {
          modelNode.addReference(property.getName(), propertyType, modelNode.getDescriptor());
        }
      }
    } else {
      ModelRegistrations.Builder registrationBuilder;
      if (shouldHaveANodeInitializer(property, propertySchema)) {
        registrationBuilder =
            managedRegistrationBuilder(childPath, property, nodeInitializerRegistry, publicType);
      } else {
        registrationBuilder = ModelRegistrations.of(childPath);
      }
      registrationBuilder.withProjection(new UnmanagedModelProjection<P>(propertyType));
      addLink(modelNode, registrationBuilder, property.isInternal());
    }
  }
 private static boolean isScalarCollection(ModelType<?> type, ModelSchemaStore schemaStore) {
   ModelSchema<?> schema = schemaStore.getSchema(type);
   return schema instanceof ScalarCollectionSchema;
 }