Class: Wongi::Engine::Template
- Inherits:
-
Struct
- Object
- Struct
- Wongi::Engine::Template
- Defined in:
- lib/wongi-engine/template.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#predicate ⇒ Object
Returns the value of attribute predicate.
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
- .concrete?(thing) ⇒ Boolean
- .match?(wme_element, template_element) ⇒ Boolean
- .placeholder?(thing) ⇒ Boolean
- .variable?(thing) ⇒ Boolean
Instance Method Summary collapse
- #==(other) ⇒ Object
- #=~(template) ⇒ Object
- #bitmap ⇒ Object
- #concrete? ⇒ Boolean
- #inspect ⇒ Object
- #resolve!(token) ⇒ Object
-
#root? ⇒ Boolean
TODO: reintroduce Network#import when bringing back RDF support.
- #to_s ⇒ Object
- #variables ⇒ Object
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object
2 3 4 |
# File 'lib/wongi-engine/template.rb', line 2 def object @object end |
#predicate ⇒ Object
Returns the value of attribute predicate
2 3 4 |
# File 'lib/wongi-engine/template.rb', line 2 def predicate @predicate end |
#subject ⇒ Object
Returns the value of attribute subject
2 3 4 |
# File 'lib/wongi-engine/template.rb', line 2 def subject @subject end |
Class Method Details
.concrete?(thing) ⇒ Boolean
11 12 13 |
# File 'lib/wongi-engine/template.rb', line 11 def self.concrete?(thing) !variable?(thing) && !placeholder?(thing) end |
.match?(wme_element, template_element) ⇒ Boolean
15 16 17 18 19 |
# File 'lib/wongi-engine/template.rb', line 15 def self.match?(wme_element, template_element) return true if variable?(template_element) || placeholder?(template_element) wme_element == template_element end |
.placeholder?(thing) ⇒ Boolean
7 8 9 |
# File 'lib/wongi-engine/template.rb', line 7 def self.placeholder?(thing) thing.is_a?(Symbol) && thing.start_with?('_') end |
.variable?(thing) ⇒ Boolean
3 4 5 |
# File 'lib/wongi-engine/template.rb', line 3 def self.variable?(thing) thing.is_a?(Symbol) && thing[0] >= 'A' && thing[0] <= 'Z' end |
Instance Method Details
#==(other) ⇒ Object
43 44 45 |
# File 'lib/wongi-engine/template.rb', line 43 def ==(other) other.is_a?(Template) && subject == other.subject && predicate == other.predicate && object == other.object end |
#=~(template) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wongi-engine/template.rb', line 47 def =~(template) case template when Template (template.subject == :_ || template.subject == subject) && (template.predicate == :_ || template.predicate == predicate) && (template.object == :_ || template.object == object) else raise Error, "templates can only match other templates" end end |
#bitmap ⇒ Object
31 32 33 |
# File 'lib/wongi-engine/template.rb', line 31 def bitmap (Template.concrete?(subject) ? 4 : 0) | (Template.concrete?(predicate) ? 2 : 0) | (Template.concrete?(object) ? 1 : 0) end |
#concrete? ⇒ Boolean
27 28 29 |
# File 'lib/wongi-engine/template.rb', line 27 def concrete? Template.concrete?(subject) && Template.concrete?(predicate) && Template.concrete?(object) end |
#inspect ⇒ Object
58 59 60 |
# File 'lib/wongi-engine/template.rb', line 58 def inspect "<~#{subject.inspect} #{predicate.inspect} #{object.inspect}>" end |
#resolve!(token) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/wongi-engine/template.rb', line 66 def resolve!(token) s = if Template.variable?(subject) raise DefinitionError, "unbound variable #{subject} in token #{token}" unless token.has_var?(subject) token[subject] else subject end p = if Template.variable?(predicate) raise DefinitionError, "unbound variable #{predicate} in token #{token}" unless token.has_var?(predicate) token[predicate] else predicate end o = if Template.variable?(object) raise DefinitionError, "unbound variable #{object} in token #{token}" unless token.has_var?(object) token[object] else object end [s, p, o] end |
#root? ⇒ Boolean
TODO: reintroduce Network#import when bringing back RDF support
23 24 25 |
# File 'lib/wongi-engine/template.rb', line 23 def root? subject == :_ && predicate == :_ && object == :_ end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/wongi-engine/template.rb', line 62 def to_s inspect end |
#variables ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/wongi-engine/template.rb', line 35 def variables [].tap do |a| a << subject if Template.variable?(subject) a << predicate if Template.variable?(predicate) a << object if Template.variable?(object) end end |