Class: ShEx::Algebra::StemRange

Inherits:
Operator::Binary show all
Defined in:
lib/shex/algebra/stem_range.rb

Direct Known Subclasses

IriStemRange, LanguageStemRange, LiteralStemRange

Constant Summary collapse

NAME =
:stemRange

Constants inherited from Operator::Binary

Operator::Binary::ARITY

Constants inherited from Operator

Operator::ARITY

Instance Attribute Summary

Attributes inherited from Operator

#id, #logger, #operands, #options, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Operator::Binary

#initialize

Methods inherited from Operator

#base_uri, #closed?, #dup, #each_descendant, #eql?, #expression, #expressions, #find, #focus, #focus=, #initialize, #inspect, #iri, iri, #json_type, #matched, #matched=, #message, #message=, #not_matched, #not_satisfied, #operand, #parent, #parent=, #references, #satisfied, #satisfied=, #satisfy, #semact?, #semantic_actions, #serialize_value, #status, #structure_error, #to_h, #to_json, #to_sxp, #to_sxp_bin, #triple_expression?, #unmatched, #unmatched=, #unsatisfied, #unsatisfied=, #validate!, #value, value

Constructor Details

This class inherits a constructor from ShEx::Algebra::Operator::Binary

Class Method Details

.from_shexj(operator, **options) ⇒ Operator

Creates an operator instance from a parsed ShExJ representation

Returns:

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shex/algebra/stem_range.rb', line 10

def self.from_shexj(operator, **options)
  raise ArgumentError unless operator.is_a?(Hash) && %w(IriStemRange LiteralStemRange LanguageStemRange).include?(operator['type'])
  raise ArgumentError, "missing stem in #{operator.inspect}" unless operator.has_key?('stem')

  # Normalize wildcard representation
  operator['stem'] = :wildcard if operator['stem'] =={'type' => 'Wildcard'}

  # Note that the type may be StemRange, but if there's no exclusions, it's really just a Stem
  if operator.has_key?('exclusions')
    super
  else
    # Remove "Range" from type
    case operator['type']
    when 'IriStemRange'
      IriStem.from_shexj(operator.merge('type' => 'IriStem'), **options)
    when 'LiteralStemRange'
      LiteralStem.from_shexj(operator.merge('type' => 'LiteralStem'), **options)
    when 'LanguageStemRange'
      LanguageStem.from_shexj(operator.merge('type' => 'LanguageStem'), **options)
    end
  end
end

Instance Method Details

#exclusionsObject



65
66
67
# File 'lib/shex/algebra/stem_range.rb', line 65

def exclusions
  (operands.last.is_a?(Array) && operands.last.first == :exclusions) ? operands.last[1..-1] : []
end

#match?(value, depth: 0) ⇒ Boolean

For a node n and constraint value v, nodeSatisfies(n, v) if n matches some valueSetValue vsv in v. A term matches a valueSetValue if:

  • vsv is a StemRange with stem st and exclusions excls and nodeIn(n, st) and there is no x in excls such that nodeIn(n, excl).

  • vsv is a Wildcard with exclusions excls and there is no x in excls such that nodeIn(n, excl).

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shex/algebra/stem_range.rb', line 38

def match?(value, depth: 0)
  initial_match = case operands.first
  when :wildcard then true
  when RDF::Value then value.start_with?(operands.first)
  else false
  end

  unless initial_match
    status "#{value} does not match #{operands.first}", depth: depth
    return false
  end

  if exclusions.any? do |exclusion|
      case exclusion
      when RDF::Value then value == exclusion
      when Stem       then exclusion.match?(value, depth: depth + 1)
      else                 false
      end
    end
    status "#{value} excluded", depth: depth
    return false
  end

  status "matched #{value}", depth: depth
  true
end