diff --git a/translate_test_cases.py b/.CI/tests/translate_test_cases.py similarity index 62% rename from translate_test_cases.py rename to .CI/tests/translate_test_cases.py index 4aa49f3..cbd1c96 100644 --- a/translate_test_cases.py +++ b/.CI/tests/translate_test_cases.py @@ -1,3 +1,5 @@ +import sys +sys.path.append('../../') import cdl_plc cxf_json_path = 'cxf/ModelicaTestCases/' @@ -6,7 +8,7 @@ cxf_json_path + 'SingleBlocks/Reals/' + 'Add', cxf_json_path + 'SingleBlocks/Reals/' + 'MultiplyByParameter', - cxf_json_path + 'CompositeBlocks/' + "customPWithLimiter", + cxf_json_path + 'CompositeBlocks/' + "CustomPWithLimiter", cxf_json_path + 'CompositeBlocks/' + "Custom01", cxf_json_path + 'CompositeBlocks/' + "Custom02", cxf_json_path + 'CompositeBlocks/' + "Custom03", @@ -14,4 +16,8 @@ for test_case in test_cases: cxf_json = test_case + ".jsonld" - cdl_plc.Cdl2Plc(cxf_json, debug=True).translate() + cdl_plc.Cdl2Plc( + cxf_json, + output_folder='check_translation_to_IEC_XML/', + debug=True, + ).translate() diff --git a/.github/workflows/cxf_check.yml b/.github/workflows/cxf_check.yml new file mode 100644 index 0000000..db63f90 --- /dev/null +++ b/.github/workflows/cxf_check.yml @@ -0,0 +1 @@ +# Workflow for CI tests name: CI-check_modelica-json_translator # Controls when the action will run. on: # Triggers the workflow on push events but only for the master branch push: branches: [ main ] # Allows you to run this workflow manually from the Actions tab # workflow_dispatch: defaults: run: shell: bash # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: test-modelica-json: # Skip if the commit message contains "ci skip" #if: "!contains(github.event.head_commit.message, 'ci skip')" runs-on: ubuntu-22.04 steps: # Checks-out repository under $GITHUB_WORKSPACE, so job can access it - name: Checkout code uses: actions/checkout@v3 - uses: actions/setup-python@v4 with: python-version: '3.10.11' # Version range or exact version of a Python version to use, using SemVer's version range syntax architecture: 'x64' # optional x64 or x86. Defaults to x64 if not specified - name: "Install pip" run: pip3 install --upgrade pip wheel - name: Download modelica-json-1.2.0.zip run: wget https://github.com/lbl-srg/modelica-json/releases/download/v1.2.0/modelica-json-1.2.0.zip -O modelica-json-1.2.0.zip - name: Unzip modelica-json-1.2.0.zip run: unzip modelica-json-1.2.0.zip -d modelica-json - name: Set MODELICAPATH environment variable run: echo "export MODELICAPATH=\${MODELICAPATH}:/usr/local/Modelica/Library/" >> ~/.bashrc - name: Install Java dependencies run: sudo apt-get update && sudo apt-get install -y default-jdk default-jre - name: Install parser dependencies run: | cd modelica-json/modelica-json-1.2.0 make install - name: Compile Java files run: | cd modelica-json/modelica-json-1.2.0 make compile - name: Translate Modelica test cases to jsonld run: | cd modelica-json/modelica-json-1.2.0 node app.js -f ../../ModelicaTestCases/CompositeBlocks/CustomPWithLimiter.mo -o cxf node app.js -f ../../ModelicaTestCases/CompositeBlocks/Custom01.mo -o cxf node app.js -f ../../ModelicaTestCases/CompositeBlocks/Custom02.mo -o cxf node app.js -f ../../ModelicaTestCases/CompositeBlocks/Custom03.mo -o cxf node app.js -f ../../ModelicaTestCases/SingleBlocks/Reals/Add.mo -o cxf node app.js -f ../../ModelicaTestCases/SingleBlocks/Reals/MultiplyByParameter.mo -o cxf - name: Compare changes between existing and new jsonld's run: | diff -r modelica-json/modelica-json-1.2.0/cxf/home/runner/work/cdl-plc/cdl-plc/ModelicaTestCases/ cxf/ModelicaTestCases/ - name: Run translate_test_cases.py run: | pip install Jinja2 cd .CI/tests python translate_test_cases.py - name: Compare changes between existing and new IEC XML's run: | diff -r --exclude="README.md" IEC61131-10XML/ check_translation_to_IEC_XML/IEC61131-10XML/ \ No newline at end of file diff --git a/IEC61131-10XML/Add/plc.xml b/IEC61131-10XML/Add/plc.xml index 3e91252..5282402 100644 --- a/IEC61131-10XML/Add/plc.xml +++ b/IEC61131-10XML/Add/plc.xml @@ -1,108 +1,108 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - u1 - - - - - - - u2 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - y1 - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + u1 + + + + + + + u2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y1 + + + + + + + + + + + + + + + + + + diff --git a/IEC61131-10XML/Custom01/plc.xml b/IEC61131-10XML/Custom01/plc.xml index 58d513a..0a31ecf 100644 --- a/IEC61131-10XML/Custom01/plc.xml +++ b/IEC61131-10XML/Custom01/plc.xml @@ -1,297 +1,297 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - e - - - - - - - uSet - - - - - - - yMax - - - - - - - r3 - - - - - - - k - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + e + + + + + + + uSet + + + + + + + yMax + + + + + + + r3 + + + + + + + k + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IEC61131-10XML/Custom02/plc.xml b/IEC61131-10XML/Custom02/plc.xml index 0eaf3da..40e4df6 100644 --- a/IEC61131-10XML/Custom02/plc.xml +++ b/IEC61131-10XML/Custom02/plc.xml @@ -1,200 +1,200 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ymax - - - - - - - ymax1 - - - - - - - r1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - y - - - - - - - - y1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ymax + + + + + + + ymax1 + + + + + + + r1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y + + + + + + + + y1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IEC61131-10XML/Custom03/plc.xml b/IEC61131-10XML/Custom03/plc.xml index e4f730b..e44ba98 100644 --- a/IEC61131-10XML/Custom03/plc.xml +++ b/IEC61131-10XML/Custom03/plc.xml @@ -1,158 +1,158 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ymax - - - - - - - r1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - y - - - - - - - - y1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ymax + + + + + + + r1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y + + + + + + + + y1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IEC61131-10XML/CustomPWithLimiter/plc.xml b/IEC61131-10XML/CustomPWithLimiter/plc.xml index 3b783de..6653c5c 100644 --- a/IEC61131-10XML/CustomPWithLimiter/plc.xml +++ b/IEC61131-10XML/CustomPWithLimiter/plc.xml @@ -1,215 +1,215 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - e - - - - - - - yMax - - - - - - - k - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + e + + + + + + + yMax + + + + + + + k + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IEC61131-10XML/MultiplyByParameter/plc.xml b/IEC61131-10XML/MultiplyByParameter/plc.xml index 4c30285..47f30c5 100644 --- a/IEC61131-10XML/MultiplyByParameter/plc.xml +++ b/IEC61131-10XML/MultiplyByParameter/plc.xml @@ -1,145 +1,145 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - u2 - - - - - - - r1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - y1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + u2 + + + + + + + r1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + y1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cdl_plc.py b/cdl_plc.py index cd81f9c..359d40b 100644 --- a/cdl_plc.py +++ b/cdl_plc.py @@ -6,6 +6,14 @@ import ast +import os + +abspath = os.path.abspath(__file__) +dname = os.path.dirname(abspath) +print(dname) +os.chdir(dname) + + class Cdl2Plc: dict_assign_cdl_to_iec_standard_lib = { @@ -69,9 +77,11 @@ class Cdl2Plc: def __init__( self, cxf_file, - debug, + output_folder=None, + debug=False, ): self.cxf_file = cxf_file + self.output_folder = output_folder # properties self._multi_input_blocks = None @@ -1091,19 +1101,22 @@ def create_dict_cdl_blocks(self): cdl_block = fileBlock.read() self.dict_cdl_blocks["scalar_inputs"].add(cdl_block) - def create_iec_xml(self): + def create_iec_xml(self, debug=False): """ Render based on dicts """ template_loader = jinja2.FileSystemLoader(searchpath="./") template_env = jinja2.Environment(loader=template_loader) - # fileTemplateVariable = "templateVariable.xml" file_template_global = [ - "xml_templates/structure/global.xml", + "xml_templates/structure/Global.xml", "templateVariable.xml", ][0] + + if debug: + print('cwd: ', os.getcwd()) + template = template_env.get_template(file_template_global) self.output_text = template.render( dictInputVars=self.program_inputs, @@ -1125,13 +1138,21 @@ def create_iec_xml(self): if self.debug: print(xml_export) - directory = 'IEC61131-10XML/{}'.format(self._program_name) + if self.output_folder is not None: + directory = self.output_folder + 'IEC61131-10XML/{}'.format(self._program_name) + else: + directory = 'IEC61131-10XML/{}'.format(self._program_name) if os.path.exists(directory): shutil.rmtree(directory) os.makedirs(directory) # Writing to sample.json - with open("{}/plc.xml".format(directory), "w") as outfile: + with open( + "{}/plc.xml".format(directory), + "w", + encoding="utf-8", + newline="\n", + ) as outfile: outfile.write(xml_export) def translate(self): diff --git a/cxf/ModelicaTestCases/SingleBlocks/Reals/Add.jsonld b/cxf/ModelicaTestCases/SingleBlocks/Reals/Add.jsonld index 7bfd7ee..88358e4 100644 --- a/cxf/ModelicaTestCases/SingleBlocks/Reals/Add.jsonld +++ b/cxf/ModelicaTestCases/SingleBlocks/Reals/Add.jsonld @@ -50,7 +50,7 @@ "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.Add.u1", "@type": "S231P:RealInput", "S231P:accessSpecifier": "public", - "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":-120,\"y\":0},{\"x\":-80,\"y\":40}]}}}", + "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":-140,\"y\":0},{\"x\":-100,\"y\":40}]}}}", "S231P:isConnectedTo": { "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.Add.add2.u1" }, @@ -60,7 +60,7 @@ "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.Add.u2", "@type": "S231P:RealInput", "S231P:accessSpecifier": "public", - "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":-120,\"y\":-40},{\"x\":-80,\"y\":0}]}}}", + "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":-140,\"y\":-40},{\"x\":-100,\"y\":0}]}}}", "S231P:isConnectedTo": { "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.Add.add2.u2" }, @@ -70,7 +70,7 @@ "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.Add.y1", "@type": "S231P:RealOutput", "S231P:accessSpecifier": "public", - "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":72,\"y\":-20},{\"x\":112,\"y\":20}]}}}", + "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":100,\"y\":-20},{\"x\":140,\"y\":20}]}}}", "S231P:label": "y1" } ] diff --git a/cxf/ModelicaTestCases/SingleBlocks/Reals/MultiplyByParameter.jsonld b/cxf/ModelicaTestCases/SingleBlocks/Reals/MultiplyByParameter.jsonld index 57440bd..0aa4cab 100644 --- a/cxf/ModelicaTestCases/SingleBlocks/Reals/MultiplyByParameter.jsonld +++ b/cxf/ModelicaTestCases/SingleBlocks/Reals/MultiplyByParameter.jsonld @@ -21,7 +21,7 @@ "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.MultiplyByParameter.gain", "@type": "https://data.ashrae.org/S231P#Buildings.Controls.OBC.CDL.Reals.MultiplyByParameter", "S231P:accessSpecifier": "public", - "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":-8,\"y\":-10},{\"x\":12,\"y\":10}]}}}", + "S231P:graphics": "{\"Placement\":{\"transformation\":{\"extent\":[{\"x\":-6,\"y\":-10},{\"x\":14,\"y\":10}]}}}", "S231P:hasInstance": [ { "@id": "http://example.org#ModelicaTestCases.SingleBlocks.Reals.MultiplyByParameter.gain.k" diff --git a/xml_templates/structure/block.xml b/xml_templates/structure/Block.xml similarity index 100% rename from xml_templates/structure/block.xml rename to xml_templates/structure/Block.xml diff --git a/xml_templates/structure/blockIEC.xml b/xml_templates/structure/BlockIEC.xml similarity index 100% rename from xml_templates/structure/blockIEC.xml rename to xml_templates/structure/BlockIEC.xml diff --git a/xml_templates/structure/global.xml b/xml_templates/structure/Global.xml similarity index 94% rename from xml_templates/structure/global.xml rename to xml_templates/structure/Global.xml index dcab48f..370c5ae 100644 --- a/xml_templates/structure/global.xml +++ b/xml_templates/structure/Global.xml @@ -1 +1 @@ -{% for program in programs %}{% include "xml_templates/structure/program.xml" %}{% endfor %}{% for cdlBlock in dictCdlBlocks.scalar_inputs %}{{ cdlBlock }}{% endfor %}{% for cdlBlock in dictCdlBlocks.array_inputs %}{{ cdlBlock }}{% endfor %} \ No newline at end of file +{% for program in programs %}{% include "xml_templates/structure/Program.xml" %}{% endfor %}{% for cdlBlock in dictCdlBlocks.scalar_inputs %}{{ cdlBlock }}{% endfor %}{% for cdlBlock in dictCdlBlocks.array_inputs %}{{ cdlBlock }}{% endfor %} \ No newline at end of file diff --git a/xml_templates/structure/program.xml b/xml_templates/structure/Program.xml similarity index 73% rename from xml_templates/structure/program.xml rename to xml_templates/structure/Program.xml index f63ef64..14191f1 100644 --- a/xml_templates/structure/program.xml +++ b/xml_templates/structure/Program.xml @@ -1 +1 @@ -{% for key, values in dictInputVars.items() %}{% include "xml_templates/structure/variable.xml" %}{% endfor %}{% for key, values in dictOutputVars.items() %}{% include "xml_templates/structure/variable.xml" %}{% endfor %}{% for key, values in dictFunctionBlockInstances.items() %}{% include "xml_templates/structure/variable.xml" %}{% endfor %}{% for key, values in dictParameters.items() %}{% include "xml_templates/structure/Parameter.xml" %}{% endfor %}{% for key, values in dictInputVars.items() %}{% include "xml_templates/structure/FbdInVar.xml" %}{% endfor %}{% for key, values in dictParameters.items() %}{% include "xml_templates/structure/FbdPar.xml" %}{% endfor %}{% for key, values in dictFunctionBlockInstances.items() %}{% include "xml_templates/structure/block.xml" %}{% endfor %}{% for key, values in dictFunctionBlockInstancesIEC.items() %}{% include "xml_templates/structure/blockIEC.xml" %}{% endfor %}{% for key, values in dictOutputVars.items() %}{% include "xml_templates/structure/FbdOutVar.xml" %}{% endfor %} \ No newline at end of file +{% for key, values in dictInputVars.items() %}{% include "xml_templates/structure/Variable.xml" %}{% endfor %}{% for key, values in dictOutputVars.items() %}{% include "xml_templates/structure/Variable.xml" %}{% endfor %}{% for key, values in dictFunctionBlockInstances.items() %}{% include "xml_templates/structure/Variable.xml" %}{% endfor %}{% for key, values in dictParameters.items() %}{% include "xml_templates/structure/Parameter.xml" %}{% endfor %}{% for key, values in dictInputVars.items() %}{% include "xml_templates/structure/FbdInVar.xml" %}{% endfor %}{% for key, values in dictParameters.items() %}{% include "xml_templates/structure/FbdPar.xml" %}{% endfor %}{% for key, values in dictFunctionBlockInstances.items() %}{% include "xml_templates/structure/Block.xml" %}{% endfor %}{% for key, values in dictFunctionBlockInstancesIEC.items() %}{% include "xml_templates/structure/BlockIEC.xml" %}{% endfor %}{% for key, values in dictOutputVars.items() %}{% include "xml_templates/structure/FbdOutVar.xml" %}{% endfor %} \ No newline at end of file diff --git a/xml_templates/structure/program_instance.xml b/xml_templates/structure/ProgramInstance.xml similarity index 100% rename from xml_templates/structure/program_instance.xml rename to xml_templates/structure/ProgramInstance.xml