Skip to content

Commit

Permalink
Merge pull request #231 from lbl-srg/issue214_cxf
Browse files Browse the repository at this point in the history
Trranslate CDL to CXF
  • Loading branch information
anandkp92 authored Jun 5, 2024
2 parents 7ea86fc + 1301acb commit ad84dbf
Show file tree
Hide file tree
Showing 173 changed files with 39,412 additions and 25,357 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ modelica-json.log
/models
/outputs
/objects
/cxf
.nyc_output
.DS_Store
1 change: 1 addition & 0 deletions CXF-Core.jsonld

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ generate-reference-output:
(for ff in `find . -name '*.mo'`; do \
node app.js -l warn -f $${ff} -o raw-json -d ./test/reference; \
node app.js -l warn -f $${ff} -o json -d ./test/reference; \
node app.js -l warn -f $${ff} -o semantic -d ./test/reference; \
node app.js -l warn -f $${ff} -o semantic -d ./test/reference -p;\
node app.js -l warn -f $${ff} -o cxf -d ./test/reference -p;\
done)

clean-node-packages:
Expand Down
32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ This parser takes a .mo file in input and has three possible outputs, that can b
- **raw-json** : detailed transcription of a Modelica file in JSON
- **json**: simplified JSON format, easier to read an interpret
- **semantic**: generate semantic model from semantic information included within `annotation` in the Modelica file
- **cxf**: generate CXF representation in `.jsonld` of a CDL sequence complying with ASHRAE S231P

##### --mode / -m

Expand All @@ -111,22 +112,43 @@ Specify the output directory. The default option is the current directory.

##### --prettyPrint / -p

Boolean flag to specify if prettyprint the JSON output. The default option is `false`.
If `-p` flag is specified, the JSON output conforms to prettyprint. The default option is `false`.

##### --elementary

If `--elementary` flag is specified, the CXF (jsonld) files for the elementary blocks are also generated. Else, they are ignored. The default option is `false`.
`-o`/`--output` should be `cxf`.

##### --cxfCore

If `--cxfCore` flag is specified, generate the CXF-core.jsonld files for all the elementary blocks. The default option is `false`.
`-o`/`--output` should be `cxf`, `-f`/`--file` should be `path/to/CDL` and `--elementary` flag must be used.

## 4. JSON Schemas

The JSON representation of Modelica and CDL models must be compliant with the corresponding JSON Schema. This is applicable for the JSON output, not for the raw-json one.
The JSON representation of Modelica and CXF models must be compliant with the corresponding JSON Schema. This is applicable for the JSON and CXF output respectively.

JSON Schemas describe the data format and file structure to ensure the quality of the JSON files.

Two schemas are available (links to the raw files) :
- [schema-cdl.json](schema-cdl.json) validates the JSON files parsed from CDL
- [schema-cxf.json](schema-cxf.json) validates the JSON files parsed from CDL classes to form CXF representations
- [schema-modelica.json](schema-modelica.json) validates the JSON files parsed from Modelica models

Graphical viewers are available (please use right click + open in a new tab or refresh the page if necessary - this is not optimized for Firefox) :
- [CDL Schema viewer](cdl-viz.html)
- [Modelica Schema viewer](modelica-viz.html)
- [CXF Schema viewer](https://htmlpreview.github.io/?https://github.com/lbl-srg/modelica-json/blob/issue214_cxf/cxf-viz.html)
- [Modelica Schema viewer](https://htmlpreview.github.io/?https://github.com/lbl-srg/modelica-json/blob/master/modelica-viz.html)

## 5. CXF-Core.jsonld

[CXF-Core.jsonld](CXF-Core.jsonld) contains the CXF representation of all CDL elementary blocks, classes and relationships.

To generate the `CXF-Core.jsonld`, use:

```
node app.js -f <path/to/modelica-buildings>/Buildings/Controls/OBC/CDL -o cxf --elementary --cxfCore
```

The `CXF-Core.jsonld` file will be generated in `cxf` folder.

When parsing a file using `app.js`, the schema is chosen according to the mode.

Expand Down
44 changes: 39 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const pa = require('./lib/parser.js')
const ut = require('./lib/util.js')
const se = require('./lib/semanticExtractor.js')
const ce = require('./lib/cxfExtractor.js')

const logger = require('winston')
const path = require('path')
Expand All @@ -18,7 +19,7 @@ parser.addArgument(
['-o', '--output'],
{
help: 'Specify output format.',
choices: ['raw-json', 'json', 'modelica', 'semantic'],
choices: ['raw-json', 'json', 'modelica', 'semantic', 'cxf'],
defaultValue: 'json'
}
)
Expand Down Expand Up @@ -57,8 +58,26 @@ parser.addArgument(

['-p', '--prettyPrint'],
{
help: 'Pretty print JSON output.',
defaultValue: 'false'
help: 'Pretty print JSON output. The -o/--output should be raw-json/json/cxf.',
action: 'storeTrue'
}
)

parser.addArgument(

['--elementary'],
{
help: 'If this flag is present, generate CXF of elementary blocks in addition to composite blocks. -o/--output should be cxf.',
action: 'storeTrue'
}
)

parser.addArgument(

['--cxfCore'],
{
help: 'If this flag is present, generate CXF-core.jsonld. -o/--output should be cxf, -f/--file should be path/to/CDL and --elementary flag must be used.',
action: 'storeTrue'
}
)

Expand Down Expand Up @@ -93,19 +112,34 @@ if (args.output === 'modelica') {
pa.convertToModelica(args.file, args.directory, false)
} else {
// Get mo files array

if (args.elementary || args.cxfCore) {
if (!args.output === 'cxf') {
throw new Error('In order to generate CXF (jsonld) of elementary blocks, -o/--output should be cxf.')
}
}
if (args.cxfCore) {
if (!args.file.endsWith('CDL') && !args.file.endsWith('cdl')) {
throw new Error('In order to generate CXF-core.jsonld containing all elementary blocks, -f/--file should be path/to/CDL.')
}
if (!args.elementary) {
throw new Error('In order to generate CXF-core.jsonld containing all elementary blocks, --elementary flag must be used.')
}
}
const completedJsonGeneration = new Promise(
function (resolve, reject) {
const moFiles = ut.getMoFiles(args.file)
// Parse the json representation for moFiles
pa.getJsons(moFiles, args.mode, args.output, args.directory, args.prettyPrint)
pa.getJsons(moFiles, args.mode, args.output, args.directory, args.prettyPrint, args.elementary, args.cxfCore)
resolve(0)
}
)
completedJsonGeneration.then(function () {
if (args.output === 'semantic') {
se.getSemanticInformation(args.file, args.directory)
}
if (args.output === 'cxf' && args.cxfCore && args.elementary) {
ce.getCxfCore(args.file, args.directory, args.prettyPrint)
}
})
}

Expand Down
71 changes: 71 additions & 0 deletions cxf-viz.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>JSON Schema Viewer</title>

<!-- Bootstrap -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans|PT+Sans" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="css/jsv.css">
<link rel="stylesheet" href="css/app.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>

</head>
<body>

<!-- Nav/Toolbar -->
<div class="btn-group" id="app-toolbar">
<button id="visualizeButton" type="button" class="btn btn-default"><span class="glyphicon glyphicon-eye-open"> </span> Visualize</button>
<button id="sourceButton" type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-left"> </span> Source</button>
</div>

<!-- JSV -->
<div id="main-body"></div>

<!-- Editor -->
<div id="editor">
<pre id="json"></pre>
</div>

<div id="legend-container">
<div id="legend">
<h3 class="ui-mini">Legend</h3>
<hr />
<div id="legend-items">
</div>
</div>
</div>



<script>
NProgress.start();
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.5/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.5/mode-json.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.5/theme-chrome.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tv4/1.2.7/tv4.min.js"></script>
<script src="js/jsv.js"></script>
<script src="js/ref-parser.min.js"></script>
<script src="js/app.js"></script>
<script type="text/javascript">
$.getJSON( "./schema-cxf.json", function( data ) {
console.log(data);
document.getElementById("json").innerHTML = JSON.stringify(data, null, 4)
});
</script>
<script>
NProgress.done();
</script>
</body>
</html>

Loading

0 comments on commit ad84dbf

Please sign in to comment.