Class: RDF::N3::Algebra::Log::Implies

Inherits:
SPARQL::Algebra::Operator::Binary show all
Includes:
Builtin, SPARQL::Algebra::Query, SPARQL::Algebra::Update
Defined in:
vendor/bundler/ruby/2.7.0/bundler/gems/rdf-n3-edf63ce7750f/lib/rdf/n3/algebra/log/implies.rb

Overview

Logical implication.

This is the relation between the antecedent (subject) and conclusion (object) of a rule. The application of a rule to a knowledge-base is as follows. For every substitution which, applied to the antecedent, gives a formula which is a subset of the knowledge-base, then the result of applying that same substitution to the conclusion may be added to the knowledge-base.

related: See log:conclusion.

Constant Summary collapse

NAME =
:logImplies
URI =
RDF::N3::Log.implies

Constants included from Util::Logger

Util::Logger::IOWrapper

Constants inherited from SPARQL::Algebra::Operator::Binary

SPARQL::Algebra::Operator::Binary::ARITY

Constants inherited from SPARQL::Algebra::Operator

SPARQL::Algebra::Operator::ARITY, SPARQL::Algebra::Operator::IsURI

Instance Attribute Summary

Attributes included from Enumerable

#existentials, #universals

Attributes included from SPARQL::Algebra::Query

#solutions

Attributes inherited from SPARQL::Algebra::Operator

#operands

Instance Method Summary collapse

Methods included from Builtin

#evaluate, #hash, #input_operand, #rank, #to_uri

Methods included from Util::Logger

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

Methods included from Enumerable

add_entailment, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #triples, #valid?, #validate!

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Countable

#count, #empty?

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from SPARQL::Algebra::Update

#graph_name=, #unshift, #variables

Methods included from SPARQL::Algebra::Query

#each_solution, #empty?, #failed?, #graph_name=, #matched?, #query_yields_boolean?, #query_yields_solutions?, #query_yields_statements?, #unshift, #variables

Methods inherited from SPARQL::Algebra::Operator::Binary

#initialize

Methods inherited from SPARQL::Algebra::Operator

#aggregate?, arity, base_uri, #base_uri, base_uri=, #bind, #boolean, #constant?, #deep_dup, #each_descendant, #eql?, #evaluatable?, evaluate, #executable?, #first_ancestor, for, #formulae, #initialize, #inspect, #ndvars, #node?, #operand, #optimize, #optimize!, #parent, #parent=, #prefixes, prefixes, prefixes=, #rewrite, #to_binary, #to_sxp, #to_sxp_bin, #validate!, #variable?, #vars

Methods included from SPARQL::Algebra::Expression

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

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Method Details

#each(solutions: RDF::Query::Solutions()) {|statement| ... } ⇒ Object

Yields statements from the object based on solutions determined from the subject. Each solution formed by querying queryable from the subject is used to create a graph, which must be a subgraph of queryable. If so, that solution is used to generate triples from the object formula which are yielded.

Yields:

  • (statement)

    each matching statement

Yield Parameters:

Yield Returns:

  • (void)

    ignored



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-n3-edf63ce7750f/lib/rdf/n3/algebra/log/implies.rb', line 67

def each(solutions: RDF::Query::Solutions(), &block)
  # Merge solutions in with those for the evaluation of this implication
  solutions = Array(@solutions)
  log_depth do
    super

    solutions.each do |solution|
      log_debug("(logImplies each) solution") {SXP::Generator.string solution.to_sxp_bin}
      object = operand(1).evaluate(solution.bindings, formulae: formulae)
      log_info(("(logImplies each) object")) {SXP::Generator.string object.to_sxp_bin}

      # Yield inferred statements
      #require 'byebug'; byebug if solution[:y]
      object.each(solutions: RDF::Query::Solutions(solution)) do |statement|
        log_debug(("(logImplies each) infer\s")) {statement.to_sxp}
        block.call(RDF::Statement.from(statement.to_quad, inferred: true))
      end
    end
  end
end

#execute(queryable, solutions:, **options) ⇒ RDF::Solutions

Returns solutions from subject. Solutions are created by evaluating subject against queryable.

Solutions are kept within this instance, and used for conclusions. Note that the evaluated solutions do not affect that of the invoking formula, as the solution spaces are disjoint.

Parameters:

Options Hash (**options):

Returns:

  • (RDF::Solutions)

    distinct solutions



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-n3-edf63ce7750f/lib/rdf/n3/algebra/log/implies.rb', line 28

def execute(queryable, solutions:, **options)
  @queryable = queryable
  @solutions = RDF::Query::Solutions(solutions.map do |solution|
    log_debug(NAME) {"solution: #{SXP::Generator.string solution.to_sxp_bin}"}
    subject = operand(0).evaluate(solution.bindings, formulae: formulae)
    object = operand(1).evaluate(solution.bindings, formulae: formulae)
    log_info(NAME) {"subject: #{SXP::Generator.string subject.to_sxp_bin}"}
    log_info(NAME) {"object: #{SXP::Generator.string object.to_sxp_bin}"}

    # Nothing to do if variables aren't resolved.
    next unless subject && object

    solns = log_depth {subject.execute(queryable, solutions: RDF::Query::Solutions(solution), **options)}

    # Execute object as well (typically used for log:outputString)
    solns = solns.map do |soln|
      log_depth {object.execute(queryable, solutions: RDF::Query::Solutions(soln), **options)}
    end.flatten.compact

    # filter solutions where not all variables in antecedant are bound.
    vars = subject.universal_vars
    solns = solns.select do |soln|
      vars.all? {|v| soln.bound?(v)}
    end
    solns
  end.flatten.compact)
  log_info(NAME) {"solutions: #{SXP::Generator.string @solutions.to_sxp_bin}"}

  # Return original solutions, without bindings
  solutions
end

#graph_nameRDF::Resource

Graph name associated with this operation, using the name of the parent

Returns:



90
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-n3-edf63ce7750f/lib/rdf/n3/algebra/log/implies.rb', line 90

def graph_name; parent.graph_name; end