Neutrino/python/ORCTest.ipynb

173 lines
7.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"source": [
"## DEVOUR Virtual Data Structure\r\n",
"Dynamic: The structure of the output DEVOUR graph is dynamically generated from raw input data\r\n",
"\r\n",
"Explicit: Every node in a DEVOUR graph is explicitly typed and namespaced for reliability and interoperability between programs\r\n",
"\r\n",
"Virtual: DEVOUR graphs contain their own serialized virtual memory space\r\n",
"\r\n",
"Ordered: A DEVOUR graph's virtual memory is structured as an ordered list for performance\r\n",
"\r\n",
"Unique: A DEVOUR graph contains only one instance of a given \"real\" value, mapped into its virtual memory\r\n",
"\r\n",
"Relational: A DEVOUR graph's virtual memory is referenced via indexed pointers within its data structure"
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 98,
"source": [
"class Graph:\r\n",
" def __init__(self):\r\n",
" self.graph = []\r\n",
" self.cache = {\r\n",
" \"shape\": [],\r\n",
" \"memory\": []\r\n",
" }\r\n",
"\r\n",
" # adds a new object to the graph\r\n",
" def add(self, node):\r\n",
" # add to graph\r\n",
" self.graph.append(node)\r\n",
"\r\n",
" def redirect_node(self, node):\r\n",
" shape = self.cache[\"shape\"]\r\n",
" memory = self.cache[\"memory\"]\r\n",
"\r\n",
" # for dictionaries, add each member node\r\n",
" if type(node) == dict:\r\n",
" # cache keys and values\r\n",
" new_dict = {}\r\n",
" for key, value in node.items():\r\n",
" if type(value) == dict:\r\n",
" value = self.redirect_node(value)\r\n",
"\r\n",
" # add dict key to memory\r\n",
" if key not in memory:\r\n",
" memory.append(key)\r\n",
" key_index = \"#\" + hex(len(memory) - 1)\r\n",
" else:\r\n",
" key_index = \"#\" + hex(memory.index(key))\r\n",
"\r\n",
" # add dict value to memory\r\n",
" if value not in memory:\r\n",
" memory.append(value)\r\n",
" value_index = \"#\" + hex(len(memory) - 1)\r\n",
" else:\r\n",
" value_index = \"#\" + hex(memory.index(value))\r\n",
"\r\n",
" new_dict[key_index] = value_index\r\n",
"\r\n",
" # check if the dict is in memory already\r\n",
" if new_dict in memory:\r\n",
" node = \"#\" + hex(memory.index(new_dict))\r\n",
" else:\r\n",
" node = new_dict\r\n",
"\r\n",
" # redirect object to virtual memory\r\n",
" if type(node) == str:\r\n",
" is_pointer = node[0] == \"#\"\r\n",
" else:\r\n",
" is_pointer = False\r\n",
" \r\n",
" if not is_pointer:\r\n",
" if node not in memory:\r\n",
" memory.append(node)\r\n",
" value_pointer = \"#\" + hex(len(memory) - 1)\r\n",
" else:\r\n",
" value_pointer = \"#\" + hex(memory.index(node))\r\n",
"\r\n",
" # add a redirector to shape\r\n",
" shape.append(value_pointer)\r\n",
"\r\n",
" # return a pointer to the node\r\n",
" return \"#\" + hex(len(memory) - 1)\r\n",
"\r\n",
" # exports the graph as a single dictionary object\r\n",
" def serialize(self):\r\n",
" # serialize DevourObject instances\r\n",
" for node in self.graph:\r\n",
" self.redirect_node(node)\r\n",
"\r\n",
" return self.cache"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 99,
"source": [
"# test\r\n",
"graph = Graph()\r\n",
"graph.add(1.0)\r\n",
"graph.add(1.0)\r\n",
"graph.add(3.0)\r\n",
"graph.add(3.0)\r\n",
"graph.add(1.0)\r\n",
"graph.add({\r\n",
" \"name\": \"TestObject\",\r\n",
" \"visible\": True\r\n",
" })\r\n",
"graph.add({\r\n",
" \"name\": \"TestObject\",\r\n",
" \"visible\": True\r\n",
" })\r\n",
"graph.add({\r\n",
" \"name\": \"TestObject\",\r\n",
" \"visible\": True,\r\n",
" \"transform\": {\r\n",
" \"position\": [0.0, 0.0, 0.0],\r\n",
" \"rotation\": [0.0, 0.0, 0.0],\r\n",
" \"scale\": [1.0, 1.0, 1.0]\r\n",
" }\r\n",
" })\r\n",
"graph.add({\r\n",
" \"name\": \"TestObject\",\r\n",
" \"visible\": True,\r\n",
" \"transform\": {\r\n",
" \"position\": [0.0, 0.0, 0.0],\r\n",
" \"rotation\": [0.0, 0.0, 0.0],\r\n",
" \"scale\": [1.0, 1.0, 1.0]\r\n",
" }\r\n",
" })\r\n",
"\r\n",
"# write to disk\r\n",
"import json\r\n",
"with open(\"DEVOURTest.json\", \"w\") as outfile:\r\n",
" json.dump(graph.serialize(), outfile, indent = 4)"
],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"orig_nbformat": 4,
"language_info": {
"name": "python",
"version": "3.7.8",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.7.8 64-bit"
},
"interpreter": {
"hash": "57baa5815c940fdaff4d14510622de9616cae602444507ba5d0b6727c008cbd6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}