

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# AND/OR 邏輯
<a name="and-or-logic"></a>

在篩選政策中使用 AND/OR 邏輯，以符合 Amazon SNS 中的訊息屬性或訊息內文屬性。這可實現更精確和靈活的訊息篩選。

## AND 邏輯
<a name="and-logic"></a>

您可以使用多個屬性名稱來套用 AND 邏輯。

舉例下列政策：

```
{
  "customer_interests": ["rugby"],
  "price_usd": [{"numeric": [">", 100]}]
}
```

它符合 `customer_interests` 的值設為 `rugby` *且* `price_usd` 的值設為大於 100 數字的任何訊息屬性或訊息內文屬性。

**注意**  
您無法將 AND 邏輯套用至相同屬性的值。

## OR 邏輯
<a name="or-logic"></a>

您可以將多個值指派給屬性名稱來套用 OR 邏輯。

舉例下列政策：

```
{
   "customer_interests": ["rugby", "football", "baseball"]
}
```

它符合 `customer_interests` 的值設為 `rugby`、`football` *或* `baseball` 的任何訊息屬性或訊息內文屬性。

## OR 運算子
<a name="or-operator"></a>

您可以使用 `"$or"` 運算子明確定義篩選政策，以表示政策中多個屬性之間的 OR 關係。

Amazon SNS 只會在政策符合下列所有條件時辨識 `"$or"` 關係。當所有這些條件都不符合時，`"$or"` 會被視為一般屬性名稱，與政策中的任何其他字串相同。
+ 例如 `“$or” : []`，規則中有一個 `"$or"` 欄位屬性，其後面有一個陣列。
+ `"$or"` 陣列中至少有兩個物件：`"$or": [{}, {}]`。
+ `"$or"` 陣列中沒有任何物件具有保留關鍵字的欄位名稱。

否則，`"$or"` 會被視為一般屬性名稱，與政策中的其他字串相同。

下列政策不會剖析為 OR 關係，因為數字和前置詞是保留的關鍵字。

```
{ 
   "$or": [ {"numeric" : 123}, {"prefix": "abc"} ] 
}
```

**`OR` 運算子範例**

標準`OR`：

```
{
  "source": [ "aws.cloudwatch" ], 
  "$or": [
    { "metricName": [ "CPUUtilization" ] },
    { "namespace": [ "AWS/EC2" ] }
  ] 
}
```

此政策的篩選邏輯為：

```
"source" && ("metricName" || "namespace")
```

它符合以下任一組訊息屬性：

```
"source": {"Type": "String", "Value": "aws.cloudwatch"},
"metricName": {"Type": "String", "Value": "CPUUtilization"}
```

或

```
"source": {"Type": "String", "Value": "aws.cloudwatch"},
"namespace": {"Type": "String", "Value": "AWS/EC2"}
```

它也符合以下任一訊息內文：

```
{
    "source": "aws.cloudwatch",
    "metricName": "CPUUtilization"
}
```

或

```
{
    "source": "aws.cloudwatch",
    "namespace": "AWS/EC2"
}
```

### 包含 `OR` 關係的政策限制
<a name="or-operator-constraints"></a>

舉例下列政策：

```
{
    "source": [ "aws.cloudwatch" ],
    "$or": [
      { "metricName": [ "CPUUtilization", "ReadLatency" ] },
      {
        "metricType": [ "MetricType" ] ,
        "$or" : [
          { "metricId": [ 1234, 4321 ] },
          { "spaceId": [ 1000, 2000, 3000 ] }
        ]
      }
    ]
  }
```

此政策的邏輯也可以簡化為：

```
("source" AND "metricName") 
OR 
("source" AND "metricType" AND "metricId") 
OR 
("source" AND "metricType" AND "spaceId")
```

具有 OR 關係的政策，複雜度計算可以簡化為每個 OR 陳述式的組合複雜性總和。

組合總計的計算如下：

```
(source * metricName) + (source * metricType * metricId) + (source * metricType * spaceId)
= (1 * 2) + (1 * 1 * 2) + (1 * 1 * 3) 
= 7
```

`source` 有一個值、`metricName` 有兩個值、`metricType` 有一個值、`metricId` 有兩個值和 `spaceId` 有三個值。

請考慮下列巢狀篩選政策：

```
{
    "$or": [
      { "metricName": [ "CPUUtilization", "ReadLatency" ] },
      { "namespace": [ "AWS/EC2", "AWS/ES" ] }
    ],
    "detail" : {
      "scope" : [ "Service" ],
      "$or": [
        { "source": [ "aws.cloudwatch" ] },
        { "type": [ "CloudWatch Alarm State Change"] }
      ]
    }
  }
```

此政策的邏輯可簡化為：

```
("metricName" AND ("detail"."scope" AND "detail"."source")
OR
("metricName" AND ("detail"."scope" AND "detail"."type")
OR
("namespace" AND ("detail"."scope" AND "detail"."source")
OR
("namespace" AND ("detail"."scope" AND "detail"."type")
```

對於非巢狀政策，組合總計的計算相同，除非我們需要考慮關鍵的巢狀等級。

組合總計的計算如下：

```
(2 * 2 * 2) + (2 * 2 * 2) + (2 * 2 * 2) + (2 * 2 * 2) = 32
```

`metricName` 有兩個值、`namespace` 有兩個值、`scope` 具有兩個巢狀索引鍵與一個值，`source` 具有兩個巢狀索引鍵與一個值，並且 `type` 具有兩個巢狀索引鍵與一個值。