/*
* 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.core.cell;
import org.debellor.core.Cell;
import org.debellor.core.Sample;
import org.debellor.core.Cell.Stream;
import org.debellor.core.Sample.SampleType;
import org.debellor.core.exception.DebellorException;
import org.debellor.core.exception.cell.CellIsNotOperatorException;
/**
* Wrapper class that lets the cell to be used like a simple input-output operator
* that can be applied to an input sample in order to produce an output sample.
* If the cell cannot be treated in this way, because
* it does not produce exactly one output sample for every input sample,
* exception is thrown during execution.
*
* @author Marcin Wojnarski
*
*/
public class OperatorFromCell {
private Cell cell;
private SingleSample buf = new SingleSample();
public void setType(SampleType type) {
buf.setType(type);
}
public void setCell(Cell cell) throws DebellorException {
this.cell = cell;
this.cell.setSource(buf);
// this.cell.open();
}
public Sample applyTo(Sample in) throws DebellorException {
if(in == null) return null;
buf.put(in);
/* TODO: Important. Don't reopen cell for every single sample, in OperatorFromCell.
* This may slow down TrainAndTest very much. */
Stream stream = cell.open();
Sample out = stream.next();
stream.close();
if((out == null) || !buf.isEmpty())
throw new CellIsNotOperatorException(cell);
return out;
}
public void closeCell() throws DebellorException {
// cell.close();
}
}