-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demos: added a demo for using mps-cli-py in a jupyter notebook
- Loading branch information
Showing
4 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## Jupyter Notebook - Demo Project | ||
|
||
This project demonstrates the use of the MPS CLI Python library from within a Jupyter Notebook. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0c9c2589-01a6-48ea-a4ec-f01e452cb134", | ||
"metadata": {}, | ||
"source": [ | ||
"# Using the MPS-CLI Python Library\n", | ||
"\n", | ||
"## Loading the model" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f5969fb2-8bdc-4b6e-ab0e-5d48e41ec810", | ||
"metadata": {}, | ||
"source": [ | ||
"import sys\n", | ||
"sys.path.insert(1, '..\\..\\mps-cli-py\\src')\n", | ||
"\n", | ||
"from mpscli.model.builder.SSolutionsRepositoryBuilder import SSolutionsRepositoryBuilder\n", | ||
"\n", | ||
"builder = SSolutionsRepositoryBuilder()\n", | ||
"repo = builder.build('..\\\\..\\\\mps_test_projects\\\\mps_cli_lanuse_file_per_root')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "44a68574-8c57-4186-bac2-3930491120c3", | ||
"metadata": {}, | ||
"source": [ | ||
"## Accessing solutions" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 13, | ||
"id": "713d0c76-eae1-4bb2-a7f5-8b133f1193f1", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"number of parsed solutions: 2\n", | ||
"\tmps.cli.lanuse.library_second\n", | ||
"\tmps.cli.lanuse.library_top\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(\"number of parsed solutions: \" + str(len(repo.solutions)))\n", | ||
"print(\"parsed solutions:\")\n", | ||
"for sol in repo.solutions:\n", | ||
" print(\"\\t\" + sol.name)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c950f571-906a-494f-84bc-d3cfe9f84a30", | ||
"metadata": {}, | ||
"source": [ | ||
"## Accessing models and root nodes" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 18, | ||
"id": "9722e60a-2d79-4131-8cef-59101237df13", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"models and root nodes of \"mps.cli.lanuse.library_second\" are:\n", | ||
"\tmps.cli.lanuse.library_second.library_top\n", | ||
"\t\troot nodes are:\n", | ||
"\t\t\t_library\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"library_second_solution = repo.find_solution_by_name(\"mps.cli.lanuse.library_second\")\n", | ||
"print(f'models and root nodes of \"{ library_second_solution.name }\" are:')\n", | ||
"for model in library_second_solution.models:\n", | ||
" print(\"\\t\" + model.name)\n", | ||
" print(\"\\t\\troot nodes are:\")\n", | ||
" for root in model.root_nodes:\n", | ||
" print(\"\\t\\t\\t\" + root.get_property(\"name\"))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c6edf78e-e4fc-4657-8ce9-d3e818dd2907", | ||
"metadata": {}, | ||
"source": [ | ||
"## Accessing node information" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"id": "16cbb784-9ccb-4bb1-8207-12c53a785ca5", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"root node _library\n", | ||
"child node: Tom Sawyer with concept mps.cli.landefs.library.structure.Book\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"library_top_model = repo.find_model_by_name(\"mps.cli.lanuse.library_second.library_top\")\n", | ||
"for root in library_top_model.root_nodes:\n", | ||
" print(f'root node { root.get_property(\"name\") }')\n", | ||
" for child in root.children:\n", | ||
" print(f'\\t child node: {child.get_property(\"name\") } with concept {child.concept.name}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f2e9d929-990f-4070-9f39-52035697a8d4", | ||
"metadata": {}, | ||
"source": [ | ||
"## Accessing the meta-information about languages and concepts" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"id": "45802a94-2ee3-4dda-8373-a47e4fd8d140", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"language: mps.cli.landefs.library\n", | ||
"\tconcepts:\n", | ||
"\t\tmps.cli.landefs.library.structure.Library\n", | ||
"\t\t\tchildren roles:\n", | ||
"\t\t\t\tentities\n", | ||
"\t\tmps.cli.landefs.library.structure.LibraryEntityBase\n", | ||
"\t\t\tproperties:\n", | ||
"\t\t\t\tisbn\n", | ||
"\t\t\t\tavailable\n", | ||
"\t\tmps.cli.landefs.library.structure.Book\n", | ||
"\t\t\tproperties:\n", | ||
"\t\t\t\tpublicationDate\n", | ||
"\t\t\tchildren roles:\n", | ||
"\t\t\t\tauthors\n", | ||
"\t\tmps.cli.landefs.library.structure.Magazine\n", | ||
"\t\t\tproperties:\n", | ||
"\t\t\t\tperiodicity\n", | ||
"language: jetbrains.mps.lang.core\n", | ||
"\tconcepts:\n", | ||
"\t\tjetbrains.mps.lang.core.structure.INamedConcept\n", | ||
"\t\t\tproperties:\n", | ||
"\t\t\t\tname\n", | ||
"language: mps.cli.landefs.people\n", | ||
"\tconcepts:\n", | ||
"\t\tmps.cli.landefs.people.structure.PersonRef\n", | ||
"\t\t\treferences roles:\n", | ||
"\t\t\t\tperson\n", | ||
"\t\tmps.cli.landefs.people.structure.PersonsContainer\n", | ||
"\t\t\tchildren roles:\n", | ||
"\t\t\t\tpersons\n", | ||
"\t\tmps.cli.landefs.people.structure.Person\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for language in repo.languages:\n", | ||
" print(\"language: \" + language.name)\n", | ||
" print(\"\\tconcepts:\")\n", | ||
" for concept in language.concepts:\n", | ||
" print(\"\\t\\t\" + concept.name)\n", | ||
" if (len(concept.properties) > 0):\n", | ||
" print(\"\\t\\t\\tproperties:\")\n", | ||
" for property in concept.properties:\n", | ||
" print(\"\\t\\t\\t\\t\" + property)\n", | ||
" if (len(concept.children) > 0):\n", | ||
" print(\"\\t\\t\\tchildren roles:\")\n", | ||
" for child in concept.children:\n", | ||
" print(\"\\t\\t\\t\\t\" + child)\n", | ||
" if (len(concept.references) > 0):\n", | ||
" print(\"\\t\\t\\treferences roles:\")\n", | ||
" for reference in concept.references:\n", | ||
" print(\"\\t\\t\\t\\t\" + reference) " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "e67706cf-07a2-486c-91ad-329d7f0433b4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters