{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# polymake file format and the polyDB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Polymake Data Files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### JSON File Format"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "With release 4.0 polymake adopts a new file format for its data files. \n",
    "In the new version all data is now serialized to JSON instead of XML."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "polymake can still read files in XML format."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "$p = new Polytope(POINTS=>[[1,0,0],[1,1,0],[1,0,1]]);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"_ns\" : {\n",
       "    \"polymake\" : [\n",
       "      \"https://polymake.org\",\n",
       "      \"4.0\"\n",
       "    ]\n",
       "  },\n",
       "  \"POINTS\" : [\n",
       "    [\n",
       "      \"1\",\n",
       "      \"0\",\n",
       "      \"0\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"1\",\n",
       "      \"0\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"0\",\n",
       "      \"1\"\n",
       "    ]\n",
       "  ],\n",
       "  \"_id\" : \"p\",\n",
       "  \"CONE_AMBIENT_DIM\" : 3,\n",
       "  \"_type\" : \"polytope::Polytope<Rational>\"\n",
       "}\n"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode(Core::Serializer::serialize($p));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "JSON is a pretty simple format. Entries in a JSON object are either\n",
    " * number: no distinction between float and int\n",
    " * string\n",
    " * boolean: true/false\n",
    " * array: comma separated list of elements in square brackets\n",
    " * object: comma separeted key-value pairs in curly brackets. associative array\n",
    " * null"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Polymake stores \n",
    " * perl builtin numbers a number\n",
    " * vectors, matrices as array\n",
    " * tuples as object\n",
    " * Integer/Rartional as string\n",
    " * Bool as boolean\n",
    " * big objects as objects (with properties as keys)\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Type information that cannot be infered from the rule base (sparse, attachment) is stored in an separate key-value pair."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "There are only a few meta tags left in a data file:\n",
    " * _id: name\n",
    " * _ns: mark it as a polymake file\n",
    " * _info: credits and description\n",
    " * _type: big object type\n",
    " * _attrs: additional attributes for properties, marks e.g. attachments, non-standard types (e.g. sparse)\n",
    " * _ext: extensions\n",
    " * _load: further applications needed to read data file\n",
    " * _polyDB: database information, if object is retrieved from db"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Object Arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "You can now use standard perl arrays to store a list of objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "$cube = cube(2);\n",
    "$simplex = simplex(2);\n",
    "$array = [ $cube, $simplex ];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"data\" : [\n",
       "    {\n",
       "      \"_type\" : \"polytope::Polytope<Rational>\",\n",
       "      \"_info\" : {\n",
       "        \"description\" : \"cube of dimension 2\\n\"\n",
       "      },\n",
       "      \"FACETS\" : [\n",
       "        {\n",
       "          \"0\" : \"1\",\n",
       "          \"1\" : \"1\"\n",
       "        },\n",
       "        {\n",
       "          \"0\" : \"1\",\n",
       "          \"1\" : \"-1\"\n",
       "        },\n",
       "        {\n",
       "          \"0\" : \"1\",\n",
       "          \"2\" : \"1\"\n",
       "        },\n",
       "        {\n",
       "          \"0\" : \"1\",\n",
       "          \"2\" : \"-1\"\n",
       "        },\n",
       "        {\n",
       "          \"cols\" : 3\n",
       "        }\n",
       "      ],\n",
       "      \"VERTICES_IN_FACETS\" : [\n",
       "        [\n",
       "          0,\n",
       "          2\n",
       "        ],\n",
       "        [\n",
       "          1,\n",
       "          3\n",
       "        ],\n",
       "        [\n",
       "          0,\n",
       "          1\n",
       "        ],\n",
       "        [\n",
       "          2,\n",
       "          3\n",
       "        ],\n",
       "        {\n",
       "          \"cols\" : 4\n",
       "        }\n",
       "      ],\n",
       "      \"_attrs\" : {\n",
       "        \"FACETS\" : {\n",
       "          \"_type\" : \"SparseMatrix<Rational, NonSymmetric>\"\n",
       "        }\n",
       "      },\n",
       "      \"BOUNDED\" : true,\n",
       "      \"CONE_AMBIENT_DIM\" : 3,\n",
       "      \"CONE_DIM\" : 3,\n",
       "      \"AFFINE_HULL\" : [\n",
       "        {\n",
       "          \"cols\" : 3\n",
       "        }\n",
       "      ],\n",
       "      \"_id\" : \"cube\"\n",
       "    },\n",
       "    {\n",
       "      \"_id\" : \"simplex\",\n",
       "      \"CONE_DIM\" : 3,\n",
       "      \"N_VERTICES\" : 3,\n",
       "      \"CONE_AMBIENT_DIM\" : 3,\n",
       "      \"BOUNDED\" : true,\n",
       "      \"_attrs\" : {\n",
       "        \"VERTICES\" : {\n",
       "          \"_type\" : \"SparseMatrix<Rational, NonSymmetric>\"\n",
       "        }\n",
       "      },\n",
       "      \"POINTED\" : true,\n",
       "      \"FEASIBLE\" : true,\n",
       "      \"_info\" : {\n",
       "        \"description\" : \"standard simplex of dimension 2\\n\"\n",
       "      },\n",
       "      \"CENTERED\" : false,\n",
       "      \"SIMPLICIALITY\" : 2,\n",
       "      \"_type\" : \"polytope::Polytope<Rational>\",\n",
       "      \"VERTICES\" : [\n",
       "        {\n",
       "          \"0\" : \"1\"\n",
       "        },\n",
       "        {\n",
       "          \"0\" : \"1\",\n",
       "          \"1\" : \"1\"\n",
       "        },\n",
       "        {\n",
       "          \"0\" : \"1\",\n",
       "          \"2\" : \"1\"\n",
       "        },\n",
       "        {\n",
       "          \"cols\" : 3\n",
       "        }\n",
       "      ]\n",
       "    }\n",
       "  ],\n",
       "  \"_type\" : null,\n",
       "  \"_ns\" : {\n",
       "    \"polymake\" : [\n",
       "      \"https://polymake.org\",\n",
       "      \"4.0\"\n",
       "    ]\n",
       "  }\n",
       "}\n"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode(Core::Serializer::serialize($array));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Background: JSON Schemata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "$p = new Polytope(VERTICES=>[[1,0,0],[1,1,0],[1,0,1]]);\n",
    "$p->N_VERTICES;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "cell_style": "center",
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$schema = create_restrictive_schema($p);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"definitions\" : {\n",
       "    \"common-Matrix-Rational-NonSymmetric\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"oneOf\" : [\n",
       "          {\n",
       "            \"$ref\" : \"#/definitions/common-Vector-Rational\"\n",
       "          },\n",
       "          {\n",
       "            \"properties\" : {\n",
       "              \"cols\" : {\n",
       "                \"type\" : \"integer\",\n",
       "                \"minimum\" : 0\n",
       "              }\n",
       "            },\n",
       "            \"required\" : [\n",
       "              \"cols\"\n",
       "            ],\n",
       "            \"type\" : \"object\",\n",
       "            \"additionalProperties\" : false\n",
       "          }\n",
       "        ]\n",
       "      }\n",
       "    },\n",
       "    \"common-Rational\" : {\n",
       "      \"type\" : \"string\",\n",
       "      \"pattern\" : \"^-?(\\\\d+(/\\\\d+)?|inf)$\"\n",
       "    },\n",
       "    \"common-Vector-Rational\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Rational\"\n",
       "      }\n",
       "    },\n",
       "    \"common-Int\" : {\n",
       "      \"type\" : \"integer\"\n",
       "    }\n",
       "  },\n",
       "  \"additionalProperties\" : false,\n",
       "  \"required\" : [\n",
       "    \"_ns\",\n",
       "    \"_type\",\n",
       "    \"VERTICES\",\n",
       "    \"CONE_AMBIENT_DIM\",\n",
       "    \"N_VERTICES\"\n",
       "  ],\n",
       "  \"properties\" : {\n",
       "    \"_ns\" : {\n",
       "      \"additionalProperties\" : false,\n",
       "      \"properties\" : {\n",
       "        \"polymake\" : {\n",
       "          \"items\" : [\n",
       "            {\n",
       "              \"const\" : \"https://polymake.org\"\n",
       "            },\n",
       "            {\n",
       "              \"const\" : \"4.0\"\n",
       "            }\n",
       "          ],\n",
       "          \"additionalItems\" : false,\n",
       "          \"type\" : \"array\"\n",
       "        }\n",
       "      },\n",
       "      \"type\" : \"object\"\n",
       "    },\n",
       "    \"_type\" : {\n",
       "      \"const\" : \"polytope::Polytope<Rational>\"\n",
       "    },\n",
       "    \"VERTICES\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Matrix-Rational-NonSymmetric\"\n",
       "    },\n",
       "    \"_info\" : {\n",
       "      \"$ref\" : \"https://polymake.org/schemas/data.json#/definitions/obj_info\"\n",
       "    },\n",
       "    \"CONE_AMBIENT_DIM\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"N_VERTICES\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"_canonical\" : {\n",
       "      \"type\" : \"boolean\"\n",
       "    },\n",
       "    \"_load\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"type\" : \"string\"\n",
       "      }\n",
       "    },\n",
       "    \"_id\" : {\n",
       "      \"$ref\" : \"https://polymake.org/schemas/data.json#/definitions/obj_id\"\n",
       "    }\n",
       "  },\n",
       "  \"type\" : \"object\",\n",
       "  \"$schema\" : \"http://json-schema.org/draft-07/schema#\"\n",
       "}\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode($schema->source);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $schema->validate(Core::Serializer::serialize($p));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use a schema to modify a data file so that it validates against the schema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$q = new Polytope(VERTICES=>[[1,0,0],[1,1,0],[1,0,1],[1,1,1]]);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"_type\" : \"polytope::Polytope<Rational>\",\n",
       "  \"VERTICES\" : [\n",
       "    [\n",
       "      \"1\",\n",
       "      \"0\",\n",
       "      \"0\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"1\",\n",
       "      \"0\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"0\",\n",
       "      \"1\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"1\",\n",
       "      \"1\"\n",
       "    ]\n",
       "  ],\n",
       "  \"CONE_AMBIENT_DIM\" : 3,\n",
       "  \"_ns\" : {\n",
       "    \"polymake\" : [\n",
       "      \"https://polymake.org\",\n",
       "      \"4.0\"\n",
       "    ]\n",
       "  },\n",
       "  \"_id\" : \"q\"\n",
       "}\n"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode(Core::Serializer::serialize($q));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"VERTICES\" : [\n",
       "    [\n",
       "      \"1\",\n",
       "      \"0\",\n",
       "      \"0\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"1\",\n",
       "      \"0\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"0\",\n",
       "      \"1\"\n",
       "    ],\n",
       "    [\n",
       "      \"1\",\n",
       "      \"1\",\n",
       "      \"1\"\n",
       "    ]\n",
       "  ],\n",
       "  \"_type\" : \"polytope::Polytope<Rational>\",\n",
       "  \"N_VERTICES\" : 4,\n",
       "  \"CONE_AMBIENT_DIM\" : 3,\n",
       "  \"_ns\" : {\n",
       "    \"polymake\" : [\n",
       "      \"https://polymake.org\",\n",
       "      \"4.0\"\n",
       "    ]\n",
       "  },\n",
       "  \"_id\" : \"q\"\n",
       "}\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode(Core::Serializer::serialize($q, { schema => $schema } ));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "core/objects/Schema/methods/prescribe_property_types:\n",
       "prescribe_property_types(properties;  ... )\n",
       "\n",
       " Change the stored data type for properties in a restrictive schema\n",
       "\n",
       " Selected properties will be coerced to a different type when the object is stored in a data file\n",
       " or in PolyDB using this schema.\n",
       "\n",
       " Note: This function does not try to verify that the properties can be serialized as the given type\n",
       " or will be successfully deserialized when reading the object from a file or retrieving it from a database.\n",
       " It's solely in the users responsibility to choose a compatible and convertible data type.\n",
       "\n",
       "Arguments:\n",
       "  Any \u001b[4mproperties\u001b[24m list of pairs PROPERTY_NAME => type\n",
       "    A property in a subobject is written in dotted path notation: \"NAME1.NAME2.NAME3...\"\n",
       "    Types can be specified by name in a string or as a typeof expression or as a type of an exsiting data object.\n",
       "    Specifying undef or \"default\" will enforce storing the properties in their original form, as declared in the rulebase.\n",
       "    This operation might be useful e.g. to update the schema after a data mode change.\n",
       "\n",
       "Examples:\n",
       "\n",
       "*) Require FACETS to be stored as a sparse matrix\n",
       "   > $schema->prescribe_property_types(FACETS => typeof SparseMatrix<Rational>);\n",
       "*) Require F_VECTOR and F2_VECTOR to be stored with simple integer entries\n",
       "   > $schema->prescribe_property_types(F_VECTOR => \"Vector<Int>\", F2_VECTOR => \"Matrix<Int>\");\n",
       "*) Require VERTICES to be stored as declared in the rules, that is, as a dense matrix\n",
       "   > $schema->prescribe_property_types(VERTICES => \"default\");\n"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "help \"prescribe_property_types\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We can change the type of VERTICES in our schema:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$schema->prescribe_property_types(VERTICES => typeof SparseMatrix<Rational>);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"VERTICES\" : [\n",
       "    {\n",
       "      \"0\" : \"1\"\n",
       "    },\n",
       "    {\n",
       "      \"0\" : \"1\",\n",
       "      \"1\" : \"1\"\n",
       "    },\n",
       "    {\n",
       "      \"0\" : \"1\",\n",
       "      \"2\" : \"1\"\n",
       "    },\n",
       "    [\n",
       "      \"1\",\n",
       "      \"1\",\n",
       "      \"1\"\n",
       "    ],\n",
       "    {\n",
       "      \"cols\" : 3\n",
       "    }\n",
       "  ],\n",
       "  \"_type\" : \"polytope::Polytope<Rational>\",\n",
       "  \"_attrs\" : {\n",
       "    \"VERTICES\" : {\n",
       "      \"_type\" : \"SparseMatrix<Rational, NonSymmetric>\"\n",
       "    }\n",
       "  },\n",
       "  \"N_VERTICES\" : 4,\n",
       "  \"CONE_AMBIENT_DIM\" : 3,\n",
       "  \"_id\" : \"q\",\n",
       "  \"_ns\" : {\n",
       "    \"polymake\" : [\n",
       "      \"https://polymake.org\",\n",
       "      \"4.0\"\n",
       "    ]\n",
       "  }\n",
       "}\n"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode(Core::Serializer::serialize($q, { schema => $schema } ));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We can also create a schema for a complete big object type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "$full_schema=create_permissive_schema(typeof graph::Graph);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"$schema\" : \"http://json-schema.org/draft-07/schema#\",\n",
       "  \"definitions\" : {\n",
       "    \"common-Array-Int\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Int\"\n",
       "      }\n",
       "    },\n",
       "    \"common-SparseVector-Int\" : {\n",
       "      \"type\" : [\n",
       "        \"array\",\n",
       "        \"object\"\n",
       "      ],\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Int\"\n",
       "      },\n",
       "      \"additionalProperties\" : false,\n",
       "      \"patternProperties\" : {\n",
       "        \"^\\\\d+$\" : {\n",
       "          \"$ref\" : \"#/definitions/common-Int\"\n",
       "        }\n",
       "      },\n",
       "      \"properties\" : {\n",
       "        \"_dim\" : {\n",
       "          \"minimum\" : 0,\n",
       "          \"type\" : \"integer\"\n",
       "        }\n",
       "      }\n",
       "    },\n",
       "    \"common-Int\" : {\n",
       "      \"type\" : \"integer\"\n",
       "    },\n",
       "    \"common-SparseVector-Float\" : {\n",
       "      \"patternProperties\" : {\n",
       "        \"^\\\\d+$\" : {\n",
       "          \"$ref\" : \"#/definitions/common-Float\"\n",
       "        }\n",
       "      },\n",
       "      \"additionalProperties\" : false,\n",
       "      \"properties\" : {\n",
       "        \"_dim\" : {\n",
       "          \"minimum\" : 0,\n",
       "          \"type\" : \"integer\"\n",
       "        }\n",
       "      },\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Float\"\n",
       "      },\n",
       "      \"type\" : [\n",
       "        \"array\",\n",
       "        \"object\"\n",
       "      ]\n",
       "    },\n",
       "    \"common-Float\" : {\n",
       "      \"type\" : [\n",
       "        \"number\",\n",
       "        \"string\"\n",
       "      ],\n",
       "      \"pattern\" : \"^-?[Ii]nf$\"\n",
       "    },\n",
       "    \"common-Vector-Float\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Float\"\n",
       "      }\n",
       "    },\n",
       "    \"common-HashMap-Int-Rational\" : {\n",
       "      \"additionalProperties\" : false,\n",
       "      \"patternProperties\" : {\n",
       "        \"^-?\\\\d+$\" : {\n",
       "          \"$ref\" : \"#/definitions/common-Rational\"\n",
       "        }\n",
       "      },\n",
       "      \"type\" : \"object\"\n",
       "    },\n",
       "    \"common-Bool\" : {\n",
       "      \"type\" : \"boolean\"\n",
       "    },\n",
       "    \"common-String\" : {\n",
       "      \"type\" : \"string\"\n",
       "    },\n",
       "    \"common-Set-Int\" : {\n",
       "      \"uniqueItems\" : true,\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Int\"\n",
       "      },\n",
       "      \"type\" : \"array\"\n",
       "    },\n",
       "    \"common-SparseMatrix-Int-NonSymmetric\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"oneOf\" : [\n",
       "          {\n",
       "            \"$ref\" : \"#/definitions/common-SparseVector-Int\"\n",
       "          },\n",
       "          {\n",
       "            \"additionalProperties\" : false,\n",
       "            \"properties\" : {\n",
       "              \"cols\" : {\n",
       "                \"minimum\" : 0,\n",
       "                \"type\" : \"integer\"\n",
       "              }\n",
       "            },\n",
       "            \"type\" : \"object\",\n",
       "            \"required\" : [\n",
       "              \"cols\"\n",
       "            ]\n",
       "          }\n",
       "        ]\n",
       "      }\n",
       "    },\n",
       "    \"common-UniPolynomial-Rational-Int\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Serialized-UniPolynomial-Rational-Int\"\n",
       "    },\n",
       "    \"common-IncidenceMatrix-NonSymmetric\" : {\n",
       "      \"items\" : {\n",
       "        \"oneOf\" : [\n",
       "          {\n",
       "            \"$ref\" : \"#/definitions/common-Set-Int\"\n",
       "          },\n",
       "          {\n",
       "            \"required\" : [\n",
       "              \"cols\"\n",
       "            ],\n",
       "            \"additionalProperties\" : false,\n",
       "            \"properties\" : {\n",
       "              \"cols\" : {\n",
       "                \"type\" : \"integer\",\n",
       "                \"minimum\" : 0\n",
       "              }\n",
       "            },\n",
       "            \"type\" : \"object\"\n",
       "          }\n",
       "        ]\n",
       "      },\n",
       "      \"type\" : \"array\"\n",
       "    },\n",
       "    \"common-Set-Set-Int\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Set-Int\"\n",
       "      }\n",
       "    },\n",
       "    \"common-Serialized-UniPolynomial-Rational-Int\" : {\n",
       "      \"minItems\" : 1,\n",
       "      \"items\" : [\n",
       "        {\n",
       "          \"$ref\" : \"#/definitions/common-HashMap-Int-Rational\"\n",
       "        }\n",
       "      ],\n",
       "      \"type\" : \"array\",\n",
       "      \"additionalItems\" : false\n",
       "    },\n",
       "    \"common-Rational\" : {\n",
       "      \"pattern\" : \"^-?(\\\\d+(/\\\\d+)?|inf)$\",\n",
       "      \"type\" : \"string\"\n",
       "    },\n",
       "    \"common-Array-String\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-String\"\n",
       "      }\n",
       "    },\n",
       "    \"common-Graph-Undirected\" : {\n",
       "      \"properties\" : {\n",
       "        \"_dim\" : {\n",
       "          \"type\" : \"integer\",\n",
       "          \"minimum\" : 0\n",
       "        }\n",
       "      },\n",
       "      \"patternProperties\" : {\n",
       "        \"^\\\\d+$\" : {\n",
       "          \"$ref\" : \"#/definitions/common-Set-Int\"\n",
       "        }\n",
       "      },\n",
       "      \"additionalProperties\" : false,\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Set-Int\"\n",
       "      },\n",
       "      \"type\" : [\n",
       "        \"array\",\n",
       "        \"object\"\n",
       "      ]\n",
       "    },\n",
       "    \"common-Map-Int-Int\" : {\n",
       "      \"patternProperties\" : {\n",
       "        \"^-?\\\\d+$\" : {\n",
       "          \"$ref\" : \"#/definitions/common-Int\"\n",
       "        }\n",
       "      },\n",
       "      \"additionalProperties\" : false,\n",
       "      \"type\" : \"object\"\n",
       "    },\n",
       "    \"graph-Graph-Undirected\" : {\n",
       "      \"allOf\" : [\n",
       "        {\n",
       "          \"$ref\" : \"https://polymake.org/schemas/data.json#/definitions/big_obj\"\n",
       "        },\n",
       "        {\n",
       "          \"properties\" : {\n",
       "            \"MAX_CLIQUES\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Set-Set-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"N_CONNECTED_COMPONENTS\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"BIPARTITE\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Bool\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"CHARACTERISTIC_POLYNOMIAL\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-UniPolynomial-Rational-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"NODE_LABELS\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Array-String\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"CONNECTIVITY\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"NODE_DEGREES\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Array-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"CONNECTED_COMPONENTS\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-IncidenceMatrix-NonSymmetric\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"NodePerm.pure\" : false,\n",
       "            \"SIGNATURE\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"DIAMETER\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"TRIANGLE_FREE\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Bool\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"SIGNED_INCIDENCE_MATRIX\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-SparseMatrix-Int-NonSymmetric\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"DEGREE_SEQUENCE\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Map-Int-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"NodePerm\" : false,\n",
       "            \"CONNECTED\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Bool\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"N_NODES\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"BICONNECTED_COMPONENTS\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-IncidenceMatrix-NonSymmetric\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"AVERAGE_DEGREE\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Rational\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"N_EDGES\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Int\"\n",
       "                }\n",
       "              ]\n",
       "            },\n",
       "            \"ADJACENCY\" : {\n",
       "              \"anyOf\" : [\n",
       "                {\n",
       "                  \"type\" : \"null\"\n",
       "                },\n",
       "                {\n",
       "                  \"$ref\" : \"#/definitions/common-Graph-Undirected\"\n",
       "                }\n",
       "              ]\n",
       "            }\n",
       "          }\n",
       "        },\n",
       "        {\n",
       "          \"anyOf\" : [\n",
       "            {\n",
       "              \"properties\" : {\n",
       "                \"_attrs\" : {\n",
       "                  \"properties\" : {\n",
       "                    \"EIGENVALUES_LAPLACIAN\" : {\n",
       "                      \"properties\" : {\n",
       "                        \"_type\" : false\n",
       "                      }\n",
       "                    }\n",
       "                  }\n",
       "                },\n",
       "                \"EIGENVALUES_LAPLACIAN\" : {\n",
       "                  \"anyOf\" : [\n",
       "                    {\n",
       "                      \"type\" : \"null\"\n",
       "                    },\n",
       "                    {\n",
       "                      \"$ref\" : \"#/definitions/common-Vector-Float\"\n",
       "                    }\n",
       "                  ]\n",
       "                }\n",
       "              }\n",
       "            },\n",
       "            {\n",
       "              \"properties\" : {\n",
       "                \"_attrs\" : {\n",
       "                  \"properties\" : {\n",
       "                    \"EIGENVALUES_LAPLACIAN\" : {\n",
       "                      \"properties\" : {\n",
       "                        \"_type\" : {\n",
       "                          \"const\" : \"SparseVector<Float>\"\n",
       "                        }\n",
       "                      }\n",
       "                    }\n",
       "                  }\n",
       "                },\n",
       "                \"EIGENVALUES_LAPLACIAN\" : {\n",
       "                  \"$ref\" : \"#/definitions/common-SparseVector-Float\"\n",
       "                }\n",
       "              }\n",
       "            }\n",
       "          ]\n",
       "        }\n",
       "      ]\n",
       "    }\n",
       "  },\n",
       "  \"title\" : \"polymake data objects of type graph::Graph<Undirected>\",\n",
       "  \"allOf\" : [\n",
       "    {\n",
       "      \"properties\" : {\n",
       "        \"_type\" : {\n",
       "          \"const\" : \"graph::Graph<Undirected>\"\n",
       "        }\n",
       "      }\n",
       "    },\n",
       "    {\n",
       "      \"$ref\" : \"https://polymake.org/schemas/data.json#/definitions/top_level\"\n",
       "    },\n",
       "    {\n",
       "      \"$ref\" : \"#/definitions/graph-Graph-Undirected\"\n",
       "    }\n",
       "  ]\n",
       "}\n"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print JSON->new->pretty->encode($full_schema->source);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "$m=graph::petersen();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $full_schema->validate(Core::Serializer::serialize($m));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## PolyDB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### General Information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Database for mathematical objects. Intended for data related to the projects within OSCAR. \n",
    "\n",
    "Database is independent of particular software package, but information necessary for in-/output to a software can be stored alongside.\n",
    "\n",
    "Currently accessible via web (still at an experimental stage) and via polymake."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://polydb.org"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A database connection is established by calling the function ```polyDB```:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "cell_style": "center",
    "hide_input": false,
    "scrolled": true,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<details><summary><pre style=\"display:inline\"><small>Click here for additional output</small></pre></summary>\n",
       "<pre>\n",
       "polymake: used package polyDB\n",
       "   Access to the polymake polytope database.\n",
       "   (c) 2013-2019 Silke Horn, Andreas Paffenholz\n",
       "   http://www.mathematik.tu-darmstadt.de/~paffenholz\n",
       "\n",
       "</pre>\n",
       "</details>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "$pdb_default=polyDB();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Get info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "===============\n",
       "available polydb collections\n",
       "===============\n",
       "\n",
       "SECTION: Manifolds\n",
       "This database contains combinatorial manifolds\n",
       "\n",
       "COLLECTION: DIM2_3\n",
       "This is a collection of  combinatorial 2-  and  3-manifolds with up to 10 vertices given as triangulations and calculcated by Frank Lutz found at: http://page.math.tu-berlin.de/~lutz/stellar/mixed.html.\n",
       "\n",
       "SECTION: Matroids\n",
       "This database contains various classes of matroids.\n",
       "\n",
       "COLLECTION: Small\n",
       "Collection of small matroids based on the census by Yoshitake Matsumoto, Sonoko Moriyama, Hiroshi Imai, David Bremner.\n",
       "\n",
       "SECTION: Polytopes\n",
       "A collection of families of polytopes\n",
       "\n",
       "SECTION: Polytopes.Combinatorial\n",
       "This database contains various classes of combinatorial polytopes.\n",
       "\n",
       "COLLECTION: 01Polytopes\n",
       "0/1-Polytopes up to combinatorial equivalence\n",
       "\n",
       "COLLECTION: CombinatorialTypes\n",
       "Combinatorial types of polytopes of small dimension and number of vertices\n",
       "\n",
       "COLLECTION: FacesBirkhoffPolytope\n",
       "Combinatorial types of faces up to dimension  8 of a Birkhoff polytope in any dimension\n",
       "\n",
       "COLLECTION: SmallSpheresDim4\n",
       "Combinatorial 3-spheres with up to 9 vertices\n",
       "\n",
       "SECTION: Polytopes.Geometric\n",
       "This database contains various classes of geometric polytopes.\n",
       "\n",
       "COLLECTION: 01Polytopes\n",
       "0/1-Polytopes up to rotation and reflection\n",
       "\n",
       "SECTION: Polytopes.Lattice\n",
       "This database contains various classes of lattice polytopes.\n",
       "\n",
       "COLLECTION: 01Polytopes\n",
       "0/1-Polytopes up to lattice equivalence\n",
       "\n",
       "COLLECTION: ExceptionalMaximalHollow\n",
       "Exceptional maximal hollow lattice polytopes up to dimension 3\n",
       "\n",
       "COLLECTION: Reflexive\n",
       "Reflexive Polytopes in dimension 4\n",
       "\n",
       "COLLECTION: SmoothReflexive\n",
       "Smooth reflexive polytopes in dimensions 1 to 9 with splitting data\n",
       "\n",
       "SECTION: Tropical\n",
       "This database contains tropical objects.\n",
       "\n",
       "COLLECTION: Cubics\n",
       "Regular unimodular triangulations of the triple tetrahedron and their motifs.\n",
       "\n",
       "COLLECTION: Polytropes\n",
       "Representatives for all combinatorial tropical types of full-dimensional polytropes in TP3.\n",
       "\n",
       "COLLECTION: TOM\n",
       "All known non-realisable tropical oriented matroids with parameters n=6, d=3 or n=d=4.\n"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$pdb_default->info();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "For individual collections we can get more detailed information:\n",
    " * level 1: short description (default if no collection is given),\n",
    " * level  2: description, \n",
    " * level 3: description, authors, maintainers, \n",
    " * level 4: full info\n",
    " * level 5: full info and list of recommended search fields, if defined in info (default if collection is given)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "===============\n",
       "available polydb collections\n",
       "===============\n",
       "\n",
       "SECTION: Polytopes.Combinatorial\n",
       "This database contains various classes of combinatorial polytopes.\n",
       "\n",
       "COLLECTION: CombinatorialTypes\n",
       "Combinatorial types of polytopes of small dimension and number of vertices, see [[http://www-imai.is.s.u-tokyo.ac.jp/~hmiyata/oriented_matroids/index.html]]\n",
       "Author(s): \n",
       "  Hiroyuki Miyata, University of Tokyo, Bunkyo-ku, Tokyo, Japan, hmiyata@is.s.u-tokyo.ac.jp\n",
       "  Sonoko Moriyama, Tokuyama laboratory, System Information Sciences, Graduate School of Information Sciences, Tohoku University, moriso@dais.is.tohoku.ac.jp \n",
       "  Komei Fukuda, Theory of Combinatorial Algorithms, Department of Mathematics, ETHZ, fukuda@math.ethz.ch\n",
       "Contributor(s): \n",
       "  Constantin Fischer, TU Berlin, cfischer@mailbox.tu-berlin.de\n",
       "Maintainer(s): \n",
       "  Constantin Fischer, TU Berlin, cfischer@mailbox.tu-berlin.de\n",
       "Online Resources:\n",
       "Classification of Oriented Matroids: http://www-imai.is.s.u-tokyo.ac.jp/~hmiyata/oriented_matroids/index.html\n",
       "Searchable fields in this collection: \n",
       "_ns\n",
       "_type\n",
       "_polyDB\n",
       "FLAG_VECTOR\n",
       "COMBINATORIAL_DIM\n",
       "N_FACETS\n",
       "G_VECTOR\n",
       "H_VECTOR\n",
       "F2_VECTOR\n",
       "TWO_FACE_SIZES\n",
       "SIMPLICIAL\n",
       "FACET_SIZES\n",
       "F_VECTOR\n",
       "N_VERTICES\n",
       "SIMPLE\n",
       "VERTICES_IN_FACETS\n"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$pdb_default->info(section=>\"Polytopes.Combinatorial\", collection=>\"CombinatorialTypes\", info_level=>4);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Reading Data from polymake"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Simple reading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "cell_style": "center"
   },
   "outputs": [],
   "source": [
    "$coll_default=$pdb_default->get_collection(\"Polytopes.Lattice.SmoothReflexive\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$a=$coll_default->find({\"DIM\"=>3});"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This returns a cursor over the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $a->has_next;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$p=$a->next;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8 12 6"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $p->F_VECTOR;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6 9 5\n",
       "10 15 7\n",
       "12 18 8\n",
       "10 15 7\n",
       "8 12 6\n",
       "8 12 6\n",
       "10 15 7\n",
       "10 15 7\n",
       "12 18 8\n",
       "8 12 6\n",
       "8 12 6\n",
       "8 12 6\n",
       "6 9 5\n",
       "6 9 5\n",
       "8 12 6\n",
       "6 9 5\n",
       "4 6 4\n"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "while ( $p = $a->next ) {\n",
    "  print $p->F_VECTOR, \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Objects from the database know where they come from"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$a=$coll_default->find({\"DIM\"=>3});\n",
    "$p=$a->next;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "app: polytope\n",
       "section: Polytopes.Lattice\n",
       "version: 2.1\n",
       "uri: http://polymake.org/polytopes/paffenholz/www/fano.html\n",
       "id: F.3D.0000\n",
       "database: LatticePolytopes\n",
       "tag: object\n",
       "collection: SmoothReflexive\n",
       "creation_date: 2019-08-01\n",
       "type: polytope::Polytope<Rational>\n"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$info=$p->get_attachment(\"_polyDB\");\n",
    "foreach (keys %$info) { print $_, \": \", $info->{$_}, \"\\n\"; };"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Queries"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Queries are written in the query language of MongoDB. No parsing/wrapping inbetween.\n",
    "\n",
    "Simple form is just a json object (in perl array encoding) with key-value pairs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$cur=$coll_default->find({\"DIM\"=>3, \"N_VERTICES\" => 8});"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A query usually returns a cursor over all matching results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 8) (3, 8) (3, 8) (3, 8) (3, 8) (3, 8) (3, 8) "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "while ( $p = $cur->next ) {\n",
    "  print \"(\", $p->DIM, \", \", $p->N_VERTICES, \") \";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "The cursor keeps its position, so to return to the first result we need to reset the cursor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$cur->reset;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 8) (3, 8) (3, 8) (3, 8) (3, 8) (3, 8) (3, 8) "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "while ( $p = $cur->next ) {\n",
    "  print \"(\", $p->DIM, \", \", $p->N_VERTICES, \") \";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We can collect all results into an array."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "@a=$coll_default->find({\"DIM\"=>3, \"N_VERTICES\" => 8})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print scalar(@a);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We can ask for ranges:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "@a=$coll_default->find({\"DIM\" => { '$lt' => 4}})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print scalar(@a);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "2\n",
       "3 3\n",
       "4 4\n",
       "4 4\n",
       "5 5\n",
       "6 6\n",
       "8 12 6\n",
       "6 9 5\n",
       "10 15 7\n",
       "12 18 8\n",
       "10 15 7\n",
       "8 12 6\n",
       "8 12 6\n",
       "10 15 7\n",
       "10 15 7\n",
       "12 18 8\n",
       "8 12 6\n",
       "8 12 6\n",
       "8 12 6\n",
       "6 9 5\n",
       "6 9 5\n",
       "8 12 6\n",
       "6 9 5\n",
       "4 6 4\n"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foreach (@a) {\n",
    "  print $_->F_VECTOR, \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Boolean operations are possible:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "@a=$coll_default->find({'$or' => [{ 'DIM' => 2}, { 'DIM' => 4}]})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3 3\n",
       "4 4\n",
       "4 4\n",
       "5 5\n",
       "6 6\n",
       "11 22 18 7\n",
       "8 16 14 6\n",
       "17 34 25 8\n",
       "13 26 20 7\n",
       "15 30 23 8\n",
       "18 36 27 9\n",
       "15 30 23 8\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "12 24 19 7\n",
       "15 30 23 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "13 26 20 7\n",
       "12 24 19 7\n",
       "12 24 19 7\n",
       "9 18 15 6\n",
       "21 42 30 9\n",
       "17 34 25 8\n",
       "17 34 25 8\n",
       "23 46 32 9\n",
       "30 60 40 10\n",
       "13 26 20 7\n",
       "17 34 25 8\n",
       "15 30 23 8\n",
       "18 36 27 9\n",
       "16 32 24 8\n",
       "15 30 23 8\n",
       "12 24 19 7\n",
       "17 34 25 8\n",
       "21 42 30 9\n",
       "21 42 30 9\n",
       "18 36 26 8\n",
       "24 48 33 9\n",
       "18 36 26 8\n",
       "17 34 25 8\n",
       "16 32 24 8\n",
       "13 26 20 7\n",
       "13 26 20 7\n",
       "12 24 19 7\n",
       "15 30 23 8\n",
       "12 24 19 7\n",
       "9 18 15 6\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "20 40 29 9\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "20 40 29 9\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "20 40 29 9\n",
       "20 40 29 9\n",
       "16 32 24 8\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "20 40 29 9\n",
       "21 42 30 9\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "24 48 34 10\n",
       "20 40 29 9\n",
       "20 40 29 9\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "25 50 35 10\n",
       "30 60 41 11\n",
       "36 72 48 12\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "20 40 29 9\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "20 40 29 9\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "17 34 25 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "20 40 29 9\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "15 30 23 8\n",
       "18 36 27 9\n",
       "15 30 23 8\n",
       "20 40 29 9\n",
       "24 48 34 10\n",
       "18 36 27 9\n",
       "15 30 23 8\n",
       "15 30 23 8\n",
       "16 32 24 8\n",
       "11 22 18 7\n",
       "11 22 18 7\n",
       "12 24 19 7\n",
       "12 24 19 7\n",
       "12 24 19 7\n",
       "13 26 20 7\n",
       "16 32 24 8\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "12 24 19 7\n",
       "12 24 19 7\n",
       "8 16 14 6\n",
       "8 16 14 6\n",
       "12 24 19 7\n",
       "9 18 15 6\n",
       "16 32 24 8\n",
       "12 24 19 7\n",
       "8 16 14 6\n",
       "8 16 14 6\n",
       "9 18 15 6\n",
       "5 10 10 5\n"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foreach (@a) {\n",
    "  print $_->F_VECTOR, \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "@a=$coll_default->find({'$and' => [{ 'DIM' => 2}, { 'N_VERTICES' => 4}]})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4 4\n",
       "4 4\n"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foreach (@a) {\n",
    "  print $_->F_VECTOR, \"\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Type matters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "@a=$coll_default->find({'LATTICE_VOLUME' => \"8\"})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print scalar(@a);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "@a=$coll_default->find({'LATTICE_VOLUME' => 8})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print scalar(@a);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Creating and filling new collections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some preparations for a test database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "$PolyDB::default::db_host=\"db-test-server\";\n",
    "$PolyDB::default::useSSL = 0;\n",
    "$pdb_local_admin = polyDB(user=>\"admin\", password=>\"admin\");\n",
    "$pdb_local_admin->create_default_user_and_role();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We initiate a new collection. MongoDB is lazy, so what this function actually does is just creating access permisssions. The collection is only created when the first document is inserted."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "$pdb_local_admin->initiate_collection(collection=>\"Polytopes.01Polytopes\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We insert an info document for the section. This can only be done by database admins. A documentation entry is necessary, otherwise the section is not listed by the ```info()```-function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"description\" : \"A collection of families of polytopes\",\n",
       "  \"sectionDepth\" : 1,\n",
       "  \"section\" : [\n",
       "    \"Polytopes\"\n",
       "  ],\n",
       "  \"maintainer\" : {\n",
       "    \"email\" : \"paffenholz@opt.tu-darmstadt.de\",\n",
       "    \"affiliation\" : \"TU Darmstadt\",\n",
       "    \"name\" : \"Andreas Paffenholz\"\n",
       "  },\n",
       "  \"short_description\" : \"A collection of families of polytopes\",\n",
       "  \"polydb_version\" : \"2.1\"\n",
       "}\n"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$section_doc=load_json(\"files/json_and_polydb/polydb_meta/Polytopes/Polytopes.2.1.json\");\n",
    "print JSON->new->pretty->encode($section_doc);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "$pdb_local_admin->set_section_doc($section_doc, section=>\"Polytopes\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "From now on we can hand over the administration of the collection to a user that has admin permissions just on this collection, not the whole database. We create a new one, we could also extend an existing user."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "$pdb_local_admin->create_user(user=>\"workshop\", password=>\"workshop\", admin_collections=>[\"Polytopes.01Polytopes\"]);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $pdb_local_admin->user_exists(\"workshop\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Now we can connect as the newly created user with admin rights on the new collection."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$pdb_local = polyDB(user=>\"workshop\", password=>\"workshop\");\n",
    "$coll_local = $pdb_local->get_collection(\"Polytopes.01Polytopes\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We first set the documentation for the collection. Again, without a documentation entry the collection is not listed by the ```info()``` command."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"contributor\" : [\n",
       "    {\n",
       "      \"affiliation\" : \"TU Darmstadt\",\n",
       "      \"email\" : \"paffenholz@opt.tu-darmstadt.de\",\n",
       "      \"name\" : \"Andreas Paffenholz\"\n",
       "    }\n",
       "  ],\n",
       "  \"description\" : \"0/1-Polytopes up to rotation and reflection\",\n",
       "  \"collection\" : \"01Polytopes\",\n",
       "  \"polydb_version\" : \"2.1\",\n",
       "  \"_id\" : \"Polytopes.01Polytopes.2.1\",\n",
       "  \"maintainer\" : [\n",
       "    {\n",
       "      \"name\" : \"Andreas Paffenholz\",\n",
       "      \"email\" : \"paffenholz@opt.tu-darmstadt.de\",\n",
       "      \"affiliation\" : \"TU Darmstadt\"\n",
       "    }\n",
       "  ],\n",
       "  \"section\" : [\n",
       "    \"Polytopes\"\n",
       "  ],\n",
       "  \"author\" : [\n",
       "    {\n",
       "      \"name\" : \"Oswin Aichholzer\"\n",
       "    }\n",
       "  ],\n",
       "  \"short_description\" : \"0/1-Polytopes up to rotation and reflection\"\n",
       "}\n"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$collection_doc=load_json(\"files/json_and_polydb/polydb_meta/Polytopes/01Polytopes/Polytopes.01Polytopes.2.1.json\");\n",
    "print JSON->new->pretty->encode($collection_doc);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "$coll_local->set_collection_doc($collection_doc);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We need to insert two further documents before we can insert data:\n",
    " * a schema\n",
    " * an info document with some meta information on the collection.\n",
    "The second needs the id of the schema, so the schema must be inserted first."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create a schema from a polymake object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$a = load_data(\"files/json_and_polydb/data/geometric_01.pdata\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CONE_AMBIENT_DIM VERTICES"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print join \" \", $a->[0]->list_properties;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<details><summary><pre style=\"display:inline\"><small>Click here for additional output</small></pre></summary>\n",
       "<pre>\n",
       "polymake: used package ppl\n",
       "  The Parma Polyhedra Library ([[wiki:external_software#PPL]]): A C++ library for convex polyhedra\n",
       "  and other numerical abstractions.\n",
       "  http://www.cs.unipr.it/ppl/\n",
       "\n",
       "</pre>\n",
       "</details>\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "$p=$a->[0];\n",
    "$p->provide(\"VERTICES\",\"FACETS\",\"F_VECTOR\",\"SIMPLE\",\"SIMPLICIAL\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CONE_AMBIENT_DIM VERTICES FEASIBLE N_VERTICES POINTED BOUNDED FAR_FACE AFFINE_HULL CONE_DIM LINEALITY_DIM LINEALITY_SPACE COMBINATORIAL_DIM SIMPLICITY SIMPLICIALITY SIMPLE SIMPLICIAL FACETS N_FACETS F_VECTOR"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print join \" \", $p->list_properties;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"definitions\" : {\n",
       "    \"common-Set-Int\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"uniqueItems\" : true,\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Int\"\n",
       "      }\n",
       "    },\n",
       "    \"common-Int\" : {\n",
       "      \"type\" : \"integer\"\n",
       "    },\n",
       "    \"common-Vector-Rational\" : {\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Rational\"\n",
       "      },\n",
       "      \"type\" : \"array\"\n",
       "    },\n",
       "    \"common-Integer\" : {\n",
       "      \"type\" : \"string\",\n",
       "      \"pattern\" : \"^-?(\\\\d+|inf)$\"\n",
       "    },\n",
       "    \"common-Bool\" : {\n",
       "      \"type\" : \"boolean\"\n",
       "    },\n",
       "    \"common-Vector-Integer\" : {\n",
       "      \"items\" : {\n",
       "        \"$ref\" : \"#/definitions/common-Integer\"\n",
       "      },\n",
       "      \"type\" : \"array\"\n",
       "    },\n",
       "    \"common-Rational\" : {\n",
       "      \"pattern\" : \"^-?(\\\\d+(/\\\\d+)?|inf)$\",\n",
       "      \"type\" : \"string\"\n",
       "    },\n",
       "    \"common-Matrix-Rational-NonSymmetric\" : {\n",
       "      \"items\" : {\n",
       "        \"oneOf\" : [\n",
       "          {\n",
       "            \"$ref\" : \"#/definitions/common-Vector-Rational\"\n",
       "          },\n",
       "          {\n",
       "            \"additionalProperties\" : false,\n",
       "            \"required\" : [\n",
       "              \"cols\"\n",
       "            ],\n",
       "            \"properties\" : {\n",
       "              \"cols\" : {\n",
       "                \"type\" : \"integer\",\n",
       "                \"minimum\" : 0\n",
       "              }\n",
       "            },\n",
       "            \"type\" : \"object\"\n",
       "          }\n",
       "        ]\n",
       "      },\n",
       "      \"type\" : \"array\"\n",
       "    }\n",
       "  },\n",
       "  \"additionalProperties\" : false,\n",
       "  \"properties\" : {\n",
       "    \"AFFINE_HULL\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Matrix-Rational-NonSymmetric\"\n",
       "    },\n",
       "    \"N_FACETS\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"_load\" : {\n",
       "      \"type\" : \"array\",\n",
       "      \"items\" : {\n",
       "        \"type\" : \"string\"\n",
       "      }\n",
       "    },\n",
       "    \"N_VERTICES\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"LINEALITY_SPACE\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Matrix-Rational-NonSymmetric\"\n",
       "    },\n",
       "    \"F_VECTOR\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Vector-Integer\"\n",
       "    },\n",
       "    \"POINTED\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Bool\"\n",
       "    },\n",
       "    \"SIMPLICIAL\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Bool\"\n",
       "    },\n",
       "    \"COMBINATORIAL_DIM\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"SIMPLICITY\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"VERTICES\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Matrix-Rational-NonSymmetric\"\n",
       "    },\n",
       "    \"SIMPLICIALITY\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"_type\" : {\n",
       "      \"const\" : \"polytope::Polytope<Rational>\"\n",
       "    },\n",
       "    \"_id\" : {\n",
       "      \"$ref\" : \"https://polymake.org/schemas/data.json#/definitions/obj_id\"\n",
       "    },\n",
       "    \"CONE_AMBIENT_DIM\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"LINEALITY_DIM\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"BOUNDED\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Bool\"\n",
       "    },\n",
       "    \"_ns\" : {\n",
       "      \"properties\" : {\n",
       "        \"polymake\" : {\n",
       "          \"additionalItems\" : false,\n",
       "          \"items\" : [\n",
       "            {\n",
       "              \"const\" : \"https://polymake.org\"\n",
       "            },\n",
       "            {\n",
       "              \"const\" : \"4.0\"\n",
       "            }\n",
       "          ],\n",
       "          \"type\" : \"array\"\n",
       "        }\n",
       "      },\n",
       "      \"type\" : \"object\",\n",
       "      \"additionalProperties\" : false\n",
       "    },\n",
       "    \"FACETS\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Matrix-Rational-NonSymmetric\"\n",
       "    },\n",
       "    \"_canonical\" : {\n",
       "      \"type\" : \"boolean\"\n",
       "    },\n",
       "    \"CONE_DIM\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Int\"\n",
       "    },\n",
       "    \"SIMPLE\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Bool\"\n",
       "    },\n",
       "    \"FAR_FACE\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Set-Int\"\n",
       "    },\n",
       "    \"FEASIBLE\" : {\n",
       "      \"$ref\" : \"#/definitions/common-Bool\"\n",
       "    },\n",
       "    \"_info\" : {\n",
       "      \"$ref\" : \"https://polymake.org/schemas/data.json#/definitions/obj_info\"\n",
       "    }\n",
       "  },\n",
       "  \"required\" : [\n",
       "    \"_ns\",\n",
       "    \"_type\",\n",
       "    \"CONE_AMBIENT_DIM\",\n",
       "    \"VERTICES\",\n",
       "    \"FEASIBLE\",\n",
       "    \"N_VERTICES\",\n",
       "    \"POINTED\",\n",
       "    \"BOUNDED\",\n",
       "    \"FAR_FACE\",\n",
       "    \"AFFINE_HULL\",\n",
       "    \"CONE_DIM\",\n",
       "    \"LINEALITY_DIM\",\n",
       "    \"LINEALITY_SPACE\",\n",
       "    \"COMBINATORIAL_DIM\",\n",
       "    \"SIMPLICITY\",\n",
       "    \"SIMPLICIALITY\",\n",
       "    \"SIMPLE\",\n",
       "    \"SIMPLICIAL\",\n",
       "    \"FACETS\",\n",
       "    \"N_FACETS\",\n",
       "    \"F_VECTOR\"\n",
       "  ],\n",
       "  \"type\" : \"object\",\n",
       "  \"$schema\" : \"http://json-schema.org/draft-07/schema#\"\n",
       "}\n"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$schema=create_restrictive_schema($p);\n",
    "print JSON->new->pretty->encode($schema->source);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "$coll_local->set_schema($schema->source);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Now we can insert the info document."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\n",
       "  \"_id\" : \"info.2.1\",\n",
       "  \"polydb_version\" : \"2.1\",\n",
       "  \"packages\" : {\n",
       "    \"polymake\" : {\n",
       "      \"type\" : \"polytope::Polytope<Rational>\",\n",
       "      \"version\" : \"3.4\"\n",
       "    }\n",
       "  },\n",
       "  \"description\" : \"0/1 Polytopes\",\n",
       "  \"collection\" : \"01Polytopes\",\n",
       "  \"contributor\" : \"Andreas Paffenholz\",\n",
       "  \"creator\" : \"Oswin Aichholzer\",\n",
       "  \"uri\" : \"https://polymake.org\",\n",
       "  \"section\" : [\n",
       "    \"Polytopes\"\n",
       "  ],\n",
       "  \"maintainer\" : \"Andreas Paffenholz\"\n",
       "}\n"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$info_doc=load_json(\"files/json_and_polydb/polydb_meta/Polytopes/01Polytopes/info.2.1.json\");\n",
    "print JSON->new->pretty->encode($info_doc);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "$coll_local->set_info($info_doc, schema_id=>\"schema.2.1\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Now the new collection is fully prepared and is listed with ```info()```:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "===============\n",
       "available polydb collections\n",
       "===============\n",
       "\n",
       "SECTION: Polytopes\n",
       "A collection of families of polytopes\n",
       "\n",
       "COLLECTION: 01Polytopes\n",
       "0/1-Polytopes up to rotation and reflection\n"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "$pdb_local->info();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We can now insert our data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "$coll_local->insert(@$a);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $coll_local->count({});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "@b=$coll_local->find({})->all;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print scalar(@b);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "$p=$coll_local->find_one({});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FACETS LINEALITY_DIM BOUNDED AFFINE_HULL CONE_AMBIENT_DIM N_FACETS COMBINATORIAL_DIM SIMPLICITY VERTICES SIMPLICIALITY SIMPLE FAR_FACE FEASIBLE POINTED SIMPLICIAL LINEALITY_SPACE F_VECTOR CONE_DIM N_VERTICES"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print join \" \", $p->list_properties;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "$coll_local->remove($p->name);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print $coll_local->count({});"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "polymake",
   "language": "polymake",
   "name": "polymake"
  },
  "language_info": {
   "codemirror_mode": "perl",
   "file_extension": ".pl",
   "mimetype": "text/x-polymake",
   "name": "polymake"
  },
  "pygments_lexer": "perl",
  "rise": {
   "auto_select": "code",
   "auto_select_fragment": true,
   "center": false,
   "controls": false,
   "scroll": true,
   "width": "90%"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
