EntityType etype = EntityType.forName(eTypeName); logger.debug("{}: reading entities of type {}", name, etype); EntityDefaults entityDefaults = EntityDefaults.lookup(etype); format.setEntityType(etype); format.setEntityBuilder(entityDefaults != null ? entityDefaults.getDefaultBuilder() : BasicEntityBuilder.class); List<TypedName<?>> defColumns = entityDefaults != null ? entityDefaults.getDefaultColumns() : null; if (defColumns == null) { throw new IllegalArgumentException("no columns specified and no default columns available"); for (TypedName<?> attr: entityDefaults.getDefaultColumns()) { format.addColumn(attr);
return new EntityDefaults(type, attrs.values(), cols, builder.asSubclass(EntityBuilder.class), derivs);
/** * Look up the defaults for a particular entity type. * @param type The entity type. * @return The defaults. */ @Nullable public static EntityDefaults lookup(EntityType type) { // TODO Cache these defaults String name = type.getName(); String path = String.format("META-INF/lenskit/entity-defaults/%s.yaml", name); try (InputStream stream = ClassLoaders.inferDefault(EntityDefaults.class) .getResourceAsStream(path)) { if (stream == null) { return null; } ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); DefaultsBean bean = mapper.readValue(stream, DefaultsBean.class); return fromBean(type, bean); } catch (IOException e) { throw new UncheckedIOException("error defaults for " + type, e); } }
EntityType etype = EntityType.forName(eTypeName); logger.debug("{}: reading entities of type {}", name, etype); EntityDefaults entityDefaults = EntityDefaults.lookup(etype); format.setEntityType(etype); format.setEntityBuilder(entityDefaults != null ? entityDefaults.getDefaultBuilder() : BasicEntityBuilder.class);
EntityDefaults defaults = EntityDefaults.lookup(type); if (defaults == null) { continue; derivations.addAll(defaults.getDefaultDerivations());
@Test public void testRatingDefaults() { EntityDefaults defaults = EntityDefaults.lookup(EntityType.forName("rating")); assertThat(defaults, notNullValue()); assertThat(defaults.getEntityType(), equalTo(EntityType.forName("rating"))); assertThat(defaults.getCommonAttributes(), containsInAnyOrder((TypedName) CommonAttributes.USER_ID, CommonAttributes.ITEM_ID, CommonAttributes.RATING, CommonAttributes.TIMESTAMP)); assertThat(defaults.getDefaultColumns(), contains((TypedName) CommonAttributes.USER_ID, CommonAttributes.ITEM_ID, CommonAttributes.RATING, CommonAttributes.TIMESTAMP)); // FIXME Re-enable this assert when rating builders work // assertThat(defaults.getDefaultBuilder(), // equalTo((Class) RatingBuilder.class)); } }
EntityType etype = EntityType.forName(type); format.setEntityType(etype); EntityDefaults defaults = EntityDefaults.lookup(etype); if (defaults == null) { logger.warn("no defaults found for entity type {}", type); } else { format.clearColumns(); for (TypedName<?> col: defaults.getDefaultColumns()) { format.addColumn(col);
@Test public void testMissingDefaults() { EntityDefaults defaults = EntityDefaults.lookup(EntityType.forName("wombat")); // we don't know anything about wombats assertThat(defaults, nullValue()); }
static TypedName<?> parseAttribute(EntityDefaults entityDefaults, JsonNode col) { if (col.isNull() || col.isMissingNode()) { return null; } else if (col.isObject()) { String name = col.path("name").asText(null); String type = col.path("type").asText(null); Preconditions.checkArgument(name != null, "no attribute name specified"); Preconditions.checkArgument(type != null, "no attribute type specified"); return TypedName.create(name, type); } else if (col.isTextual()) { String name = col.asText(); TypedName<?> attr = entityDefaults != null ? entityDefaults.getAttributeDefaults(name) : null; if (attr == null) { attr = TypedName.create(col.asText(), col.asText().equals("id") ? (Class) Long.class : String.class); } return attr; } else { throw new IllegalArgumentException("invalid attribute specification: " + col.toString()); } } }
EntityType etype = EntityType.forName(eTypeName); logger.debug("{}: reading entities of type {}", name, etype); EntityDefaults entityDefaults = EntityDefaults.lookup(etype); format.setEntityType(etype); format.setEntityBuilder(entityDefaults != null ? entityDefaults.getDefaultBuilder() : BasicEntityBuilder.class);
EntityDefaults defaults = EntityDefaults.lookup(type); if (defaults == null) { continue; derivations.addAll(defaults.getDefaultDerivations());
static TypedName<?> parseAttribute(EntityDefaults entityDefaults, JsonNode col) { if (col.isNull() || col.isMissingNode()) { return null; } else if (col.isObject()) { String name = col.path("name").asText(null); String type = col.path("type").asText(null); Preconditions.checkArgument(name != null, "no attribute name specified"); Preconditions.checkArgument(type != null, "no attribute type specified"); return TypedName.create(name, type); } else if (col.isTextual()) { String name = col.asText(); TypedName<?> attr = entityDefaults != null ? entityDefaults.getAttributeDefaults(name) : null; if (attr == null) { attr = TypedName.create(col.asText(), col.asText().equals("id") ? (Class) Long.class : String.class); } return attr; } else { throw new IllegalArgumentException("invalid attribute specification: " + col.toString()); } } }
EntityType etype = EntityType.forName(eTypeName); logger.debug("{}: reading entities of type {}", name, etype); EntityDefaults entityDefaults = EntityDefaults.lookup(etype); format.setEntityType(etype); format.setEntityBuilder(entityDefaults != null ? entityDefaults.getDefaultBuilder() : BasicEntityBuilder.class); List<TypedName<?>> defColumns = entityDefaults != null ? entityDefaults.getDefaultColumns() : null; if (defColumns == null) { throw new IllegalArgumentException("no columns specified and no default columns available"); for (TypedName<?> attr: entityDefaults.getDefaultColumns()) { format.addColumn(attr);
/** * Look up the defaults for a particular entity type. * @param type The entity type. * @return The defaults. */ @Nullable public static EntityDefaults lookup(EntityType type) { // TODO Cache these defaults String name = type.getName(); String path = String.format("META-INF/lenskit/entity-defaults/%s.yaml", name); try (InputStream stream = ClassLoaders.inferDefault(EntityDefaults.class) .getResourceAsStream(path)) { if (stream == null) { return null; } ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); DefaultsBean bean = mapper.readValue(stream, DefaultsBean.class); return fromBean(type, bean); } catch (IOException e) { throw new UncheckedIOException("error defaults for " + type, e); } }
return new EntityDefaults(type, attrs.values(), cols, builder.asSubclass(EntityBuilder.class), derivs);