def generated_metamodel(pygen_output_dir):
    rset = ResourceSet()
    resource = rset.get_resource(URI('input/A.ecore'))
    library_model = resource.contents[0]
    generator = EcoreGenerator(with_dependencies=True)
    generator.generate(library_model, pygen_output_dir)
    return importlib.import_module('a')
Beispiel #2
0
def generated_library(pygen_output_dir):
    rset = ResourceSet()
    resource = rset.get_resource(URI('input/library.ecore'))
    library_model = resource.contents[0]
    generator = EcoreGenerator()
    generator.generate(library_model, pygen_output_dir)
    return importlib.import_module('library')
Beispiel #3
0
def test__ecore_generator__filter_pyfqn(package_in_hierarchy):
    assert EcoreGenerator.filter_pyfqn(package_in_hierarchy) == 'pkg1.pkg2.pkg3'
    assert EcoreGenerator.filter_pyfqn(package_in_hierarchy, relative_to=1) == '.pkg2.pkg3'
    assert EcoreGenerator.filter_pyfqn(package_in_hierarchy, relative_to=2) == '.pkg3'
    assert EcoreGenerator.filter_pyfqn(package_in_hierarchy, relative_to=3) == '.'

    with pytest.raises(ValueError):
        assert EcoreGenerator.filter_pyfqn(package_in_hierarchy, relative_to=4) == '.'
Beispiel #4
0
def generate_meta_model(model,
                        output_dir,
                        *,
                        user_module=None,
                        auto_register_package=None):
    generator = EcoreGenerator(user_module=user_module,
                               auto_register_package=auto_register_package)
    generator.generate(model, output_dir)
    return importlib.import_module(model.name)
Beispiel #5
0
def test__ecore_generator__manage_default_value_simple_types():
    attribute = EAttribute('with_default', EString)
    attribute.defaultValueLiteral = 'str_val'
    result = EcoreGenerator.manage_default_value(attribute)
    assert result == "'str_val'"

    attribute.eType = EInt
    attribute.defaultValueLiteral = '123456'
    result = EcoreGenerator.manage_default_value(attribute)
    assert result == 123456
Beispiel #6
0
def generate_code():
    def format_autopep8(raw: str) -> str:
        import autopep8
        return autopep8.fix_code(raw, options={'experimental': True})

    rset = setup_resourceset()
    uml_root = rset.get_resource('model/uml.ecore').contents[0]
    generator = EcoreGenerator(user_module='pyuml2.uml_mixins')
    for task in generator.tasks:
        task.formatter = format_autopep8
    generator.generate(uml_root, 'pyuml2')
Beispiel #7
0
def test__ecore_generator__manage_default_value_enumeration():
    enumeration = EEnum('MyEnum', literals=('None_', 'A', 'B'))
    attribute = EAttribute('with_default', enumeration)
    attribute.defaultValueLiteral = 'A'
    result = EcoreGenerator.manage_default_value(attribute)
    assert result == 'MyEnum.A'

    attribute.defaultValueLiteral = 'None_'
    result = EcoreGenerator.manage_default_value(attribute)
    assert result == 'MyEnum.None_'

    attribute.defaultValueLiteral = 'None'
    result = EcoreGenerator.manage_default_value(attribute)
    assert result == 'MyEnum.None_'
Beispiel #8
0
def test__ecore_generator__test_opposite_before_self():
    mock_element = mock.MagicMock()
    mock_element.eOpposite = mock.sentinel.OPPOSITE

    elements = [mock_element, mock.sentinel.OPPOSITE]
    assert not EcoreGenerator.test_opposite_before_self(mock_element, elements)

    elements.reverse()
    assert EcoreGenerator.test_opposite_before_self(mock_element, elements)

    elements = [mock_element]
    assert not EcoreGenerator.test_opposite_before_self(mock_element, elements)

    elements = [mock.sentinel.OPPOSITE]
    assert not EcoreGenerator.test_opposite_before_self(mock_element, elements)
Beispiel #9
0
def generate_from_cli(args):
    """CLI entry point."""
    parser = argparse.ArgumentParser(
        description="Generate Python classes from an Ecore model.")
    parser.add_argument('--ecore-model',
                        '-e',
                        help="Path to Ecore XMI file.",
                        required=True)
    parser.add_argument(
        '--out-folder',
        '-o',
        help="Path to directory, where output files are generated.",
        required=True)
    parser.add_argument(
        '--auto-register-package',
        help=
        "Generate package auto-registration for the PyEcore 'global_registry'.",
        action='store_true')
    parser.add_argument('--verbose',
                        '-v',
                        help="Increase logging verbosity.",
                        action='count')

    parsed_args = parser.parse_args(args)

    configure_logging(parsed_args)
    model = load_model(parsed_args.ecore_model)
    EcoreGenerator(parsed_args.auto_register_package).generate(
        model, parsed_args.out_folder)
Beispiel #10
0
def generate_from_cli(args):
    """CLI entry point."""
    parser = argparse.ArgumentParser(
        description="Generate Python classes from an Ecore model.")
    parser.add_argument('--ecore-model',
                        '-e',
                        help="Path to Ecore XMI file.",
                        required=True)
    parser.add_argument(
        '--out-folder',
        '-o',
        help="Path to directory, where output files are generated.",
        required=True)
    parser.add_argument(
        '--auto-register-package',
        help=
        "Generate package auto-registration for the PyEcore 'global_registry'.",
        action='store_true')
    parser.add_argument(
        '--user-module',
        help=
        "Dotted name of module with user-provided mixins to import from generated classes.",
    )
    parser.add_argument(
        '--with-dependencies',
        help=
        "Generates code for every metamodel the input metamodel depends on.",
        action='store_true')
    parser.add_argument('--verbose',
                        '-v',
                        help="Increase logging verbosity.",
                        action='count')
    parser.add_argument('--textX',
                        help="Generate textX compatible classes",
                        action='store_true')

    parsed_args = parser.parse_args(args)

    configure_logging(parsed_args)
    model = load_model(parsed_args.ecore_model)
    EcoreGenerator(auto_register_package=parsed_args.auto_register_package,
                   user_module=parsed_args.user_module,
                   with_dependencies=parsed_args.with_dependencies,
                   textX=parsed_args.textX).generate(model,
                                                     parsed_args.out_folder)
Beispiel #11
0
# This File reads the metamodel, reads the model and implements all
# the behavior of the Classes to introduce ROS code in every component
# Written in 14/2/2020
# Written by Rafael Brouzos

from pyecore.resources import ResourceSet, URI, global_registry
from pyecore.resources.json import JsonResource
from pyecore.ecore import EClass, EAttribute
import pyecore.ecore as Ecore
from pyecoregen.ecore import EcoreGenerator
import pyecore.behavior as behavior
from pyecore.utils import DynamicEPackage
import os

# We load the Ecore metamodel first
global_registry[Ecore.nsURI] = Ecore
rset = ResourceSet()
resource = rset.get_resource(URI('metageneros.ecore'))
# ~ rset.resource_factory['json'] = lambda uri: JsonResource(uri)
root = resource.contents[0]  # We get the root (an EPackage here)
# Register the metamodel (in case we open an XMI model later)
rset.metamodel_registry[root.nsURI] = root

# ~ model_root.hasPackages[0].package_init()
# ~ for x in model_root.hasPackages[0].allInstances():
# ~ print(x) # display 2 instances
# ~ os.chdir('/home/raf/Desktop/Thesis Project/ecoreWork')
generator = EcoreGenerator()
generator.generate(root, '')
Beispiel #12
0
    kpi_costs = KPI(name="KPI Total costs",
                    value=None,
                    quantityAndUnit=costs_unit)

    # Add CO2-emissions and total costs KPIs to KPIs
    es.instance[0].area.KPIs.kpi.append(kpi_co2)
    es.instance[0].area.KPIs.kpi.append(kpi_costs)

    print("Energy system: {}".format(attr_to_dict(es)))
    print("OutPort connectedTo: {}".format(outPort.connectedTo))
    print("InPort connectedTo: {}".format(inPort.connectedTo))
    resource = rset.create_resource(URI('mpoc.esdl'))
    resource.append(es)
    resource.save()


if __name__ == '__main__':
    # Load the ESDL model from GitHub
    rset = ResourceSet()
    resource = rset.get_resource(
        HttpURI(
            'https://raw.githubusercontent.com/EnergyTransition/ESDL/master/esdl/model/esdl.ecore'
        ))
    esdl_model = resource.contents[0]

    # Generate the classes
    generator = EcoreGenerator()
    generator.generate(esdl_model, ".")

    main()