Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dana Benedicto authored and anandkp92 committed Apr 2, 2024
1 parent 2d8affd commit 4e8247b
Show file tree
Hide file tree
Showing 15 changed files with 1,159 additions and 35 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ See the directory `test/FromModelica` from __modelica-json__ for simple examples

## 2. Installation and help

__modelica-json__ requires both Java and Node.js.
This pacakged has been tested with Java (JDK 21, JRE 8u391 and Node.js 18).

### Linux

First, set the MODELICAPATH environment variable by adding the following line to your ~/.bashrc file:
Expand Down Expand Up @@ -67,18 +70,18 @@ make clean-installation

- Finally, to install dependencies and compile the Java files, run `InstallOnWindows.bat`.

To test the installation, from the `\modelica-json` directory, run the parser on Command Prompt:
To test the installation, from the `path\to\modelica-json` directory, run the parser on Command Prompt:
```
node app.js -f test\FromModelica\Enable.mo
```

Now the `\modelica-json` directory should have a new folder `json` and in the folder, there should be a file in the path `test\FromModelica\Enable.json`.
Now the `path\to\modelica-json` directory should have a new folder `json` and in the folder, there should be a file in the path `test\FromModelica\Enable.json`.

## 3. How to use the parser

The parser can be run with the app.js file as follows:
```
node app.js -f <path of the file to parse>
node app.js -f <path of the file to parse> -o <output format> -m <parser mode> -l <log level> -d <output directory>
```

#### Arguments :
Expand All @@ -88,11 +91,20 @@ The only required input is the path of the file or package to be parsed.

##### --output / -o

This parser takes a .mo file in input and has three possible outputs, that can be specified with the argument -o :
This parser can take a .mo file in input and produce three possible outputs, that can be specified with the argument -o :

<<<<<<< HEAD
- **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
=======
- **raw-json** : intermediate JSON output, aligning closely with the Modelica syntax
- **json**: simplified JSON format, to be used by all applications
- **semantic**: generate a semantic model from the Modelica model, if annotations containing semantic information are present in the Modelica model

This parser can also take a .json file as an input and if it aligns with the simplified JSON output format, a corresponding Modelica or CDL model can be generated using the followed argument in the output format (-o):
- **modelica**: Generate a Modelica/CDL file (.mo) from a JSON file that conforms to the simplified JSON schema.
>>>>>>> ae0dee5722f82ce552a298c4e11fb05b1d90f5d9
##### --mode / -m

Expand Down
27 changes: 18 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ if (args.output === 'modelica' && !args.file.endsWith('.json')) {
throw new Error('Modelica output requires the input file (-f) to be a json file.')
}

if (args.output === 'modelica' && !args.file.endsWith('.json')) {
throw new Error('modelica output requires a input file (-f) to be a json file')
}

if (args.output !== 'modelica' && args.file.endsWith('.json')) {
throw new Error("The json input file required only when the output format (-o) is 'modelica'.")
}
Expand Down Expand Up @@ -116,16 +120,21 @@ if (args.output === 'json') {
} else {
schema = path.join(`${__dirname}`, 'schema-modelica.json')
}
<<<<<<< HEAD
const jsonDir = (args.directory === 'current') ? process.cwd() : args.directory
let jsonFiles = ut.findFilesInDir(path.join(jsonDir, 'json'), '.json')
=======
console.log('parsing mode: ', schema)
// let jsonFiles = ut.findFilesInDir(path.join(args.directory, 'json'), '.json')
>>>>>>> ae0dee5722f82ce552a298c4e11fb05b1d90f5d9
// exclude CDL folder and possibly Modelica folder
const pathSep = path.sep
const cdlPath = path.join(pathSep, 'CDL', pathSep)
const modelicaPath = path.join('Modelica', pathSep)
jsonFiles = jsonFiles.filter(obj => !(obj.includes(cdlPath) || obj.includes(modelicaPath)))
// validate json schema
for (let i = 0; i < jsonFiles.length; i++) {
const eachFile = jsonFiles[i]
setTimeout(function () { ut.jsonSchemaValidation(args.mode, eachFile, 'json', schema) }, 100)
}
// const pathSep = path.sep
// const cdlPath = path.join(pathSep, 'CDL', pathSep)
// const modelicaPath = path.join('Modelica', pathSep)
// jsonFiles = jsonFiles.filter(obj => !(obj.includes(cdlPath) || obj.includes(modelicaPath)))
// // validate json schema
// for (let i = 0; i < jsonFiles.length; i++) {
// const eachFile = jsonFiles[i]
// setTimeout(function () { ut.jsonSchemaValidation(args.mode, eachFile, 'json', schema) }, 100)
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
public class EquationVisitor extends modelicaBaseVisitor<Equation> {
@Override
public Equation visitEquation(modelicaParser.EquationContext ctx) {

Simple_expressionVisitor simple_expressionVisitor = new Simple_expressionVisitor();
Simple_expression lhs = ctx.simple_expression() == null ? null : ctx.simple_expression().accept(simple_expressionVisitor);

Expand All @@ -35,7 +34,6 @@ public Equation visitEquation(modelicaParser.EquationContext ctx) {

For_equationVisitor for_equationVisitor = new For_equationVisitor();
For_equation for_equation = ctx.for_equation() == null ? null : ctx.for_equation().accept(for_equationVisitor);

Connect_clauseVisitor connect_clauseVisitor = new Connect_clauseVisitor();
Connect_clause connect_clause = ctx.connect_clause() == null ? null : ctx.connect_clause().accept(connect_clauseVisitor);

Expand Down
2 changes: 1 addition & 1 deletion json2mo/argumentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function parse (content, rawJson = false) {
if (argumentStr != null) {
argumentStr.forEach(argument => {
moOutput += argumentParser.parse(argument, rawJson)
moOutput += ','
moOutput += ', '
})
moOutput = moOutput.slice(0, -2)
}
Expand Down
7 changes: 4 additions & 3 deletions json2mo/arraySubscripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function parse (content, rawJson = false) {
if (subscripts != null) {
subscripts.forEach(subscript => {
moOutput += subscriptParser.parse(subscript, rawJson)
moOutput += ','
moOutput += ', '
})
moOutput = moOutput.slice(0, -2)
}
Expand All @@ -23,12 +23,13 @@ function parse (content, rawJson = false) {
arraySubscripts.forEach(ele => {
if (ele.colon_op != null) {
if (ele.colon_op) {
moOutput += ':'
moOutput = moOutput.slice(0, -2)
moOutput += ': '
}
} else if (ele.expression != null) {
moOutput += expressionParser.parse(ele.expression, rawJson)
moOutput += ', '
}
moOutput += ','
})
moOutput = moOutput.slice(0, -2)
}
Expand Down
4 changes: 2 additions & 2 deletions json2mo/classModification.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function parse (content, rawJson = false) {
if (content.argument_list != null) {
moOutput += argumentListParser.parse(content.argument_list, rawJson)
}
moOutput += ')\n\t'
moOutput += ')\n'
} else {
const argumentList = content
moOutput += '(\n\t'
Expand All @@ -19,7 +19,7 @@ function parse (content, rawJson = false) {
})

moOutput = moOutput.slice(0, moOutput.lastIndexOf(','))
moOutput += ')'
moOutput += ')\n'
}
return moOutput
}
Expand Down
2 changes: 1 addition & 1 deletion json2mo/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function parse (content, rawJson = false) {
let moOutput = ''
if (rawJson) {
if (content.string_comment != null) {
moOutput += util.format('\n"\t%s"', content.string_comment)
moOutput += util.format('\n\t"%s"', content.string_comment)
}
if (content.annotation != null) {
moOutput += annotationParser.parse(content.annotation, rawJson)
Expand Down
2 changes: 1 addition & 1 deletion json2mo/componentDeclaration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function parse (content, rawJson = false) {
const declarationParser = require('./declaration')
const conditionAttributeParser = require('./condition_attribute')
const conditionAttributeParser = require('./conditionAttribute')
const commentParser = require('./comment')

let moOutput = ''
Expand Down
2 changes: 1 addition & 1 deletion json2mo/conditionAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function parse (content, rawJson = false) {
moOutput += expressionParser.parse(content.expression, rawJson)
}
} else {
const expression = content
const expression = content.expression
if (expression != null) {
moOutput += expressionParser.parse(expression, rawJson)
}
Expand Down
4 changes: 2 additions & 2 deletions json2mo/elementModification.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function parse (content, rawJson = false) {
if (content.modification != null) {
moOutput += modificationParser.parse(content.modification, rawJson)
}
if (content.string_comment != null) {
moOutput += util.format('%s', content.string_comment, rawJson)
if (content.description_string != null) {
moOutput += util.format(' "%s" ', content.description_string)
}
}
return moOutput
Expand Down
2 changes: 1 addition & 1 deletion json2mo/equationSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function parse (content, rawJson = false) {
moOutput += 'initial '
}
}
moOutput += 'equation'
moOutput += 'equation\n'
let equations
if (rawJson) {
equations = content.equations
Expand Down
10 changes: 5 additions & 5 deletions json2mo/graphic.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ function placementParse (obj) {
strArr.push('iconVisible=' + util.format('%s', iconVisible))
}
if (transformation != null) {
strArr.push('transformation' + transformationParse(transformation) + ')')
strArr.push('transformation' + transformationParse(transformation))
}
if (iconTransformation != null) {
strArr.push('iconTransformation' + transformationParse(iconTransformation) + ')')
strArr.push('iconTransformation' + transformationParse(iconTransformation))
}
return 'Placement(' + strArr.join(',') + ')'
}
Expand All @@ -103,8 +103,8 @@ function graphicsParse (obj) {
for (let i = 0; i < obj.length; i++) {
const ithEle = obj[i]
const name = ithEle.name
const attibutes = ithEle.attribute
const graComIte = commonGraphicItems(attibutes)
const attributes = ithEle.attribute
const graComIte = commonGraphicItems(attributes)
strArr.push(name + '(' + graComIte.join(',') + ')')
}
return 'graphics=' + '{' + strArr.join(',') + '}'
Expand Down Expand Up @@ -228,7 +228,7 @@ function commonGraphicItems (obj) {
strArr.push('fontSize=' + util.format('%s', fontSize))
}
if (textColor != null) {
strArr.push('textColor=' + colorParse(extent))
strArr.push('textColor=' + colorParse(textColor))
}
if (horizontalAlignment != null) {
strArr.push('horizontalAlignment=' + util.format('%s', horizontalAlignment))
Expand Down
5 changes: 3 additions & 2 deletions json2mo/longClassSpecifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ function parse (content, rawJson = false) {
}
}
}
moOutput += '\n'

if (rawJson) {
const stringComment = content.string_comment
if (stringComment != null) {
moOutput += util.format('\n%s\n', stringComment)
moOutput += util.format('%s\n', stringComment)
}
} else {
const descriptionString = content.description_string
if (descriptionString != null) {
moOutput += util.format('\n"%s"\n', descriptionString)
moOutput += util.format('"%s"\n', descriptionString)
}
}

Expand Down
Loading

0 comments on commit 4e8247b

Please sign in to comment.