CIMgen is a command-line tool that generates language-specific data models (classes, structs) from Common Information Model (CIM) RDF schemas. It is designed to parse complex CIM RDF files and produce idiomatic code in various programming languages like Go, C++, Java, and Python.
Several projects use CIMgen, see Projects using CIMgen.
This is a rewrite of CIMgen in the Go programming language.
Make sure that you have cloned the repo recursively to include the CGMES schema files from ENTSO-E
git clone --recurse-submodules [...]
or clone the submodule in a second step
git submodule update --init --recursive
Ensure that GOPATH is set and included in your PATH.
Change into the cimgen-go folder:
cd cimgen-go
Use the go run command, specifying the target language with the -lang flag.
# Example for generating C++ code
go run cmd/cimgen/main.go -lang cppAlternatively, you can install cimgen.
go install ./...
Run the test suite using the go test command. The -v flag provides verbose output.
go test -v ./...
To analyze differences between generated file you can use
git diff --no-index --word-diff --diff-filter=MR --ignore-space-change --ignore-blank-lines output/[dir 1] output/[dir 2]
diff -wB output/[dir 1] output/[dir 2]
The code generation process follows these main steps:
- Schema Loading: The tool begins by finding and parsing the relevant CIM RDF schema files based on the specified version and profiles.
- Schema Processing: It processes the parsed RDF data into an internal, language-agnostic representation of CIM classes, properties, datatypes, and their relationships.
- Code Generation: Using Go's
text/templateengine, it feeds the internal representation into language-specific templates (lang-templates/*.tmpl) to generate the final source code files.
cmd/cimgen/main.go: The main entry point for the CLI tool. It parses command-line arguments and orchestrates the code generation process.cim_generate.go: Contains the core logic for driving the generation process for a specific language.cim_schema_import.go: Handles the discovery and parsing of the CIM RDF schema files.cim_schema_processing.go: Responsible for transforming the raw parsed schema into the internal representation used by the generators.generator_*.go: A set of files (e.g.,generator_go.go,generator_cpp.go) that implement the generation logic for each target language.
Python tool for code generation from CIM data model for several programming languages
pip install -e .
cimgen --outdir=output/cpp/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=cpp --cgmes_version=cgmes_v2_4_15This will build version CGMES_2.4.15_27JAN2020 in the subfolder output/cpp/CGMES_2.4.15_27JAN2020.
Note
If you wish to build an alternative version, you can see available options in the subfolder called cgmes_schema.
For the schema CGMES_3.0.0 you have to use the option
--cgmes_version=cgmes_v3_0_0. outdir can be set to whichever absolute path you wish to create the files in.
Alternatively, you can leverage Makefile:
pip install -e .
#unset BUILD_IN_DOCKER # if you previously set to use docker
#export SCHEMA=CGMES_3.0.0 # to use CGMES 3.0.0
make cppdocker build --tag cimgen --file Dockerfile .
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/cpp/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=cpp --cgmes_version=cgmes_v2_4_15alternatively, you can leverage Makefile:
export BUILD_IN_DOCKER=true
#export SCHEMA=CGMES_3.0.0 to use CGMES 3.0.0
make cpppip install -e .
cimgen --outdir=output/python/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=python --cgmes_version=cgmes_v2_4_15alternatively, you can leverage Makefile:
pip install -e .
#unset BUILD_IN_DOCKER # if you previously set to use docker
#export SCHEMA=CGMES_3.0.0 # to use CGMES 3.0.0
make pythondocker build --tag cimgen --file Dockerfile .
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/python/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=python --cgmes_version=cgmes_v2_4_15alternatively, you can leverage Makefile:
export BUILD_IN_DOCKER=true
#export SCHEMA=CGMES_3.0.0 to use CGMES 3.0.0
make pythonpip install -e .
cimgen --outdir=output/modernpython/CGMES_2.4.15_27JAN2020 --schemadir=cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=modernpython --cgmes_version=cgmes_v2_4_15outdir can be set to whichever absolute path you wish to create the files in.
alternatively, you can leverage Makefile:
pip install -e .
#unset BUILD_IN_DOCKER # if you previously set to use docker
#export SCHEMA=CGMES_3.0.0 # to use CGMES 3.0.0
make modernpythondocker build --tag cimgen --file Dockerfile .
docker run --volume "$(pwd)/output:/output" cimgen --outdir=/output/modernpython/CGMES_2.4.15_27JAN2020 --schemadir=/cimgen/cgmes_schema/CGMES_2.4.15_27JAN2020 --langdir=modernpython --cgmes_version=cgmes_v2_4_15alternatively, you can leverage Makefile:
export BUILD_IN_DOCKER=true
#export SCHEMA=CGMES_3.0.0 to use CGMES 3.0.0
make modernpythonTo generate files for custom profiles, you have to copy the profile files to a subdirectory of the schema directory. All profiles in the schema directory and its subdirectories are read, but the profiles in the main directory are read first.
mkdir cgmes_schema/<schemadir>/<customdir>/
cp <custom_profile>.rdf ... cgmes_schema/<schemadir>/<customdir>/
cimgen --outdir=output/ --schemadir=cgmes_schema/<schemadir> --langdir=<lang> --cgmes_version=<version>git clone https://github.com/sogno-platform/cimgen.git
cd cimgenFor the python toolchain, install the package in dev mode:
pip install -e .[dev]Run pre-commit checks manually:
pre-commit run --all-filesInstall pre-commit hook to run it automatically:
pre-commit installThis project is released under the terms of the Apache 2.0 license.
If you are using CIMgen for your research, please cite the following paper in your publications:
Dinkelbach, J., Razik, L., Mirz, M., Benigni, A., Monti, A.: Template-based generation of programming language specific code for smart grid modelling compliant with CIM and CGMES. J. Eng. 2023, 1-13 (2022). https://doi.org/10.1049/tje2.12208
