Aalto-LeTech / JAAL

A data format for recording answers to JSAV exercises

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

From arrays to dictionaries

atilante opened this issue · comments

This is a future idea for JAAL 2.1.

Modify the data structure specifications: when a data structure contains an array of data structures, those could be a dictionary instead.

Current example:

{
  "description": "An undirected, weighted graph.",
  "id": "graph1",
  "node": [
    { "id": "nodeA", "key": "A" },
    { "id": "nodeB", "key": "B" }
  ],
  "edge": [
    {
      "id": "edgeAB",
      "node": [ "nodeA", "nodeB" ],
      "tag": "2"
    },
    {
      "id": "edgeBA",
      "node": [ "nodeB", "nodeA" ],
      "tag": "2"
    }
  ],
  "directed": true,
  "dsClass": "graph",
  "dsSubClass": "weighted"
}

Improved example:

{
  "description": "An undirected, weighted graph.",
  "id": "graph1",
  "node": {
    "nodeA": { "key": "A" },
    "nodeB": { "key": "B" }
  },
  "edge": {
    "edgeAB": {
      "node": [ "nodeA", "nodeB" ],
      "tag": "2"
    },
    "edgeBA": {
      "node": [ "nodeB", "nodeA" ],
      "tag": "2"
    }
  },
  "directed": true,
  "dsClass": "graph",
  "dsSubClass": "weighted"
}

What changed properties node and edge are now objects in JSON Schema terms, not arrays.

Why this is beneficial: when the JAAL data is accessed in JavaScript or Python, now we can access a node by graph.node[nodeId] instead of having to scan linearly an array graph.node. This would eliminate the need to build a dictionary from an array. Result: cleaner data, faster JAAL analysis code.

How to implement: https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties

Essentially, we want to eliminate this kind of code in the JAAL analysis phase:

    # Create a mapping from edge ID to the edge for easy access
    edge_by_id = dict()
    for edge in jaal['initialState']['dataStructures'][0]['edge']:
        edge_by_id[edge['id']] = edge