summaryrefslogtreecommitdiff
path: root/libzeitgeist/ontology.vala.in
blob: 768fd00548025c384815ac9fbde712c9384febe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ontology.vala
 *
 * Copyright © 2011 Collabora Ltd.
 *             By Seif Lotfy <seif@lotfy.com>
 *             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * Get information about interpretation/manifestation symbols
 */

namespace Zeitgeist
{

    namespace Symbol
    {
        private static HashTable<string, Info> all_symbols = null;
        private static bool initialized = false;

        public static unowned string get_display_name (string symbol_uri)
        {
            initialize_symbols ();

            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return symbol_uri;

            return symbol.display_name;
        }

        public static unowned string get_description (string symbol_uri)
        {
            initialize_symbols ();

            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return "";

            return symbol.description;
        }

        public static List<unowned string> get_all_parents (string symbol_uri)
        {
            initialize_symbols ();

            var results = new List<unowned string> ();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.parents)
            {
                results.append (uri);
                // Recursively get the other parents
                foreach (unowned string parent_uri in get_all_parents (uri))
                    if (results.index (parent_uri) == -1)
                        results.append (parent_uri);
            }

            return results;
        }

        public static List<unowned string> get_all_children (string symbol_uri)
        {
            initialize_symbols ();

            var results = new List<unowned string> ();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.all_children)
                results.append (uri);

            return results;
        }

        public static List<unowned string> get_children (string symbol_uri)
        {
            initialize_symbols ();
            var results = new List<unowned string> ();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.children)
                results.append(uri);

            return results;
        }

        public static List<unowned string> get_parents (string symbol_uri)
        {
            initialize_symbols ();

            var results = new List<unowned string>();
            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return results;

            foreach (unowned string uri in symbol.parents)
                results.append (uri);

            return results;
        }

        public static bool is_a (string? symbol_uri, string? parent_uri)
        {
            if (parent_uri == null || symbol_uri == null) return false;
            initialize_symbols ();

            var symbol = all_symbols.lookup (symbol_uri);
            if (symbol == null) return false;
            if (parent_uri == symbol_uri) return true;

            foreach (unowned string uri in get_all_parents (symbol_uri))
                if (parent_uri == uri)
                    return true;
            return false;
        }

        private static void initialize_symbols ()
        {
            if (initialized) return;
            initialized = true;
            // *insert-auto-generated-code*
        }

    }

    private class Symbol.Info
    {
        public List<string> parents;
        public List<string> children;
        public List<string> all_children;
        public string uri;
        public string display_name;
        public string description;

        private Info (string uri, string display_name, string description,
            string[] parents, string[] children, string[] all_children)
        {
            this.uri = uri;
            this.display_name = display_name;
            this.description = description;
            this.parents = new List<string> ();
            for (int i = 0; i < parents.length; i++)
                this.parents.append (parents[i]);
            this.children = new List<string> ();
            for (int i = 0; i < children.length; i++)
                this.children.append (children[i]);
            this.all_children = new List<string> ();
            for (int i = 0; i < all_children.length; i++)
                this.all_children.append (all_children[i]);
        }

        internal static void register (string uri, string display_name,
            string description, string[] parents, string[] children,
            string[] all_children)
        {
            if (all_symbols == null)
                all_symbols = new HashTable<string, Info> (str_hash, str_equal);
            Info symbol = new Info (uri, display_name, description,
                parents, children, all_children);
            all_symbols.insert (uri, symbol);
        }

    }

}

// vim:expandtab:ts=4:sw=4