Ting Xu | ce4b645 | 2022-04-24 06:14:25 +0000 | [diff] [blame] | 1 | # Copyright (c) 2022 Intel and/or its affiliates. |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at: |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
| 14 | from EdgeAction import * |
| 15 | import json |
| 16 | |
| 17 | |
| 18 | class Edge: |
| 19 | def __init__(self): |
| 20 | self.JSON = None |
| 21 | self.Start = None |
| 22 | self.End = None |
| 23 | self.actionList = [] |
| 24 | |
| 25 | def Create(jsonfile): |
| 26 | f = open(jsonfile, "r", encoding="utf-8") |
| 27 | token = json.load(f) |
| 28 | |
| 29 | if token == None: |
| 30 | return None |
| 31 | |
| 32 | if token["type"] != "edge": |
| 33 | return None |
| 34 | |
| 35 | edgeList = [] |
| 36 | |
| 37 | startNodes = token["start"] |
| 38 | endNodes = token["end"] |
| 39 | |
| 40 | if startNodes == None or endNodes == None: |
| 41 | return None |
| 42 | |
| 43 | startTokens = startNodes.split(",") |
| 44 | endTokens = endNodes.split(",") |
| 45 | |
| 46 | for start in startTokens: |
| 47 | for end in endTokens: |
Ting Xu | ce4b645 | 2022-04-24 06:14:25 +0000 | [diff] [blame] | 48 | edge = Edge() |
| 49 | |
| 50 | edge.Start = start |
| 51 | edge.End = end |
| 52 | |
| 53 | if "actions" in token: |
| 54 | for at in token["actions"]: |
| 55 | action = EdgeAction.Create(at) |
| 56 | if not action: |
| 57 | return None |
| 58 | |
| 59 | edge.actionList.append(action) |
| 60 | |
| 61 | edge.JSON = jsonfile |
| 62 | edgeList.append(edge) |
| 63 | |
| 64 | return edgeList |
| 65 | |
| 66 | def Apply(self, first, second): |
| 67 | exp = [] |
| 68 | |
| 69 | for i in range(len(self.actionList)): |
| 70 | act = self.actionList[i] |
| 71 | |
| 72 | if act.FromStartObject == True: |
| 73 | exp.append(first.GetValue(act.FromExpression)) |
| 74 | elif act.FromStartObject == False: |
| 75 | exp.append(second.GetValue(act.FromExpression)) |
| 76 | else: |
| 77 | exp.append(act.FromExpression) |
| 78 | |
| 79 | for i in range(len(exp)): |
| 80 | act = self.actionList[i] |
| 81 | |
| 82 | if act.ToStartObject: |
| 83 | first.SetFieldAuto(act.ToExpression, exp[i]) |
| 84 | else: |
| 85 | second.SetFieldAuto(act.ToExpression, exp[i]) |
| 86 | |
| 87 | def Actions(self): |
| 88 | return self.actionList |
| 89 | |
| 90 | def Name(self): |
| 91 | return self.Start + "_" + self.End |