Xtext - Get cross-referenced child - children

I have a grammar that looks like:
A:
myField=[B]
B:
C | D | E
I have a function that gets A (let's say a) as a parameter and I want to access C, for example.
I did a.myField that returns a B object (let's say b). Than I used
EcoreUtil2.getAllContentsOfType(b,C) - but it returns an empty list.
Maybe the reason is that B is not really parsed again, but cross-referenced. If so, is there any function that allows me to access C/D/E in the above example?
Thank you.
Update
Apparently b is null, so of course getAllContentsOfType() returns an empty list. How do I access B (which is cross-referenced from A)?

Had to check that a.myField isn't null.

Related

Gremlin - Using an OR step to get different types of connected vertices

So I have a graph schema where vertex type A can connect inwards to vertex type B or type C in a one to many relationship. I'm trying to write a query that outputs any of those relationships if they exist, for instance a sample output would be:
Type A | Type B | Type C
Sample1A,'', Sample1C
Sample2A, Sample2B, ''
Sample3A, Sample3B, Sample3C
Sample4A, 'Sample4Ba, Sample4Bb', Sample4C
The fourth example is if A is connected to multiple B types. If B and C don't exist, then nothing is output.
So far I have the query: g.V().hasLabel('A').as('A').in('connect').hasLabel('B').as('B').or().in('connect').hasLabel('C').as('C').select('A','B','C')
But this query only returns the A vertices without any B's or C's.
Using AWS Neptune if that matters.
As kevin mentioned in the comment, you can use .project() method for this scenario.
g.V().hasLabel("A").as("A").project("a","b", "c")
.by(select("A"))
.by(choose(in("connect").hasLabel("B").count().is(0), constant("NO_B").value(), in("connect").hasLabel("B")))
.by(choose(in("connect").hasLabel("C").count().is(0), constant("NO_C").value() , in("connect").hasLabel("C")));
Your or() steps are not returning a result as written. You could simplify the query as follows:
g.V().hasLabel('A').as('A').in('connect').hasLabel(within('B','C').as('B').select('A','B')
This avoids using select('A','B','C') as only one of 'B' or 'C' will have a result in the or() case.
Here is a version that still uses or()
g.V().hasLabel('A').as('A').in().or(hasLabel('B'),hasLabel('C')).as('B').select('A','B')

Variable duplicates and copies in Julia

I'm confused by the answers to this previous question Creating copies in Julia with = operator:
Specifically I'm confused by the comments under StefanKarpinki's October 7th answer to that question, specifically when RedPointyJackson said
"Ok, I undestand that. But when I do b=a, it should be assignment because it's an operation in the x = ... style, right? So now I have b 'pointed' to a, and all changes in a should reflect whenever I evaluate b, don't they?" – RedPointyJackson Oct 9 '15 at 12:49
and then StefanKarpinski said
"Yes, that's correct and all of this behavior is completely in line with that. If you do a = b then any change to b also affects a. If the value bound to b is an immutable value like 42 then you can't mutate it anyway, so there's no way to tell if it was copied or referenced." – StefanKarpinski Oct 10 '15 at 4:47
Why are these previous comments suggesting that the Julia commands
a = 1;
b = a;
a = 2;
will change the value of b to 2? RedPointyJackson started that thread with evidence that b will remain equal to 1!! So why are the quoted comments suggesting that the value of b will change to 2!?
So why are the quoted comments suggesting that the value of b will change to 2!?
It looks like you misunderstood those comments.
If you do a = b then any change to b also affects a.
To be precise, this should be rephrased as "any change to the value bound to b also affects a." Note the only thing that matters here is the value to be bound, not the variable name b. Accordingly,
a = 1; # the variable name `a` is binding to the value `1`
b = a; # the variable name `b` is binding to the value(`1`) bound to `a`
a = 2; # the variable name `a` is binding to the value `2` (`b` is still binding to `1`.)
As 1 is an immutable value and there is no way to mutate it, we have to rebind a/b if we'd like to change "their content". Here is another example:
A = [1]; # the variable name `A` is binding to the value `[1]`(an array)
B = A; # the variable name `B` is binding to the value(`[1]`) bound to `A`
A[1] = 2; # this is called mutation. the value(`[1]`) bound to `A` has been changed to `[2]`. this value is also bound to `B`, so the "content" of `B` is changed accordingly.
A = 1; # the variable name `A` is binding to the value `1` (`B` is still binding to `[2]`.)
I am trying to explain this issue in the following terms (from an upcoming book):
Memory and copy issues
In order to avoid copying large amount of data, Julia by default copies only the memory address of objects, unless the programmer explicitly request a so-called "deep" copy or the compiler "judges" an actual copy more efficient.
Use copy() or deepcopy() when you don't want that subsequent modifications to the copied object would apply to the original object.
In details:
Equal sign (a=b)
performs a name binding, i.e. binds (assigns) the entity (object) referenced by b also to the a identifier (the variable name)
it results that:
if b then rebinds to some other object, a remains referenced to the original object
if the object referenced by b mutates (i.e. it internally changes), so does (being the same object) those referenced by a
if b is immutable and small in memory, under some circumstances, the compiler would instead create a new object and bind it to a, but being immutable for the user this difference would not be noticeable
a = copy(b)
creates a new, "independent" copy of the object and bind it to a. This new object may however reference in turn other objects trough their memory address. In this case it is their memory address that is copied and not the referenced objects themselves.
it results that:
if these referenced objects (e.g. the individual elements of a vector) are rebound to some other objects, the new object referenced by a maintains the reference to the original objects
if these referenced objects mutate, so do (being the same objects) those referenced by the new object referenced by a
a = deepcopy(b)
everything is deep copied recursively

Rocket Universe Dictionary passing VM attribute value to subroutine

Okay this might get a tad complex or not.
Have a file with a multivalues in attribute 4
I want to write another dictionary item that loops through the multivalue list, calls a subroutine and returns calculated values for each item in attribute 4.
somthing like
<4> a]b]c]d]e
New attribute
#RECORD<4>;SUBR("SUB.CALC.AMT", #1)
Result
<4> AMT
a 5.00
b 15.00
c 13.50
d 3.25
Not quite sure how to pass in the values from RECORD<4>, had a vague notion of a #CNT system variable, but that's not working, which might mean it was from SB+ or one of the other 4GLs.
You might be over thinking this.
You should just be able to reference it without out doing the ";" and #1 thing (I am not familiar with that convention). Using an I-Descriptor this should do the trick, though I have traditionally used the actual dictionary names instead of the #RECORD thing.
SUBR("SUB.CALC.AMT", #RECORD<4>)
This should work provided your subroutine is compiled, cataloged and returns your desired value with the same value/subvalue structure as #RECORD<4> in the first parameter of the Subroutine.
SUBROUTINE SUB.CALC.AMT(RETURN.VALUE,JUICY.BITS)
JBC = DCOUNT(JUICY.BITS<1>,#VM)
FOR X=1 TO JBC
RETURN.VALUE<1,X> = JUICY.BITS<1,X>:" or something else"
NEXT X
RETURN
END
Good luck.

Is the same Empty returned when it matched in a function?

we have mapOptional from the NICTA course:
mapOptional :: (a -> b) -> Optional a -> Optional b
mapOptional _ Empty = Empty
mapOptional f (Full a) = Full (f a)
When matching f we obviously use that function that was passed, what about the Empty? and what about Full?
There is nothing in Haskell that lets you observe whether the two Emptys are the same Empty or not, and no guarantees about what an implementation must do with that code in that regard.
That said, in GHC, nullary constructors for a given parameterized type are shared across all parameterizations; so there is just one Empty in the whole program, and just one [], and so forth.
They can't be the same Empty, the argument has the type Optional a and the output has the type Optional b. When I try to force some sort of reuse, I will typically use something of the type
mapOptional _ a#Empty = a
This won't compile, and I don't think that's implementation dependent.

Joining on expanded variables in zsh

I am trying to do the following without an intermediate variable:
list_a=(a b c) # build a list
list_b=(1${^list_a}) # 1a 1b 1c
joined=${(j:,:)list_b} # 1a,1b,1c
I would expect something like this work...
${(j:,:)(1${^(a b c)})}
Then I realized that my core assumptions was just wrong...
1${^(a b c)} # this gives a bad substitution error
I am pretty sure I fundamentally don't understand how nested array substitution works in zsh...
You cannot use an array declaration inside a parameter substitution. And even if it did work, it would be mainly an overly complicated way to build the string "1a,1b,1c".
If you really need this for some reason, you can go with
echo ${(j:,:):-1${^${=:-a b c}}}
Explanation:
${:-a b c} is substituted with "a b c". This way a string can be injected like it would be from a parameter substiton. This may seem like a null-operation, but it is needed for the next step.
${=spec} performs word splitting during the evaluation of spec. This requires spec to be a parameter name or parameter substitution. In this case ${=:-a b c} is split into the array (a b c)
${^spec} allows for expansions like foo${^list}bar ⇒ fooabar foobbar foocbar instead of foo${list}bar ⇒ fooa b cbar. (with list=(a b c))
${(j:,:)array} joins the elements of array with , as separator. This again requires array to be an parameter name or parameter expansion. As the previous expansion is combined with the string 1, it needs to be inserted with a ${:-word} substitution.
As I said initially, this is little more than a complex way to say "1a,1b,1c". In my opinion it would only make sense, if the array is already declared outside of the substitution. In which case you can just replace the ${=:-a b c} part with the name of the array parameter:
list=(a b c)
echo ${(j:,:):-1${^list}}

Resources