Class: Wongi::Engine::Template

Inherits:
Struct
  • Object
show all
Defined in:
lib/wongi-engine/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#objectObject

Returns the value of attribute object

Returns:

  • (Object)

    the current value of object



2
3
4
# File 'lib/wongi-engine/template.rb', line 2

def object
  @object
end

#predicateObject

Returns the value of attribute predicate

Returns:

  • (Object)

    the current value of predicate



2
3
4
# File 'lib/wongi-engine/template.rb', line 2

def predicate
  @predicate
end

#subjectObject

Returns the value of attribute subject

Returns:

  • (Object)

    the current value of subject



2
3
4
# File 'lib/wongi-engine/template.rb', line 2

def subject
  @subject
end

Class Method Details

.concrete?(thing) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

#bitmapObject



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

Returns:

  • (Boolean)


27
28
29
# File 'lib/wongi-engine/template.rb', line 27

def concrete?
  Template.concrete?(subject) && Template.concrete?(predicate) && Template.concrete?(object)
end

#inspectObject



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

Returns:

  • (Boolean)


23
24
25
# File 'lib/wongi-engine/template.rb', line 23

def root?
  subject == :_ && predicate == :_ && object == :_
end

#to_sObject



62
63
64
# File 'lib/wongi-engine/template.rb', line 62

def to_s
  inspect
end

#variablesObject



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