Class: DataModelInterpreter

Inherits:
DslContext show all
Defined in:
lib/DataModelInterpreter.rb

Instance Method Summary collapse

Methods inherited from DslContext

bubble, execute, polish_text

Constructor Details

#initializeDataModelInterpreter

Returns a new instance of DataModelInterpreter.



6
7
8
9
10
# File 'lib/DataModelInterpreter.rb', line 6

def initialize
  @elements = []
  @elementTree = []
  @depth=0
end

Instance Method Details

#coverage(*args) ⇒ Object



60
61
62
# File 'lib/DataModelInterpreter.rb', line 60

def coverage(*args)
  createNewElement(args[0]+ "Coverage",nil,true,@elementTree.last,"coverages")
end

#createNewElement(name, ctype, coverage, parent, type) ⇒ Object

the type tells us if it is an entity, or coverage, or nil if a simple element



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/DataModelInterpreter.rb', line 24

def createNewElement(name,ctype,coverage,parent,type) #the type tells us if it is an entity, or coverage, or nil if a simple element
  e = ElementFactory(name,coverage,parent,type)
  @elementTree.last.children.push e if @elementTree.last and @elementTree.length > 0
  if ctype and ctype != name
    #copy fields from complex type definition to be used in this clone
    @elements.each do |node|
      if node.name == ctype
        e.fields = node.fields
        e.children = node.children
        e.parent = @elementTree.last
      end
    end
  end
  @elementTree.push e
end

#datamodel(*args) ⇒ Object

–parse - implement the grammar



18
19
# File 'lib/DataModelInterpreter.rb', line 18

def datamodel(*args)
end

#dumpParsedStructureObject

———– dump



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/DataModelInterpreter.rb', line 94

def dumpParsedStructure
  @dumpStructure = ""
  @elements.each do |e|
    if (e.coverage)
      @dumpStructure << "#{e.name}:\n"
      listChildren(e)
      @dumpStructure << "\n"
    end
  end
  puts "#{@dumpStructure}"
end

#element(*args) ⇒ Object



52
53
54
# File 'lib/DataModelInterpreter.rb', line 52

def element(*args)
  createNewElement(args[0],nil,false,@elementTree.last,nil)
end

#ElementFactory(name, coverage, parent, type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/DataModelInterpreter.rb', line 40

def ElementFactory(name,coverage,parent,type)
  @elements.each do |node|
    if node.name == name
      node.parent = parent
      return node
    end
  end
  e = Element.new(name,coverage,parent,type)
  @elements.push e
  return e
end

#endcoverage(*args) ⇒ Object



64
65
66
# File 'lib/DataModelInterpreter.rb', line 64

def endcoverage(*args)
  @elementTree.pop
end

#enddatamodel(*args) ⇒ Object



21
22
# File 'lib/DataModelInterpreter.rb', line 21

def enddatamodel(*args)
end

#endelement(*args) ⇒ Object



56
57
58
# File 'lib/DataModelInterpreter.rb', line 56

def endelement(*args)
  @elementTree.pop
end

#endentity(*args) ⇒ Object



72
73
74
# File 'lib/DataModelInterpreter.rb', line 72

def endentity(*args)
  @elementTree.pop
end

#entity(*args) ⇒ Object



68
69
70
# File 'lib/DataModelInterpreter.rb', line 68

def entity(*args)
  createNewElement(args[0],nil,true,@elementTree.last,"entities")
end

#field(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/DataModelInterpreter.rb', line 76

def field(*args)
  if (args[0][:ctype] != nil)
    if (@elementTree.last.coverage)
      parent = nil
    else
      parent = @elementTree.last
    end
    createNewElement(args[0][:name],args[0][:ctype],false,parent,nil)
    #pop the tree since createNewElement pushes new element to the tree
    #since has to work for enties and coverages
     @elementTree.pop
  else
    @elementTree.last.fields.push args[0]
  end
end

#getResultObject



12
13
14
15
16
# File 'lib/DataModelInterpreter.rb', line 12

def getResult
  #dumpParsedStructure
  result = @elements
  result
end

#listChildren(e) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/DataModelInterpreter.rb', line 106

def listChildren(e)
   @depth = @depth + 1
   e.children.each do |c|
    str = ""
    @depth.times { str << "\t"}
    @dumpStructure << "#{str}#{c.name}\n"
    c.fields.each do |f|
       str = ""
       @depth.times { str << "\t\t"}
       @dumpStructure << "#{str}field:#{f}\n"
    end
    listChildren(c)
    @depth = @depth - 1
  end
end