Class: RDF::SAK::Transform::Partial

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/sak/transform.rb

Direct Known Subclasses

Application

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject, transform, params = {}) ⇒ Partial

Returns a new instance of Partial.

Raises:

  • (ArgumentError)


715
716
717
718
719
720
721
722
# File 'lib/rdf/sak/transform.rb', line 715

def initialize subject, transform, params = {}
  raise ArgumentError, 'transform must be a Transform' unless
    transform.is_a? RDF::SAK::Transform
  @subject   = subject
  @transform = transform
  @params    = transform.validate params unless
    params.is_a? RDF::SAK::Transform::Partial
end

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



713
714
715
# File 'lib/rdf/sak/transform.rb', line 713

def subject
  @subject
end

#transformObject (readonly)

Returns the value of attribute transform.



713
714
715
# File 'lib/rdf/sak/transform.rb', line 713

def transform
  @transform
end

Class Method Details

.resolve(harness, subject: nil, transform: nil, params: {}) ⇒ Object

Resolve a partial function application with the given parameters.

Parameters:

  • harness (RDF::SAK::Transform::Harness)

    the harness

  • subject (RDF::Resource) (defaults to: nil)

    the identity of the partial

  • transform (RDF::Resource) (defaults to: nil)

    the identity of the transform

  • params (Hash) (defaults to: {})

    key-value pairs

Raises:

  • (ArgumentError)


651
652
653
654
655
656
657
658
659
660
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
# File 'lib/rdf/sak/transform.rb', line 651

def self.resolve harness, subject: nil, transform: nil, params: {}
  raise ArgumentError, 'Must supply either a subject or a transform' unless
    subject or transform

  repo = harness.repo

  # coerce the transform to a Transform object if it isn't already
  if transform
    transform = harness.resolve(transform) or
      return unless transform.is_a?(RDF::SAK::Transform)
  elsif subject.is_a? RDF::URI
    # locate the transform if given the subject
    transform = RDF::SAK::Util.objects_for(repo, subject,
      RDF::SAK::TFO.transform, only: :resource).first or return
    transform = harness.resolve(transform) or return
    warn transform
  end

  # obtain the subject for the given parameters
  if subject
    params = {}
    transform.keys.each do |p|
      o = repo.query([subject, p, nil]).objects.uniq.sort
      params[p] = o unless o.empty?
    end
  else
    params = transform.validate params, symbols: false, defaults: false

    candidates = RDF::Query.new do
      # XXX we should sort parameters by longest value since
      # longer values will probably be less common; anyway this is
      # gonna all need to be rethought
      params.each { |p, objs| objs.each { |o| pattern [:s, p, o] } }
      pattern [:s, RDF.type, RDF::SAK::TFO.Partial]
      pattern [:s, RDF::SAK::TFO.transform, transform.subject]

      # add any remaining parameters
      # XXX this actually messes up; we don't want this
      # (transform.keys - params.keys.sort).each { |r| pattern [:s, r, nil] }
    end.execute(repo).map { |sol| [sol[:s], {}] }.to_h

    # warn "yo #{transform.subject} #{params} #{candidates}"

    # this is ruby being cheeky
    candidates.select! do |s, ps|
      transform.keys.each do |p|
        o = repo.query([s, p, nil]).objects.uniq.sort
        ps[p] = o unless o.empty?
      end
      ps == params
    end

    return if candidates.empty?

    # sort it so we always get the same thing
    subject = candidates.keys.sort.first
    params  = candidates[subject]
  end

  self.new subject, transform, params
end

Instance Method Details

#==(other) ⇒ Object



745
746
747
# File 'lib/rdf/sak/transform.rb', line 745

def ==(other)
  self === other and subject == other.subject
end

#===(other) ⇒ Object



740
741
742
743
# File 'lib/rdf/sak/transform.rb', line 740

def ===(other)
  return false unless other.is_a? RDF::SAK::Transform::Partial
  transform == other.transform and matches? other.params
end

#[](key) ⇒ Object



724
725
726
# File 'lib/rdf/sak/transform.rb', line 724

def [](key)
  @params[key]
end

#keysObject



728
729
730
# File 'lib/rdf/sak/transform.rb', line 728

def keys
  @params.keys
end

#matches?(params) ⇒ Boolean

Returns:

  • (Boolean)


736
737
738
# File 'lib/rdf/sak/transform.rb', line 736

def matches? params
  @params == @transform.validate(params)
end

#paramsObject



732
733
734
# File 'lib/rdf/sak/transform.rb', line 732

def params
  @params.dup
end