/*
* Debellor
*
* Copyright (C) 2008-2009 by Marcin Wojnarski
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package org.debellor.base;
import java.util.ArrayList;
import java.util.Arrays;
import org.debellor.core.Cell;
import org.debellor.core.DataObject;
import org.debellor.core.DataType;
import org.debellor.core.Sample;
import org.debellor.core.Sample.SampleType;
import org.debellor.core.data.DataVector;
import org.debellor.core.data.DataVector.DataVectorType;
/**
* Flattens DataVectors whose items are other DataVectors.
* The items are concatenated into a single DataVector.
* This is done only for <code>data</code> field of samples,
* decision is untouched.
*
* @author Marcin Wojnarski
*
*/
public class FlattenVectors extends Cell {
/** Temporary variable used in onNext */
private DataObject[] items;
private Stream input;
public FlattenVectors() {
super(false);
}
@Override
protected SampleType onOpen() throws Exception {
input = openInputStream();
SampleType inputType = input.sampleType;
ArrayList<DataType> listOfItems = new ArrayList<DataType>();
DataVectorType inputData = (DataVectorType) inputType.data;
for(int i = 0; i < inputData.size(); i++) {
// assumption: all items are DataVectors themselves
DataVectorType item = (DataVectorType) inputData.get(i);
listOfItems.addAll(Arrays.asList( item.get() ));
}
DataType[] itemTypes = new DataType[ listOfItems.size() ];
listOfItems.toArray(itemTypes);
DataVectorType dataType = new DataVectorType(itemTypes);
SampleType type = new SampleType(dataType, inputType.decision);
items = new DataObject[ itemTypes.length ];
return type;
}
@Override
protected Sample onNext() throws Exception {
Sample s = input.next();
if(s == null) return null;
DataVector inputData = (DataVector) s.data;
for(int i = 0, j = 0; i < inputData.size(); i++) {
DataVector v = (DataVector) inputData.get(i);
for(DataObject item : v.get())
items[j++] = item;
}
DataVector data = new DataVector(items);
return s.setData(data);
}
@Override
protected void onClose() throws Exception {
input.close();
items = null;
}
}