|
| 1 | +module LinkedData |
| 2 | + module Concerns |
| 3 | + module Concept |
| 4 | + module Tree |
| 5 | + def tree(concept_schemes: [], concept_collections: [], roots: nil) |
| 6 | + bring(parents: [:prefLabel]) if bring?(:parents) |
| 7 | + return self if parents.nil? || parents.empty? |
| 8 | + extra_include = [:hasChildren, :isInActiveScheme, :isInActiveCollection] |
| 9 | + roots = self.submission.roots( extra_include, concept_schemes:concept_schemes) if roots.nil? |
| 10 | + path = path_to_root(roots) |
| 11 | + threshold = 99 |
| 12 | + |
| 13 | + return self if path.nil? |
| 14 | + |
| 15 | + attrs_to_load = %i[prefLabel synonym obsolete] |
| 16 | + attrs_to_load << :subClassOf if submission.hasOntologyLanguage.obo? |
| 17 | + attrs_to_load += self.class.concept_is_in_attributes if submission.skos? |
| 18 | + self.class.in(submission) |
| 19 | + .models(path) |
| 20 | + .include(attrs_to_load).all |
| 21 | + load_children(path, threshold: threshold) |
| 22 | + |
| 23 | + path.reverse! |
| 24 | + path.last.instance_variable_set("@children", []) |
| 25 | + |
| 26 | + childrens_hash = {} |
| 27 | + path.each do |m| |
| 28 | + next if m.id.to_s["#Thing"] |
| 29 | + m.children.each do |c| |
| 30 | + childrens_hash[c.id.to_s] = c |
| 31 | + c.load_computed_attributes(to_load:extra_include , |
| 32 | + options: {schemes: concept_schemes, collections: concept_collections}) |
| 33 | + end |
| 34 | + m.load_computed_attributes(to_load:extra_include , |
| 35 | + options: {schemes: concept_schemes, collections: concept_collections}) |
| 36 | + end |
| 37 | + |
| 38 | + load_children(childrens_hash.values, threshold: threshold) |
| 39 | + |
| 40 | + build_tree(path) |
| 41 | + end |
| 42 | + |
| 43 | + def tree_sorted(concept_schemes: [], concept_collections: [], roots: nil) |
| 44 | + tr = tree(concept_schemes: concept_schemes, concept_collections: concept_collections, roots: roots) |
| 45 | + self.class.sort_tree_children(tr) |
| 46 | + tr |
| 47 | + end |
| 48 | + |
| 49 | + def paths_to_root(tree: false, roots: nil) |
| 50 | + bring(parents: [:prefLabel, :synonym, :definition]) if bring?(:parents) |
| 51 | + return [] if parents.nil? || parents.empty? |
| 52 | + |
| 53 | + paths = [[self]] |
| 54 | + traverse_path_to_root(self.parents.dup, paths, 0, tree, roots) unless tree_root?(self, roots) |
| 55 | + paths.each do |p| |
| 56 | + p.reverse! |
| 57 | + end |
| 58 | + paths |
| 59 | + end |
| 60 | + |
| 61 | + def path_to_root(roots) |
| 62 | + paths = [[self]] |
| 63 | + paths = paths_to_root(tree: true, roots: roots) |
| 64 | + #select one path that gets to root |
| 65 | + path = nil |
| 66 | + paths.each do |p| |
| 67 | + p.reverse! |
| 68 | + unless (p.map { |x| x.id.to_s } & roots.map { |x| x.id.to_s }).empty? |
| 69 | + path = p |
| 70 | + break |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + if path.nil? |
| 75 | + # do one more check for root classes that don't get returned by the submission.roots call |
| 76 | + paths.each do |p| |
| 77 | + root_node = p.last |
| 78 | + root_parents = root_node.parents |
| 79 | + |
| 80 | + if root_parents.empty? |
| 81 | + path = p |
| 82 | + break |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + path |
| 88 | + end |
| 89 | + |
| 90 | + def tree_root?(concept, roots) |
| 91 | + (roots &&roots.map{|r| r.id}.include?(concept.id)) || concept.id.to_s["#Thing"] |
| 92 | + end |
| 93 | + |
| 94 | + private |
| 95 | + |
| 96 | + def load_children(concepts, threshold: 99) |
| 97 | + LinkedData::Models::Class |
| 98 | + .partially_load_children(concepts, threshold, submission) |
| 99 | + end |
| 100 | + |
| 101 | + def build_tree(path) |
| 102 | + root_node = path.first |
| 103 | + tree_node = path.first |
| 104 | + path.delete_at(0) |
| 105 | + while tree_node && |
| 106 | + !tree_node.id.to_s["#Thing"] && |
| 107 | + !tree_node.children.empty? && (!path.empty?) do |
| 108 | + next_tree_node = nil |
| 109 | + tree_node.load_has_children |
| 110 | + tree_node.children.each_index do |i| |
| 111 | + if tree_node.children[i].id.to_s == path.first.id.to_s |
| 112 | + next_tree_node = path.first |
| 113 | + children = tree_node.children.dup |
| 114 | + children[i] = path.first |
| 115 | + tree_node.instance_variable_set("@children", children) |
| 116 | + children.each do |c| |
| 117 | + c.load_has_children |
| 118 | + end |
| 119 | + else |
| 120 | + tree_node.children[i].instance_variable_set("@children", []) |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + if !path.empty? && next_tree_node.nil? |
| 125 | + tree_node.children << path.shift |
| 126 | + end |
| 127 | + tree_node = next_tree_node |
| 128 | + path.delete_at(0) |
| 129 | + end |
| 130 | + |
| 131 | + root_node |
| 132 | + end |
| 133 | + |
| 134 | + end |
| 135 | + end |
| 136 | + |
| 137 | + end |
| 138 | +end |
| 139 | + |
0 commit comments