class OntologyParser

Attributes

declaration[RW]
filename[RW]
subclassof[RW]
xmldoc[RW]

Public Class Methods

new(filename) click to toggle source
# File lib/ontology_parser.rb, line 70
def initialize(filename)
  self.filename=filename
  xmlfile=File.open(self.filename)
  self.xmldoc=Document.new(xmlfile)
end

Public Instance Methods

find_semi_root() click to toggle source
# File lib/ontology_parser.rb, line 76
def find_semi_root
  self.declaration-self.subclassof.keys
end
generate_engine(subclassof_hash) click to toggle source
# File lib/ontology_parser.rb, line 31
def generate_engine(subclassof_hash)
  #below will get {"c"=>[["sitea.com", "c"], ["siteb.com", "c"]], "b"=>[["sitec.com", "b"]]}
  subclassof_hash=subclassof_hash.group_by {|k,v| v}
  #zheng li yi xia
  subclassof_hash.each do |k,v|
    tmp=[]
    v.each do |lis|
      tmp<<lis[0]  #abandon the 2th element
    end
    #make it as {"c"=>["sitea.com", "siteb.com"], "b"=>["sitec.com"]}
    subclassof_hash[k]=tmp
  end
  semi_root_lis=self.find_semi_root()
  semi_root_hash_list=[]
  #make the semi_root as Hash List
  semi_root_lis.each do |ele|
    semi_root_hash_list<<{"name"=>ele, "parent"=>"null", "children"=>""}
  end
  res_without_semi_root=self.recursively_get_tree(semi_root_hash_list,subclassof_hash)
  things={'name'=>'Things','parent'=>'null','children'=>''}
  semi_lis=[]
  res_without_semi_root.each do |ele|
    semi_lis<<ele
  end
  things['children']=semi_lis
  return [things]
end
get_declarations() click to toggle source
# File lib/ontology_parser.rb, line 8
def get_declarations
  self.declaration=[]
  self.xmldoc.elements.each('Ontology/Declaration/Class') do |declaration|
    self.declaration<<declaration.attributes['IRI']
  end
end
get_subclassof() click to toggle source
# File lib/ontology_parser.rb, line 59
def get_subclassof
  self.subclassof=Hash.new
  list=[]
  self.xmldoc.elements.each('Ontology/SubClassOf/Class') do |subclassof|
    list<<subclassof.attributes['IRI']
  end
  list.each_slice(2).each do |each2|
    self.subclassof[each2[0]]=each2[1]
  end
end
get_tree() click to toggle source
# File lib/ontology_parser.rb, line 80
def get_tree
  self.get_declarations
  self.get_subclassof
  require 'json'
  return JSON.generate(self.generate_engine(self.subclassof))
end
recursively_get_tree(semi_root_hash_lis, subclassof_hash) click to toggle source
# File lib/ontology_parser.rb, line 15
def recursively_get_tree(semi_root_hash_lis, subclassof_hash)
  if semi_root_hash_lis.nil?
    return;
  end
  for i in 0...semi_root_hash_lis.size
    if !subclassof_hash[semi_root_hash_lis[i]['name']].nil?
      semi_root_hash_lis[i]['children']=[]
      subclassof_hash[semi_root_hash_lis[i]['name']].each do |ele|
        semi_root_hash_lis[i]['children']<<{'name'=>ele, 'children'=>"",'parent'=>semi_root_hash_lis[i]['name']}
      end
      self.recursively_get_tree(semi_root_hash_lis[i]['children'],subclassof_hash)
    end
  end
  return semi_root_hash_lis
end