Question

Resume (I shrinked down the following long story to the simple problem)

tree = {:properties => [{:a => 'b'}, {:c => 'd'}]}
big_tree = {:properties => [{:a => 'b'}, {:c => 'd'}], :moves => [{:a => 'b'}, {:c => 'd'}]}

trans = Parslet::Transform.new do
    rule(:properties => subtree(:nested)) do
        out = {}
        nested.each {|pair| out = out.merge pair}
        {:properties => out}
    end
end

pp tree
pp trans.apply(tree)
pp big_tree
pp trans.apply(big_tree)

# OUTPUT

{:properties=>[{:a=>"b"}, {:c=>"d"}]}
{:properties=>{:a=>"b", :c=>"d"}} # Worked with small tree
{:properties=>[{:a=>"b"}, {:c=>"d"}], :moves=>[{:a=>"b"}, {:c=>"d"}]}
{:properties=>[{:a=>"b"}, {:c=>"d"}], :moves=>[{:a=>"b"}, {:c=>"d"}]} # Didn't work with bigger tree

=========================FULL STORY (Not so relevant after the header)

I am making an SGF files parser with Parslet.

Now I am at the stage to make a Transformer.

From the Parser I already get the structs like that:

[{:properties=>
   [{:name=>"GM"@2, :values=>[{:value=>"1"@5}]},
    {:name=>"FF"@7, :values=>[{:value=>"4"@10}]},
    {:name=>"SZ"@12, :values=>[{:value=>"19"@15}]},
    {:name=>"AP"@18, :values=>[{:value=>"SmartGo Kifu:2.2"@21}]},
    {:name=>"GN"@40, :values=>[{:value=>"2013-05-11g"@43}]},
    {:name=>"PW"@57, :values=>[{:value=>"Dahan"@60}]},
    {:name=>"PB"@68, :values=>[{:value=>"SmartGo"@71}]},
    {:name=>"DT"@81, :values=>[{:value=>"2013-05-11"@84}]},
    {:name=>"KM"@97, :values=>[{:value=>"6.5"@100}]},
    {:name=>"RE"@106, :values=>[{:value=>"W+R"@109}]},
    {:name=>"RU"@115, :values=>[{:value=>"AGA (Area)"@118}]},
    {:name=>"ID"@129, :values=>[{:value=>"ch0"@132}]}],
  :moves=>
   [{:player=>"B"@137, :place=>"oq"@139},
    {:player=>"W"@143, :place=>"dd"@145},
    {:player=>"B"@149, :place=>"oo"@151},
    ...etc...

The ruleset I am using to Transform:

    # Rewrite player: COLOR, place: X to COLOR: X
    rule( player: simple(:p), place: simple(:pl)) do
        if p == 'W'
            { white: pl }
        elsif p == 'B'
            { black: pl }
        end
    end
    # Un-nest single-value hash
    rule( value: simple(:v)) { v }
    # Rewrite name: KEY, values: SINGLE_VALUE to KEY: SINGLE_VALUE
    rule( name: simple(:n), values: [ simple(:v) ]) { {n.to_sym => v} }
    # A Problem!!!
    rule( properties: subtree(:props) ) do
        out = {}
        props.each {|pair| pair.each {|k, v| out[k] = v}}
        { properties: out }
    end

With such rules I get the following struct:

[{:properties=>
   [{:GM=>"1"@5},
    {:FF=>"4"@10},
    {:SZ=>"19"@15},
    {:AP=>"SmartGo Kifu:2.2"@21},
    {:GN=>"2013-05-11g"@43},
    {:PW=>"Dahan"@60},
    {:PB=>"SmartGo"@71},
    {:DT=>"2013-05-11"@84},
    {:KM=>"6.5"@100},
    {:RE=>"W+R"@109},
    {:RU=>"AGA (Area)"@118},
    {:ID=>"ch0"@132}],
  :moves=>
   [{:black=>"oq"@139},
    {:white=>"dd"@145},
    {:black=>"oo"@151},
    ...etc...

Everything is perfect. The only Problem of mine is that :properties Array of Hashes.

In the end I want to have

[{:properties=>
   {:GM=>"1"@5,
    :FF=>"4"@10,
    :SZ=>"19"@15,
    :AP=>"SmartGo Kifu:2.2"@21,
    :GN=>"2013-05-11g"@43,
    :PW=>"Dahan"@60,
    :PB=>"SmartGo"@71,
    :DT=>"2013-05-11"@84,
    :KM=>"6.5"@100,
    :RE=>"W+R"@109,
    :RU=>"AGA (Area)"@118,
    :ID=>"ch0"@132},
  :moves=>
   [{:black=>"oq"@139},
    {:white=>"dd"@145},
    {:black=>"oo"@151},
    ...etc...

You see? Merge all arrayed hashes inside :properties, because after the previous transformations they now have unique keys. Also flatten the struct a bit.

Hey! I can do it manually. I mean to run a separate method like

merged_stuff = {}
tree.first[:properties].each {|pair| pair.each {|k, v| merged_stuff[k] = v}}
tree.first[:properties] = merged_stuff

But Why I Cannot Do That With The Neat Transform Rules, To Have All Transformation Logic In One Place?

The point is that rule( properties: subtree(:props) ) does not get fired at all. Even if I just return nil from the block, it doesn't change anything. So, seems, that this subtree doesn't catch the things, or I don't.

Was it helpful?

Solution

The problem is that :properties and :moves are keys in the same hash, and subtree apparently doesn't want to match part of a hash. If you remove :moves, the rule will be executed. It is kinda explained in the documentation:

A word on patterns

Given the PORO hash

{ 
  :dog => 'terrier', 
  :cat => 'suit' }

one might assume that the following rule matches :dog and replaces it by 'foo':

rule(:dog => 'terrier') { 'foo' }

This is frankly impossible. How would 'foo' live besides :cat => 'suit' inside the hash? It cannot. This is why hashes are either matched completely, cats n’ all, or not at all.

though I must admit it's not a really clear example.

So the problem rule should look like this:

rule( properties: subtree(:props), moves: subtree(:m) ) do
    out = {}
    props.each {|pair| pair.each {|k, v| out[k] = v}}
    { properties: out , moves: m}
end

OTHER TIPS

Transform rules match a whole node and replace it, so you need to match the whole hash, not just one key.

rule( properties: subtree(:props),  moves: subtree(:moves) ) 

If you converted the {:name=>"GM", :values=>[{:value=>"1"}]} type things into objects (using OpenStruct say) then you don't need to use subtree, you can use sequence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top