Class: ShEx::Algebra::Operator Abstract

Inherits:
Object
  • Object
show all
Extended by:
SPARQL::Algebra::Expression
Includes:
RDF::Util::Logger
Defined in:
vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb

Overview

This class is abstract.

The ShEx operator.

Defined Under Namespace

Classes: Binary, Unary

Constant Summary collapse

ARITY =

variable arity

-1 # variable arity

Constants included from SPARQL::Algebra::Expression

SPARQL::Algebra::Expression::PATTERN_PARENTS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SPARQL::Algebra::Expression

cast, constant?, evaluate, extension, extension?, extensions, for, invalid?, new, node?, open, optimize, optimize!, parse, register_extension, valid?, variable?

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Constructor Details

#initialize(*operands) ⇒ Operator #initialize(*operands, **options) ⇒ Operator

Initializes a new operator instance.

Overloads:

  • #initialize(*operands) ⇒ Operator

    Parameters:

  • #initialize(*operands, **options) ⇒ Operator

    Parameters:

    Options Hash (**options):

    • :memoize (Boolean) — default: false

      whether to memoize results for particular operands

    • :id (RDF::Resource)

      Identifier of the operator

Raises:

  • (TypeError)

    if any operand is invalid



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 38

def initialize(*operands, **options)
  @options  = options.dup
  @operands = operands.map! do |operand|
    case operand
      when Array
        operand.each do |op|
          op.parent = self if op.respond_to?(:parent=)
        end
        operand
      when Operator, RDF::Term, RDF::Query, RDF::Query::Pattern, Array, Symbol
        operand.parent = self if operand.respond_to?(:parent=)
        operand
      when TrueClass, FalseClass, Numeric, String, DateTime, Date, Time
        RDF::Literal(operand)
      when NilClass
        raise ArgumentError, "Found nil operand for #{self.class.name}"
      else raise TypeError, "invalid ShEx::Algebra::Operator operand: #{operand.inspect}"
    end
  end

  @id = options[:id]
end

Instance Attribute Details

#idRDF::Resource

The id (or subject) of this operand

Returns:



198
199
200
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 198

def id
  @id
end

#logger=(value) ⇒ Logger (writeonly)

Logging support (reader is in RDF::Util::Logger)

Returns:

  • (Logger)


203
204
205
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 203

def logger=(value)
  @logger = value
end

#operandsArray (readonly)

The operands to this operator.

Returns:



193
194
195
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 193

def operands
  @operands
end

#optionsObject

Initialization options



19
20
21
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 19

def options
  @options
end

#schemaObject

Location of schema including this operator



16
17
18
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 16

def schema
  @schema
end

Class Method Details

.from_shexj(operator, **options) ⇒ Operator

Creates an operator instance from a parsed ShExJ representation

Parameters:

  • operator (Hash)
  • options (Hash)

    ({})

Options Hash (**options):

Returns:



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 265

def self.from_shexj(operator, **options)
  options[:context] ||= JSON::LD::Context.parse(ShEx::CONTEXT)
  operands = []
  id = nil

  operator.each do |k, v|
    case k
    when /length|clusive|digits/           then operands << [k.to_sym, RDF::Literal(v)]
    when 'id'                              then id = iri(v, **options)
    when 'flags'                           then ; # consumed in pattern below
    when 'min', 'max'                      then operands << [k.to_sym, (v == -1 ? '*' : v)]
    when 'inverse', 'closed'               then operands << k.to_sym
    when 'nodeKind'                        then operands << v.to_sym
    when 'object'                          then operands << value(v, **options)
    when 'languageTag'                     then operands << v
    when 'pattern'
      # Include flags as well
      operands << [:pattern, RDF::Literal(v), operator['flags']].compact
    when 'start'
      if v.is_a?(String)
        operands << Start.new(iri(v, **options))
      else
        operands << Start.new(ShEx::Algebra.from_shexj(v, **options))
      end
    when '@context'                        then
      options[:context] = JSON::LD::Context.parse(v)
      options[:base_uri] ||= options[:context].base
    when 'shapes'
      operands << case v
      when Array
        [:shapes] + v.map {|vv| ShEx::Algebra.from_shexj(vv, **options)}
      else
        raise "Expected value of shapes #{v.inspect}"
      end
    when 'stem', 'name'
      # Value may be :wildcard for stem
      if [IriStem, IriStemRange, SemAct].include?(self)
        operands << (v.is_a?(Symbol) ? v : value(v, **options))
      else
        operands << v
      end
    when 'predicate' then operands << [:predicate, iri(v, **options)]
    when 'extra', 'datatype'
      v = [v] unless v.is_a?(Array)
      operands << (v.map {|op| iri(op, **options)}).unshift(k.to_sym)
    when 'exclusions'
      v = [v] unless v.is_a?(Array)
      operands << v.map do |op|
        if op.is_a?(Hash) && op.has_key?('type')
          ShEx::Algebra.from_shexj(op, **options)
        elsif [IriStem, IriStemRange].include?(self)
          value(op, **options)
        else
          RDF::Literal(op)
        end
      end.unshift(:exclusions)
    when 'semActs', 'startActs', 'annotations'
      v = [v] unless v.is_a?(Array)
      operands += v.map {|op| ShEx::Algebra.from_shexj(op, **options)}
    when 'expression', 'expressions', 'shapeExpr', 'shapeExprs', 'valueExpr'
      v = [v] unless v.is_a?(Array)
      operands += v.map do |op|
        # It's a URI reference to a Shape
        op.is_a?(String) ? iri(op, **options) : ShEx::Algebra.from_shexj(op, **options) 
      end
    when 'code'
      operands << v
    when 'values'
      v = [v] unless v.is_a?(Array)
      operands += v.map do |op|
        Value.new(value(op, **options))
      end
    end
  end

  new(*operands, **options.merge(id: id))
end

.iri(value, **options) ⇒ RDF::Value

Create URIs

Parameters:

Options Hash (**options):

Returns:



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 490

def self.iri(value, **options)
  # If we have a base URI, use that when constructing a new URI
  base_uri = options[:base_uri]

  case value
  when Hash
    # A JSON-LD node reference
    v = options[:context].expand_value(value)
    raise "Expected #{value.inspect} to be a JSON-LD Node Reference" unless JSON::LD::Utils.node_reference?(v)
    self.iri(v['@id'], **options)
  when RDF::URI
    if base_uri && value.relative?
      base_uri.join(value)
    else
      value
    end
  when RDF::Value then value
  when /^_:/ then
    id = value[2..-1].to_s
    RDF::Node.intern(id)
  when /^(\w+):(\S+)$/
    prefixes = options.fetch(:prefixes, {})
    if prefixes.has_key?($1)
      prefixes[$1].join($2)
    elsif RDF.type == value
      a = RDF.type.dup; a.lexical = 'a'; a
    elsif options[:context]
      options[:context].expand_iri(value, vocab: true)
    else
      RDF::URI(value)
    end
  else
    if base_uri
      base_uri.join(value)
    else
      RDF::URI(value)
    end
  end
end

.value(value, **options) ⇒ RDF::Value

Create Values, with "clever" matching to see if it might be a value, IRI or BNode.

Parameters:

Options Hash (**options):

Returns:



544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 544

def self.value(value, **options)
  # If we have a base URI, use that when constructing a new URI
  case value
  when Hash
    # Either a value object or a node reference
    if value['uri'] || value['@id']
      iri(value['uri'] || value['@id'], **options)
    elsif value['value'] || value['@value']
      RDF::Literal(value['value'] || value['@value'], datatype: value['type'] || value['@type'], language: value['language'] || value['@language'])
    else
      ShEx::Algebra.from_shexj(value, **options)
    end
  else iri(value, **options)
  end
end

Instance Method Details

#base_uriHRDF::URI

Returns the Base URI defined for the parser, as specified or when parsing a BASE prologue element.

Examples:

base  #=> RDF::URI('http://example.com/')

Returns:

  • (HRDF::URI)


471
472
473
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 471

def base_uri
  @options[:base_uri]
end

#closed?Boolean

Is this shape closed?

Returns:

  • (Boolean)


64
65
66
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 64

def closed?
  operands.include?(:closed)
end

#dupObject



676
677
678
679
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 676

def dup
  operands = @operands.map {|o| o.dup rescue o}
  self.class.new(*operands, id: @id)
end

#each_descendant(include_self = false) {|operator| ... } ⇒ Enumerator Also known as: descendants, each

Enumerate via depth-first recursive descent over operands, yielding each operator

Parameters:

  • include_self (Boolean) (defaults to: false)

Yields:

  • operator

Yield Parameters:

Returns:

  • (Enumerator)


604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 604

def each_descendant(include_self = false, &block)
  if block_given?

    block.call(self) if include_self

    operands.each do |operand|
      case operand
      when Array
        operand.each do |op|
          op.each_descendant(true, &block) if op.respond_to?(:each_descendant)
        end
      else
        operand.each_descendant(true, &block) if operand.respond_to?(:each_descendant)
      end
    end
  end
  enum_for(:each_descendant)
end

#eql?(other) ⇒ Boolean Also known as: ==

Comparison does not consider operand order

Parameters:

  • other (Statement)

Returns:

  • (Boolean)


591
592
593
594
595
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 591

def eql?(other)
  other.class == self.class &&
    other.id == self.id &&
    other.operands.sort_by(&:to_s) == self.operands.sort_by(&:to_s)
end

#expressionRDF::Resource, Operand

The first expression from #expressions.

Returns:



226
227
228
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 226

def expression
  expressions.first
end

#expressionsRDF::Resource, Operand

Expressions are all operands which are Operators or RDF::Resource

Returns:



218
219
220
221
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 218

def expressions
  @expressions = operands.
    select {|op| op.is_a?(RDF::Resource) || op.is_a?(ShapeExpression) || op.is_a?(TripleExpression)}
end

#find(id) ⇒ TripleExpression, ShapeExpression

Find a ShapeExpression or TripleExpression by identifier

Parameters:

  • id (#to_s)

Returns:



643
644
645
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 643

def find(id)
  each_descendant(false).detect {|op| op.id == id}
end

#focusObject

On a result instance, the focus of the expression



83
84
85
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 83

def focus
  Array(operands.detect {|op| op.is_a?(Array) && op[0] == :focus} || [:focus])[1]
end

#focus=(node) ⇒ Object



86
87
88
89
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 86

def focus=(node)
  operands.delete_if {|op| op.is_a?(Array) && op[0] == :focus}
  operands << [:focus, node]
end

#inspectString

Returns a developer-friendly representation of this operator.

Returns:



583
584
585
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 583

def inspect
  sprintf("#<%s:%#0x(%s%s)>", self.class.name, __id__, ("id: #{id} " if id), operands.inspect)
end

#iri(value, options = @options) ⇒ RDF::Value

Create URIs

Parameters:

Options Hash (options):

Returns:



482
483
484
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 482

def iri(value, options = @options)
  self.class.iri(value, **options)
end

#json_typeObject



343
344
345
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 343

def json_type
  self.class.name.split('::').last
end

#matchedArray<Statement>

On a result instance, the statements that matched this expression.

Returns:



94
95
96
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 94

def matched
  Array((operands.detect {|op| op.is_a?(Array) && op[0] == :matched} || [:matched])[1..-1])
end

#matched=(statements) ⇒ Object



97
98
99
100
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 97

def matched=(statements)
  operands.delete_if {|op| op.is_a?(Array) && op[0] == :matched}
  operands << statements.unshift(:matched) unless (statements || []).empty?
end

#messageString

On a result instance, the failure message. (failure only).

Returns:



138
139
140
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 138

def message
  (operands.detect {|op| op.is_a?(Array) && op[0] == :message} || [:message])[1..-1]
end

#message=(str) ⇒ Object



141
142
143
144
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 141

def message=(str)
  operands.delete_if {|op| op.is_a?(Array) && op[0] == :message}
  operands << [:message, str]
end

#not_matched(message, **opts, &block) ⇒ Object

Exception handling

Raises:

  • (exception)


164
165
166
167
168
169
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 164

def not_matched(message, **opts, &block)
  expression = opts.fetch(:expression, self).satisfy(message: message, **opts)
  exception = opts.fetch(:exception, ShEx::NotMatched)
  status(message, **opts) {(block_given? ? block.call : "") + "expression: #{expression.to_sxp}"}
  raise exception.new(message, expression: expression)
end

#not_satisfied(message, **opts) ⇒ Object

Raises:

  • (exception)


171
172
173
174
175
176
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 171

def not_satisfied(message, **opts)
  expression = opts.fetch(:expression, self).satisfy(message: message, **opts)
  exception = opts.fetch(:exception, ShEx::NotSatisfied)
  status(message, **opts) {(block_given? ? block.call : "") + "expression: #{expression.to_sxp}"}
  raise exception.new(message, expression: expression)
end

#operand(index = 0) ⇒ RDF::Term

Returns the operand at the given index.

Parameters:

  • index (Integer) (defaults to: 0)

    an operand index in the range (0...(operands.count))

Returns:



211
212
213
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 211

def operand(index = 0)
  operands[index]
end

#parentOperator

Parent expression, if any

Returns:



629
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 629

def parent; @options[:parent]; end

#parent=(operator) ⇒ Operator

Parent operator, if any

Returns:



635
636
637
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 635

def parent=(operator)
  @options[:parent]= operator
end

#referencesRDF::Resource, Operand

References are all operands which are RDF::Resource

Returns:



233
234
235
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 233

def references
  @references = operands.select {|op| op.is_a?(RDF::Resource)}
end

#satisfiedArray<Operator>

On a result instance, the sub-expressions which were matched.

Returns:



116
117
118
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 116

def satisfied
  Array((operands.detect {|op| op.is_a?(Array) && op[0] == :satisfied} || [:satisfied])[1..-1])
end

#satisfied=(ops) ⇒ Object



119
120
121
122
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 119

def satisfied=(ops)
  operands.delete_if {|op| op.is_a?(Array) && op[0] == :satisfied}
  operands << ops.unshift(:satisfied) unless (ops || []).empty?
end

#satisfy(focus: nil, matched: nil, unmatched: nil, satisfied: nil, unsatisfied: nil, message: nil, **opts) ⇒ Operand

Duplication this operand, and add matched, unmatched, satisfied, and unsatisfied operands for accessing downstream.

Returns:

  • (Operand)


150
151
152
153
154
155
156
157
158
159
160
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 150

def satisfy(focus: nil, matched: nil, unmatched: nil, satisfied: nil, unsatisfied: nil, message: nil, **opts)
  log_debug(self.class.const_get(:NAME), "satisfied", **opts) unless message
  expression = self.dup
  expression.message = message if message
  expression.focus = focus if focus
  expression.matched = Array(matched) if matched
  expression.unmatched = Array(unmatched) if unmatched
  expression.satisfied = Array(satisfied) if satisfied
  expression.unsatisfied = Array(unsatisfied) if unsatisfied
  expression
end

#semact?Boolean

Does this operator a SemAct?

Returns:

  • (Boolean)


79
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 79

def semact?; false; end

#semantic_actionsArray<SemAct>

Semantic Actions

Returns:



71
72
73
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 71

def semantic_actions
  operands.select {|o| o.is_a?(SemAct)}
end

#serialize_value(value) ⇒ String

Serialize a value, either as JSON, or as modififed N-Triples

Parameters:

Returns:



565
566
567
568
569
570
571
572
573
574
575
576
577
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 565

def serialize_value(value)
  case value
    when RDF::Literal
      {'value' => value.to_s}.
      merge(value.has_datatype? ? {'type' => value.datatype.to_s} : {}).
      merge(value.has_language? ? {'language' => value.language.to_s} : {})
    when RDF::Resource
      value.to_s
    when String
      {'value' => value}
    else value.to_h
  end
end

#status(message, **opts, &block) ⇒ Object



184
185
186
187
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 184

def status(message, **opts, &block)
  log_debug(self.class.const_get(:NAME).to_s + (@id ? "(#{@id})" : ""), message, **opts, &block)
  true
end

#structure_error(message, **opts) ⇒ Object



178
179
180
181
182
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 178

def structure_error(message, **opts)
  expression = opts.fetch(:expression, self)
  exception = opts.fetch(:exception, ShEx::StructureError)
  log_error(message, depth: options.fetch(:depth, 0), exception: exception) {"expression: #{expression.to_sxp}"}
end

#to_hHash

Create a hash version of the operator, suitable for turning into JSON.

Returns:



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 354

def to_h
  obj = json_type == 'Schema' ? {'@context' => ShEx::CONTEXT} :  {}
  obj['id'] = id.to_s if id
  obj['type'] = json_type
  operands.each do |op|
    case op
    when Array
      # First element should be a symbol
      case sym = op.first
      when :datatype        then obj['datatype'] = op.last.to_s
      when :exclusions
        obj['exclusions'] = Array(op[1..-1]).map do |v|
          case v
          when Operator then v.to_h
          else v.to_s
          end
        end
      when :extra           then (obj['extra'] ||= []).concat Array(op[1..-1]).map(&:to_s)
      when :pattern
        obj['pattern'] = op[1]
        obj['flags'] = op[2] if op[2]
      when :shapes          then obj['shapes'] = Array(op[1..-1]).map {|v| v.to_h}
      when :minlength,
           :maxlength,
           :length,
           :mininclusive,
           :maxinclusive,
           :minexclusive,
           :maxexclusive,
           :totaldigits,
           :fractiondigits  then obj[op.first.to_s] = op.last.object
      when :min, :max       then obj[op.first.to_s] = op.last == '*' ? -1 : op.last
      when :predicate       then obj[op.first.to_s] = op.last.to_s
      when :base, :prefix
        # Ignore base and prefix
      when Symbol           then obj[sym.to_s] = Array(op[1..-1]).map(&:to_h)
      else
        raise "Expected array to start with a symbol for #{self}"
      end
    when :wildcard  then obj['stem'] = {'type' => 'Wildcard'}
    when Annotation then (obj['annotations'] ||= []) << op.to_h
    when SemAct     then (obj[is_a?(Schema) ? 'startActs' : 'semActs'] ||= []) << op.to_h
    when Start
     obj['start'] =  case op.operands.first
      when RDF::Resource then op.operands.first.to_s
      else                    op.operands.first.to_h
      end
    when RDF::Value
      case self
      when Stem, StemRange
        obj['stem'] =  case op
        when Operator then op.to_h
        else op.to_s
        end
      when SemAct           then obj[op.is_a?(RDF::URI) ? 'name' : 'code'] = op.to_s
      when TripleConstraint then obj['valueExpr'] = op.to_s
      when Shape            then obj['expression'] = op.to_s
      when EachOf, OneOf    then (obj['expressions'] ||= []) << op.to_s
      when And, Or          then (obj['shapeExprs'] ||= []) << op.to_s
      when Not              then obj['shapeExpr'] = op.to_s
      when Language         then obj['languageTag'] = op.to_s
      else
        raise "How to serialize Value #{op.inspect} to json for #{self}"
      end
    when Symbol
      case self
      when NodeConstraint   then obj['nodeKind'] = op.to_s
      when Shape            then obj['closed'] = true
      when TripleConstraint then obj['inverse'] = true
      else
        raise "How to serialize Symbol #{op.inspect} to json for #{self}"
      end
    when TripleConstraint, EachOf, OneOf
      case self
      when EachOf, OneOf
        (obj['expressions'] ||= []) << op.to_h
      else
        obj['expression'] = op.to_h
      end
    when NodeConstraint
      case self
      when And, Or
        (obj['shapeExprs'] ||= []) << op.to_h
      when Not
        obj['shapeExpr'] = op.to_h
      else
        obj['valueExpr'] = op.to_h
      end
    when And, Or, Shape, Not
      case self
      when And, Or
        (obj['shapeExprs'] ||= []) << op.to_h
      when TripleConstraint
        obj['valueExpr'] = op.to_h
      else
        obj['shapeExpr'] = op.to_h
      end
    when Value
      obj['values'] ||= []
      Array(op).map {|o| o.operands}.flatten.each do |oo|
        obj['values'] << serialize_value(oo)
      end
    else
      raise "How to serialize #{op.inspect} to json for #{self}"
    end
  end
  obj
end

#to_json(options = nil) ⇒ Object



347
348
349
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 347

def to_json(options = nil)
  self.to_h.to_json(options)
end

#to_sxp(**options) ⇒ String

Returns an S-Expression (SXP) representation of this operator

Returns:



252
253
254
255
256
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 252

def to_sxp(**options)
  require 'sparql/algebra/sxp_extensions'

  to_sxp_bin.to_sxp(**options)
end

#to_sxp_binArray

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



242
243
244
245
246
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 242

def to_sxp_bin
  [self.class.const_get(:NAME)] +
  (id ? [[:id, id]] : []) +
  (operands || []).map(&:to_sxp_bin)
end

#triple_expression?Boolean

Does this operator include TripleExpression?

Returns:

  • (Boolean)


76
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 76

def triple_expression?; false; end

#unmatchedArray<Statement>

On a result instance, the statements that did not match this expression (failure only).

Returns:



105
106
107
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 105

def unmatched
  Array((operands.detect {|op| op.is_a?(Array) && op[0] == :unmatched} || [:unmatched])[1..-1])
end

#unmatched=(statements) ⇒ Object



108
109
110
111
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 108

def unmatched=(statements)
  operands.delete_if {|op| op.is_a?(Array) && op[0] == :unmatched}
  operands << statements.unshift(:unmatched) unless (statements || []).empty?
end

#unsatisfiedArray<Operator>

On a result instance, the sub-satisfieables which were not satisfied. (failure only).

Returns:



127
128
129
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 127

def unsatisfied
  Array((operands.detect {|op| op.is_a?(Array) && op[0] == :unsatisfied} || [:unsatisfied])[1..-1])
end

#unsatisfied=(ops) ⇒ Object



130
131
132
133
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 130

def unsatisfied=(ops)
  operands.delete_if {|op| op.is_a?(Array) && op[0] == :unsatisfied}
  operands << ops.unshift(:unsatisfied) unless (ops || []).empty?
end

#validate!SPARQL::Algebra::Expression

Validate all operands, operator specific classes should override for operator-specific validation.

A schema must not contain any shape expression S with negated references, either directly or transitively, to S.

Returns:

Raises:



661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 661

def validate!
  operands.each do |op|
    op.validate! if op.respond_to?(:validate!)
    if op.is_a?(RDF::Resource) && (is_a?(ShapeExpression) || is_a?(TripleExpression))
      ref = schema.find(op)
      structure_error("Missing reference: #{op}") if ref.nil?
      #if ancestors.unshift(self).include?(ref)
      #  structure_error("Self-recursive reference to #{op}")
      #end
      structure_error("Self referencing shape: #{operands.first}") if ref == self
    end
  end
  self
end

#value(value, options = @options) ⇒ RDF::Value

Create Values, with "clever" matching to see if it might be a value, IRI or BNode.

Parameters:

Options Hash (options):

Returns:



536
537
538
# File 'vendor/bundler/ruby/3.2.0/bundler/gems/shex-89c901640d70/lib/shex/algebra/operator.rb', line 536

def value(value, options = @options)
  self.class.value(value, **options)
end