Browse Source

Add info on how to create MMs for tutorial

Yentl Van Tendeloo 3 years ago
parent
commit
535d223100
2 changed files with 476 additions and 42 deletions
  1. 476 0
      New_MM.ipynb
  2. 0 42
      tutorial.ipynb

+ 476 - 0
New_MM.ipynb

@@ -0,0 +1,476 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Create a new metamodel"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Default initialization as usual."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import sys\n",
+    "sys.path.append(\"wrappers\")\n",
+    "from modelverse import *\n",
+    "\n",
+    "init()\n",
+    "login(\"admin\", \"admin\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "## New metamodel"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Define a new metamodel by instantiation SimpleClassDiagrams."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Add the different attributes to be used, and define their name.\n",
+    "Action language attributes can also be used, which goes outside of the simple attributes."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Attributes are defined manually by the language engineer, so the constraints must also be made explicit, by mapping it to physical primitives."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (is_physical_string(model[\"model\"][name])):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"String has non-string value\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Add the different classes and associations of the metamodel."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (is_physical_string(model[\"model\"][name])):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"String has non-string value\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "\n",
+    "    Class State {\n",
+    "        name = \"State\"\n",
+    "\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (string_len(read_attribute(model, name, \"name\")) > 3):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"Name is too short\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    Association Transition (State, State) {\n",
+    "    }\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Define attributes in the state and transitions, using the previously defined attribute types."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (is_physical_string(model[\"model\"][name])):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"String has non-string value\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "\n",
+    "    Class State {\n",
+    "        name = \"State\"\n",
+    "        name : String\n",
+    "\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (string_len(read_attribute(model, name, \"name\")) > 3):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"Name is too short\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    Association Transition (State, State) {\n",
+    "        trigger? : String {}\n",
+    "        raise? : String {}\n",
+    "        script? : Action {}\n",
+    "    }\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Still missing the initial state, which we can define using inheritance."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (is_physical_string(model[\"model\"][name])):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"String has non-string value\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "\n",
+    "    Class State {\n",
+    "        name = \"State\"\n",
+    "        name : String\n",
+    "        \n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (string_len(read_attribute(model, name, \"name\")) > 3):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"Name is too short\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    Class InitialState : State {\n",
+    "    }\n",
+    "\n",
+    "    Association Transition (State, State) {\n",
+    "        trigger? : String {}\n",
+    "        raise? : String {}\n",
+    "        script? : Action {}\n",
+    "    }\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Require that there is exactly one initial state, by using a global constraint."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "    include \"object_operations.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (is_physical_string(model[\"model\"][name])):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"String has non-string value\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "\n",
+    "    Class State {\n",
+    "        name = \"State\"\n",
+    "        name : String\n",
+    "\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (string_len(read_attribute(model, name, \"name\")) > 3):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"Name is too short\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    Class InitialState : State {\n",
+    "    }\n",
+    "\n",
+    "    Association Transition (State, State) {\n",
+    "        trigger? : String {}\n",
+    "        raise? : String {}\n",
+    "        script? : Action {}\n",
+    "    }\n",
+    "\n",
+    "    GlobalConstraint {\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element):\n",
+    "                Integer initials\n",
+    "                initials = set_len(allInstances(model, \"InitialState\"))\n",
+    "                if (initials == 0):\n",
+    "                    return \"No initial state found\"!\n",
+    "                elif (initials > 1):\n",
+    "                    return \"Too many initial states defined\"!\n",
+    "                else:\n",
+    "                    return \"OK\"!\n",
+    "            $\n",
+    "    }\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "More efficient implementation using predefined constructs (lower_cardinality and upper_cardinality)."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "model_add(\"formalisms/MyOwnFSA\", \"formalisms/SimpleClassDiagrams\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "    include \"object_operations.alh\"\n",
+    "\n",
+    "    SimpleAttribute String {\n",
+    "        name = \"String\"\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (is_physical_string(model[\"model\"][name])):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"String has non-string value\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    ActionLanguage Action {}\n",
+    "\n",
+    "    Class State {\n",
+    "        name = \"State\"\n",
+    "        name : String\n",
+    "\n",
+    "        constraint = $\n",
+    "            String function constraint(model : Element, name : String):\n",
+    "                if (string_len(read_attribute(model, name, \"name\")) > 3):\n",
+    "                    return \"OK\"!\n",
+    "                else:\n",
+    "                    return \"Name is too short\"!\n",
+    "            $\n",
+    "    }\n",
+    "\n",
+    "    Class InitialState : State {\n",
+    "        lower_cardinality = 1\n",
+    "        upper_cardinality = 1\n",
+    "    }\n",
+    "\n",
+    "    Association Transition (State, State) {\n",
+    "        trigger? : String {}\n",
+    "        raise? : String {}\n",
+    "        script? : Action {}\n",
+    "    }\n",
+    "    \"\"\")"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "Alternatively, the metamodel can be created incrementally."
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "mm = \"formalisms/MyOwnFSA2\"\n",
+    "\n",
+    "model_add(mm, \"formalisms/SimpleClassDiagrams\")\n",
+    "\n",
+    "str_attr = instantiate(mm, \"SimpleAttribute\")\n",
+    "attr_assign(mm, str_attr, \"name\", \"String\")\n",
+    "attr_assign_code(mm, str_attr, \"constraint\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    \n",
+    "    String function constraint(model : Element, name : String):\n",
+    "        if (is_physical_string(model[\"model\"][name])):\n",
+    "            return \"OK\"!\n",
+    "        else:\n",
+    "            return \"String has non-string value\"!\n",
+    "    \"\"\")\n",
+    "al_attr = instantiate(mm, \"ActionLanguage\")\n",
+    "\n",
+    "state = instantiate(mm, \"Class\")\n",
+    "attr_assign(mm, state, \"name\", \"State\")\n",
+    "define_attribute(mm, state, \"name\", str_attr)\n",
+    "attr_assign_code(mm, state, \"constraint\", \"\"\"\n",
+    "    include \"primitives.alh\"\n",
+    "    include \"modelling.alh\"\n",
+    "    \n",
+    "    String function constraint(model : Element, name : String):\n",
+    "        if (string_len(read_attribute(model, name, \"name\")) > 3):\n",
+    "            return \"OK\"!\n",
+    "        else:\n",
+    "            return \"Name is too short\"!\n",
+    "    \"\"\")\n",
+    "\n",
+    "initial = instantiate(mm, \"Class\")\n",
+    "instantiate(mm, \"Inheritance\", edge=(initial, state))\n",
+    "attr_assign(mm, initial, \"lower_cardinality\", 1)\n",
+    "attr_assign(mm, initial, \"upper_cardinality\", 1)\n",
+    "\n",
+    "transition = instantiate(mm, \"Association\", edge=(state, state))\n",
+    "define_attribute(mm, transition, \"trigger\", str_attr)\n",
+    "attribute_optional(mm, transition, \"trigger\", True)\n",
+    "define_attribute(mm, transition, \"raise\", str_attr)\n",
+    "attribute_optional(mm, transition, \"raise\", True)\n",
+    "define_attribute(mm, transition, \"script\", al_attr)\n",
+    "attribute_optional(mm, transition, \"script\", True)"
+   ]
+  },
+  {
+   "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.6.5"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 0 - 42
tutorial.ipynb

@@ -84,48 +84,6 @@
     "svg.data = show(\"models/my_FSA\")\n",
     "display(svg)"
    ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "# Semantics"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "## Manual"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "## Action Language"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "## Model Transformation"
-   ]
-  },
-  {
-   "cell_type": "markdown",
-   "metadata": {},
-   "source": [
-    "# Process Enactment"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {},
-   "outputs": [],
-   "source": []
   }
  ],
  "metadata": {