Update get_machine_from_json.py
Browse files- get_machine_from_json.py +49 -50
get_machine_from_json.py
CHANGED
|
@@ -1,50 +1,49 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
new_clean_dict["
|
| 13 |
-
new_clean_dict["
|
| 14 |
-
new_clean_dict["
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
new_clean_dict["
|
| 18 |
-
new_clean_dict["
|
| 19 |
-
new_clean_dict["
|
| 20 |
-
new_clean_dict["
|
| 21 |
-
new_clean_dict["
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
machine_json =
|
| 43 |
-
|
| 44 |
-
xml_string
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 1 |
+
import utils as dp # 使用别名dp,方便调用
|
| 2 |
+
from utils import extract_json_from_string
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def format_json(input_json):
|
| 6 |
+
try:
|
| 7 |
+
new_clean_json=[]
|
| 8 |
+
for json_info in input_json:
|
| 9 |
+
new_clean_dict={}
|
| 10 |
+
if int(json_info["id"]) not in [7,9]:
|
| 11 |
+
new_clean_dict["id"]=str(json_info["id"])
|
| 12 |
+
new_clean_dict["order_id"]=int(json_info["order_id"])
|
| 13 |
+
new_clean_dict["parent"]=int(json_info["parent"])
|
| 14 |
+
new_clean_dict["bp_id"]=int(json_info["bp_id"])
|
| 15 |
+
else:
|
| 16 |
+
new_clean_dict["id"]=str(json_info["id"])
|
| 17 |
+
new_clean_dict["order_id"]=int(json_info["order_id"])
|
| 18 |
+
new_clean_dict["parent_a"]=int(json_info["parent_a"])
|
| 19 |
+
new_clean_dict["bp_id_a"]=int(json_info["bp_id_a"])
|
| 20 |
+
new_clean_dict["parent_b"]=int(json_info["parent_b"])
|
| 21 |
+
new_clean_dict["bp_id_b"]=int(json_info["bp_id_b"])
|
| 22 |
+
new_clean_json.append(new_clean_dict)
|
| 23 |
+
return new_clean_json
|
| 24 |
+
except:
|
| 25 |
+
return input_json
|
| 26 |
+
|
| 27 |
+
def get_machine_from_json(solution_str,step,score,save_root):
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
tmp_path = Rf"{save_root}/step{step}score{score}.bsg"
|
| 31 |
+
solution_str =solution_str.replace("", "").replace("\\", "")
|
| 32 |
+
machine_json = extract_json_from_string(solution_str)
|
| 33 |
+
|
| 34 |
+
machine_json = format_json(machine_json)
|
| 35 |
+
dp.json_to_xml(machine_json,tmp_path)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def string_to_bsg(solution_str):
|
| 39 |
+
solution_str =solution_str.replace("", "").replace("\\", "")
|
| 40 |
+
|
| 41 |
+
machine_json = extract_json_from_string(solution_str)
|
| 42 |
+
machine_json = format_json(machine_json)
|
| 43 |
+
xml_string = dp.json_to_xml(machine_json)
|
| 44 |
+
return xml_string
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
|
|
|