def test_issue108_obj_proc_multifile(): """ see issue 108 for a detailed error report """ mm = textx.metamodel_from_str(''' Model: imports*=Import classes*=Class; Import: 'import' importURI=STRING ';'; Class: 'class' name=ID '{' '}'; ''') lst_class_names = [] lst_models = [] def print_obj(x): lst_class_names.append(x.name) def print_model(m, mm): lst_models.append(m) mm.register_scope_providers( {'*.*': scoping_providers.PlainNameImportURI()}) mm.register_obj_processors({'Class': print_obj}) mm.register_model_processor(print_model) current_dir = dirname(__file__) mm.model_from_file(join(current_dir, 'issue108', 'a.dsl')) assert 2 == len(lst_models) assert 2 == len(lst_class_names)
def get_survey_mm(): """Returns the meta-model for survey-dsl language.""" current_dir = os.path.dirname(__file__) grammar_path = os.path.join(current_dir, 'survey.tx') object_processors = { 'SurveyContent': survey_content_object_processor, 'Question': question_object_processor, 'ParameterValue': parameter_value_object_processor, } # build metamodel metamodel = metamodel_from_file(grammar_path, classes=[Parameter, QuestionType], builtins=get_built_in_question_types(), global_repository=True) metamodel.register_scope_providers({ "*.*": scoping_providers.PlainNameImportURI(), "ParameterValue.parameter": scoping_providers.RelativeName("parent.type.parameters"), }) metamodel.register_obj_processors(object_processors) return metamodel
def test_model_with_imports_and_search_path_bad_case_search_and_glob2(): """ Basic test for ImportURI (bad case: with search path no file patterns are allowed) """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( abspath(dirname(__file__)) + '/issue66/task_specification.tx') search_path = [ abspath(dirname(__file__)) + '/issue66/somewhere1', # assembly abspath(dirname(__file__)) + '/issue66/somewhere2' # position ] my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI(search_path=search_path)}) ################################# # MODEL PARSING ################################# with raises(IOError, match=r'.*assembly\.\*.*'): _ = my_meta_model.model_from_file( abspath(dirname(__file__)) + "/issue66/assembly_car2.prog")
def test_model_with_imports_and_search_path_bad_case2b(): """ Basic test for ImportURI (bad case) """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( join(abspath(dirname(__file__)), 'issue66', 'task_specification.tx')) search_path = [ join(abspath(dirname(__file__)), 'issue66', 'somewhere1xx'), # assembly join(abspath(dirname(__file__)), 'issue66', 'somewhere2') # position ] my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI(search_path=search_path)}) ################################# # MODEL PARSING ################################# with raises(IOError, match=r'.*assembly\.tasks.*'): my_meta_model.model_from_file( join(abspath(dirname(__file__)), "issue66", "assembly_car1.prog"))
def plan_lang(): mm = metamodel_from_file(get_grammar_path("Plan.tx"), classes=[PlanEntry]) mm.register_scope_providers({ "*.*": scoping_providers.PlainNameImportURI( ) # noqa: each import is a recipe model }) return mm
def plan_lang(): this_folder = dirname(abspath(__file__)) mm = metamodel_from_file(join(this_folder, "grammar", "Plan.tx"), classes=[PlanEntry]) mm.register_scope_providers({ "*.*": scoping_providers.PlainNameImportURI() # each import is a recipe model }) return mm
def test_inclusion_check_1(): """ Test to demonstrate how to check if a file is used by a model. This can be used by an IDE to determine, if a model has to be updated/reloaded. """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( join(abspath(dirname(__file__)), 'issue66', 'task_specification.tx')) search_path = [ join(abspath(dirname(__file__)), 'issue66', 'somewhere1'), # assembly join(abspath(dirname(__file__)), 'issue66', 'somewhere2') # position ] my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI(search_path=search_path)}) ################################# # MODEL PARSING ################################# # This model load two files # * one file exists locally and in a search path --> the local one should # be preferred. # * one only exists locally. m = my_meta_model.model_from_file( join(abspath(dirname(__file__)), 'issue66', 'assembly_car1.prog')) # the model file itself is "included" (special case) assert is_file_included( join(abspath(dirname(__file__)), 'issue66', 'assembly_car1.prog'), m) # another model file assert not is_file_included( join(abspath(dirname(__file__)), 'issue66', 'local', 'assembly_car3.prog'), m) # file in folder "local" assert not is_file_included( join(abspath(dirname(__file__)), 'issue66', 'local', 'mylib', 'local.tasks'), m) # file in folder "local" assert not is_file_included( join(abspath(dirname(__file__)), 'issue66', 'local', 'mylib', 'position.tasks'), m) # distant file (part of search path) assert is_file_included( join(abspath(dirname(__file__)), 'issue66', 'somewhere1', 'mylib', 'assembly.tasks'), m) # distant file (part of search path) assert is_file_included( join(abspath(dirname(__file__)), 'issue66', 'somewhere2', 'mylib', 'position.tasks'), m)
def get_metamodel(): this_folder = dirname(abspath(__file__)) # get the "mydsl" meta model other_meta_model = mydsl.get_metamodel() # create the meta model and reference "mydsl" meta_model = metamodel_from_file(join(this_folder, "MyDsl1.tx"), referenced_metamodels=[other_meta_model]) # register scope provider (allow import models into mydsl1 models) meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI()}) # register file endings scoping.MetaModelProvider.add_metamodel("*.mydsl", other_meta_model) scoping.MetaModelProvider.add_metamodel("*.mydsl1", meta_model) return meta_model
def test_model_with_imports_and_search_path_bad_case_search_and_glob1(): """ Basic test for ImportURI (bad case: search_path + globbing) """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( join(abspath(dirname(__file__)), 'issue66', 'task_specification.tx')) search_path = [ join(abspath(dirname(__file__)), 'issue66', 'somewhere1'), # assembly join(abspath(dirname(__file__)), 'issue66', 'somewhere2') # position ] with raises(Exception, match=r'you cannot use globbing together with a search path'): my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI( glob_args={}, search_path=search_path)})
def test_model_with_imports_and_search_path_bad_case1(): """ Basic test for ImportURI (bad case: no search path) """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( abspath(dirname(__file__)) + '/issue66/task_specification.tx') my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI()}) ################################# # MODEL PARSING ################################# with raises(IOError, match=r'.*lib.*tasks.*'): _ = my_meta_model.model_from_file( abspath(dirname(__file__)) + "/issue66/assembly_car1.prog")
def test_model_with_imports_and_search_path_good_case(): """ Basic test for ImportURI (good case) """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( abspath(dirname(__file__)) + '/issue66/task_specification.tx') search_path = [ abspath(dirname(__file__)) + '/issue66/somewhere1', abspath(dirname(__file__)) + '/issue66/somewhere2' ] my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI(search_path=search_path)}) ################################# # MODEL PARSING ################################# _ = my_meta_model.model_from_file( abspath(dirname(__file__)) + "/issue66/assembly_car1.prog")
def test_model_with_imports_relative_to_current_model(): """ Basic test for ImportURI (good case: it should always be preferred to load relative to the current model file) """ ################################# # META MODEL DEF ################################# my_meta_model = metamodel_from_file( join(abspath(dirname(__file__)), 'issue66', 'task_specification.tx')) search_path = [ join(abspath(dirname(__file__)), 'issue66', 'somewhere1'), # assembly join(abspath(dirname(__file__)), 'issue66', 'somewhere2') # position ] my_meta_model.register_scope_providers( {"*.*": scoping_providers.PlainNameImportURI(search_path=search_path)}) ################################# # MODEL PARSING ################################# # This model load two files # * one file exists locally and in a search path --> the local one should # be preferred. # * on only exists locally. m = my_meta_model.model_from_file( join(abspath(dirname(__file__)), 'issue66', 'local', 'assembly_car3.prog')) # the model could be loaded --> the local path works (one file exists # only locally). assert 2 == len(m.imports) dirs = list(map(lambda i: dirname(i._tx_loaded_models[0]._tx_filename), m.imports)) assert dirs[0] == dirs[1]
def test_metamodel_provider_advanced_test3_import(): """ Advanced test for ExtRelativeName and PlainNameImportURI. Here we have a global model repository shared between different meta models. The meta models interact (refer to each other, different directions). """ ################################# # META MODEL DEF ################################# this_folder = dirname(abspath(__file__)) def get_meta_model(provider, grammar_file_name): mm = metamodel_from_file(join(this_folder, grammar_file_name), debug=False, classes=[Cls, Obj]) mm.register_scope_providers({ "*.*": provider, }) return mm import_lookup_provider = scoping_providers.PlainNameImportURI() a_mm = get_meta_model( import_lookup_provider, join(this_folder, "metamodel_provider3", "A.tx")) b_mm = get_meta_model( import_lookup_provider, join(this_folder, "metamodel_provider3", "B.tx")) c_mm = get_meta_model( import_lookup_provider, join(this_folder, "metamodel_provider3", "C.tx")) clear_language_registrations() register_language( 'a-dsl', pattern='*.a', description='Test Lang A', metamodel=a_mm) register_language( 'b-dsl', pattern='*.b', description='Test Lang B', metamodel=b_mm) register_language( 'c-dsl', pattern='*.c', description='Test Lang C', metamodel=c_mm) ################################# # MODEL PARSING ################################# m = a_mm.model_from_file( join(this_folder, "metamodel_provider3", "circular", "model_a.a")) model_repo = m._tx_model_repository.all_models ################################# # TEST MODEL ################################# def get_all(model_repo, what): lst = [] for m in model_repo.filename_to_model.values(): lst = lst + get_children_of_type(what, m) return lst lst = get_all(model_repo, "Obj") # print(lst) assert len(lst) == 3 # check all references to be resolved (!=None) for a in lst: assert a.ref ################################# # END ################################# clear_language_registrations()
def test_metamodel_provider_advanced_test3_import(): """ Advanced test for ExtRelativeName and PlainNameImportURI. Here we have a global model repository shared between different meta models. The meta models interact (refer to each other, different directions). """ ################################# # META MODEL DEF ################################# this_folder = dirname(abspath(__file__)) def get_meta_model(provider, grammar_file_name): mm = metamodel_from_file(join(this_folder, grammar_file_name), debug=False) mm.register_scope_providers({ "*.*": provider, }) return mm import_lookup_provider = scoping_providers.PlainNameImportURI() a_mm = get_meta_model( import_lookup_provider, this_folder + "/metamodel_provider3/A.tx") b_mm = get_meta_model( import_lookup_provider, this_folder + "/metamodel_provider3/B.tx") c_mm = get_meta_model( import_lookup_provider, this_folder + "/metamodel_provider3/C.tx") scoping.MetaModelProvider.clear() scoping.MetaModelProvider.add_metamodel("*.a", a_mm) scoping.MetaModelProvider.add_metamodel("*.b", b_mm) scoping.MetaModelProvider.add_metamodel("*.c", c_mm) ################################# # MODEL PARSING ################################# m = a_mm.model_from_file( this_folder + "/metamodel_provider3/circular/model_a.a") model_repo = m._tx_model_repository.all_models ################################# # TEST MODEL ################################# def get_all(model_repo, what): lst = [] for m in model_repo.filename_to_model.values(): lst = lst + get_children_of_type(what, m) return lst lst = get_all(model_repo, "Obj") # print(lst) assert len(lst) == 3 # check all references to be resolved (!=None) for a in lst: assert a.ref ################################# # END ################################# scoping.MetaModelProvider.clear()