{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# User/Issue model from [ShEx primer](http://shex.io/shex-primer/#simple-expressions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-01-05T21:24:24.644220Z",
     "iopub.status.busy": "2021-01-05T21:24:24.643660Z",
     "iopub.status.idle": "2021-01-05T21:24:25.782030Z",
     "shell.execute_reply": "2021-01-05T21:24:25.782504Z"
    }
   },
   "outputs": [],
   "source": [
    "!pip install linkml --upgrade --disable-pip-version-check -q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2021-01-05T21:24:25.789824Z",
     "iopub.status.busy": "2021-01-05T21:24:25.789150Z",
     "iopub.status.idle": "2021-01-05T21:24:26.313052Z",
     "shell.execute_reply": "2021-01-05T21:24:26.313332Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Auto generated from None by pythongen.py version: 0.9.0\n",
      "# Generation date: 2021-01-05 15:24\n",
      "# Schema: issue\n",
      "#\n",
      "# id: http://shex.io/shex-primer/issueshape\n",
      "# description:\n",
      "# license:\n",
      "\n",
      "import dataclasses\n",
      "import sys\n",
      "import re\n",
      "from typing import Optional, List, Union, Dict, ClassVar, Any\n",
      "from dataclasses import dataclass\n",
      "from linkml_model.meta import EnumDefinition, PermissibleValue, PvFormulaOptions\n",
      "\n",
      "from linkml_runtime.utils.slot import Slot\n",
      "from linkml_runtime.utils.metamodelcore import empty_list, empty_dict, bnode\n",
      "from linkml_runtime.utils.yamlutils import YAMLRoot, extended_str, extended_float, extended_int\n",
      "if sys.version_info < (3, 7, 6):\n",
      "    from linkml.utils.dataclass_extensions_375 import dataclasses_init_fn_with_kwargs\n",
      "else:\n",
      "    from linkml_runtime.utils.dataclass_extensions_376 import dataclasses_init_fn_with_kwargs\n",
      "from linkml_runtime.utils.formatutils import camelcase, underscore, sfx\n",
      "from linkml_runtime.utils.enumerations import EnumDefinitionImpl\n",
      "from rdflib import Namespace, URIRef\n",
      "from linkml_runtime.utils.curienamespace import CurieNamespace\n",
      "\n",
      "\n",
      "metamodel_version = \"1.7.0\"\n",
      "\n",
      "# Overwrite dataclasses _init_fn to add **kwargs in __init__\n",
      "dataclasses._init_fn = dataclasses_init_fn_with_kwargs\n",
      "\n",
      "# Namespaces\n",
      "EX = CurieNamespace('ex', 'http://ex.example/#')\n",
      "FOAF = CurieNamespace('foaf', 'http://xmlns.com/foaf/0.1/')\n",
      "ISSUE = CurieNamespace('issue', 'http://shex.io/shex-primer/issue/')\n",
      "XSD = CurieNamespace('xsd', 'http://www.w3.org/2001/XMLSchema#')\n",
      "DEFAULT_ = ISSUE\n",
      "\n",
      "\n",
      "# Types\n",
      "class Str(str):\n",
      "    type_class_uri = XSD.string\n",
      "    type_class_curie = \"xsd:string\"\n",
      "    type_name = \"str\"\n",
      "    type_model_uri = ISSUE.Str\n",
      "\n",
      "\n",
      "class Int(int):\n",
      "    type_class_uri = XSD.integer\n",
      "    type_class_curie = \"xsd:integer\"\n",
      "    type_name = \"int\"\n",
      "    type_model_uri = ISSUE.Int\n",
      "\n",
      "\n",
      "# Class references\n",
      "\n",
      "\n",
      "\n",
      "class Person(YAMLRoot):\n",
      "    _inherited_slots: ClassVar[List[str]] = []\n",
      "\n",
      "    class_class_uri: ClassVar[URIRef] = ISSUE.Person\n",
      "    class_class_curie: ClassVar[str] = \"issue:Person\"\n",
      "    class_name: ClassVar[str] = \"person\"\n",
      "    class_model_uri: ClassVar[URIRef] = ISSUE.Person\n",
      "\n",
      "\n",
      "class Enrolee(Person):\n",
      "    _inherited_slots: ClassVar[List[str]] = []\n",
      "\n",
      "    class_class_uri: ClassVar[URIRef] = ISSUE.Enrolee\n",
      "    class_class_curie: ClassVar[str] = \"issue:Enrolee\"\n",
      "    class_name: ClassVar[str] = \"enrolee\"\n",
      "    class_model_uri: ClassVar[URIRef] = ISSUE.Enrolee\n",
      "\n",
      "\n",
      "# Enumerations\n",
      "\n",
      "\n",
      "# Slots\n",
      "class slots:\n",
      "    pass\n",
      "\n",
      "slots.name = Slot(uri=ISSUE.name, name=\"name\", curie=ISSUE.curie('name'),\n",
      "                   model_uri=ISSUE.name, domain=Person, range=Union[dict, Person])\n",
      "\n",
      "slots.age = Slot(uri=FOAF.age, name=\"age\", curie=FOAF.curie('age'),\n",
      "                   model_uri=ISSUE.age, domain=Enrolee, range=Optional[int])\n",
      "\n",
      "slots.hasGuardian = Slot(uri=ISSUE.hasGuardian, name=\"hasGuardian\", curie=ISSUE.curie('hasGuardian'),\n",
      "                   model_uri=ISSUE.hasGuardian, domain=Enrolee, range=Union[Union[dict, Person], List[Union[dict, Person]]])\n",
      "BASE <http://shex.io/shex-primer/issue/>\n",
      "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n",
      "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n",
      "\n",
      "\n",
      "<Str> xsd:string\n",
      "\n",
      "<Int> xsd:integer\n",
      "\n",
      "<Enrolee> CLOSED {\n",
      "    (  $<Enrolee_tes> (  &<Person_tes> ;\n",
      "          rdf:type [ <Person> ] ?\n",
      "       ) ;\n",
      "       rdf:type [ <Enrolee> ] ?\n",
      "    )\n",
      "}\n",
      "\n",
      "<Person>  (\n",
      "    CLOSED {\n",
      "       (  $<Person_tes> rdf:type . * ;\n",
      "          rdf:type [ <Person> ] ?\n",
      "       )\n",
      "    } OR @<Enrolee>\n",
      ")\n",
      "\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from linkml.generators.shexgen import ShExGenerator\n",
    "from linkml.generators.pythongen import PythonGenerator\n",
    "\n",
    "model = \"\"\"\n",
    "id: http://shex.io/shex-primer/issueshape\n",
    "title: Issue Shape from ShEx primer\n",
    "name: issue\n",
    "version: 0.1.0\n",
    "\n",
    "prefixes:\n",
    "   issue: http://shex.io/shex-primer/issue/\n",
    "   ex: http://ex.example/#\n",
    "   xsd: http://www.w3.org/2001/XMLSchema#\n",
    "   foaf: http://xmlns.com/foaf/0.1/\n",
    "\n",
    "default_prefix: issue\n",
    "\n",
    "default_range: str\n",
    "\n",
    "types:\n",
    "   str:\n",
    "       base: str\n",
    "       uri: xsd:string\n",
    "\n",
    "   int:\n",
    "      base: int\n",
    "      uri: xsd:integer\n",
    "\n",
    "classes:\n",
    "    person:\n",
    "    enrolee:\n",
    "        is_a: person\n",
    "\n",
    "slots:\n",
    "    name:\n",
    "        domain: person\n",
    "        range: str\n",
    "        key: true\n",
    "\n",
    "    age:\n",
    "        domain: enrolee\n",
    "        range: int\n",
    "        slot_uri: foaf:age\n",
    "\n",
    "    hasGuardian:\n",
    "        domain: enrolee\n",
    "        range: person\n",
    "        multivalued: true\n",
    "        required: true\n",
    "\n",
    "\"\"\"\n",
    "print(PythonGenerator(model).serialize())\n",
    "print(ShExGenerator(model).serialize(collections=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
