package org.syrup.functions;
import org.syrup.Context;
import org.syrup.Function;
import org.syrup.Result;
import org.syrup.helpers.NetworkImpl;
import org.syrup.helpers.NetworkParser;
import org.syrup.helpers.ResultImpl;
import org.syrup.helpers.Utils;
import org.syrup.helpers.WorkflowImpl;
import java.util.logging.Logger;
/**
* Copies the first input to the second output while it replaces it with a
* Duplicate Task. Used to feed looping constructs with an initial element.
*
* @author Robbert van Dalen
*/
public class Initiate implements Function
{
static final String COPYRIGHT = "Copyright 2005 Robbert van Dalen."
+ "At your option, you may copy, distribute, or make derivative works under "
+ "the terms of The Artistic License. This License may be found at "
+ "http://www.opensource.org/licenses/artistic-license.php. "
+ "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";
private final static Logger logger = Logger.getLogger("org.syrup.functions.Initiate");
private final static String replaceWorkflow = "<workflow><binding in-1='d-1' out-1='d-1' /><task name='d' functionClass='org.syrup.functions.Duplicate' orType='true'/></workflow>";
/**
*/
public Result execute(Context context)
{
try
{
ResultImpl im = null;
if (Utils.isFull(context.in_1_link()))
{
ResultImpl r = new ResultImpl(context, true, true, null, context.in_1_link().content());
// [TODO: optimize by prefabricating the NetworkImpl]
NetworkImpl network = new NetworkParser().parse(replaceWorkflow.getBytes());
// Returns the Workflow.
return new WorkflowImpl(r, network);
}
else
{
throw new Exception("first input needs to be full");
}
}
catch (Throwable e1)
{
return new ResultImpl(context, true, true, null, org.syrup.helpers.Utils.manageError(logger, e1, "Concat error"));
}
}
}