Class: RDF::SAK::Transform::Harness

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

Overview

This class is the main harness for holding all the transforms and operating over them. This is the primary interface through which we manipulate transforms.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, root) ⇒ Harness

Create a new harness instance.

Parameters:

  • repo (RDF::Repository)

    the repository to find RDF data

  • root (String, Pathname)

    the root directory for implementations

Raises:

  • (ArgumentError)


510
511
512
513
514
515
516
517
518
519
520
# File 'lib/rdf/sak/transform.rb', line 510

def initialize repo, root
  raise ArgumentError,
    "repo is #{repo.class}, not an RDF::Repository" unless
    repo.is_a? RDF::Repository
  @repo = repo
  @root = Pathname(root).expand_path
  raise ArgumentError, "Root #{@root} does not exist" unless
    @root.directory? and @root.readable?
  @cache    = {}
  @partials = RDF::SAK::Transform::PartialCache.new self
end

Instance Attribute Details

#partialsObject (readonly)

Returns the value of attribute partials.



503
504
505
# File 'lib/rdf/sak/transform.rb', line 503

def partials
  @partials
end

#repoObject (readonly)

Returns the value of attribute repo.



503
504
505
# File 'lib/rdf/sak/transform.rb', line 503

def repo
  @repo
end

#rootObject (readonly)

Returns the value of attribute root.



503
504
505
# File 'lib/rdf/sak/transform.rb', line 503

def root
  @root
end

Class Method Details

.load(repo, root) ⇒ RDF::SAK::Transform::Harness

Bootstrap all the transforms.

Parameters:

  • repo (RDF::Repository)

    the repository to find RDF data

  • root (String, Pathname)

    the root directory for implementations

Returns:



527
528
529
# File 'lib/rdf/sak/transform.rb', line 527

def self.load repo, root
  self.new(repo, root).load
end

Instance Method Details

#application_matches?(subject, transform: nil, params: {}, partial: nil) ⇒ true, false

Returns true if the Application with the given subject URI matches either the transform-params pair, or a partial.

Parameters:

Returns:

  • (true, false)

    whether or not the application matches



602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/rdf/sak/transform.rb', line 602

def application_matches? subject, transform: nil, params: {}, partial: nil

  # unbundle the params; partial overrides transform+params
  if partial
    partial   = resolve_partial partial unless
      partial.is_a? RDF::SAK::Transform::Partial
    transform = partial.transform
    params    = partial.params
  else
    transform = resolve transform unless
      transform.is_a? RDF::SAK::Transform
    params = transform.validate params
  end

  if subject.is_a? RDF::SAK::Transform::Application
    return true if partial and subject.completes? partial
    return true if
      subject.transform == transform and subject.matches? params
  else
    # this should say, try matching the partial if there is one
    # to match, otherwise attempt to directly match the transform
    return true if partial and repo.has_statement?(
      RDF::Statement(subject, RDF::SAK::TFO.completes, partial.subject))

    if repo.has_statement?(
      RDF::Statement(subject, RDF::SAK::TFO.transform, transform.subject))
      testp = transform.keys.map do |p|
        o = repo.query([subject, p, nil]).objects.uniq.sort
        o.empty? ? nil : [p, o]
      end.compact.to_h

      # this will clear any explicit declarations of defaults
      testp = transform.validate testp, defaults: false, silent: true
      # true means it matches
      return testp == params
    end
  end

  false
end

#loadArray

Load transforms into an existing instance

Returns:

  • (Array)

    the transforms



533
534
535
536
537
538
539
540
541
# File 'lib/rdf/sak/transform.rb', line 533

def load
  RDF::SAK::Util.subjects_for(@repo, RDF.type,
    RDF::SAK::TFO.Transform, only: :resource).each do |subject|
    resolve subject
  end

  # return self so we can daisy-chain
  self
end

#resolve(subject) ⇒ RDF::SAK::Transform

Resolve a Transform based on its URI.

Parameters:

  • subject (RDF::Resource)

    the identifier for the transform.

Returns:



556
557
558
559
560
561
562
# File 'lib/rdf/sak/transform.rb', line 556

def resolve subject
  return @cache[subject] if @cache[subject]
  # XXX raise???
  transform =
    RDF::SAK::Transform.resolve(self, subject) or return
  @cache[subject] = transform
end

#resolve_application(subject: nil, transform: nil, params: {}, partial: nil, input: nil, output: nil) ⇒ RDF::SAK::Transform::Application

Resolve a total function application record based on either its subject URI, a transform-params pair, or a Partial.

Parameters:

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

    the Application’s subject

  • transform (RDF::Resource, RDF::SAK::Transform) (defaults to: nil)

    the Transform

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

    an instance of parameters

  • partial (RDF::Resource, RDF::SAK::Transform::Partial) (defaults to: nil)

    a Partial

Returns:



585
586
587
588
589
590
# File 'lib/rdf/sak/transform.rb', line 585

def resolve_application subject: nil, transform: nil, params: {},
    partial: nil, input: nil, output: nil
  RDF::SAK::Transform::Application.resolve self, subject: subject,
    transform: transform, params: params, partial: partial,
    input: input, output: output
end

#resolve_partial(subject: nil, transform: nil, params: nil) ⇒ RDF::SAK::Transform::Partial

Resolve a Partial based on either its subject URI or the transform-params pair.

Parameters:

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

    the Partial’s subject

  • transform (RDF::Resource, RDF::SAK::Transform) (defaults to: nil)

    the transform

  • params (Hash) (defaults to: nil)

    an instance of parameters

Returns:



572
573
574
# File 'lib/rdf/sak/transform.rb', line 572

def resolve_partial subject: nil, transform: nil, params: nil
  partials.resolve subject: subject, transform: transform, params: params
end

#transformsArray

Return all cached Transform identities.

Returns:

  • (Array)

    the URIs of known Transforms



547
548
549
# File 'lib/rdf/sak/transform.rb', line 547

def transforms
  @cache.keys.sort
end