Class: GRel::QL::BGP

Inherits:
Object
  • Object
show all
Defined in:
lib/grel/ql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, context, inverse = false) ⇒ BGP

Returns a new instance of BGP.



654
655
656
657
658
659
# File 'lib/grel/ql.rb', line 654

def initialize(data, context, inverse=false)
  @data = data
  @context = context
  @bgp_id = nil
  @inverse = inverse
end

Instance Attribute Details

#bgp_idObject (readonly)

Returns the value of attribute bgp_id.



652
653
654
# File 'lib/grel/ql.rb', line 652

def bgp_id
  @bgp_id
end

#contextObject (readonly)

Returns the value of attribute context.



652
653
654
# File 'lib/grel/ql.rb', line 652

def context
  @context
end

#dataObject (readonly)

Returns the value of attribute data.



652
653
654
# File 'lib/grel/ql.rb', line 652

def data
  @data
end

Instance Method Details

#parse_property(k) ⇒ Object



732
733
734
735
736
737
738
739
# File 'lib/grel/ql.rb', line 732

def parse_property(k)
  if(k.is_a?(String))
    k.gsub(":@type","<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>").
      gsub(":$inv_","^:")
  else
    QL.to_query(k, context)
  end
end

#to_queryObject



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/grel/ql.rb', line 661

def to_query
  acum = []

  node_id = @context.next_node_id
  id = @data[:@id]
  id = "@id(#{id})" if id && id.is_a?(String) && id.index("@id(").nil?        
  id = QL.to_query(id, context) if id
  subject_var_id,pred_var_id, obj_var_id = @context.register_node(id,node_id,@inverse)

  filters = []

  @data.inject([]) do |ac,(k,v)| 
    # $optional can point to an array of conditions that must be handled separetedly
    if(k.to_s == "$optional" && v.is_a?(Array))
      v.each{ |c| ac << [k,c] }
    else
      ac << [k,v]
    end
    ac
  end.each do |(k,v)|
    # process each property in the query hash
    inverse = false
    optional = false
    unless(k == :@id)
      prop = if(k.to_s.index("$inv_"))
               inverse = true
               k.to_s                     
             elsif(k.to_s == "$optional")
               context.optional = true
               optional = true
             else
               parse_property(k)
             end
      value = QL.to_query(v,context,inverse)
      if(value.is_a?(Array))
        context.append(value,false)
        value = value.triples_id
      elsif(value.is_a?(Filter))
        filters << value
        value = value.variable
      elsif(value == context) # It was a nested hash, retrieve the subject as object value
        if(optional)
          subject_to_replace = value.last_registered_subject
          context.node_to_optional(subject_to_replace)#, subject_var_id)
          context.optional_triples = context.optional_triples.map do |(s,p,o)|
            s = (s == subject_to_replace ? subject_var_id : s)
            o = (o == subject_to_replace ? subject_var_id : o)
            [s, p, o]
          end
          context.optional = false
        else
          value = context.last_registered_subject
        end
      end
      acum << [prop,value] unless optional
    end
  end

  bgp_id = subject_var_id
  acum = acum.map do |(p,o)| 
    if(p.index("$inv_"))
      [o,QL.to_query(p.to_sym,@context),subject_var_id]
    else
      [subject_var_id,p,o]
    end
  end
  acum << [subject_var_id, pred_var_id, obj_var_id] if acum.empty?
  acum += filters
  acum
end