Class: ShEx::Algebra::ShapeRef

Inherits:
Operator::Unary show all
Includes:
Satisfiable
Defined in:
lib/shex/algebra/shape_ref.rb

Constant Summary collapse

NAME =
:shapeRef

Constants inherited from Operator::Unary

Operator::Unary::ARITY

Constants inherited from Operator

Operator::ARITY

Instance Attribute Summary

Attributes inherited from Operator

#label, #logger, #operands, #options, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Satisfiable

#satisfiable?

Methods inherited from Operator

#base_uri, #closed?, #each_descendant, #eql?, #first_ancestor, #focus, #focus=, #inspect, #iri, iri, #json_type, #matched, #matched=, #message, #message=, #not_matched, #not_satisfied, #operand, #parent, #parent=, #satisfiable?, #satisfied, #satisfied=, #satisfy, #semact?, #semantic_actions, #serialize_value, #status, #structure_error, #to_h, #to_json, #to_sxp, #triple_expression?, #unmatched, #unmatched=, #unsatisfied, #unsatisfied=, value, #value

Constructor Details

#initialize(arg, **options) ⇒ ShapeRef

Returns a new instance of ShapeRef.



7
8
9
10
# File 'lib/shex/algebra/shape_ref.rb', line 7

def initialize(arg, **options)
  structure_error("Shape reference must be an IRI or BNode: #{arg}", exception: ArgumentError) unless arg.is_a?(RDF::Resource)
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ShEx::Algebra::Operator

Class Method Details

.from_shexj(operator, options = {}) ⇒ Operator

Creates an operator instance from a parsed ShExJ representation

Returns:

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/shex/algebra/shape_ref.rb', line 16

def self.from_shexj(operator, options = {})
  raise ArgumentError unless operator.is_a?(Hash) && operator['type'] == "ShapeRef"
  raise ArgumentError, "missing reference in #{operator.inspect}" unless operator.has_key?('reference')
  super
end

Instance Method Details

#referenced_shapeShape

Returns the referenced shape

Returns:



47
48
49
# File 'lib/shex/algebra/shape_ref.rb', line 47

def referenced_shape
  @referenced_shape ||= schema.shapes.detect {|s| s.label == operands.first}
end

#satisfies?(focus, depth: 0) ⇒ Boolean, Operator

Satisfies referenced shape.

Parameters:

  • focus (RDF::Resource)
  • depth (Integer) (defaults to: 0)

    for logging

  • options (Hash{Symbol => Object})

    Other, operand-specific options

Returns:

  • (Boolean)
  • (Operator)

    with ‘matched` and `satisfied` accessors for matched triples and sub-expressions

Raises:

  • (ShEx::NotMatched)

    with ‘expression` accessor to access `matched` and `unmatched` statements along with `satisfied` and `unsatisfied` operations.

See Also:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shex/algebra/shape_ref.rb', line 28

def satisfies?(focus, depth: 0)
  status "ref #{operands.first.to_s}", depth: depth
  schema.enter_shape(operands.first, focus) do |shape|
    if shape
      matched_shape = shape.satisfies?(focus, depth: depth + 1)
      satisfy focus: focus, satisfied: matched_shape, depth: depth
    else
      status "Satisfy as #{operands.first} was re-entered for #{focus}", depth: depth
      satisfy focus: focus, satisfied: referenced_shape, depth: depth
    end
  end
rescue ShEx::NotSatisfied => e
  not_satisfied e.message, focus: focus, unsatisfied: e.expression, depth: depth
end

#to_sxp_binArray

Returns the binary S-Expression (SXP) representation of this operator.

Returns:

  • (Array)

See Also:



67
68
69
# File 'lib/shex/algebra/shape_ref.rb', line 67

def to_sxp_bin
  ([:shapeRef, ([:label, @label] if @label)].compact + operands).to_sxp_bin
end

#validate!Object

A ShapeRef is valid if it’s ancestor schema has any shape with a label the same as it’s reference. A ref cannot reference itself (via whatever path) without going through a TripleConstraint. Even when going through TripleConstraints, there can’t be a negative reference.



56
57
58
59
60
# File 'lib/shex/algebra/shape_ref.rb', line 56

def validate!
  structure_error("Missing referenced shape: #{operands.first}") if referenced_shape.nil?
  raise ShEx::StructureError, "Self referencing shape: #{operands.first}" if referenced_shape == first_ancestor(Satisfiable)
  super
end