Xtext , Defining a Structure for a plot - eclipse

i have to make a type curve that is a curve plot , the data is given by a CSV file (The interpreter is who read the data file). i have two type of values , x-axis, time (i think INT:INT) minutes:seconds format ; and y-axis voltage ( double ) . My grammar datatypes are the following :
Model:
elements+=Element*;
Element:
DataType /* | Operation | Control*/ ;
DataType :
Text | Real
;
Text:
("String" | "Text") name=ID (value=STRING)?
;
Double returns ecore::EDouble:
INT ('.' INT)?
;
Real:
("Double" | "Real") name=ID (value=Double)?
;
I was traying to make a map in my grammar with this format : Curve(Double voltage,time INT:INT) but i dont know why it doesnt work . what do you think guys ? should i define a new type for the type time ? or how make the map properly? Thanks for your time
CurveCollection :
'Curve['keys+=[Real]*','values+=Time*']'
;
Time:
INT':'INT
;
I tried to define the structure , but i dont know if is going to work like to arrays of (keys , values ) like a Map in java

you can enable a default unique name validation by uncommenting in and rerunning the language workflow
validator = {
composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}
alternatively you can implement a own unique name validation in <YourDsl>Validator

Related

What I have to write in the string "table" of combiTable1D parameters when data are stored in a txtFile?

I have created a txt.File named combitable1.txt in
C:\Users\Yamaha R6\Desktop\FileOpenModelica
I want to "load" data of this file into combitable1D in OpenModelica. If you see the image, under the voice "table", I have wrote :
loadResource("C:/Users/Yamaha R6/Desktop/FileOpenModelica/combitable1.txt")
but when I simulate the model following error occurs :
15:51:20 Translation Error
Class loadResource not found in scope (looking for a function or record)
What can I do?
You don't need to use loadResource function in this case. You can leave the table name as it is
parameter Real table[:, :] = fill(0.0, 0, 2)
"Table matrix (grid = first column; e.g., table=[0,2])"
Your file formattig should be like following, assuming a text file myFile.txt:
#1
double myTable(200000,2)
0.000000 0.110519
0.001000 0.316248
0.002000 0.505827
0.003000 0.703204
0.004000 0.894942
0.005000 1.072796
... ...
With following inputs to the Modelica.Blocks.Sources.CombiTimeTable
parameter String fileName = "C:\\SomeLocation\\myFile.txt";
parameter String tableName = "myTable";
The fields do not have the right values.
table: leave empty
tableName: "tab1" (might be able to skip quotes)
filename: use loadResource - but give the full Modelica name:
ModelicaServices.ExternalReferences.loadResource("c:/users....");
(Technically loadResource is more for the case ModelicaServices.ExternalReferences.loadResource("modelica://A/combiTable.txt"); )

swapping variable name and variable type

I have a text file with a long list of variables like this:
a VARCHAR(32),
b INT,
c TINYINT,
.
.
.
I want to quickly swap the order of the name and type so I have:
VARCHAR(32) a,
INT b,
TINYINT c
.
.
.
Im happy to use a bash terminal or notepad ++ and I do have a basic knowledge of regex but Im not sure how to tackle this problem.
How can I go about doing this?
you can use this app I just wrote:
http://codepen.io/franzskuffka/pen/Ndxejz
Or run it through this function
function swap (text) {
let lines = text.split(',\n')
let parts = lines.map(function (line) {
var lineParts = line.split(' ')
lineParts[2] = lineParts[0]
delete lineParts[0]
return lineParts.join(' ')
})
return parts.join(',\n')
}
Generally I recommend the text editor Kakoune for this task which is awesome for text processing in general due to the multicursor support and incremental editing.

xText Variable/Attribute Assignment

I built a grammar in xText to recognize formal expressions of a specific format
and to use the generated object tree in Java.
This is what it looks like:
grammar eu.gemtec.device.espa.texpr.Texpr with org.eclipse.xtext.common.Terminals
generate texpr "http://www.gemtec.eu/device/espa/texpr/Texpr"
Model:
(expressions+=AbstractExpression)*
;
AbstractExpression:
MatcherExpression | Assignment;
MatcherExpression:
TerminalMatcher ({Operation.left=current} operator='or' right= MatcherExpression)?
;
TerminalMatcher returns MatcherExpression:
'(' MatcherExpression ')' | {MatcherLiteral} value=Literal
;
Literal:
CharMatcher | ExactMatcher
;
CharMatcher:
type=('text'|'number'|'symbol'|'whitespace') ('(' cardinality=Cardinality ')')?
;
/* Kardinalitäten für CharMatcher*/
Cardinality:
CardinalityMin | CardinalityMinMax | CardinalityMax| CardinalityExact
;
CardinalityMin: min=INT '->';
CardinalityMinMax: min=INT '->' max=INT;
CardinalityMax: '->' max=INT;
CardinalityExact: exact=INT;
ExactMatcher:
(ignoreCase='ignoreCase''(' expected=STRING ')') | expected=STRING
;
/* Variablenzuweisung
*
* z.B. $myVar=number
* */
Assignment:
'$' name=ID '=' expression=MatcherExpression
;
Everything works fine except for the 'cardinality' assignment.
The Expressions look like this:
text number(3) - (an arbitrary amount of letters followed by exactly 3 numbers)
symbol number(2->) - (an arbitrary amount of special characters followed by at least 2 numbers)
whitespace number(->4) - (an arbitrary amount of whitespaces followed by a maximum of 4 numbers)
number(3->6) - (at least 3 numbers but not more than 6)
When I run Eclipse with this grammar (so that my language is recognized and has code completion and so on), everything I type is shown in the "Outline"-tab as a tree-structure as it should, except for the cardinality values.
When I add a cardinality statement to a CharMatcher, the little plus appears before it, but when I click on it it just disappears.
Can anyone tell me why this does not work?
I found the solution myself, I think the problem was that the compiler could not decide which class to create at this point:
Cardinality:
CardinalityMin | CardinalityMinMax | CardinalityMax| CardinalityExact
;
CardinalityMin: min=INT '->';
CardinalityMinMax: min=INT '->' max=INT;
CardinalityMax: '->' max=INT;
CardinalityExact: exact=INT;
So I simplified the whole thing a little, it now looks like this:
Cardinality:
CardinalityMinMax | CardinalityExact
;
CardinalityMinMax: (min=INT '..' max=INT) | (min=INT '..') | ('..' max=INT);
CardinalityExact: exact=INT;
It is still not shown in the "Outline"-Tab, but I suppose that is a problem of the visualisation.
The generated classes now work as intended.

matlab saving a cellarray

I have a script which does not fully work:
inputfield=input('Which field would you like to see: ','s')
if isfield(package, inputfield)
fprintf('The value of the %s field is: %c\n',inputfield,...
eval(['package.' inputfield]))
else
fprintf('Error: %s is not valid field\n', inputfield)
end
First I define a structure in matlab and then i use the script on the structure:
package=struct('item_no',123,'cost',19.99,'price',39.95,'code','g')
package =
item_no: 123
cost: 19.9900
price: 39.9500
code: 'g'
structurevalue
Which field would you like to see: cost
inputfield =
cost
The value of the cost field is: 1.999000e+001
structurevalue
Which field would you like to see: item_no
inputfield =
item_no
The value of the item_no field is: {
why cant it read value for item_no?
Try:
fprintf('The value of the %s field is: %s\n',inputfield,...
num2str(package.(inputfield)))
There were two issues with your version.
You were passing both numbers and strings into the %c field in your fprintf string. When a decimal goes in, it is interpreted as a number and displayed in full precision, which is why 19.99 got displayed as 1.999000e+001. But when an integer goes in, it gets interpreted as a character, which is why 123 got displayed as '{' (ASCII character 123). Use num2str to convert numbers to strings for display. Also, use %s for a string of any length, rather than %c for a character.
In general, it's not a good idea to use eval unless you have to. In this case, it's more convenient to use inputfield as a dynamic field name of package.

Handling multiple return values in ANTLR

I have a simple rule in ANTLR:
title returns [ElementVector<Element> v]
#init{
$v = new ElementVector<Element>() ;
}
: '[]'
| '[' title_args {$v.add($title_args.ele);} (',' title_args {$v = $title_args.ele ;})* ']'
;
with title_args being:
title_args returns [Element ele]
: author {$ele = new Element("author", $author.text); }
| location {$ele = new Element("location", $location.text); }
;
Trying to compile that I get confronted with a 127 error in the title rule: title_args is a non-unique reference.
I've followed the solution given to another similar question in this website (How to deal with list return values in ANTLR) however it only seems to work with lexical rules.
Is there a specific way to go around it ?
Thank you,
Christos
You have 2 title_args in your expression, you need to alias them. Try this:
| '[' t1=title_args {$v.add($t1.ele);} (',' t2=title_args {$v = $t2.ele ;})* ']'
t1 and t2 are arbitrary aliases you can choose anything you want as long as they match up.
I think the problem is your reusing the title_args var. Try changing one of those variable names.
Yeah, I had the same problem.
You need to change one of the variable names; for example, do like the following:
title_args
title_args1
in your code instead of using title_args twice.
If title_args is a parser rule, then just create the same rule with the name title_args1.
So, basically there would be two rules with the same functionality.