Method: Sbn::Net.from_xmlbif

Defined in:
lib/sbn/formats.rb

.from_xmlbif(source) ⇒ Object

Reconstitute a saved network.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sbn/formats.rb', line 47

def self.from_xmlbif(source)
  # convert tags to lower case
  source.gsub!(/(<.*?>)/, '\\1'.downcase)
  
  doc = XmlSimple.xml_in(source)
  netname = doc['network'].first['name'].first
        
  # find net name
  returnval = Net.new(netname)
  
  # find variables
  count = 0
  variables = {}
  variable_elements = doc['network'].first['variable'].each do |var|
    varname = var['name'].first.to_sym
    properties = var['property']
    vartype = nil
    manager_name = nil
    text_to_match = ""
    options = {}
    thresholds = []
    properties.each do |prop|
      key, val = prop.split('=').map {|e| e.strip }
      vartype = val if key == 'SbnVariableType'
      manager_name = val if key == 'ManagerVariableName'
      text_to_match = eval(val) if key == 'TextToMatch'
      options[key.to_sym] = val.to_i if key =~ /stdev_state_count/
      thresholds = val.map {|e| e.to_f } if key == 'StateThresholds'
    end
    states = var['outcome']
    table = []
    doc['network'].first['definition'].each do |defn|
      if defn['for'].first.to_sym == varname
        table = defn['table'].first.split.map {|prob| prob.to_f }
      end
    end
    count += 1
    variables[varname] = case vartype
      when "Sbn::StringVariable" then StringVariable.new(returnval, varname)
      when "Sbn::NumericVariable" then NumericVariable.new(returnval, varname, table, thresholds, options)
      when "Sbn::Variable" then Variable.new(returnval, varname, table, states)
      when "Sbn::StringCovariable" then StringCovariable.new(returnval, manager_name, text_to_match, table)
    end
  end

  # find relationships between variables

  # connect covariables to their managers
  variable_elements = doc['network'].first['variable'].each do |var|
    varname = var['name'].first.to_sym
    properties = var['property']
    vartype = nil
    covars = nil
    parents = nil
    properties.each do |prop|
      key, val = prop.split('=').map {|e| e.strip }
      covars = val.split(',').map {|e| e.strip.to_sym } if key == 'Covariables'
      parents = val.split(',').map {|e| e.strip.to_sym } if key == 'Parents'
      vartype = val if key == 'SbnVariableType'
    end
    if vartype == "Sbn::StringVariable"
      parents.each {|p| variables[varname].add_parent(variables[p]) } if parents
      covars.each {|covar| variables[varname].add_covariable(variables[covar]) } if covars
    end
  end

  # connect all other variables to their parents
  doc['network'].first['definition'].each do |defn|
    varname = defn['for'].first.to_sym
    parents = defn['given']
    parents.each {|p| variables[varname].add_parent(variables[p.to_sym]) } if parents
  end
  returnval
end