简体   繁体   English

Azure 逻辑应用中的随机列表

[英]Randomize list in Azure Logic Apps

是否可以仅使用逻辑应用程序中的 WDL 函数来随机化数组的元素?

Let's imagine you have array from 1 to 10假设你有 1 到 10 的数组

在此处输入图片说明

You create one lement randomized array variable您创建一个元素随机数组变量

first(variables('Array'))

在此处输入图片说明

And a temp value ( tempValue ) and temp array ( temp ) (used in later steps because self referencing is not allowed)还有一个临时值 ( tempValue ) 和临时数组 ( temp )(在后面的步骤中使用,因为不允许自引用)

在此处输入图片说明

Then you create until loop like so然后你像这样创建直到循环

在此处输入图片说明

With Until condition being直到条件为

equals(length(variables('Randomized')), length(variables('Array')))

Calculate random index tempValue in array (random index at which we will split array)计算数组中的随机索引tempValue (我们将拆分数组的随机索引)

rand(0,sub(length('Randomized'),1))

And combine new array temp by inserting current iteration value at random place并通过在随机位置插入当前迭代值来组合新的数组temp

union(
  take(
    variables('Randomized'),
    variables('tempInteger')
  ),
  array(variables('Array')[add(iterationIndexes('Until'),1)]),
  skip(
    variables('Randomized'),
    variables('tempInteger')
  )
)

And after condition statement set randomized variable to temp value并在条件语句后将随机变量设置为临时

Which after the last iteration will net you randomized input table在最后一次迭代之后,您将获得随机输入表

在此处输入图片说明

And full code example和完整的代码示例

{
  "definition": {
    "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    "actions": {
      "Init_Randomized_One_Element_Array": {
        "inputs": {
          "variables": [
            {
              "name": "Randomized",
              "type": "Array",
              "value": [
                "@first(variables('Array'))"
              ]
            }
          ]
        },
        "runAfter": {
          "Init_Unrandomized_Array": [
            "Succeeded"
          ]
        },
        "type": "InitializeVariable"
      },
      "Init_Randomized_Temp_Array": {
        "inputs": {
          "variables": [
            {
              "name": "temp",
              "type": "Array"
            }
          ]
        },
        "runAfter": {
          "Init_Randomized_One_Element_Array": [
            "Succeeded"
          ]
        },
        "type": "InitializeVariable"
      },
      "Init_Temp_Calculated_Value": {
        "inputs": {
          "variables": [
            {
              "name": "tempInteger",
              "type": "integer"
            }
          ]
        },
        "runAfter": {
          "Init_Randomized_Temp_Array": [
            "Succeeded"
          ]
        },
        "type": "InitializeVariable"
      },
      "Init_Unrandomized_Array": {
        "inputs": {
          "variables": [
            {
              "name": "Array",
              "type": "Array",
              "value": [
                1,
                2,
                3,
                4,
                5,
                6,
                7,
                8,
                9,
                10
              ]
            }
          ]
        },
        "runAfter": {},
        "type": "InitializeVariable"
      },
      "Until": {
        "actions": {
          "Add_random_element_to_temp_array": {
            "inputs": {
              "name": "temp",
              "value": "@union(\r\n\ttake(\r\n\t\t   variables('Randomized'),\r\n\t\t   variables('tempInteger')\r\n\t),\r\n\tarray(variables('Array')[add(iterationIndexes('Until'),1)]),\r\n\tskip(\r\n\t   variables('Randomized'),\r\n\t   variables('tempInteger')\r\n\t)\r\n)\r\n"
            },
            "runAfter": {
              "Random_Index_to_insert": [
                "Succeeded"
              ]
            },
            "type": "SetVariable"
          },
          "Random_Index_to_insert": {
            "inputs": {
              "name": "tempInteger",
              "value": "@rand(0,sub(length('Randomized'),1))"
            },
            "runAfter": {},
            "type": "SetVariable"
          },
          "Set_randomized_from_temp": {
            "inputs": {
              "name": "Randomized",
              "value": "@variables('temp')"
            },
            "runAfter": {
              "Add_random_element_to_temp_array": [
                "Succeeded"
              ]
            },
            "type": "SetVariable"
          }
        },
        "expression": "@equals(length(variables('Randomized')), length(variables('Array')))",
        "limit": {
          "count": 60,
          "timeout": "PT1H"
        },
        "runAfter": {
          "Init_Temp_Calculated_Value": [
            "Succeeded"
          ]
        },
        "type": "Until"
      }
    },
    "contentVersion": "1.0.0.0",
    "outputs": {},
    "parameters": {},
    "triggers": {
      "manual": {
        "inputs": {
          "schema": {}
        },
        "kind": "Http",
        "type": "Request"
      }
    }
  }
}

There is no direct way to randomize anything in a LogicApp.没有直接的方法来随机化 LogicApp 中的任何内容。

I suppose you could come up with some pattern that could pseudorandomize a list entirely within a Logic App, but....我想你可以想出一些可以在逻辑应用程序中完全伪随机化列表的模式,但是......

The more 'correct' way to handle this would be with a Function.处理这个更“正确”的方法是使用函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM