Hi all,
I'm trying to create custom GQI operator that would remove all rows except a single, random one. I have a working example in which I'm using a random generator to determine which row I would like to keep. However, I need to know upfront how many rows there are in the source, so my random index doesn't go out of bounds. In my test version I'm providing that row count as an argument, which wouldn't make sense in a real situation.
Below is my code. If you feel like there is a better way to tackle this please let me know. Also, I don't want to go an create custom data source just for this use case.
Kind regards,
public class RandomRowOperator : IGQIRowOperator, IGQIInputArguments
{
private GQIIntArgument rowCountArgument = new GQIIntArgument("Row Count") { IsRequired = true };
private GQIIntArgument seedArgument = new GQIIntArgument("Seed") { IsRequired = false };private int _rowIndex;
public GQIArgument[] GetInputArguments()
{
return new GQIArgument[] {
rowCountArgument,
seedArgument
};
}public void HandleRow(GQIEditableRow row)
{
if (_rowIndex != 0)
{
row.Delete();
}_rowIndex--;
}public OnArgumentsProcessedOutputArgs OnArgumentsProcessed(OnArgumentsProcessedInputArgs args)
{
int rowCount = args.GetArgumentValue(rowCountArgument);Random random;
if (args.HasArgumentValue(seedArgument))
{
int seed = args.GetArgumentValue(seedArgument);
random = new Random(seed);
}
else
{
random = new Random();
}_rowIndex = random.Next(rowCount);
return new OnArgumentsProcessedOutputArgs();
}
}
Hi Thomas,
To get rid of the input parameters, you could add an extra column with a random number in your custom operator. Then you could use that new column to sort and take the first row.