Here's the test
[Test]
public void TestOperation()
{
TestProcess process = new TestProcess();
process.Execute();
//make sure rows were split up and then rejoined properly
Assert.AreEqual(2, process.results.Count);
}
public class TestProcess : BaseProcess
{
public List<Row> results = new List<Row>();
public TestProcess()
: base()
{
Operations = new BaseOperation[] {
new TestGenerateOperation(this),
new BranchAndReturnOperation(this)
.AddBranch(
new TestFilterOnTypeOperation(this, "1")
)
.AddBranch(
new TestFilterOnTypeOperation(this, "2")
),
new TestGetResultsOperation(this, results)
};
}
}
public class TestGenerateOperation : BaseOperation
{
public TestGenerateOperation(BaseProcess process) : base
(process) { }
public override IEnumerable<Row> Execute(IEnumerable<Row>
rows)
{
Row row = null;
row = new Row();
row["column1"] = "something";
row["type"] = "1";
yield return row;
row = new Row();
row["column1"] = "something else";
row["type"] = "2";
yield return row;
}
}
public class TestFilterOnTypeOperation : BaseOperation
{
public TestFilterOnTypeOperation(BaseProcess process,
string type) : base(process)
{
this.type = type;
}
string type;
public override IEnumerable<Row> Execute(IEnumerable<Row>
rows)
{
foreach(Row row in rows)
if(row["type"].ToString() == type)
yield return row;
}
}
On Dec 23, 4:06 pm, webpaul <[email protected]> wrote:
> I created a BranchAndReturnOperation which allows me to do this:
>
> new BranchAndReturnOperation(this)
> .AddBranch(
> new FilterRowsOnType(this, "type1")
> new DoSomeStuff1AndReturnRows(this)
> )
> .AddBranch(
> new FilterRowsOnType(this, "type2"),
> new DoSomeStuff2AndReturnRows(this)
> ),
>
> And then use the result rows from both of those branches. Just putting
> the idea here in case anyone wants it, the code below will need a
> little bit of cleanup before it could go in the etl trunk. But I'm
> more curious if anyone sees a reason why this wasn't done in the first
> place. I also changed the syntax a bit because I didn't like the
> partial.register stuff. If you didn't want one of the branches to
> return rows you could just make a null operation which ate the rows.
>
> public class BranchAndReturnOperation : BaseOperation
> {
> private readonly List<IOperation> operations = new
> List<IOperation>
> ();
>
> public BranchAndReturnOperation(BaseProcess Process) : base
> (Process) { }
>
> /// <summary>
> /// Adds the specified operation to this branching operation
> /// </summary>
> /// <param name="operation">The operation.</param>
> /// <returns></returns>
> public BranchAndReturnOperation Add(IOperation operation)
> {
> operations.Add(operation);
> return this;
> }
>
> public BranchAndReturnOperation AddBranch(params IOperation[]
> branchOperations)
> {
> PartialProcessOperation subProcess = new
> PartialProcessOperation();
>
> foreach (IOperation operation in branchOperations)
> subProcess.Register(operation);
>
> operations.Add(subProcess);
>
> return this;
> }
>
> /// <summary>
> /// Initializes this instance
> /// </summary>
> /// <param name="pipelineExecuter">The current pipeline
> executer.</
> param>
> public override void PrepareForExecution(IPipelineExecuter
> pipelineExecuter)
> {
> base.PrepareForExecution(pipelineExecuter);
> foreach (IOperation operation in operations)
> {
>
> operation.PrepareForExecution(pipelineExecuter);
> }
> }
>
> /// <summary>
> /// Executes this operation, sending the input of this
> operation
> /// to all its child operations
> /// </summary>
> /// <param name="rows">The rows.</param>
> /// <returns></returns>
> public override IEnumerable<Row> Execute(IEnumerable<Row>
> rows)
> {
> List<Row> copiedRows = new List<Row>(rows);
> foreach (IOperation operation in operations)
> {
> List<Row> cloned =
> copiedRows.ConvertAll<Row>(delegate(Row row)
> {
> return row.Clone();
> });
>
> IEnumerable<Row> enumerable = operation.Execute
> (cloned);
> foreach (Row row in enumerable)
> yield return row;
> }
> }
> }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Rhino Tools Dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rhino-tools-dev?hl=en
-~----------~----~----~----~------~----~------~--~---