Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Use object as type instead of String
Use object as type instead of String [message #1779350] Fri, 05 January 2018 16:43 Go to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Hello,

my grammar looks like:

Eq:
	'eq' value=STRING | NUMBER |NULL;

NUMBER:
	'-'? INT ('.' INT)?;

terminal NULL:
	'null';




Currently the interface Eq constains a method `String getValue`, how can i change this to`Object getValue`?

[Updated on: Fri, 05 January 2018 17:32]

Report message to a moderator

Re: Use object as type instead of String [message #1779359 is a reply to message #1779350] Fri, 05 January 2018 17:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you please give some more context. if (at all) the type would be EObject? or do you mean java.lang.Object? if yes what do you want to fill it with?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779361 is a reply to message #1779359] Fri, 05 January 2018 17:38 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
The EObject is ok as well.
Currently the value is always a String (at the modell class), but it can be null or 112 or 11.2. or "A".
The issue is I do not know the original value anymore.

Small example with null:
	@Test
	public void parseNull() {
		final String value = null;
		String expression = "eq"+ value;
                //parsing...
		Eq eq;
		assertThat(eq.getValue()).isEqualTo(value);
	}

JUnit result said: Expected null Actual: "null"

Thanks for your help

[Updated on: Fri, 05 January 2018 17:39]

Report message to a moderator

Re: Use object as type instead of String [message #1779367 is a reply to message #1779361] Fri, 05 January 2018 17:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
that sounds like a parse error?!?

the question is your semantic?

wouldnt it be better to have a StringLiteral, a NumberLiteral, and Null as separate types?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779371 is a reply to message #1779367] Fri, 05 January 2018 18:06 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 17:51
that sounds like a parse error?!?



Why should this be a parse error?


Christian Dietrich wrote on Fri, 05 January 2018 17:51

the question is your semantic?


The semantic is like:

eq(attribute, "a") or eq(attribute, 1) or eq(attribute, null)

Christian Dietrich wrote on Fri, 05 January 2018 17:51

wouldnt it be better to have a StringLiteral, a NumberLiteral, and Null as separate types?


Do you mean something like this:

Instead of
Eq:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING | INT | NULL')';


this:

Eq:
	EqString | EqInt ...
EqString:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING')';
EqInt:
       'eq' '(' attribute=FIELD_VALUE ',' value=INT')';
Re: Use object as type instead of String [message #1779372 is a reply to message #1779367] Fri, 05 January 2018 18:07 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 17:51
that sounds like a parse error?!?



Why should this be a parse error?


Christian Dietrich wrote on Fri, 05 January 2018 17:51

the question is your semantic?


The semantic is like:

eq(attribute, "a") or eq(attribute, 1) or eq(attribute, null)

Christian Dietrich wrote on Fri, 05 January 2018 17:51

wouldnt it be better to have a StringLiteral, a NumberLiteral, and Null as separate types?


Do you mean something like this:

Instead of
Eq:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING | INT | NULL')';


this:

Eq:
	EqString | EqInt ...
EqString:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING')';
EqInt:
       'eq' '(' attribute=FIELD_VALUE ',' value=INT')';
Re: Use object as type instead of String [message #1779373 is a reply to message #1779367] Fri, 05 January 2018 18:07 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 17:51
that sounds like a parse error?!?



Why should this be a parse error?


Christian Dietrich wrote on Fri, 05 January 2018 17:51

the question is your semantic?


The semantic is like:

eq(attribute, "a") or eq(attribute, 1) or eq(attribute, null)

Christian Dietrich wrote on Fri, 05 January 2018 17:51

wouldnt it be better to have a StringLiteral, a NumberLiteral, and Null as separate types?


Do you mean something like this:

Instead of
Eq:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING | INT | NULL')';


this:

Eq:
	EqString | EqInt ...
EqString:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING')';
EqInt:
       'eq' '(' attribute=FIELD_VALUE ',' value=INT')';
Re: Use object as type instead of String [message #1779374 is a reply to message #1779373] Fri, 05 January 2018 18:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
so eq is (field, value) ?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779375 is a reply to message #1779367] Fri, 05 January 2018 18:08 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 17:51
that sounds like a parse error?!?



Why should this be a parse error?


Christian Dietrich wrote on Fri, 05 January 2018 17:51

the question is your semantic?


The semantic is like:

eq(attribute, "a") or eq(attribute, 1) or eq(attribute, null)

Christian Dietrich wrote on Fri, 05 January 2018 17:51

wouldnt it be better to have a StringLiteral, a NumberLiteral, and Null as separate types?


Do you mean something like this:

Instead of
Eq:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING | INT | NULL')';


this:

Eq:
	EqString | EqInt ...
EqString:
	'eq' '(' attribute=FIELD_VALUE ',' value=STRING')';
EqInt:
       'eq' '(' attribute=FIELD_VALUE ',' value=INT')';
Re: Use object as type instead of String [message #1779376 is a reply to message #1779375] Fri, 05 January 2018 18:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i mean something like

value=ValueExpression

ValueExpression: StringLiteral | IntLiteral | NullLiteral;

StringLiteral: value=STRING
IntLiteral: value=INT
NullLiteral: {NullLiteral} "NULL";


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779377 is a reply to message #1779376] Fri, 05 January 2018 18:23 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 18:11
i mean something like



I have already done that, I have abbreviated this only for the example.
Sorry for the confusion:

This are my type rules

Eq:
	'eq' '(' attribute=FIELD_VALUE ',' value=Literal ')';

Literal:
	(StringOrNumberLiteral | BOOLEAN | NULL);

StringOrNumberLiteral:
	STRING | NUMBER;

NUMBER:
	'-'? INT ('.' INT)?;

terminal BOOLEAN returns ecore::EBoolean:
	'true' | 'false';

terminal NULL:
	'null';

terminal FIELD_VALUE:
	('A'..'Z' | 'a'..'z') ('A'..'Z' | 'a'..'z' | '0'..'9' | '.' | '_' | '-')*;
Re: Use object as type instead of String [message #1779378 is a reply to message #1779377] Fri, 05 January 2018 18:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
And Whats your actual question

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779390 is a reply to message #1779378] Fri, 05 January 2018 22:08 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 18:37
And Whats your actual question


The question is why looks the Eq like this

class Eq
String getValue()


insteadOf

class Eq
EObject getValue()


I have the same issue for the In operator

class In
List<Strings> getValues()


How should i validate the datatype, if all values are strings?
How can I check if it was a 1 or "1"
Re: Use object as type instead of String [message #1779393 is a reply to message #1779390] Fri, 05 January 2018 22:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
As I said. Make the literals eobjects
By using assignments
Then you can instanceof


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779399 is a reply to message #1779393] Sat, 06 January 2018 10:23 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 22:46
As I said. Make the literals eobjects
By using assignments
Then you can instanceof


Ahh sorry didn't see the assignments. I will try that.
Thanks

Edit: Works :)

[Updated on: Mon, 08 January 2018 06:57]

Report message to a moderator

Re: Use object as type instead of String [message #1779458 is a reply to message #1779359] Mon, 08 January 2018 10:18 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Fri, 05 January 2018 17:25
if (at all) the type would be EObject? or do you mean java.lang.Object? if yes what do you want to fill it with?


1 more question, sorry. Now the value of Literal(s) are EObjects. Is it possible to replace it with java.lang.Object?

Model code:
 EObject getValue();
Re: Use object as type instead of String [message #1779461 is a reply to message #1779458] Mon, 08 January 2018 10:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
why do you want to?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779462 is a reply to message #1779461] Mon, 08 January 2018 10:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
and how does the grammar look like. maybe you did something wrong

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779466 is a reply to message #1779461] Mon, 08 January 2018 10:39 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Mon, 08 January 2018 10:23
why do you want to?


Because a EObject is a interface and a Object is easier to use:
This will not compile if the list contains EObjects
assertThat(inFilter.getValues()).contains("a", "b");




I found this solution, but this no good idea, becuase the conversion is not working anymore
Literal returns ecore::EJavaObject:
	StringOrNumberLiteral | BOOLEAN | NULL;

StringOrNumberLiteral returns ecore::EJavaObject:
	(STRING | NUMBER);

[Updated on: Mon, 08 January 2018 11:58]

Report message to a moderator

Re: Use object as type instead of String [message #1779472 is a reply to message #1779462] Mon, 08 January 2018 12:17 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Mon, 08 January 2018 10:24
and how does the grammar look like. maybe you did something wrong


Currently looks like, but iam not happy with it.

That's way too many classes. That's why an object would be my favorite. Is that not possible?
Literal returns ecore::EObject:
	(StringOrNumberLiteral | BooleanValue | NullValue);
	
StringOrNumberLiteral returns ecore::EObject:
	(StringLiteral | NumberValue);
	
StringLiteral returns ecore::EObject:
	StringValue;
	
StringValue:
	value = STRING;

NumberValue:
	value = DOUBLE;
	
BooleanValue:
	value = BOOLEAN;
	
NullValue:
	value = NULL;

:

[Updated on: Mon, 08 January 2018 12:35]

Report message to a moderator

Re: Use object as type instead of String [message #1779475 is a reply to message #1779472] Mon, 08 January 2018 12:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no i meant

Literal:
(StringOrNumberLiteral | BooleanValue | NullValue);

StringOrNumberLiteral:
(StringLiteral | NumberValue);

StringLiteral:
value = STRING;

NumberValue:
value = DOUBLE;

BooleanValue:
value = BOOLEAN;

NullValue:
value = NULL;



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779477 is a reply to message #1779475] Mon, 08 January 2018 12:54 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Yes but then i have a Literal Class and a lot of sub classes.
Is that unavoidable?

i would prefer something like
value=(STRING | INT)

Object getValue()

[Updated on: Mon, 08 January 2018 13:00]

Report message to a moderator

Re: Use object as type instead of String [message #1779480 is a reply to message #1779477] Mon, 08 January 2018 13:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Model:
greetings+=Greeting*;

Greeting:
'Hello' name=WhyTypesIfYouCanHaveObject '!';

// TODO implement value converter
WhyTypesIfYouCanHaveObject returns ecore::EJavaObject:
INT | STRING
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Use object as type instead of String [message #1779481 is a reply to message #1779480] Mon, 08 January 2018 13:19 Go to previous messageGo to next message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Okay understand will try to solve it better with types.
Re: Use object as type instead of String [message #1779482 is a reply to message #1779475] Mon, 08 January 2018 14:00 Go to previous message
Dennis Melzer is currently offline Dennis MelzerFriend
Messages: 48
Registered: December 2014
Member
Christian Dietrich wrote on Mon, 08 January 2018 12:48
no i meant

Literal:
(StringOrNumberLiteral | BooleanValue | NullValue);

StringOrNumberLiteral:
(StringLiteral | NumberValue);

StringLiteral:
value = STRING;

NumberValue:
value = DOUBLE;

BooleanValue:
value = BOOLEAN;

NullValue:
value = NULL;



Mhm ok this works, but with this solution i have a lots of cast, because the Literal class has no getValue(). Just the subclasses has a getValue
		StringLiteral literal = (StringLiteral) eq.getLiteral();
		assertThat(literal.getValue()).isEqualTo("4711");

Previous Topic:Unit Test with @Traced
Next Topic:Deploying xtext features
Goto Forum:
  


Current Time: Thu Apr 18 18:06:09 GMT 2024

Powered by FUDForum. Page generated in 0.03187 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top