Class: ShEx::Algebra::ShapeRef
- Inherits:
-
Operator::Unary
- Object
- Operator
- Operator::Unary
- ShEx::Algebra::ShapeRef
- Includes:
- Satisfiable
- Defined in:
- lib/shex/algebra/shape_ref.rb
Constant Summary collapse
- NAME =
:shapeRef
Constants inherited from Operator::Unary
Constants inherited from Operator
Instance Attribute Summary
Attributes inherited from Operator
#label, #logger, #operands, #options, #schema
Class Method Summary collapse
-
.from_shexj(operator, options = {}) ⇒ Operator
Creates an operator instance from a parsed ShExJ representation.
Instance Method Summary collapse
-
#initialize(arg, **options) ⇒ ShapeRef
constructor
A new instance of ShapeRef.
-
#referenced_shape ⇒ Shape
Returns the referenced shape.
-
#satisfies?(focus, depth: 0) ⇒ Boolean, Operator
Satisfies referenced shape.
-
#to_sxp_bin ⇒ Array
Returns the binary S-Expression (SXP) representation of this operator.
-
#validate! ⇒ Object
A ShapeRef is valid if it’s ancestor schema has any shape with a label the same as it’s reference.
Methods included from 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, **) 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
16 17 18 19 20 |
# File 'lib/shex/algebra/shape_ref.rb', line 16 def self.from_shexj(operator, = {}) 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_shape ⇒ Shape
Returns the referenced shape
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.
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., focus: focus, unsatisfied: e.expression, depth: depth end |
#to_sxp_bin ⇒ Array
Returns the binary S-Expression (SXP) representation of this operator.
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 |