123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>Examples — Modelverse 0.4.0 documentation</title>
-
- <link rel="stylesheet" href="_static/classic.css" type="text/css" />
- <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
-
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: './',
- VERSION: '0.4.0',
- COLLAPSE_INDEX: false,
- FILE_SUFFIX: '.html',
- HAS_SOURCE: true
- };
- </script>
- <script type="text/javascript" src="_static/jquery.js"></script>
- <script type="text/javascript" src="_static/underscore.js"></script>
- <script type="text/javascript" src="_static/doctools.js"></script>
- <link rel="top" title="Modelverse 0.4.0 documentation" href="index.html" />
- <link rel="next" title="Advanced examples" href="advanced.html" />
- <link rel="prev" title="Modelling Language" href="modellanguage.html" />
- </head>
- <body role="document">
- <div class="related" role="navigation" aria-label="related navigation">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="advanced.html" title="Advanced examples"
- accesskey="N">next</a> |</li>
- <li class="right" >
- <a href="modellanguage.html" title="Modelling Language"
- accesskey="P">previous</a> |</li>
- <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> »</li>
- </ul>
- </div>
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body" role="main">
-
- <div class="section" id="examples">
- <h1>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h1>
- <p>To run this code, store it in a file (<em>e.g.</em>, test.alc), and execute the following commands:</p>
- <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">run_local_modelverse</span><span class="o">.</span><span class="n">py</span> <span class="mi">8001</span> <span class="o">&</span>
- <span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">make_parallel</span><span class="o">.</span><span class="n">py</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">localhost</span><span class="p">:</span><span class="mi">8001</span> <span class="n">test</span> <span class="n">test</span><span class="o">.</span><span class="n">alc</span> <span class="n">bootstrap</span><span class="o">/</span><span class="n">primitives</span><span class="o">.</span><span class="n">alc</span>
- <span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">prompt</span><span class="o">.</span><span class="n">py</span>
- </pre></div>
- </div>
- <p>In the prompt, log on to <a class="reference external" href="http://localhost:8001">http://localhost:8001</a> as user <em>test</em>.
- Now, all input you send, will be caught in the <em>input()</em> calls seen in the code.
- Results will also be printed.</p>
- <p>Note that all content will, by default, be typed as string.
- If you want to send integers or so, prepend the input with a backslash (\), which allows you to directly input the JSON code.
- For example, input <em>5</em> will be received as the string “5”.
- To send the integer 5, the input should be <em>\5</em>.</p>
- <div class="section" id="fibonacci-server">
- <h2>Fibonacci Server<a class="headerlink" href="#fibonacci-server" title="Permalink to this headline">¶</a></h2>
- <p>The first example is a simple Fibonacci server.
- The code is identical to the action language example from before, as this already included the <em>server</em> part (<em>i.e.</em>, the while loop).
- Now we will just connect to it using <em>prompt.py</em> and try out the code directly.</p>
- <p>The code is repeated below:</p>
- <div class="highlight-default"><div class="highlight"><pre>include "primitives.alh"
- Integer function fib(param : Integer):
- if (param <= 2):
- return 1!
- else:
- return fib(param - 1) + fib(param - 2)!
- Void function main():
- while(True):
- output(fib(input()))
- </pre></div>
- </div>
- <p>To run this code, save it to a file (<em>e.g.</em>, fibonacci.alc) and execute the following command:</p>
- <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">run_local_modelverse</span><span class="o">.</span><span class="n">py</span> <span class="mi">8001</span>
- </pre></div>
- </div>
- <p>Now, the Modelverse is running, but we still need to upload our code.
- To compile the file, together with the primitives.alc file, execute the following command:</p>
- <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">make_parallel</span><span class="o">.</span><span class="n">py</span> <span class="n">http</span><span class="p">:</span><span class="o">//</span><span class="n">localhost</span><span class="p">:</span><span class="mi">8001</span> <span class="n">test</span> <span class="n">fibonacci</span><span class="o">.</span><span class="n">alc</span> <span class="n">bootstrap</span><span class="o">/</span><span class="n">primitives</span><span class="o">.</span><span class="n">alc</span>
- </pre></div>
- </div>
- <p>When this finishes, the Modelverse now stores a copy of our code in its own format.
- The Modelverse will automatically create the user <em>test</em> and start executing the <em>main</em> function as this user.
- We can therefore simply connect to the Modelverse as the <em>test</em> user and start seeing the responses.
- To start the prompt, execute the following command:</p>
- <div class="highlight-default"><div class="highlight"><pre><span class="n">python</span> <span class="n">scripts</span><span class="o">/</span><span class="n">prompt</span><span class="o">.</span><span class="n">py</span>
- </pre></div>
- </div>
- <p>In this prompt tool, you first have to configure the location of the Modelverse and the username.
- The defaults should be fine, so just press <return> twice.
- After that, you are in direct connection with the Modelverse.
- Each message you type in, is made available in the <em>input()</em> function of the code.
- Remember that, to send integers, you have to prefix this with a backslash (\).
- To get, for example, the 1st Fibonacci number, type in the command:</p>
- <div class="highlight-default"><div class="highlight"><pre>\<span class="mi">1</span>
- </pre></div>
- </div>
- <p>You will now see the following.</p>
- <img alt="_images/prompt_fibonacci.png" src="_images/prompt_fibonacci.png" />
- <p>Since we are in an unconditional loop, you can send as many requests as you want, as long as they are understandable by the <em>fib</em> function.</p>
- <img alt="_images/prompt_fibonacci_more.png" src="_images/prompt_fibonacci_more.png" />
- <p>Inputting a string directly, such as <em>1</em> instead of <em>\1</em>, will make the Modelverse crash on the execution of this code.
- This is normal, though in the future the Modelverse will keep running for other users: only the current user’s code will be interrupted.</p>
- </div>
- <div class="section" id="modelling-server">
- <h2>Modelling Server<a class="headerlink" href="#modelling-server" title="Permalink to this headline">¶</a></h2>
- <p>The simple Fibonacci server is not very relevant to the primary concern of the Modelverse: (meta-)modelling.
- But since we can create whatever kind of server we want, a simple (meta-)modelling server is created.
- The code offers an interface for users to execute modelling operations.
- The interface itself mostly just relays the incoming messages and operations to the internal modelling library.</p>
- <p>The full code is seen below:</p>
- <div class="highlight-default"><div class="highlight"><pre>include "primitives.alh"
- include "constructors.alh"
- include "object_operations.alh"
- include "library.alh"
- include "conformance_scd.alh"
- include "io.alh"
- include "metamodels.alh"
- include "modelling.alh"
- include "compilation_manager.alh"
- Element function model_loaded(model : Element):
- String cmd
- Element attr_list_pn
- Element attr_keys_pn
- String attr_key_pn
- Element metamodel_element_pn
- String typename
- Boolean bottom
- Element other_metamodel
- bottom = False
- other_metamodel = create_node()
- dict_add(other_metamodel, "model", model["model"])
- dict_add(other_metamodel, "type_mapping", create_node())
- dict_add(other_metamodel, "metamodel", import_node("models/LTM_bottom"))
- dict_add(other_metamodel, "inheritance", other_metamodel["metamodel"]["model"]["__Inheritance"])
- output("Model loaded, ready for commands!")
- output("Use 'help' command for a list of possible commands")
- while (True):
- output("Please give your command.")
- cmd = input()
- if (cmd == "help"):
- output("Generic model operations:")
- output(" instantiate -- Create a new model element")
- output(" delete -- Delete an existing element")
- output(" attr_add -- Add an attribute to an element")
- output(" attr_del -- Delete an attribute of an element")
- output(" constrain -- Add a constraint function to the model")
- output(" rename -- Rename an existing element")
- output(" modify -- Modify the attributes of an element")
- output(" list -- Prints the list of elements in the model")
- output(" types -- Prints the list of elements that can be instantiated")
- output(" read -- Prints the current state of a model element")
- output(" verify -- Check whether the model conforms to the metamodel")
- output(" retype -- Change the type of an element")
- output(" switch -- Switch between conformance bottom and the linguistic metamodel")
- output(" exit -- Unload the model and go back to the loading prompt")
- elif (cmd == "exit"):
- return model!
- elif (cmd == "instantiate"):
- String mm_type_name
- output("Type to instantiate?")
- mm_type_name = input()
- if (dict_in(model["metamodel"]["model"], mm_type_name)):
- String element_name
- output("Name of new element?")
- element_name = input()
- if (dict_in(model["model"], element_name)):
- output("Element already exists; aborting")
- else:
- if (is_edge(model["metamodel"]["model"][mm_type_name])):
- output("Source name?")
- String src_name
- src_name = input()
- if (dict_in(model["model"], src_name)):
- output("Destination name?")
- String dst_name
- dst_name = input()
- if (dict_in(model["model"], dst_name)):
- instantiate_link(model, mm_type_name, element_name, src_name, dst_name)
- output("Instantiation successful!")
- else:
- output("Unknown destination; aborting")
- else:
- output("Unknown source; aborting")
- else:
- instantiate_node(model, mm_type_name, element_name)
- output("Instantiation successful!")
- else:
- output("Unknown type specified; aborting")
- elif (cmd == "set_inheritance"):
- String inh_name
- output("Which link in the metamodel is the inheritance link?")
- inh_name = input()
- if (dict_in(model["metamodel"]["model"], inh_name)):
- dict_add(model, "inheritance", model["metamodel"]["model"][inh_name])
- output("Set inheritance link!")
- else:
- output("Element not found in metamodel; aborting")
- elif (cmd == "constrain"):
- output("Element to constrain (empty for global)?")
- String model_name
- model_name = input()
- if (model_name == ""):
- // Global constraint
- output("Give input to function constructors for GLOBAL constraint!")
- set_model_constraints(model, construct_function())
- elif (dict_in(model["model"], model_name)):
- // Local constraint for this model
- output("Give input to function constructors for LOCAL constraint!")
- add_constraint(model, model_name, construct_function())
- output("Added constraint to model!")
- else:
- // Local constraint, but model not found
- output("Unknown model; aborting")
- elif (cmd == "modify"):
- String model_name
- output("Element to modify?")
- model_name = input()
- if (dict_in(model["model"], model_name)):
- Element attrs
- attrs = getAttributeList(model, model_name)
- String attr_name
- output("Attribute to modify?")
- attr_name = input()
- if (set_in(dict_keys(attrs), attr_name)):
- output("New value?")
- unset_attribute(model, model_name, attr_name)
- instantiate_attribute(model, model_name, attr_name, input())
- output("Modified!")
- else:
- output("No such attribute!")
- else:
- output("No such model!")
- elif (cmd == "attr_add"):
- String model_name
- output("Which model do you want to assign an attribute to?")
- model_name = input()
- if (dict_in(model["model"], model_name)):
- Element attrs
- attrs = getAttributeList(model, model_name)
- String attr_name
- output("Which attribute do you wish to assign?")
- attr_name = input()
- if (set_in(dict_keys(attrs), attr_name)):
- output("Value of attribute?")
- instantiate_attribute(model, model_name, attr_name, input())
- output("Added attribute!")
- else:
- output("No such attribute!")
- else:
- output("No such model!")
- elif (cmd == "attr_del"):
- String model_name
- output("Which model do you want to remove an attribute of?")
- model_name = input()
- if (dict_in(model["model"], model_name)):
- Element attrs
- attrs = getAttributeList(model, model_name)
- String attr_name
- output("Which attribute do you want to delete?")
- attr_name = input()
- if (set_in(dict_keys(attrs), attr_name)):
- unset_attribute(model, model_name, attr_name)
- output("Attribute deleted!")
- else:
- output("No such attribute!")
- else:
- output("No such model!")
- elif (cmd == "delete"):
- output("What is the name of the element you want to delete?")
- cmd = input()
- if (dict_in(model["model"], cmd)):
- model_delete_element(model, cmd)
- output("Deleted!")
- else:
- output("No such element; aborting")
- elif (cmd == "rename"):
- output("Old name?")
- String old_name_e
- old_name_e = input()
- if (dict_in(model["model"], old_name_e)):
- output("New name?")
- String new_name_e
- new_name_e = input()
- if (dict_in(model["model"], new_name_e)):
- output("New name already used; aborting")
- else:
- dict_add(model["model"], new_name_e, model["model"][old_name_e])
- dict_delete(model["model"], old_name_e)
- output("Rename complete!")
- else:
- output("Unknown element; aborting")
- elif (cmd == "list"):
- Element keys_m
- keys_m = dict_keys(model["model"])
- output("List of all elements:")
- String v_m
- while (read_nr_out(keys_m) > 0):
- v_m = set_pop(keys_m)
- // Filter out anonymous objects
- if (bool_not(string_startswith(v_m, "__"))):
- typename = reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][v_m]))
- output(((" " + v_m) + " : ") + typename)
- elif (cmd == "read"):
- output("Element to read?")
- cmd = input()
- if (dict_in(model["model"], cmd)):
- Element read_elem
- read_elem = model["model"][cmd]
- metamodel_element_pn = dict_read_node(model["type_mapping"], read_elem)
- output("Name: " + cmd)
- output("Type: " + reverseKeyLookup(model["metamodel"]["model"], metamodel_element_pn))
- if (is_edge(read_elem)):
- output("Source: " + reverseKeyLookup(model["model"], read_edge_src(read_elem)))
- output("Destination: " + reverseKeyLookup(model["model"], read_edge_dst(read_elem)))
- if (cast_v2s(read_elem) != "None"):
- output("Value: " + cast_v2s(read_elem))
- output("Defines attributes:")
- attr_list_pn = getInstantiatableAttributes(model, read_elem)
- attr_keys_pn = dict_keys(attr_list_pn)
- while (0 < read_nr_out(attr_keys_pn)):
- attr_key_pn = set_pop(attr_keys_pn)
- output((((" " + attr_key_pn) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])))
- output("Attributes:")
- attr_list_pn = getAttributeList(model, cmd)
- attr_keys_pn = dict_keys(attr_list_pn)
- while (0 < read_nr_out(attr_keys_pn)):
- attr_key_pn = set_pop(attr_keys_pn)
- output(((((" " + cast_v2s(attr_key_pn)) + " : ") + cast_v2s(attr_list_pn[attr_key_pn])) + " = ") + cast_v2s(read_attribute(model, reverseKeyLookup(model["model"], read_elem), attr_key_pn)))
- else:
- output("Unknown element; aborting")
- elif (cmd == "verify"):
- output(conformance_scd(model))
- elif (cmd == "types"):
- Element keys_t
- keys_t = dict_keys(model["metamodel"]["model"])
- output("List of types:")
- String v_t
- while (read_nr_out(keys_t) > 0):
- v_t = set_pop(keys_t)
- if (bool_not(string_startswith(v_t, "__"))):
- output(string_join((" " + v_t) + " : ", reverseKeyLookup(model["metamodel"]["metamodel"]["model"], dict_read_node(model["metamodel"]["type_mapping"], model["metamodel"]["model"][v_t]))))
- elif (cmd == "retype"):
- output("Element to retype?")
- String elementname
- elementname = input()
- if (dict_in(model["model"], elementname)):
- output("New type")
- typename = input()
- if (dict_in(model["metamodel"]["model"], typename)):
- // OK, do the retyping
- // First try removing the previous type if it exists
- dict_delete(model["type_mapping"], model["model"][elementname])
- // Now add the new type
- dict_add(model["type_mapping"], model["model"][elementname], model["metamodel"]["model"][typename])
- output("Retyped!")
- else:
- output("Unknown type; aborting")
- else:
- output("Unknown element; aborting")
- elif (cmd == "switch"):
- bottom = bool_not(bottom)
- Element tmp_model
- tmp_model = model
- model = other_metamodel
- other_metamodel = tmp_model
- if (bottom):
- // The type mapping we are using is probably not complete for our model
- // so we completely recreate it from the model we have.
- output("Switching to conformance bottom mode!")
- generate_bottom_type_mapping(model)
- else:
- // We already switched the models and such, so we are already done!
- output("Switching to linguistic metamodel!")
- else:
- output("Unknown command: " + cast_v2s(cmd))
- output("Use command 'help' to get a list of available commands")
- Element function main():
- output("Welcome to the Model Management Interface, running live on the Modelverse!")
- output("Use 'help' command for a list of possible commands")
- String command
- Element root
- Element metamodel
- String name
- Element my_model
- String mm_name
- root = create_metamodels()
- while (True):
- output("Please give your command.")
- command = input()
- if (command == "help"):
- output("Currently no model is loaded, so your operations are limited to:")
- output(" new -- Create a new model and save it for future use")
- output(" load -- Load a previously made model")
- output(" rename -- Rename a previously made model")
- output(" delete -- Delete a previously made model")
- output(" list -- Show a list of all stored models")
- output(" help -- Show a list of possible commands")
- elif (command == "new"):
- output("Metamodel to instantiate?")
- mm_name = input()
- if (dict_in(root, mm_name)):
- output("Name of model?")
- name = input()
- if (dict_in(root, name)):
- output("Model exists; aborting")
- else:
- my_model = instantiate_model(root[mm_name])
- dict_add(root, name, my_model)
- model_loaded(my_model)
- else:
- output("Unknown metamodel; aborting")
- elif (command == "load"):
- output("Model to load?")
- name = input()
- if (dict_in(root, name)):
- my_model = root[name]
- model_loaded(my_model)
- else:
- output("Model not found; aborting")
- elif (command == "list"):
- Element keys
- String m_menu_list
- keys = dict_keys(root)
- output("Found models:")
- while (read_nr_out(keys) > 0):
- m_menu_list = set_pop(keys)
- output(((" " + m_menu_list) + " : ") + reverseKeyLookup(root, root[m_menu_list]["metamodel"]))
- elif (command == "delete"):
- output("Model to delete?")
- name = input()
- if (dict_in(root, name)):
- dict_delete(root, name)
- output("Deleted!")
- else:
- output("Model not found; aborting")
- elif (command == "rename"):
- output("Old name?")
- String old_name
- old_name = input()
- if (dict_in(root, old_name)):
- output("New name?")
- String new_name
- new_name = input()
- if (dict_in(root, new_name)):
- output("Model exists; aborting")
- else:
- dict_add(root, new_name, root[old_name])
- dict_delete(root, old_name)
- output("Rename complete!")
- else:
- output("Model not found; aborting")
- elif (command == "actions"):
- output("Switching to compilation manager!")
- compilation_manager()
- output("Back in model manager!")
- else:
- output("Command not recognized, use 'help' for a list of possible commands")
- </pre></div>
- </div>
- <p>This code implements a very simple (meta-)modelling tool.
- Its use is documented with the provided <em>help</em> function.
- A simple example of its use is shown below.</p>
- <img alt="_images/prompt_pn_interface.png" src="_images/prompt_pn_interface.png" />
- <p>In this case, note that the value of tokens is the string 3 instead of the integer (or natural) 3.
- Therefore, the conformance check will flag this value as incorrectly typed.</p>
- </div>
- <div class="section" id="upload-model">
- <h2>Upload model<a class="headerlink" href="#upload-model" title="Permalink to this headline">¶</a></h2>
- <p>TODO: use the execute_model.py</p>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
- <div class="sphinxsidebarwrapper">
- <h3><a href="index.html">Table Of Contents</a></h3>
- <ul>
- <li><a class="reference internal" href="#">Examples</a><ul>
- <li><a class="reference internal" href="#fibonacci-server">Fibonacci Server</a></li>
- <li><a class="reference internal" href="#modelling-server">Modelling Server</a></li>
- <li><a class="reference internal" href="#upload-model">Upload model</a></li>
- </ul>
- </li>
- </ul>
- <h4>Previous topic</h4>
- <p class="topless"><a href="modellanguage.html"
- title="previous chapter">Modelling Language</a></p>
- <h4>Next topic</h4>
- <p class="topless"><a href="advanced.html"
- title="next chapter">Advanced examples</a></p>
- <div role="note" aria-label="source link">
- <h3>This Page</h3>
- <ul class="this-page-menu">
- <li><a href="_sources/examples.txt"
- rel="nofollow">Show Source</a></li>
- </ul>
- </div>
- <div id="searchbox" style="display: none" role="search">
- <h3>Quick search</h3>
- <form class="search" action="search.html" method="get">
- <div><input type="text" name="q" /></div>
- <div><input type="submit" value="Go" /></div>
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
- <script type="text/javascript">$('#searchbox').show(0);</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
- <div class="related" role="navigation" aria-label="related navigation">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="genindex.html" title="General Index"
- >index</a></li>
- <li class="right" >
- <a href="advanced.html" title="Advanced examples"
- >next</a> |</li>
- <li class="right" >
- <a href="modellanguage.html" title="Modelling Language"
- >previous</a> |</li>
- <li class="nav-item nav-item-0"><a href="index.html">Modelverse 0.4.0 documentation</a> »</li>
- </ul>
- </div>
- <div class="footer" role="contentinfo">
- © Copyright 2016, Yentl Van Tendeloo.
- Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.6.
- </div>
- </body>
- </html>
|