File size: 2,278 Bytes
7cec401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import pickle
import networkx as nx
import matplotlib.pyplot as plt
# Define file paths
JSON = './behaviour177.json'
PKL = 'behaviour177.pkl'

def add_edges(G):
  for node_name in G.nodes:
    node = G.nodes[node_name]
    match node['type']:
      case 'place':
        G.add_edge(node_name,('scene',1))
      case 'asset':
        G.add_edge(node_name,node['place'])
      case 'object':
        G.add_edge(node_name,node['related_to'])
      case 'agent':
        G.add_edge(node_name,node['location'])
      case 'scene':
        pass
      case _:
        print(f"Unknown node type: {node['type']}")

def parse_json(tasks):
    graph = nx.Graph()
    graph.add_node(('scene',1), name =task['name'],  type='scene')
    for node in tasks:
        if node['type'] == 'place':
            graph.add_node((node['name'], node['id']), type='place')
        if node['type'] == 'asset':
            graph.add_node((node['name'], node['id']), place=(node['place'],node['place_id']), states=node['states'], affordances =node['affordances'], properties=[], type = 'asset')
        if node['type'] == 'object':
            graph.add_node((node['name'], node['id']), relation=node['relation'], related_to=(node['related_to'][0],node['related_to'][1]), states=node['states'], affordances =node['affordances'], properties=[], type = 'object')
        if node['type'] == 'agent':
            graph.add_node(('agent',1), location=(node['location'][0],node['location'][1]), holding='', type='agent')
    add_edges(graph)
    return graph

if __name__ == "__main__":
    # Load JSON data
    with open(JSON, 'r') as json_file:
        input_data = json.load(json_file)

    output_data = []
    i=0
    for task in input_data:
        i+=1
        init_graph = parse_json(task['init'])
        goal_graph = parse_json(task['goal'])
        entity = {
            'id' : i,
            'name':task['name'],
            'task':task['task'],
            'init': init_graph,
            'goal': goal_graph,
        }
        output_data.append(entity)
    # Save data to a pickle file
    with open(PKL, 'wb') as pkl_file:
        pickle.dump(output_data, pkl_file)

    print(f"Data from {JSON} has been saved to {PKL}.")