Does anyone know how to create a java snippet node which can output a table or list? all I can see is java snippet which can append table and that snippet only apples to each row of the table? I want to be able within that block to use weka forcast library. Currently I am generating a list and I have no idea how to use this code within Knime since the code runs for each row of the input table and I only can replace values for each row but not replacing the whole row.
here is my code.
// system imports import org.knime.base.node.jsnippet.expression.AbstractJSnippet; import org.knime.base.node.jsnippet.expression.Abort; import org.knime.base.node.jsnippet.expression.Cell; import org.knime.base.node.jsnippet.expression.ColumnException; import org.knime.base.node.jsnippet.expression.TypeException; import static org.knime.base.node.jsnippet.expression.Type.*; import java.util.Date; import java.util.Calendar; import org.w3c.dom.Document; // Your custom imports: import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import weka.core.Instances; import weka.classifiers.functions.LinearRegression; import weka.classifiers.evaluation.NumericPrediction; import weka.classifiers.timeseries.WekaForecaster; import org.knime.core.node.NodeLogger; // system variables public class JSnippet extends AbstractJSnippet { // Fields for output columns /** Output column: "Test" */ public Double out_Test; // Your custom variables: NodeLogger logger = NodeLogger.getLogger(AbstractJSnippet.class); // expression start public void snippet() throws TypeException, ColumnException, Abort { // Enter your code here: try { // path to the Australian wine data included with the time series // forecasting // package logger.warn("got here"); String path = "C:\\data\\dataset\\dataset.arff"; Path filepath = Paths.get(path); Charset charset = StandardCharsets.UTF_8; String content = new String(Files.readAllBytes(filepath), charset); content = content.replaceAll("@ATTRIBUTE DateTime STRING", "@ATTRIBUTE DateTime Date"); Files.write(filepath, content.getBytes(charset)); FileReader fr = new FileReader(path); Instances value = new Instances(new BufferedReader(fr)); // new forecaster WekaForecaster forecaster = new WekaForecaster(); // set the targets we want to forecast. This method calls // setFieldsToLag() on the lag maker object for us forecaster.setFieldsToForecast("Amp"); // default underlying classifier is SMOreg (SVM) - we'll use // gaussian processes for regression instead forecaster.setBaseForecaster(new LinearRegression()); forecaster.getTSLagMaker().setTimeStampField("DateTime"); // date forecaster.getTSLagMaker().setMinLag(1); forecaster.getTSLagMaker().setMaxLag(10); // monthly data // add a month of the year indicator field forecaster.getTSLagMaker().setAddMonthOfYear(true); // add a quarter of the year indicator field forecaster.getTSLagMaker().setAddQuarterOfYear(true); // build the model logger.warn("about to forcast"); forecaster.buildForecaster(value, System.out); // prime the forecaster with enough recent historical data // to cover up to the maximum lag. In our case, we could just supply // the 12 most recent historical instances, as this covers our // maximum // lag period forecaster.primeForecaster(value); // forecast for 12 units (months) beyond the end of the // training data List<List<NumericPrediction>> forecast = forecaster.forecast(10, System.out); // output the predictions. Outer list is over the steps; inner list // is over // the targets List<Double> list = new ArrayList<Double>(); logger.warn("got here"); for (int i = 0; i < 10; i++) { List<NumericPrediction> predsAtStep = forecast.get(i); NumericPrediction predForTarget = predsAtStep.get(0); if(ROWINDEX == i){ list.add(predForTarget.predicted()); } } } catch (Exception ex) { ex.printStackTrace(); } // expression end } }