Module: RDF::Isomorphic

Included in:
Enumerable, Enumerable::Enumerator
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-isomorphic-6add041cd0be/lib/rdf/isomorphic.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-isomorphic-6add041cd0be/lib/rdf/isomorphic/version.rb

Overview

Isomorphism for rdf.rb Enumerables

RDF::Isomorphic provides the functions isomorphic_with and bijection_to for RDF::Enumerable.

Defined Under Namespace

Modules: VERSION

Instance Method Summary collapse

Instance Method Details

#bijection_to(other, canonicalize: false, **opts) ⇒ Hash?

Returns a hash of RDF:Nodes: RDF::Nodes representing an isomorphic bijection of this RDF::Enumerable's to another RDF::Enumerable's blank nodes, or nil if a bijection cannot be found.

Takes a canonicalize: true argument. If true, RDF::Literals will be canonicalized while producing a bijection. This results in broader matches for isomorphism in the case of equivalent literals with different representations.

Examples:

repository_a.bijection_to repository_b

Parameters:

  • other (RDF::Enumerable)
  • canonicalize (Boolean) (defaults to: false)

    (false) If true, RDF::Literals will be canonicalized while producing a bijection. This results in broader matches for isomorphism in the case of equivalent literals with different representations.

  • opts (Hash<Symbol => Any>)

    other options ignored

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-isomorphic-6add041cd0be/lib/rdf/isomorphic.rb', line 49

def bijection_to(other, canonicalize: false, **opts)

  grounded_stmts_match = (count == other.count)

  grounded_stmts_match &&= each_statement.all? do | stmt |
    stmt.node? || other.has_statement?(stmt)
  end

  if grounded_stmts_match
    # blank_stmts and other_blank_stmts are just a performance
    # consideration--we could just as well pass in self and other.  But we
    # will be iterating over this list quite a bit during the algorithm, so
    # we break it down to the parts we're interested in.
    blank_stmts = find_all { |statement| statement.node? }
    other_blank_stmts = other.find_all { |statement| statement.node? }

    nodes = RDF::Isomorphic.blank_nodes_in(blank_stmts)
    other_nodes = RDF::Isomorphic.blank_nodes_in(other_blank_stmts)
    build_bijection_to blank_stmts, nodes, other_blank_stmts, other_nodes,
      these_grounded_hashes: {},
      other_grounded_hashes: {},
      canonicalize: false
  else
    nil
  end

end

#isomorphic_with?(other, canonicalize: false, **opts) ⇒ Boolean Also known as: isomorphic?

Returns true if this RDF::Enumerable is isomorphic with another.

Examples:

repository_a.isomorphic_with repository_b #=> true

Parameters:

  • canonicalize (Boolean) (defaults to: false)

    (false) If true, RDF::Literals will be canonicalized while producing a bijection. This results in broader matches for isomorphism in the case of equivalent literals with different representations.

  • opts (Hash<Symbol => Any>)

    other options ignored

  • other (RDF::Enumerable)

Returns:

  • (Boolean)


26
27
28
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-isomorphic-6add041cd0be/lib/rdf/isomorphic.rb', line 26

def isomorphic_with?(other, canonicalize: false, **opts)
  !(bijection_to(other, canonicalize: false, **opts).nil?)
end