import unittest

import sys
try:
    from unit.utils import *
    import unit.log_output as log_output
except ImportError: #for Python2
    from utils import *
    import log_output as log_output

sys.path.append("wrappers")
from modelverse import *

import threading

model_hierarchy = \
            {"formalisms/": {"SimpleClassDiagrams": {},
                             "TypeMapping": {},
                             "Tracability": {},
                             "ProcessModel": {},
                             "ActionLanguage": {},
                             "ManualOperation": {},
                             "Bottom": {},
                            },
            "models/": {},
            "administration/": {"core": {},
                                "CoreFormalism": {},
                               },
            "users/": {"admin/": {},
                       "LoLA/": {},
                       "HUTN/": {},
                       "files/": {},
                       "DEVS/": {},
                       "DEVS_batch/": {},
                       "JSON/": {},
                      },
            "type mappings/": {"1": {},
                               "2": {},
                               "3": {},
                               "4": {},
                               "5": {},
                               "6": {},
                               "7": {},
                               "8": {},
                               "9": {},
                              },
            "merged/": {},
            "tmp/": {},
            "RAMified/": {},
            }

def verify_clean():
    compare_locations("", set())
    compare_locations("models", set())
    compare_locations("formalisms", set())

def get_model_list(location):
    try:
        location_parts = location.split("/")
        current = model_hierarchy
        while location_parts:
            l = location_parts.pop(0)
            if l != "":
                current = current[l + "/"]
        return set(current.keys())
    except:
        return set([])

def compare_locations(location, extra_to_default):
    assert model_list(location) == get_model_list(location) | set(extra_to_default)

def compare_unordered_lists(got, expected):
    assert len(got) == len(expected)
    for i in got:
        assert i in expected
    for i in expected:
        assert i in got

class TestModelverse(unittest.TestCase):
    proc = None

    def runTest(self):
        pass

    @classmethod
    def setUpClass(self):
        TestModelverse.proc, address = start_mvc()
        init(address)
        login("user", "user")

    @classmethod
    def tearDownClass(self):
        try:
            kill(TestModelverse.proc)
        except:
            print("Got exception during teardown.")

    def setUp(self):
        verify_clean()
        folder_create("users/user/test")

    def tearDown(self):
        model_delete("users/user/test")
        verify_clean()
        pass

    def test_op_model_list(self):
        assert model_list("") == set(["formalisms/",
                                      "models/",
                                      "administration/",
                                      "type mappings/",
                                      "users/",
                                      "merged/",
                                      "RAMified/",
                                      "tmp/",
                                     ])
        assert model_list("formalisms") == set(["SimpleClassDiagrams",
                                                "ActionLanguage",
                                                "TypeMapping",
                                                "Tracability",
                                                "ProcessModel",
                                                "ManualOperation",
                                                "Bottom",
                                               ])
        assert model_list("formalisms/") == set(["SimpleClassDiagrams",
                                                 "ActionLanguage",
                                                 "TypeMapping",
                                                 "Tracability",
                                                 "ProcessModel",
                                                 "ManualOperation",
                                                 "Bottom",
                                                ])

    def test_op_model_list_full(self):
        assert model_list_full("") == set([("formalisms/", "admin", "admin", "221"),
                                           ("models/", "admin", "admin", "221"),
                                           ("administration/", "admin", "admin", "110"),
                                           ("type mappings/", "admin", "admin", "221"),
                                           ("users/", "admin", "admin", "221"),
                                           ("merged/", "admin", "nobody", "222"),
                                           ("RAMified/", "admin", "nobody", "222"),
                                           ("tmp/", "admin", "nobody", "222"),
                                          ])
        assert model_list_full("formalisms") == set([("SimpleClassDiagrams", "admin", "admin", "221"),
                                                     ("ActionLanguage", "admin", "admin", "221"),
                                                     ("TypeMapping", "admin", "admin", "221"),
                                                     ("Tracability", "admin", "admin", "221"),
                                                     ("ProcessModel", "admin", "admin", "221"),
                                                     ("ManualOperation", "admin", "admin", "221"),
                                                     ("Bottom", "admin", "admin", "221"),
                                                    ])

    def test_op_verify(self):
        assert verify("formalisms/SimpleClassDiagrams", "formalisms/SimpleClassDiagrams") == "OK"
        assert verify("formalisms/SimpleClassDiagrams", "formalisms/Bottom") == "OK"
        for tm in model_types("formalisms/SimpleClassDiagrams"):
            assert verify(tm[1], "formalisms/TypeMapping") == "OK"
        try:
            verify("formalisms/SimpleClassDiagrams", "formalisms/ProcessModel")
            self.fail()
        except:
            pass

        assert verify("formalisms/ProcessModel", "formalisms/SimpleClassDiagrams") == "OK"
        assert verify("formalisms/ProcessModel", "formalisms/Bottom") == "OK"
        for tm in model_types("formalisms/ProcessModel"):
            assert verify(tm[1], "formalisms/TypeMapping") == "OK"
        try:
            verify("formalisms/ProcessModel", "formalisms/ProcessModel")
            self.fail()
        except:
            pass

        model_add("users/user/test/a", "formalisms/ProcessModel")
        assert verify("users/user/test/a", "formalisms/ProcessModel").startswith("Lower cardinality violated for class: ")
        instantiate("users/user/test/a", "Start")
        assert verify("users/user/test/a", "formalisms/ProcessModel") == "Lower cardinality violated for class: Finish"
        instantiate("users/user/test/a", "Finish")
        assert verify("users/user/test/a", "formalisms/ProcessModel") == "OK"

    def test_op_permission_modify(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])

        # Check that we can read
        element_list_nice("users/user/test/a")
        # Check that we can write
        instantiate("users/user/test/a", "Class")

        assert permission_modify("users/user/test/a", "100") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "100")])
        # Check that we can read
        element_list_nice("users/user/test/a")
        # Check that we can't write
        try:
            instantiate("users/user/test/a", "Class")
            self.fail()
        except WritePermissionDenied:
            pass

        assert permission_modify("users/user/test/a", "000") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "000")])
        # Check that we can't read
        try:
            element_list_nice("users/user/test/a")
            self.fail()
        except ReadPermissionDenied:
            pass
        # Check that we can't write
        try:
            instantiate("users/user/test/a", "Class")
            self.fail()
        except WritePermissionDenied:
            pass
        except ReadPermissionDenied:
            pass

        assert permission_modify("users/user/test/a", "200") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])

        # Check that we can read
        element_list_nice("users/user/test/a")
        # Check that we can write
        instantiate("users/user/test/a", "Class")

        # Cannot modify permissions if we don't own the model
        try:
            permission_modify("formalisms/SimpleClassDiagrams", "222")
            self.fail()
        except UserPermissionDenied:
            pass

        # Cannot modify permissions even if we have full access, as long as we are not the owner
        model_add("users/user/test/z", "formalisms/SimpleClassDiagrams")
        permission_owner("users/user/test/z", "admin")
        try:
            permission_modify("users/user/test/z", "222")
            self.fail()
        except UserPermissionDenied:
            pass
        try:
            permission_owner("users/user/test/z", "user")
            self.fail()
        except UserPermissionDenied:
            pass

    def test_op_model_add(self):
        assert model_list_full("users/user/test") == set([])
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        # Test permissions of newly created empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])

        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", \
            """
            Class A {
                name = "A"
            }
            Class B {
                name = "B"
                abstract = True
            }
            """)

        # Test permissions of newly created non-empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200"), ("b", "user", "nobody", "200")])
        # Test content of newly created model with code upload
        lst = element_list_nice("users/user/test/b")
        assert len(lst) == 2
        assert {'__id': "A", "__type": "Class", "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "A"} in lst
        assert {'__id': "B", "__type": "Class", "abstract": True, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "B"} in lst

        try:
            # Cannot upload model to occupied location
            model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
            self.fail()
        except ModelExists:
            assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200"), ("b", "user", "nobody", "200")])

        try:
            # Cannot upload model to unwritable location at root
            model_add("z", "formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200"), ("b", "user", "nobody", "200")])

        try:
            # Cannot upload model to unwritable location somewhere in hierarchy
            model_add("users/new_model", "formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            assert model_list("users/") == set(["user/", "admin/", "LoLA/", "pypdevs_batch_service/", "HUTN/", "JSON/", "file/", "pypdevs_service/"])

        try:
            # Cannot upload model to unwritable location somewhere in hierarchy with multiple new branches
            model_add("users/new_username/new_model", "formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            assert model_list("users/") == set(["user/", "admin/", "LoLA/", "pypdevs_batch_service/", "HUTN/", "JSON/", "file/", "pypdevs_service/"])

        model_add("users/user/test/c", "users/user/test/b", """
            A a {}
            """)
        try:
            # Cannot add this model, as it cannot be auto-typecasted to formalisms/SimpleClassDiagrams
            model_add("users/user/test/d", "users/user/test/c", """
                a c {}
                """)
            self.fail()
        except UnknownM3:
            assert model_list("users/user/test") == set(["a", "b", "c"])

        # We can add this model for multiple levels, as it can be automatically typecasted to formalisms/SimpleClassDiagrams, since it is empty
        model_add("users/user/test/d", "users/user/test/b")
        model_add("users/user/test/e", "users/user/test/d")
        model_add("users/user/test/f", "users/user/test/e")

    def test_op_model_add_compilation_error(self):
        try:
            model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
                abdfaefeaf
                """)
            self.fail()
        except CompilationError:
            pass

        try:
            model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", """
                Crass A {
                    name = "b"
                }
                """)
            #TODO at the moment, this still succeeds!
            #self.fail()
        except:
            raise
            pass

    def test_op_model_move(self):
        # Test basic scenario
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        assert model_move("users/user/test/a", "users/user/test/b") == None
        assert model_list_full("users/user/test") == set([("b", "user", "nobody", "200")])

        # Test if permissions are copied as well
        permission_modify("users/user/test/b", "222")
        assert model_list_full("users/user/test") == set([("b", "user", "nobody", "222")])
        assert model_move("users/user/test/b", "users/user/test/a") == None
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "222")])

        # Test move if destination exists
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "222"), ("b", "user", "nobody", "200")])
        try:
            model_move("users/user/test/a", "users/user/test/b")
            self.fail()
        except ModelExists:
            pass
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "222"), ("b", "user", "nobody", "200")])

        # Test move if source doesn't exist
        try:
            model_move("users/user/test/z", "users/user/test/y")
            self.fail()
        except UnknownModel:
            pass

        # Test if we can move a model we can't write to (i.e., not allowed to remove it)
        try:
            model_move("formalisms/ProcessModel", "users/user/test/x")
            self.fail()
        except WritePermissionDenied:
            pass

        # Test if we can move a model to a place we can't write to
        try:
            model_move("users/user/test/a", "administration/ProcessModel2")
            self.fail()
        except WritePermissionDenied:
            pass

    def test_op_model_delete(self):
        # Test basic delete
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        assert model_delete("users/user/test/a") == None
        assert model_list_full("users/user/test") == set([])

        # Test delete of non-existing
        assert model_list_full("users/user/test") == set([])
        try:
            model_delete("users/user/test/a")
            self.fail()
        except UnknownModel:
            pass

        # Test delete of non-writable models
        try:
            model_delete("formalisms/SimpleClassDiagrams")
            self.fail()
        except WritePermissionDenied:
            pass

    def test_op_model_overwrite(self):
        # Test overwrite of simple model
        assert model_list_full("users/user/test") == set([])
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        # Test permissions of newly created empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        assert element_list_nice("users/user/test/a") == []

        model_overwrite("users/user/test/a", \
            """
            Class A {
                name = "A"
            }
            Class B {
                name = "B"
                abstract = True
            }
            """)

        # Test permissions of newly created non-empty model
        assert model_list_full("users/user/test") == set([("a", "user", "nobody", "200")])
        # Test content of newly created model with code upload
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {'__id': "A", "__type": "Class", "abstract": None, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "A"} in lst
        assert {'__id': "B", "__type": "Class", "abstract": True, "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "name": "B"} in lst

        before = element_list_nice("formalisms/SimpleClassDiagrams")
        try:
            # Cannot overwrite non-writable model
            model_overwrite("formalisms/SimpleClassDiagrams", "")
            self.fail()
        except WritePermissionDenied:
            # No changes were made
            after = element_list_nice("formalisms/SimpleClassDiagrams")

            assert len(after) == len(before)
            for entry in after:
                assert entry in before

    def test_op_user_logout(self):
        try:
            element_list_nice("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        user_logout()
        login("admin", "admin")

        # Test user permissions
        assert len(element_list_nice("administration/core")) > 0

        user_logout()
        login("user", "user")

        try:
            element_list_nice("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass
    
    def test_op_all_instances(self):
        # Test M2 level
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B : A {}
            Association C (A, B) {}
            """)
        assert all_instances("users/user/test/a", "Class") == set(["A", "B", "C"])
        assert all_instances("users/user/test/a", "Association") == set(["C"])

        # Test non-existing type
        try:
            all_instances("users/user/test/a", "non-existing")
            self.fail()
        except UnknownElement:
            pass

        # Test non-existing model
        try:
            all_instances("users/user/test/b", "Class")
            self.fail()
        except UnknownModel:
            pass

        # Test M1 level
        model_add("users/user/test/b", "users/user/test/a", """
            A a {}
            B b {}
            C c(a, b) {}
            A d {}
            """)
        assert all_instances("users/user/test/b", "A") == set(["a", "b", "d"])
        assert all_instances("users/user/test/b", "B") == set(["b"])
        assert all_instances("users/user/test/b", "C") == set(["c"])

        # No read permission
        try:
            all_instances("administration/core", "Class")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(all_instances("formalisms/SimpleClassDiagrams", "Class")) == set

    def test_op_admin_promote_demote(self):
        # Verify that user doesn't have admin permissions
        try:
            element_list("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # Cannot do admin_promote ourselves
        try:
            admin_promote("user")
        except AdminPermissionDenied:
            pass

        # Cannot do admin_demote ourselves
        try:
            admin_demote("user")
        except AdminPermissionDenied:
            pass

        # Switch to admin for now
        user_logout()
        login("admin", "admin")
        admin_promote("user")
        user_logout()
        login("user", "user")

        # Verify that we can do admin operations
        assert len(element_list("administration/core")) > 0

        # And check that we can promote/demote other users
        admin_promote("HUTN")
        admin_demote("HUTN")

        # Can also demote ourselves again
        admin_demote("user")

        # Verify that user doesn't have admin permissions
        try:
            element_list("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

    def test_op_model_types(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")

        # Read model types of simple model
        lst = model_types("users/user/test/a")
        assert len(lst) == 1
        entry = lst.pop()
        assert entry[0] == "formalisms/SimpleClassDiagrams"
        assert entry[1].startswith("type mappings/")
        assert entry[2] == None

        # Create bottom and query afterwards
        alter_context("users/user/test/a", "formalisms/Bottom")
        element_list_nice("users/user/test/a")
        alter_context("users/user/test/a", "formalisms/SimpleClassDiagrams")

        lst = model_types("users/user/test/a")
        assert len(lst) == 2
        got = set()
        while lst:
            entry = lst.pop()
            assert entry[0] in ["formalisms/SimpleClassDiagrams", "formalisms/Bottom"]
            assert entry[0] not in got
            got.add(entry[0])
            assert entry[1].startswith("type mappings/")
            assert entry[2] == None

        # No type mapping for type mappings
        assert model_types(entry[1]) == set([("formalisms/TypeMapping", None, None)])

        # But there will be as soon as we open it
        element_list_nice(entry[1])
        lst = model_types(entry[1])
        assert len(lst) == 1
        entry = lst.pop()
        assert entry[0] == "formalisms/TypeMapping"
        assert entry[1].startswith("type mappings/")
        assert entry[2] == None

    def test_op_folder_create(self):
        # Create a folder
        assert folder_create("users/user/test/folder_a") == None
        assert model_list_full("users/user/test/") == set([("folder_a/", "user", "nobody", "200")])

        # Create folder with trailing slash
        assert folder_create("users/user/test/folder_b/") == None
        assert model_list_full("users/user/test/") == set([("folder_a/", "user", "nobody", "200"), ("folder_b/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_b") == set()

        # Create multiple folders
        assert folder_create("users/user/test/folder_a/folder_b/folder_c/folder_d") == None
        assert model_list_full("users/user/test/") == set([("folder_a/", "user", "nobody", "200"), ("folder_b/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_a") == set([("folder_b/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_a/folder_b") == set([("folder_c/", "user", "nobody", "200")])
        assert model_list_full("users/user/test/folder_a/folder_b/folder_c") == set([("folder_d/", "user", "nobody", "200")])

        # Create folder in non-writable location
        try:
            folder_create("a")
            self.fail()
        except WritePermissionDenied:
            pass
        assert "a" not in model_list("") 

        # Create folder that already exists
        try:
            folder_create("users/user/test/folder_a")
            self.fail()
        except FolderExists:
            pass

    def test_op_alter_context(self):
        # Create a model that we will use
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            """)

        # List elements using default context, that of creation
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst

        # List elements using bottom context
        alter_context("users/user/test/a", "formalisms/Bottom")
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {"__id": "start", "__type": "Node"} in lst
        assert {"__id": "finish", "__type": "Node"} in lst

        # Switch back to default context
        alter_context("users/user/test/a", "formalisms/ProcessModel")
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 2
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst

        # Switch to a non-existing context
        alter_context("users/user/test/a", "non-existing")
        try:
            element_list_nice("users/user/test/a")
            self.fail()
        except UnknownModel:
            pass

        # Set context for an unknown model
        # Note that this will only register locally, without communicating with the Modelverse
        # as such, errors are not noticed until the element is accessed!
        alter_context("non-existing", "formalisms/SimpleClassDiagrams")

    def test_op_element_list(self):
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        # Basic operation
        lst = element_list("users/user/test/a")
        assert len(lst) == 3
        assert ("start", "Start") in lst
        assert ("finish", "Finish") in lst
        assert ("nxt", "Next") in lst

        # Try on non-existing model
        try:
            element_list("a")
            self.fail()
        except UnknownModel:
            pass

        # Try a non-readable model
        try:
            element_list("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(element_list("formalisms/SimpleClassDiagrams")) == set

    def test_op_element_list_nice(self):
        # Test simple element_list_nice
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        # Basic operation
        lst = element_list_nice("users/user/test/a")
        assert len(lst) == 3
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst
        assert {"__id": "nxt", "__type": "Next", "__source": "start", "__target": "finish"} in lst

        # Test a model with attributes
        model_add("users/user/test/b", "formalisms/ProcessModel", """
            Start start {}
            Exec exec1 {
                name = "first exec"
            }
            Finish finish {}

            Data d1 {
                name = "data 1"
                type = "type 1"
            }

            Next n1 (start, exec1) {}
            Next n2 (exec1, finish) {}

            Produces p1 (exec1, d1) {
                name = "producer"
            }
            """)
        lst = element_list_nice("users/user/test/b")
        assert len(lst) == 7
        assert {"__id": "start", "__type": "Start"} in lst
        assert {"__id": "finish", "__type": "Finish"} in lst
        assert {"__id": "exec1", "__type": "Exec", "name": "first exec"} in lst
        assert {"__id": "d1", "__type": "Data", "name": "data 1", "type": "type 1"} in lst
        assert {"__id": "n1", "__type": "Next", "__source": "start", "__target": "exec1"} in lst
        assert {"__id": "n2", "__type": "Next", "__source": "exec1", "__target": "finish"} in lst
        assert {"__id": "p1", "__type": "Produces", "__source": "exec1", "__target": "d1", "name": "producer"} in lst

        # Try one with defined attributes
        model_add("users/user/test/c", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class transition {
                name = "Transition"
                name : String
            }
            Association p2t (place, transition) {
                name = "P2T"
                weight : Natural
            }
            Association t2p (transition, place) {
                name = "T2P"
                weight : Natural
            }
            """)

        lst = element_list_nice("users/user/test/c")
        assert len(lst) == 11
        assert {"__id": "Natural", "__type": "SimpleAttribute", "constraint": {"AL": ""}, "name": "natural"} in lst
        assert {"__id": "String", "__type": "SimpleAttribute", "constraint": {"AL": ""}, "name": "string"} in lst
        assert {"__id": "place", "name": "Place", "__type": "Class", "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "abstract": None} in lst
        assert {"__id": "transition", "name": "Transition", "__type": "Class", "constraint": {"AL": ""}, "lower_cardinality": None, "upper_cardinality": None, "abstract": None} in lst
        assert {"__id": "p2t", "name": "P2T", "__type": "Association", "__source": "place", "__target": "transition", "constraint": {"AL": ""}, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None} in lst
        assert {"__id": "t2p", "name": "T2P", "__type": "Association", "__source": "transition", "__target": "place", "constraint": {"AL": ""}, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None} in lst
        assert {"__id": "place_name", "__type": "AttributeLink", "__source": "place", "__target": "String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "place_capacity", "__type": "AttributeLink", "__source": "place", "__target": "Natural", "name": "capacity", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "transition_name", "__type": "AttributeLink", "__source": "transition", "__target": "String", "name": "name", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "p2t_weight", "__type": "AttributeLink", "__source": "p2t", "__target": "Natural", "name": "weight", "optional": False, "constraint": {"AL": ""}} in lst
        assert {"__id": "t2p_weight", "__type": "AttributeLink", "__source": "t2p", "__target": "Natural", "name": "weight", "optional": False, "constraint": {"AL": ""}} in lst

        # Try on non-existing model
        try:
            element_list_nice("a")
            self.fail()
        except UnknownModel:
            pass

        # Try a non-readable model
        try:
            element_list_nice("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(element_list_nice("formalisms/SimpleClassDiagrams")) == list

    def test_op_types(self):
        # Try operation of unopened model
        assert types("formalisms/ProcessModel") == element_list("formalisms/SimpleClassDiagrams")

        # Try simple operation
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        assert types("users/user/test/a") == element_list("formalisms/ProcessModel")
        alter_context("users/user/test/a", "formalisms/Bottom")
        assert types("users/user/test/a") == element_list("formalisms/Bottom")

        # Try for model that we cannot read
        try:
            types("administration/core")
            self.fail()
        except ReadPermissionDenied:
            pass
        
        # Try for non-existing model
        try:
            types("a")
            self.fail()
        except UnknownModel:
            pass

        # Try for a metamodel that we are not allowed to read, although the model may be read
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams")
        assert types("users/user/test/b") == element_list("formalisms/SimpleClassDiagrams")
        alter_context("users/user/test/b", "administration/core")
        try:
            types("users/user/test/b")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission for either M or MM, but can query
        assert type(types("formalisms/SimpleClassDiagrams")) == set

    def test_op_read_info(self):
        # Basic case
        model_add("users/user/test/a", "formalisms/ProcessModel", """
            Start start {}
            Finish finish {}
            Next nxt (start, finish) {}
            """)

        assert read_info("users/user/test/a", "start") == ("Start", None)
        assert read_info("users/user/test/a", "finish") == ("Finish", None)
        assert read_info("users/user/test/a", "nxt") == ("Next", ("start", "finish"))

        # Read using different metamodel
        alter_context("users/user/test/a", "formalisms/Bottom")
        assert read_info("users/user/test/a", "start") == ("Node", None)
        assert read_info("users/user/test/a", "finish") == ("Node", None)
        assert read_info("users/user/test/a", "nxt") == ("Edge", ("start", "finish"))

        # Non-existing element
        try:
            read_info("users/user/test/a", "notthere")
            self.fail()
        except UnknownElement:
            pass

        # Non-existing model
        try:
            read_info("users/user/test/b", "start")
            self.fail()
        except UnknownModel:
            pass

        # No read permission
        try:
            read_info("administration/core", "administration")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permission, but can query
        assert type(read_info("formalisms/SimpleClassDiagrams", "Class")) == tuple

    def test_op_read_attrs(self):
        # Basic behaviour
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "natural"}
        assert read_attrs("users/user/test/a", "String") == {"constraint": {"AL": ""}, "name": "string"}
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert read_attrs("users/user/test/a", "Transition") == {"constraint": {"AL": ""}, "name": "Transition", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "P2T", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert read_attrs("users/user/test/a", "T2P") == {"constraint": {"AL": ""}, "name": "T2P", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        model_add("users/user/test/b", "users/user/test/a", """
            Place p1 {
                name = "place 1"
                capacity = 2
            }
            Place p2 {
                name = "place 2"
                capacity = 1
            }
            Transition t1 {
                name = "transition"
            }
            
            P2T p2t (p1, t1) {
                weight = 4
            }
            T2P t2p (t1, p2) {
                weight = 10
            }
            """)
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": 2}
        assert read_attrs("users/user/test/b", "p2") == {"name": "place 2", "capacity": 1}
        assert read_attrs("users/user/test/b", "t1") == {"name": "transition"}
        assert read_attrs("users/user/test/b", "p2t") == {"weight": 4}
        assert read_attrs("users/user/test/b", "t2p") == {"weight": 10}

        # Non-existing model
        try:
            read_attrs("users/afa", "p1")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            read_attrs("users/user/test/a", "PPPPPPP")
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            read_attrs("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert type(read_attrs("formalisms/SimpleClassDiagrams", "Class")) == type(dict())

    def test_op_attr_assign(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "natural"}
        assert attr_assign("users/user/test/a", "Natural", "name", "nat") == None
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "nat"}

        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_assign("users/user/test/a", "Place", "name", "NewPlace") == None
        assert attr_assign("users/user/test/a", "Place", "abstract", False) == None
        assert attr_assign("users/user/test/a", "Place", "lower_cardinality", 4) == None
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "NewPlace", "abstract": False, "lower_cardinality": 4, "upper_cardinality": None}

        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "P2T", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_assign("users/user/test/a", "P2T", "name", "PPP") == None
        assert attr_assign("users/user/test/a", "P2T", "source_lower_cardinality", 1) == None
        assert attr_assign("users/user/test/a", "P2T", "target_upper_cardinality", 10) == None
        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "PPP", "source_lower_cardinality": 1, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": 10, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        model_add("users/user/test/b", "users/user/test/a", """
            Place p1 {
                name = "place 1"
                capacity = 2
            }
            Place p2 {
                name = "place 2"
                capacity = 1
            }
            Transition t1 {
                name = "transition"
            }
            
            P2T p2t (p1, t1) {
                weight = 4
            }
            T2P t2p (t1, p2) {
                weight = 10
            }
            """)
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": 2}
        assert attr_assign("users/user/test/b", "p1", "name", "place 3") == None
        assert attr_assign("users/user/test/b", "p1", "capacity", 3) == None
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 3", "capacity": 3}

        assert read_attrs("users/user/test/b", "p2t") == {"weight": 4}
        assert attr_assign("users/user/test/b", "p2t", "weight", 0) == None
        assert read_attrs("users/user/test/b", "p2t") == {"weight": 0}

        # Non-existing model
        try:
            attr_assign("users/afa", "p1", "name", "abc")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            attr_assign("users/user/test/a", "PPPPPPP", "name", "abc")
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            attr_assign("administration/core", "formalisms", "name", "abc")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        try:
            attr_assign("formalisms/SimpleClassDiagrams", "Class", "name", "abc")
            self.fail()
        except WritePermissionDenied:
            pass

        # No such attribute
        try:
            attr_assign("users/user/test/a", "Place", "ddd", 1)
            self.fail()
        except UnknownAttribute:
            pass

        # Assign None, equals to attr_delete
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "NewPlace", "abstract": False, "lower_cardinality": 4, "upper_cardinality": None}
        attr_assign("users/user/test/a", "Place", "name", None)
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": None, "abstract": False, "lower_cardinality": 4, "upper_cardinality": None}

    def test_op_attr_delete(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": "natural"}
        assert attr_delete("users/user/test/a", "Natural", "name") == None
        assert read_attrs("users/user/test/a", "Natural") == {"constraint": {"AL": ""}, "name": None}

        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_delete("users/user/test/a", "Place", "name") == None
        assert attr_delete("users/user/test/a", "Place", "abstract") == None
        assert attr_delete("users/user/test/a", "Place", "lower_cardinality") == None
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": "P2T", "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_delete("users/user/test/a", "P2T", "name") == None
        assert attr_delete("users/user/test/a", "P2T", "source_lower_cardinality") == None
        assert attr_delete("users/user/test/a", "P2T", "target_upper_cardinality") == None
        assert read_attrs("users/user/test/a", "P2T") == {"constraint": {"AL": ""}, "name": None, "source_lower_cardinality": None, "source_upper_cardinality": None, "target_lower_cardinality": None, "target_upper_cardinality": None, "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        model_add("users/user/test/b", "users/user/test/a", """
            Place p1 {
                name = "place 1"
                capacity = 2
            }
            Place p2 {
                name = "place 2"
                capacity = 1
            }
            Transition t1 {
                name = "transition"
            }
            
            P2T p2t (p1, t1) {
                weight = 4
            }
            T2P t2p (t1, p2) {
                weight = 10
            }
            """)
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": 2}
        assert attr_delete("users/user/test/b", "p1", "capacity") == None
        assert read_attrs("users/user/test/b", "p1") == {"name": "place 1", "capacity": None}

        assert read_attrs("users/user/test/b", "p2t") == {"weight": 4}
        assert attr_delete("users/user/test/b", "p2t", "weight") == None
        assert read_attrs("users/user/test/b", "p2t") == {"weight": None}

        # Non-existing model
        try:
            attr_delete("users/afa", "p1", "name")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            attr_delete("users/user/test/a", "PPPPPPP", "name")
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            attr_delete("administration/core", "formalisms", "name")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        try:
            attr_delete("formalisms/SimpleClassDiagrams", "Class", "name")
            self.fail()
        except WritePermissionDenied:
            pass

        # No such attribute
        try:
            attr_delete("users/user/test/a", "Place", "ddd")
            self.fail()
        except UnknownAttribute:
            pass

    def test_op_attr_delete_code(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)

        # Test what happens if removing a non-existing AL attribute
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        attr_delete("users/user/test/a", "Place", "constraint")
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

        # Test what happens if removing an existing AL attribute
        attr_assign_code("users/user/test/a", "Place", "constraint", """
            String function a(value : Element):
                return "OK"!
            """)
        attrs = read_attrs("users/user/test/a", "Place")
        assert "AL" in attrs["constraint"]
        assert attrs["constraint"]["AL"] != ""
        attr_delete("users/user/test/a", "Place", "constraint")
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}

    def test_op_attr_assign_code(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute Natural {
                name = "natural"
            }
            SimpleAttribute String {
                name = "string"
            }

            Class Place {
                name = "Place"
                name : String
                capacity : Natural
            }
            Class Transition {
                name = "Transition"
                name : String
            }
            Association P2T (Place, Transition) {
                name = "P2T"
                weight : Natural
            }
            Association T2P (Transition, Place) {
                name = "T2P"
                weight : Natural
            }
            """)

        # Test ordinary case
        assert read_attrs("users/user/test/a", "Place") == {"constraint": {"AL": ""}, "name": "Place", "abstract": None, "lower_cardinality": None, "upper_cardinality": None}
        assert attr_assign_code("users/user/test/a", "Place", "constraint", """
            String function a(value : Element):
                return "OK"!
            """) == None
        attrs = read_attrs("users/user/test/a", "Place")
        assert "AL" in attrs["constraint"]
        assert attrs["constraint"]["AL"] != ""

        assert AL_text(attrs["constraint"]["AL"]).strip() == """
            String function a(value : Element):
                return "OK"!
            """.strip()

        # Test assign when already assigned
        attrs = read_attrs("users/user/test/a", "Place")
        old_location = attrs["constraint"]["AL"]
        old_text = AL_text(attrs["constraint"]["AL"])
        assert attr_assign_code("users/user/test/a", "Place", "constraint", """
            String function a(value : Element):
                return "NOK"!
            """) == None
        attrs = read_attrs("users/user/test/a", "Place")
        assert "AL" in attrs["constraint"]
        assert attrs["constraint"]["AL"] != ""
        assert attrs["constraint"]["AL"] != old_location
        assert AL_text(attrs["constraint"]["AL"]).strip() == """
            String function a(value : Element):
                return "NOK"!
            """.strip()
        assert AL_text(attrs["constraint"]["AL"]).strip () != old_text.strip()

        simple_code = \
            """
                String function a(value : Element):
                    return "OK"!
            """

        # Non-existing model
        try:
            attr_assign_code("users/afa", "p1", "name", simple_code)
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        try:
            attr_assign_code("users/user/test/a", "PPPPPPP", "name", simple_code)
            self.fail()
        except UnknownElement:
            pass

        # No read permissions
        try:
            attr_assign_code("administration/core", "formalisms", "name", simple_code)
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        try:
            attr_assign_code("formalisms/SimpleClassDiagrams", "Class", "name", simple_code)
            self.fail()
        except WritePermissionDenied:
            pass

        # No such attribute
        try:
            attr_assign_code("users/user/test/a", "Place", "ddd", simple_code)
            self.fail()
        except UnknownAttribute:
            pass

        # Compilation error
        try:
            attr_assign_code("users/user/test/a", "Place", "constraint", "abc")
            self.fail()
        except CompilationError:
            pass

    def test_op_instantiate(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams")
        assert element_list("users/user/test/a") == set([])

        # Instantiate node element
        ID = instantiate("users/user/test/a", "Class")
        assert element_list("users/user/test/a") == set([(ID, "Class")])
        assert read_info("users/user/test/a", ID) == ("Class", None)

        # Instantiate node element with ID
        ID2 = instantiate("users/user/test/a", "Class", ID="new_node")
        assert ID2 == "new_node"
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class")])
        assert read_info("users/user/test/a", ID2) == ("Class", None)

        # Instantiate edge element
        ID3 = instantiate("users/user/test/a", "Association", edge=(ID, ID2))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association")])
        assert read_info("users/user/test/a", ID3) == ("Association", (ID, ID2))

        # Instantiate edge element with ID
        ID4 = instantiate("users/user/test/a", "Association", edge=(ID, ID2), ID="new_edge")
        assert ID4 == "new_edge"
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association")])
        assert read_info("users/user/test/a", ID4) == ("Association", (ID, ID2))

        # Instantiate edge from edge (source)
        ID5 = instantiate("users/user/test/a", "Association", edge=(ID3, ID))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association"), (ID5, "Association")])
        assert read_info("users/user/test/a", ID5) == ("Association", (ID3, ID))

        # Instantiate edge from edge (target)
        ID6 = instantiate("users/user/test/a", "Association", edge=(ID, ID3))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association"), (ID5, "Association"), (ID6, "Association")])
        assert read_info("users/user/test/a", ID6) == ("Association", (ID, ID3))

        # Instantiate edge from edge (source and target)
        ID7 = instantiate("users/user/test/a", "Association", edge=(ID3, ID4))
        assert element_list("users/user/test/a") == set([(ID, "Class"), (ID2, "Class"), (ID3, "Association"), (ID4, "Association"), (ID5, "Association"), (ID6, "Association"), (ID7, "Association")])
        assert read_info("users/user/test/a", ID7) == ("Association", (ID3, ID4))

        # Non-existing model
        try:
            instantiate("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            instantiate("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        before = element_list("users/user/test/a")
        try:
            instantiate("formalisms/SimpleClassDiagrams", "Class")
            self.fail()
        except WritePermissionDenied:
            assert element_list("users/user/test/a") == before

        # Pre-existing ID requested (node)
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Class", ID=ID)
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Pre-existing ID requested (edge)
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=(ID2, ID3))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Edge from non-existing ID
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=("non-existing", ID3))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Edge to non-existing ID
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=("non-existing", ID3))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Edge both non-existing ID
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", ID=ID, edge=("non-existing", "non-existing2"))
            self.fail()
        except ElementExists:
            assert element_list("users/user/test/a") == before

        # Garbage type
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", ("a", "b"))
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Garbage edge src
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", edge=(('a', 'b'), "non-existing2"))
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Garbage edge dst
        before = element_list("users/user/test/a")
        try:
            instantiate("users/user/test/a", "Association", edge=("non-existing", ('a', 'b')))
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

    def test_op_delete_element(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {}
            """)

        # Test simple delete
        assert len(element_list_nice("users/user/test/a")) == 1
        assert delete_element("users/user/test/a", "A") == None
        assert len(element_list_nice("users/user/test/a")) == 0

        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B {}
            Association (A, B) {}
            """)

        # Test delete of element that causes other elements to be deleted as well
        assert len(element_list_nice("users/user/test/b")) == 3
        assert delete_element("users/user/test/b", "A") == None
        assert len(element_list_nice("users/user/test/b")) == 1

        model_add("users/user/test/c", "formalisms/SimpleClassDiagrams", """
            Class A {}
            Class B {}
            Association C (A, B) {}
            """)

        # Test delete of edge that causes other elements to be deleted as well
        assert len(element_list_nice("users/user/test/c")) == 3
        assert delete_element("users/user/test/c", "C") == None
        assert len(element_list_nice("users/user/test/c")) == 2

        model_add("users/user/test/d", "formalisms/SimpleClassDiagrams", """
            Class A {
                name = "A"
            }
            """)

        # Test delete of a "hidden" element
        assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": "A", "abstract": None}]
        ID = [i for i in element_list("users/user/test/d") if i[1] == "Class_name"][0][0]
        assert delete_element("users/user/test/d", ID) == None
        assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": None, "abstract": None}]

        # Non-existing model
        try:
            delete_element("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            delete_element("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            delete_element("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions
        before = element_list("formalisms/SimpleClassDiagrams")
        try:
            delete_element("formalisms/SimpleClassDiagrams", "Class")
            self.fail()
        except WritePermissionDenied:
            assert element_list("formalisms/SimpleClassDiagrams") == before

        # Give garbage
        assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": None, "abstract": None}]
        try:
            delete_element("users/user/test/d", ('abc', 'def'))
        except UnknownElement:
            assert element_list_nice("users/user/test/d") == [{"__type" : "Class", "__id": "A", "lower_cardinality": None, "upper_cardinality": None, "constraint": {"AL": ""}, "name": None, "abstract": None}]

    def test_op_AL_text(self):
        # Test normal situation with code provided by assignment
        model_add("users/user/test/b", "formalisms/SimpleClassDiagrams", """
            Class A {}
            """)
        attr_assign_code("users/user/test/b", "A", "constraint", """
            String function main(value : Element):
                return "OK"!
            """)
        assert AL_text(read_attrs("users/user/test/b", "A")["constraint"]["AL"]).strip() == """
            String function main(value : Element):
                return "OK"!
            """.strip()

        # Test normal situation with code provided by HUTN parser (model)
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            Class A {
                constraint = $
                        String function main(value : Element):
                            return "OK"!
                    $
            }
            """)
        assert AL_text(read_attrs("users/user/test/a", "A")["constraint"]["AL"]).strip() == 'String function main(value : Element):\n\treturn "OK"!'

        # Test AL_text on non-existing location
        try:
            AL_text("abc")
            self.fail()
        except UnknownLocation:
            pass

        # No permissions on locations directly, so there are no checks that have to be done

    def test_op_read_outgoing(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association D (C, B) {}
            """)

        # Test normal operation
        assert read_outgoing("users/user/test/a", "A", "Association") == set(["C"])
        assert read_outgoing("users/user/test/a", "A", "AttributeLink") == set(["A_name"])

        # Test wildcard
        assert read_outgoing("users/user/test/a", "A", "") == set(["A_name", "C"])

        # Test association on association
        assert read_outgoing("users/user/test/a", "C", "") == set(["D"])

        # Test empty result
        assert read_outgoing("users/user/test/a", "B", "") == set([])

        # Test simpleattribute
        assert read_outgoing("users/user/test/a", "String", "") == set([])

        # Non-existing model
        try:
            read_outgoing("users/afa", "Association", "")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_outgoing("users/user/test/a", "AAAAAAA", "")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing type
        before = element_list("users/user/test/a")
        try:
            read_outgoing("users/user/test/a", "A", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_outgoing("administration/core", "formalisms", "")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert type(read_outgoing("formalisms/SimpleClassDiagrams", "Class", "")) == type(set())

    def test_op_read_incoming(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association E (A, A) {}
            Association D (C, B) {}
            Association F (D, E) {}
            """)

        # Test normal operation
        assert read_incoming("users/user/test/a", "B", "Association") == set(["C", "D"])

        # Test wildcard
        assert read_incoming("users/user/test/a", "B", "") == set(["C", "D"])

        # Test association on association
        assert read_incoming("users/user/test/a", "E", "") == set(["F"])

        # Test double result
        assert read_incoming("users/user/test/a", "A", "") == set(["E"])

        # Test simpleattribute
        assert read_incoming("users/user/test/a", "String", "") == set(["A_name"])

        # Test empty result
        assert read_incoming("users/user/test/a", "F", "") == set([])

        # Non-existing model
        try:
            read_incoming("users/afa", "Association", "")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_incoming("users/user/test/a", "AAAAAAA", "")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing type
        before = element_list("users/user/test/a")
        try:
            read_incoming("users/user/test/a", "A", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_incoming("administration/core", "formalisms", "")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert type(read_incoming("formalisms/SimpleClassDiagrams", "Class", "")) == type(set())

    def test_op_read_association_source(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association E (A, A) {}
            Association D (C, B) {}
            Association F (D, E) {}
            """)

        # Test normal operation
        assert read_association_source("users/user/test/a", "C") == "A"

        # Test association on association
        assert read_association_source("users/user/test/a", "D") == "C"

        # Test node
        try:
            assert read_association_source("users/user/test/a", "A")
            self.fail()
        except NotAnAssociation:
            pass

        # Non-existing model
        try:
            read_association_source("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_association_source("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_association_source("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert read_association_source("formalisms/SimpleClassDiagrams", "Association") == "Class"

    def test_op_read_association_destination(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name : String
            }
            Class B {}
            Association C (A, B) {}
            Association E (A, A) {}
            Association D (C, B) {}
            Association F (D, E) {}
            """)

        # Test normal operation
        assert read_association_destination("users/user/test/a", "C") == "B"

        # Test association on association
        assert read_association_destination("users/user/test/a", "F") == "E"

        # Test node
        try:
            assert read_association_destination("users/user/test/a", "A")
            self.fail()
        except NotAnAssociation:
            pass

        # Non-existing model
        try:
            read_association_destination("users/afa", "Association")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element
        before = element_list("users/user/test/a")
        try:
            read_association_destination("users/user/test/a", "AAAAAAA")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            read_association_destination("administration/core", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert read_association_destination("formalisms/SimpleClassDiagrams", "Association") == "Class"

    def test_op_connections_between(self):
        model_add("users/user/test/a", "formalisms/SimpleClassDiagrams", """
            SimpleAttribute String {}
            Class A {
                name1 : String
                name2 : String
            }
            Class B {}
            Association C (A, B) {}
            Association D (A, B) {}
            Association E (A, A) {}
            Association F (C, B) {}
            Association G (E, F) {}
            """)

        # Test normal operation
        assert connections_between("users/user/test/a", "A", "A") == set(["Association", "Inheritance"])
        assert connections_between("users/user/test/a", "A", "B") == set(["Association", "Inheritance"])
        assert connections_between("users/user/test/a", "A", "C") == set(["Association", "Inheritance"])

        # Test none allowed
        assert connections_between("users/user/test/a", "A_name1", "A_name2") == set([])

        # Non-existing model
        try:
            connections_between("users/afa", "A", "B")
            self.fail()
        except UnknownModel:
            pass

        # Non-existing element (1)
        before = element_list("users/user/test/a")
        try:
            connections_between("users/user/test/a", "AA", "B")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # Non-existing element (2)
        before = element_list("users/user/test/a")
        try:
            connections_between("users/user/test/a", "A", "BB")
            self.fail()
        except UnknownElement:
            assert element_list("users/user/test/a") == before

        # No read permissions
        try:
            connections_between("administration/core", "formalisms", "formalisms")
            self.fail()
        except ReadPermissionDenied:
            pass

        # No write permissions, but can query
        assert connections_between("formalisms/SimpleClassDiagrams", "Association", "Association") == set(["Association", "Inheritance"])
        
    """
    def test_op_model_render(self):
    def test_op_transformation_between(self):
    def test_op_transformation_add_MT(self):
    def test_op_transformation_add_AL(self):
    def test_op_transformation_add_MANUAL(self):
    def test_op_transformation_execute_MT(self):
    def test_op_transformation_execute_AL(self):
    def test_op_transformation_execute_MANUAL(self):
    def test_op_transformation_signature(self):
    def test_op_process_signature(self):
    def test_op_permission_group(self):
    def test_op_permission_owner(self):
    def test_op_group_create(self):
    def test_op_group_delete(self):
    def test_op_group_owner_add(self):
    def test_op_group_owner_delete(self):
    def test_op_group_join(self):
    def test_op_group_kick(self):
    def test_op_group_list(self):
    def test_op_conformance_delete(self):
    def test_op_conformance_add(self):
    def test_op_define_attribute(self):
    """

    def test_modelling(self):
        # Add a model
        model_add("users/user/test/Empty", "formalisms/SimpleClassDiagrams")

        # Check that it exists
        compare_locations("users/user/test", set(["Empty"]))

        # Check that it conforms
        assert verify("users/user/test/Empty", "formalisms/SimpleClassDiagrams") == "OK"

        # Check for permissions
        assert model_list_full("users/user/test") == set([(name, "user", "nobody", "221") for name in get_model_list("users/user/test")]) | \
                                                set([("Empty", "user", "nobody", "200")])

        # Instantiate it further
        model_add("users/user/test/my_empty", "users/user/test/Empty")
        assert verify("users/user/test/my_empty", "users/user/test/Empty") == "OK"

        # Check that it exists
        compare_locations("users/user/test", set(["my_empty", "Empty"]))

        # Check that an instantiate of "A" fails
        try:
            res = instantiate("users/user/test/my_empty", "A")
            assert False
        except UnknownIdentifier:
            assert verify("users/user/test/Empty", "formalisms/SimpleClassDiagrams") == "OK"

        # Create something in the formalism
        instantiate("users/user/test/Empty", "Class", ID="A")
        attr_assign("users/user/test/Empty", "A", "name", "A")
        assert verify("users/user/test/Empty", "formalisms/SimpleClassDiagrams") == "OK"

        # Now instantiate that in the model as well, which now works
        instantiate("users/user/test/my_empty", "A")
        assert verify("users/user/test/my_empty", "users/user/test/Empty") == "OK"

    def test_overwrite(self):
        model_add("users/user/test/Empty", "formalisms/SimpleClassDiagrams")
        assert element_list("users/user/test/Empty") == set([])

        instantiate("users/user/test/Empty", "Class", ID="A")
        assert element_list("users/user/test/Empty") == set([("A", "Class")])

        model_overwrite("users/user/test/Empty", "")
        assert element_list("users/user/test/Empty") == set([])
        compare_locations("users/user/test", set(["Empty"]))
        
        assert element_list("users/user/test/Empty") == set([])

        instantiate("users/user/test/Empty", "Class", ID="B")
        compare_locations("users/user/test", set(["Empty"]))

    def test_operations(self):
        log = []

        def manual_callback(model):
            p1 = instantiate(model, "PetriNet_Runtime/Place")
            p2 = instantiate(model, "PetriNet_Runtime/Place")
            p3 = instantiate(model, "PetriNet_Runtime/Place")
            t1 = instantiate(model, "PetriNet_Runtime/Transition")
            p2t1 = instantiate(model, "PetriNet_Runtime/P2T", (p1, t1))
            p2t2 = instantiate(model, "PetriNet_Runtime/P2T", (p2, t1))
            t2p1 = instantiate(model, "PetriNet_Runtime/T2P", (t1, p3))
            attr_assign(model, p1, "tokens", 1)
            attr_assign(model, p1, "name", "p1")
            attr_assign(model, p2, "tokens", 2)
            attr_assign(model, p2, "name", "p2")
            attr_assign(model, p3, "tokens", 3)
            attr_assign(model, p3, "name", "p3")
            attr_assign(model, t1, "name", "t1")
            attr_assign(model, t1, "executing", False)
            attr_assign(model, p2t1, "weight", 1)
            attr_assign(model, p2t2, "weight", 1)
            attr_assign(model, t2p1, "weight", 2)

        model_add("users/user/test/PetriNet", "formalisms/SimpleClassDiagrams", open("integration/code/pn_design.mvc", "r").read())
        model_add("users/user/test/PetriNet_Runtime", "formalisms/SimpleClassDiagrams", open("integration/code/pn_runtime.mvc", "r").read())

        model_add("users/user/test/my_pn", "users/user/test/PetriNet", open("integration/code/pn_design_model.mvc", "r").read())

        def add_tracability_D2R(model):
            instantiate(model, "Association", ("PetriNet/Place", "PetriNet_Runtime/Place"), ID="D2R_PlaceLink")
            instantiate(model, "Association", ("PetriNet/Transition", "PetriNet_Runtime/Transition"), ID="D2R_TransitionLink")

        def add_tracability_R2D(model):
            instantiate(model, "Association", ("PetriNet_Runtime/Place", "PetriNet/Place"), ID="R2D_PlaceLink")
            instantiate(model, "Association", ("PetriNet_Runtime/Transition", "PetriNet/Transition"), ID="R2D_TransitionLink")

        transformation_add_MT({"PetriNet": "users/user/test/PetriNet"}, {}, "users/user/test/print_pn", open("integration/code/pn_print.mvc").read())
        transformation_add_MANUAL({"PetriNet": "users/user/test/PetriNet"}, {"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, "users/user/test/pn_design_to_runtime", add_tracability_D2R)
        transformation_add_AL({"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, {"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, "users/user/test/pn_simulate", open("integration/code/pn_simulate.alc").read())
        transformation_add_MT({"PetriNet_Runtime": "users/user/test/PetriNet_Runtime"}, {"PetriNet": "users/user/test/PetriNet"}, "users/user/test/pn_runtime_to_design", open("integration/code/pn_runtime_to_design.mvc").read(), add_tracability_R2D)

        log = []
        ctrl = log_output.Controller(log, keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        thrd.start()

        assert transformation_execute_MT("users/user/test/print_pn", {"PetriNet": "users/user/test/my_pn"}, {}, (ctrl, "inp", "outp")) == True
        thrd.join()
        assert set(log) == set(['"p1" --> 1',
                                '"p2" --> 2',
                                '"p3" --> 3'])

        assert transformation_execute_MANUAL("users/user/test/pn_design_to_runtime", {"PetriNet": "users/user/test/my_pn"}, {"PetriNet_Runtime": "users/user/test/my_pn_RT"}, manual_callback) == True
        assert transformation_execute_AL("users/user/test/pn_simulate", {"PetriNet_Runtime": "users/user/test/my_pn_RT"}, {"PetriNet_Runtime": "users/user/test/my_pn_RT"}) == True
        assert transformation_execute_MT("users/user/test/pn_runtime_to_design", {"PetriNet_Runtime": "users/user/test/my_pn_RT"}, {"PetriNet": "users/user/test/my_pn"}) == True

        log = []
        ctrl = log_output.Controller(log, keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        thrd.start()

        assert transformation_execute_MT("users/user/test/print_pn", {"PetriNet": "users/user/test/my_pn"}, {}, (ctrl, "inp", "outp")) == True
        thrd.join()
        assert set(log) == set(['"p1" --> 0',
                                '"p2" --> 1',
                                '"p3" --> 5'])

    def test_process_model_trivial_pn_subfunction(self):
        model_add("users/user/test/PetriNet", "formalisms/SimpleClassDiagrams", open("integration/code/pn_design.mvc", "r").read())
        model_add("users/user/test/ReachabilityGraph", "formalisms/SimpleClassDiagrams", open("integration/code/reachability_graph.mvc", "r").read())
        model_add("users/user/test/pn_reachability", "formalisms/ProcessModel", open("integration/code/pm_pn_reachability.mvc", "r").read())
        transformation_add_MT({}, {"PetriNet": "users/user/test/PetriNet"}, "users/user/test/initialize_PN", open("integration/code/initialize_PN.mvc", "r").read())
        transformation_add_MANUAL({"PetriNet": "users/user/test/PetriNet"}, {"PetriNet": "users/user/test/PetriNet"}, "users/user/test/refine_PN")
        transformation_add_AL({"PetriNet": "users/user/test/PetriNet"}, {"ReachabilityGraph": "users/user/test/ReachabilityGraph"}, "users/user/test/reachability", open("integration/code/reachability_subfunction.alc", "r").read())
        transformation_add_MT({"ReachabilityGraph": "users/user/test/ReachabilityGraph"}, {}, "users/user/test/reachability_print", open("integration/code/reachabilitygraph_print.mvc", 'r').read())

        def callback_refine_PN(model):
            p1 = instantiate(model, "PetriNet/Place")
            attr_assign(model, p1, "name", "p1")
            attr_assign(model, p1, "tokens", 1)

            t1 = instantiate(model, "PetriNet/Transition")
            attr_assign(model, t1, "name", "t1")

            p2t = instantiate(model, "PetriNet/P2T", (p1, t1))
            attr_assign(model, p2t, "weight", 1)

        log = []
        ctrl = log_output.Controller(log, keep_running=False)
        thrd = threading.Thread(target=ctrl.start)
        thrd.daemon = True
        thrd.start()

        process_execute("users/user/test/pn_reachability", {}, {"users/user/test/refine_PN": callback_refine_PN, "users/user/test/reachability_print": (ctrl, "inp", "outp")})
        thrd.join()

        assert set(log) == set(['"0": {"p1": 1}',
                                '"1": {"p1": 0}',
                                '"0" --["t1"]--> "1"'])

    def test_render(self):
        model_add("users/user/test/CausalBlockDiagrams", "formalisms/SimpleClassDiagrams", open("integration/code/cbd_design.mvc", 'r').read())
        model_add("users/user/test/MM_rendered_graphical", "formalisms/SimpleClassDiagrams", open("integration/code/MM_rendered_graphical.mvc", 'r').read())
        model_add("users/user/test/my_CBD", "users/user/test/CausalBlockDiagrams", open("integration/code/my_cbd.mvc", 'r').read())

        def add_tracability(model):
            instantiate(model, "Association", ("abstract/Block", "rendered/Group"), ID="TracabilityLink")

        transformation_add_MT({"abstract": "users/user/test/CausalBlockDiagrams", "rendered": "users/user/test/MM_rendered_graphical"}, {"abstract": "users/user/test/CausalBlockDiagrams", "rendered": "users/user/test/MM_rendered_graphical"}, "users/user/test/render_graphical_CBD", open("integration/code/CBD_mapper.mvc", 'r').read(), add_tracability)
        result = model_render("users/user/test/my_CBD", "users/user/test/render_graphical_CBD", "users/user/test/my_perceptualized_CBD")

        assert len(result) == 23

    def test_SCCD_basic(self):
        model_add("users/user/test/SCCD", "formalisms/SimpleClassDiagrams", open("integration/code/SCCD.mvc", 'r').read())
        model_add("users/user/test/SCCD_Trace", "formalisms/SimpleClassDiagrams", open("integration/code/SCCD_Trace.mvc", 'r').read())

        model_add("users/user/test/my_SCCD", "users/user/test/SCCD", open("integration/code/SCCD_all.mvc", 'r').read())

        transformation_add_AL({"SCCD": "users/user/test/SCCD"}, {"trace": "users/user/test/SCCD_Trace"}, "users/user/test/SCCD_execute_afap", open("integration/code/SCCD_execute.alc", 'r').read().replace("afap = False", "afap = True"))
        transformation_execute_AL("users/user/test/SCCD_execute_afap", {"SCCD": "users/user/test/my_SCCD"}, {"trace": "users/user/test/my_SCCD_trace"})

        alter_context("users/user/test/my_SCCD_trace", "users/user/test/SCCD_Trace")
        lst = element_list_nice("users/user/test/my_SCCD_trace")

        lst.sort(key=lambda i: (i["timestamp"], i["name"]))
        result = [(i["timestamp"], str(i["name"])) for i in lst if i["name"] not in ["updateTimerValue", "updateTimerColour", "resetTimer"]]

        assert result == [(5.0, "displayRed"),
                          (20.0, "displayYellow"),
                          (20.5, "displayNone"),
                          (21.0, "displayYellow"),
                          (21.5, "displayNone"),
                          (22.0, "displayYellow"),
                          (22.5, "displayNone"),
                          (23.0, "displayYellow"),
                          (23.5, "displayNone"),
                          (24.0, "displayYellow"),
                          (24.5, "displayNone"),
                          (25.0, "displayYellow"),
                          (25.5, "displayNone"),
                          (26.0, "displayYellow"),
                          (26.5, "displayNone"),
                          (27.0, "displayYellow"),
                          (27.5, "displayNone"),
                          (28.0, "displayYellow"),
                          (28.5, "displayNone"),
                          (29.0, "displayYellow"),
                          (29.4, "displayNone"),
                          (29.4, "displayRed"),
                          (89.4, "displayGreen"),
                          (129.4, "displayNone"),
                          (129.4, "displayRed"),
                          (139.4, "displayYellow"),
                          (139.9, "displayNone"),
                          (140.4, "displayYellow"),
                          (140.9, "displayNone"),
                          (141.4, "displayYellow"),
                          (141.9, "displayNone"),
                          (142.4, "displayYellow"),
                          (142.9, "displayNone"),
                          (143.4, "displayYellow"),
                          (143.9, "displayNone"),
                          (144.4, "displayYellow"),
                          (144.9, "displayNone"),
                          (145.4, "displayYellow"),
                          (145.9, "displayNone"),
                          (146.4, "displayYellow"),
                          (146.9, "displayNone"),
                          (147.4, "displayYellow"),
                          (147.9, "displayNone"),
                          (148.4, "displayYellow"),
                         ]

    def test_switch_MM(self):
        model_add("users/user/test/PetriNet", "formalisms/SimpleClassDiagrams", open("integration/code/pn_design.mvc", "r").read())
        model_add("users/user/test/my_pn", "users/user/test/PetriNet", open("integration/code/pn_design_model.mvc", "r").read())

        got = element_list_nice("users/user/test/PetriNet")
        expected = \
            [{'__id': 'Natural', '__type': 'SimpleAttribute', 'constraint': {'AL': ''}, 'name': 'Natural'},
             {'__id': 'String', '__type': 'SimpleAttribute', 'constraint': {'AL': ''}, 'name': 'String'},
             {'__id': 'Place', '__type': 'Class', 'lower_cardinality': None, 'upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'Place', 'abstract': None},
             {'__id': 'Place_tokens', '__type': 'AttributeLink', '__source': 'Place', '__target': 'Natural', 'name': 'tokens', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'Place_name', '__type': 'AttributeLink', '__source': 'Place', '__target': 'String', 'name': 'name', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'Transition', '__type': 'Class', 'lower_cardinality': None, 'upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'Transition', 'abstract': None},
             {'__id': 'Transition_name', '__type': 'AttributeLink', '__source': 'Transition', '__target': 'String', 'name': 'name', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'P2T', '__type': 'Association', '__source': 'Place', '__target': 'Transition', 'source_lower_cardinality': None, 'target_lower_cardinality': None, 'source_upper_cardinality': None, 'target_upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'P2T', "abstract": None, "lower_cardinality": None, "upper_cardinality": None},
             {'__id': 'P2T_weight', '__type': 'AttributeLink', '__source': 'P2T', '__target': 'Natural', 'name': 'weight', 'optional': False, 'constraint': {'AL': ''}},
             {'__id': 'T2P', '__type': 'Association', '__source': 'Transition', '__target': 'Place', 'source_lower_cardinality': None, 'target_lower_cardinality': None, 'source_upper_cardinality': None, 'target_upper_cardinality': None, 'constraint': {'AL': ''}, 'name': 'T2P', "abstract": None, "lower_cardinality": None, "upper_cardinality": None},
             {'__id': 'T2P_weight', '__type': 'AttributeLink', '__source': 'T2P', '__target': 'Natural', 'name': 'weight', 'optional': False, 'constraint': {'AL': ''}},
            ]
        compare_unordered_lists(got, expected)

        got = element_list_nice("users/user/test/my_pn")
        expected = \
            [{'__id': 'p1', '__type': 'Place', 'tokens': 1, 'name': 'p1'},
             {'__id': 'p2', '__type': 'Place', 'tokens': 2, 'name': 'p2'},
             {'__id': 'p3', '__type': 'Place', 'tokens': 3, 'name': 'p3'},
             {'__id': 't1', '__type': 'Transition', 'name': 't1'},
             {'__id': '__0', '__type': 'P2T', '__source': 'p1', '__target': 't1', 'weight': 1},
             {'__id': '__1', '__type': 'P2T', '__source': 'p2', '__target': 't1', 'weight': 1},
             {'__id': '__2', '__type': 'T2P', '__source': 't1', '__target': 'p3', 'weight': 2}
            ]
        compare_unordered_lists(got, expected)

        alter_context("users/user/test/PetriNet", "formalisms/Bottom")
        alter_context("users/user/test/my_pn", "formalisms/Bottom")

        count_nodes = 0
        count_edges = 0
        for entry in element_list_nice("users/user/test/PetriNet"):
            print(entry)
            assert entry["__type"] in ["Node", "Edge"]
            if entry["__type"] == "Node":
                assert len(entry) == 2
                count_nodes += 1
            else:
                assert len(entry) == 4
                count_edges += 1
        assert count_nodes == 20
        assert count_edges == 23

        count_nodes = 0
        count_edges = 0
        for entry in element_list_nice("users/user/test/my_pn"):
            assert entry["__type"] in ["Node", "Edge"]
            if entry["__type"] == "Node":
                assert len(entry) == 2
                count_nodes += 1
            else:
                assert len(entry) == 4
                count_edges += 1
        assert count_nodes == 14
        assert count_edges == 13

        alter_context("users/user/test/PetriNet", "users/user/test/PetriNet")
        alter_context("users/user/test/my_pn", "formalisms/SimpleClassDiagrams")
        try:
            element_list_nice("users/user/test/PetriNet")
            self.fail()
        except:
            pass
        try:
            element_list_nice("users/user/test/my_pn")
            self.fail()
        except:
            pass
