]> matita.cs.unibo.it Git - helm.git/blob - helm/papers/use_case/stats/stats.cc
* added a few calculations
[helm.git] / helm / papers / use_case / stats / stats.cc
1
2 #include <vector>
3 #include <cassert>
4 #include <GdomeSmartDOM.hh>
5
6 namespace DOM = GdomeSmartDOM;
7
8 unsigned n_elements;
9 unsigned n_leaf_elements;
10 unsigned n_text_nodes;
11 unsigned n_blank_text_nodes;
12 unsigned n_attributes;
13 unsigned max_attributes;
14 unsigned max_depth;
15 unsigned max_children;
16 std::vector<unsigned> depths;
17 std::vector<unsigned> widths;
18
19 bool
20 is_blank(const std::string& s)
21 {
22   for (int i = 0; i < s.length(); i++)
23     if (!isblank(s[i])) return false;
24   return true;
25 }
26
27 void
28 visit(DOM::Node node, unsigned depth)
29 {
30   assert(node);
31
32   max_depth = std::max(max_depth, depth);
33
34   switch (node.get_nodeType())
35     {
36     case DOM::Node::ELEMENT_NODE:
37       {
38         n_elements++;
39         const unsigned n_attrs = node.get_attributes().get_length();
40         n_attributes += n_attrs;
41         max_attributes = std::max(max_attributes, n_attrs);
42         if (!node.get_firstChild()) n_leaf_elements++;
43       }
44       break;
45     case DOM::Node::TEXT_NODE:
46       n_text_nodes++;
47       if (is_blank(node.get_nodeValue())) n_blank_text_nodes++;
48       break;
49     case DOM::Node::ATTRIBUTE_NODE:
50       break;
51     }
52
53   unsigned n_children = 0;
54   for (DOM::Node p = node.get_firstChild(); p; p = p.get_nextSibling())
55     {
56       visit(p, depth + 1);
57       n_children++;
58     }
59   max_children = std::max(max_children, n_children);
60
61   if (!node.get_firstChild())
62     depths.push_back(depth);
63   else
64     widths.push_back(n_children);
65 }
66
67 void
68 print_results(const std::string& URI)
69 {
70   unsigned tot_depth = 0;
71   for (std::vector<unsigned>::const_iterator p = depths.begin(); p != depths.end(); p++)
72           tot_depth += *p;
73   
74   unsigned tot_width = 0;
75   for (std::vector<unsigned>::const_iterator p = widths.begin(); p != widths.end(); p++)
76           tot_width += *p;
77
78   std::cout << "<stats for=\"" << URI << "\">" << std::endl;
79   std::cout << "  <depth>" << std::endl;
80   std::cout << "    <max>" << max_depth << "</max>" << std::endl;
81   std::cout << "    <leaf-avg>" << tot_depth / ((double) depths.size()) << "</leaf-avg>" << std::endl;
82   std::cout << "  </depth>" << std::endl;
83   std::cout << "  <width>" << std::endl;
84   std::cout << "    <max>" << max_children << "</max>" << std::endl;
85   std::cout << "    <inner-avg>" << tot_width / ((double) widths.size()) << "</inner-avg>" << std::endl;
86   std::cout << "  </width>" << std::endl;  
87   std::cout << "  <elements>" << std::endl;
88   std::cout << "    <total>" << n_elements << "</total>" << std::endl;
89   std::cout << "    <leaf>" << n_leaf_elements << "</leaf>" << std::endl;
90   std::cout << "  </elements>" << std::endl;
91   std::cout << "  <text-nodes>" << std::endl;
92   std::cout << "    <total>" << n_text_nodes << "</total>" << std::endl;
93   std::cout << "    <blank>" << n_blank_text_nodes << "</blank>" << std::endl;
94   std::cout << "  </text-nodes>" << std::endl;
95   std::cout << "  <attributes>" << std::endl;
96   std::cout << "    <total>" << n_attributes << "</total>" << std::endl;
97   std::cout << "    <max>" << max_attributes << "</max>" << std::endl;
98   std::cout << "  </attributes>" << std::endl;
99   std::cout << "</stats>" << std::endl;
100 }
101
102 int
103 main(int argc, char* argv[])
104 {
105   if (argc != 2) {
106     std::cerr << "Usage: stats <URI>" << std::endl;
107     return -1;
108   }
109
110   DOM::DOMImplementation di;
111   DOM::Document doc = di.createDocumentFromURI(argv[1]);
112   visit(doc, 0);
113   print_results(argv[1]);
114 }