Search in sources :

Example 1 with ListValueFactory

use of org.knime.core.data.v2.value.ListValueFactory in project knime-core by knime.

the class ValueFactoryUtilsTest method testGetTraitsOnCollectionValueFactory.

@Test
void testGetTraitsOnCollectionValueFactory() {
    var valueFactory = new ListValueFactory();
    valueFactory.initialize(new IntValueFactory(), IntCell.TYPE);
    // 
    assertThat(ValueFactoryUtils.getTraits(valueFactory)).isInstanceOf(// 
    ListDataTraits.class).matches(// 
    this::hasLogicalTypeTrait).matches(this::isListTypeTrait).extracting(// 
    ListDataTraits.class::cast).extracting(// 
    ListDataTraits::getInner).matches(// 
    this::hasLogicalTypeTrait).extracting(// 
    ValueFactoryUtilsTest::extractValueFactoryClassName).isEqualTo(IntValueFactory.class.getName());
    // collection of data cell value factories
    var listOfDataCells = createListValueFactory(createDataCellValueFactory(), XMLCell.TYPE);
    // 
    assertThat(ValueFactoryUtils.getTraits(listOfDataCells)).isInstanceOf(// 
    ListDataTraits.class).matches(// 
    this::hasLogicalTypeTrait).matches(// 
    this::isListTypeTrait).extracting(// 
    ListDataTraits.class::cast).extracting(// 
    ListDataTraits::getInner).extracting(// 
    ValueFactoryUtilsTest::extractLogicalTypeJson).matches(j -> j.get("value_factory_class").asText().equals(DictEncodedDataCellValueFactory.class.getName())).extracting(// 
    j -> j.get("data_type")).extracting(// 
    j -> j.get("cell_class").asText()).isEqualTo(XMLCell.class.getName());
    // nested collection
    var nestedCollection = createListValueFactory(createListValueFactory(IntValueFactory.INSTANCE, IntCell.TYPE), DataType.getType(ListCell.class, IntCell.TYPE));
    // 
    assertThat(ValueFactoryUtils.getTraits(nestedCollection)).isInstanceOf(// 
    ListDataTraits.class).matches(// 
    this::hasLogicalTypeTrait).matches(// 
    this::isListTypeTrait).extracting(// 
    ListDataTraits.class::cast).extracting(// 
    ListDataTraits::getInner).matches(// 
    this::hasLogicalTypeTrait).matches(// 
    this::isListTypeTrait).extracting(// 
    ListDataTraits.class::cast).extracting(// 
    ListDataTraits::getInner).matches(// 
    this::hasLogicalTypeTrait).extracting(// 
    ValueFactoryUtilsTest::extractValueFactoryClassName).isEqualTo(IntValueFactory.class.getName());
    // SparseListValueFactory (uses StructDataTraits)
    var sparseList = new SparseListValueFactory();
    sparseList.initialize(IntValueFactory.INSTANCE, IntCell.TYPE);
    // 
    assertThat(ValueFactoryUtils.getTraits(sparseList)).isInstanceOf(// 
    StructDataTraits.class).extracting(// 
    StructDataTraits.class::cast).matches(// 
    this::hasLogicalTypeTrait).matches(// 
    t -> extractValueFactoryClassName(t).equals(SparseListValueFactory.class.getName())).extracting(// 
    t -> t.getDataTraits(0)).matches(// 
    this::hasLogicalTypeTrait).extracting(// 
    ValueFactoryUtilsTest::extractValueFactoryClassName).isEqualTo(IntValueFactory.class.getName());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) ListCell(org.knime.core.data.collection.ListCell) LogicalTypeTrait(org.knime.core.table.schema.traits.LogicalTypeTrait) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) PNGImageCellFactory(org.knime.core.data.image.png.PNGImageCellFactory) IntReadAccess(org.knime.core.table.access.IntAccess.IntReadAccess) IntValueFactory(org.knime.core.data.v2.value.IntValueFactory) DataSpec(org.knime.core.table.schema.DataSpec) ListDataTraits(org.knime.core.table.schema.traits.ListDataTraits) StringValueFactory(org.knime.core.data.v2.value.StringValueFactory) DefaultDataTraits(org.knime.core.table.schema.traits.DefaultDataTraits) JsonNode(com.fasterxml.jackson.databind.JsonNode) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) DataCell(org.knime.core.data.DataCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell) NotInWorkflowDataRepository(org.knime.core.data.filestore.internal.NotInWorkflowDataRepository) DataCellValueFactory(org.knime.core.data.v2.value.cell.DataCellValueFactory) DoubleValueFactory(org.knime.core.data.v2.value.DoubleValueFactory) StructDataTraits(org.knime.core.table.schema.traits.StructDataTraits) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataTraits(org.knime.core.table.schema.traits.DataTraits) VoidRowKeyFactory(org.knime.core.data.v2.value.VoidRowKeyFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DoubleCell(org.knime.core.data.def.DoubleCell) NotInWorkflowWriteFileStoreHandler(org.knime.core.data.filestore.internal.NotInWorkflowWriteFileStoreHandler) XMLCell(org.knime.core.data.xml.XMLCell) DictEncodingTrait(org.knime.core.table.schema.traits.DataTrait.DictEncodingTrait) ListValueFactory(org.knime.core.data.v2.value.ListValueFactory) Test(org.junit.jupiter.api.Test) SparseListValueFactory(org.knime.core.data.v2.value.SparseListValueFactory) IntWriteAccess(org.knime.core.table.access.IntAccess.IntWriteAccess) IntListValueFactory(org.knime.core.data.v2.value.IntListValueFactory) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DefaultRowKeyValueFactory(org.knime.core.data.v2.value.DefaultRowKeyValueFactory) VoidValueFactory(org.knime.core.data.v2.value.VoidValueFactory) DataType(org.knime.core.data.DataType) DictEncodedDataCellValueFactory(org.knime.core.data.v2.value.cell.DictEncodedDataCellValueFactory) XMLCell(org.knime.core.data.xml.XMLCell) SparseListValueFactory(org.knime.core.data.v2.value.SparseListValueFactory) StructDataTraits(org.knime.core.table.schema.traits.StructDataTraits) DictEncodedDataCellValueFactory(org.knime.core.data.v2.value.cell.DictEncodedDataCellValueFactory) IntValueFactory(org.knime.core.data.v2.value.IntValueFactory) ListCell(org.knime.core.data.collection.ListCell) ListValueFactory(org.knime.core.data.v2.value.ListValueFactory) SparseListValueFactory(org.knime.core.data.v2.value.SparseListValueFactory) IntListValueFactory(org.knime.core.data.v2.value.IntListValueFactory) ListDataTraits(org.knime.core.table.schema.traits.ListDataTraits) Test(org.junit.jupiter.api.Test)

Example 2 with ListValueFactory

use of org.knime.core.data.v2.value.ListValueFactory in project knime-core by knime.

the class ValueFactoryUtilsTest method testLoadValueFactory.

@Test
void testLoadValueFactory() {
    var dataRepo = NotInWorkflowDataRepository.newInstance();
    // no logical type trait
    assertThrows(IllegalArgumentException.class, () -> ValueFactoryUtils.loadValueFactory(DefaultDataTraits.EMPTY, dataRepo));
    // void
    var voidTrait = ValueFactoryUtils.getTraits(VoidValueFactory.INSTANCE);
    assertThat(ValueFactoryUtils.loadValueFactory(voidTrait, dataRepo)).isEqualTo(VoidValueFactory.INSTANCE);
    // ordinary
    DataTraits traits = ValueFactoryUtils.getTraits(IntValueFactory.INSTANCE);
    // 
    assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isInstanceOf(IntValueFactory.class);
    // specific collection
    traits = ValueFactoryUtils.getTraits(IntListValueFactory.INSTANCE);
    // 
    assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isEqualTo(IntListValueFactory.INSTANCE);
    // nested collection
    var nestedListValueFactory = new ListValueFactory();
    nestedListValueFactory.initialize(IntListValueFactory.INSTANCE, DataType.getType(ListCell.class, DataType.getType(ListCell.class, IntCell.TYPE)));
    traits = ValueFactoryUtils.getTraits(nestedListValueFactory);
    // 
    assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isInstanceOf(// 
    ListValueFactory.class).extracting(// 
    ListValueFactory.class::cast).extracting(// 
    ListValueFactory::getElementValueFactory).isEqualTo(IntListValueFactory.INSTANCE);
    // data cell value factory
    var dictEncodedDataCellValueFactory = createDataCellValueFactory();
    traits = ValueFactoryUtils.getTraits(dictEncodedDataCellValueFactory);
    // 
    assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isInstanceOf(// 
    DictEncodedDataCellValueFactory.class).extracting(// 
    DictEncodedDataCellValueFactory.class::cast).extracting(// 
    DictEncodedDataCellValueFactory::getType).isEqualTo(XMLCell.TYPE);
    // try to load an unregistered value factory
    final var finalTraits = createSimpleTypeTraits(DummyDataValueFactory.class.getName());
    assertThrows(IllegalArgumentException.class, () -> ValueFactoryUtils.loadValueFactory(finalTraits, dataRepo));
}
Also used : DictEncodedDataCellValueFactory(org.knime.core.data.v2.value.cell.DictEncodedDataCellValueFactory) ListCell(org.knime.core.data.collection.ListCell) ListValueFactory(org.knime.core.data.v2.value.ListValueFactory) SparseListValueFactory(org.knime.core.data.v2.value.SparseListValueFactory) IntListValueFactory(org.knime.core.data.v2.value.IntListValueFactory) ListDataTraits(org.knime.core.table.schema.traits.ListDataTraits) DefaultDataTraits(org.knime.core.table.schema.traits.DefaultDataTraits) StructDataTraits(org.knime.core.table.schema.traits.StructDataTraits) DataTraits(org.knime.core.table.schema.traits.DataTraits) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)2 ListCell (org.knime.core.data.collection.ListCell)2 IntListValueFactory (org.knime.core.data.v2.value.IntListValueFactory)2 ListValueFactory (org.knime.core.data.v2.value.ListValueFactory)2 SparseListValueFactory (org.knime.core.data.v2.value.SparseListValueFactory)2 DictEncodedDataCellValueFactory (org.knime.core.data.v2.value.cell.DictEncodedDataCellValueFactory)2 DataTraits (org.knime.core.table.schema.traits.DataTraits)2 DefaultDataTraits (org.knime.core.table.schema.traits.DefaultDataTraits)2 ListDataTraits (org.knime.core.table.schema.traits.ListDataTraits)2 StructDataTraits (org.knime.core.table.schema.traits.StructDataTraits)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 DataCell (org.knime.core.data.DataCell)1 DataType (org.knime.core.data.DataType)1 IntValue (org.knime.core.data.IntValue)1