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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#ifndef __macrovsa_RelationalMap__
#define __macrovsa_RelationalMap__

#include "Symbol.hpp"
#include "Bundling.hpp"

namespace macrovsa {
  /**
   * @class RelationalMap
   * @description Implements a macroscopic ersatz of a VSA relational map.
   * - An relational map is created adding new symbols triples.
   * - It is implemented via [Bundling](./Bundling.html) and double [Binding](./Binding.html), without using a C++ map.
   * @extends Bundling
   */
  class RelationalMap: public Bundling {
public:
    RelationalMap() {}
    RelationalMap(String name) : Bundling(name) {}

    /**
     * @function add
     * @memberof RelationalMap
     * @instance
     * @description Adds a new symbolic triple to the container.
     * - For convinience the symbol can be given by its name, considering that `tau = 1, sigma = 0`.
     * @param {Symbol|String} subject The triple subject to add.
     * @param {Belief|double} [belief] An optional belief, or `tau` value, applied on the predicate.
     * @param {Symbol|String} predicate The triple predicate to add.
     * @param {Symbol|String} object The triple object to add.
     */
    void add(const Symbol& subject, const Symbol& predicate, const Symbol& object);
    void add(String subject, const Symbol& predicate, const Symbol& object)
    {
      const Symbol s(subject);
      add(s, predicate, object);
    }
    void add(const Symbol& subject, String predicate, const Symbol& object)
    {
      const Symbol p(predicate);
      add(subject, p, object);
    }
    void add(const Symbol& subject, const Symbol& predicate, String object)
    {
      const Symbol o(object);
      add(subject, predicate, o);
    }
    void add(String subject, String predicate, const Symbol& object)
    {
      const Symbol s(subject), p(predicate);
      add(s, p, object);
    }
    void add(String subject, const Symbol& predicate, String object)
    {
      Symbol s(subject), o(object);
      add(s, predicate, o);
    }
    void add(const Symbol& subject, String predicate, String object)
    {
      const Symbol p(predicate), o(object);
      add(subject, p, o);
    }
    void add(String subject, String predicate, String object)
    {
      const Symbol s(subject), p(predicate), o(object);
      add(s, p, o);
    }
    void add(const Symbol& subject, const Belief& belief, const Symbol& predicate, const Symbol& object)
    {
      add(subject, *clone(predicate, belief, symbols), object);
    }
    void add(String subject, const Belief& belief, const Symbol& predicate, const Symbol& object)
    {
      const Symbol s(subject);
      add(s, *clone(predicate, belief, symbols), object);
    }
    void add(const Symbol& subject, const Belief& belief, String predicate, const Symbol& object)
    {
      const Symbol p(predicate);
      add(subject, *clone(p, belief, symbols), object);
    }
    void add(const Symbol& subject, const Belief& belief, const Symbol& predicate, String object)
    {
      const Symbol o(object);
      add(subject, *clone(predicate, belief, symbols), o);
    }
    void add(String subject, const Belief& belief, String predicate, const Symbol& object)
    {
      Symbol s(subject), p(predicate);
      add(s, *clone(p, belief, symbols), object);
    }
    void add(String subject, const Belief& belief, const Symbol& predicate, String object)
    {
      Symbol s(subject), o(object);
      add(s, *clone(predicate, belief, symbols), o);
    }
    void add(const Symbol& subject, const Belief& belief, String predicate, String object)
    {
      Symbol p(predicate), o(object);
      add(subject, *clone(p, belief, symbols), o);
    }
    void add(String subject, const Belief& belief, String predicate, String object)
    {
      Symbol s(subject), p(predicate), o(object);
      add(s, *clone(p, belief, symbols), o);
    }
    /**
     * @function add
     * @memberof RelationalMap
     * @instance
     * @description Adds all triple of a relation map in this one.
     * @param {RelationMap} map The triples to append to this relation map
     */
    void add(const RelationalMap& map);

    /**
     * @function add
     * @memberof RelationalMap
     * @instance
     * @description Adds all triple read from a text file with N-Triples syntax.
     * @param {String} filename The filename [N-Triples](https://en.wikipedia.org/wiki/N-Triples) syntax, with the triples to append to this relation map
     */
    void add(String filename);

    /**
     * @function get
     * @memberof RelationalMap
     * @instance
     * @description Returns an approximate value of the relational map by unbinding.
     * @param {Symbol} name The symbol name.
     * @return {Symbol} value A pointer to the stored value, to be deleted after use.
     */
    Symbol *get(const Symbol& name) const;

    /**
     * @function get
     * @memberof RelationalMap
     * @instance
     * @description Defines an iterator over the relational map symbols, used in a construct of the form:
     * ```
     *  for(auto it = relationalMap.Bundling::get().cbegin(); it != relationalMap.Bundling::get().cend(); it++) {
     *    const Binding& binding_spo = dynamic_cast <const Binding&> (it->second);
     *    const Binding& binding_po = dynamic_cast <const Binding&> (binding_spo.x());
     *    const Symbol& subject = binding_spo.y(), predicate = binding_po.y(), object = binding_po.x();
     *    ../..
     *  }
     * ```
     * @return A `const std::map <unsigned int, Symbol& >&` reference for relational map iteration.
     */

    /**
     * @function asString
     * @memberof RelationalMap
     * @instance
     * @description Returns the value as a string.
     * @return {String} A string of the form `{\n\t(subject_i predicate_i object_i)_<belief> ...\n}_<belief>`, omitting belief if `tau=1, sigma=0`.
     */
    virtual std::string asString() const;

    /**
     * @function asString
     * @memberof RelationalMap
     * @static
     * @description Returns the value of a triple as a string.
     * - For convinience the symbol can be given by its name, considering that `tau = 1, sigma = 0`.
     * @param {Symbol|String} subject The triple subject to add.
     * @param {Belief} [belief] An optional belief apply on the predicate.
     * @param {Symbol|String} predicate The triple predicate to add.
     * @param {Symbol|String} object The triple object to add.
     * @return {String} A string of the form `(subject_i predicate_i object_i)_<belief>`, omitting belief if `tau=1, sigma=0`.
     */
    static std::string asString(const Symbol& subject, const Symbol& predicate, const Symbol& object);
    static std::string asString(String subject, const Symbol& predicate, const Symbol& object)
    {
      Symbol s(subject);
      return asString(s, predicate, object);
    }
    static std::string asString(const Symbol& subject, String predicate, const Symbol& object)
    {
      Symbol p(predicate);
      return asString(subject, p, object);
    }
    static std::string asString(const Symbol& subject, const Symbol& predicate, String object)
    {
      Symbol o(object);
      return asString(subject, predicate, o);
    }
    static std::string asString(String subject, String predicate, const Symbol& object)
    {
      Symbol s(subject), p(predicate);
      return asString(s, p, object);
    }
    static std::string asString(String subject, const Symbol& predicate, String object)
    {
      Symbol s(subject), o(object);
      return asString(s, predicate, o);
    }
    static std::string asString(const Symbol& subject, String predicate, String object)
    {
      Symbol p(predicate), o(object);
      return asString(subject, p, o);
    }
    static std::string asString(String subject, String predicate, String object)
    {
      Symbol s(subject), p(predicate), o(object);
      return asString(s, p, o);
    }
    static std::string asString(const Symbol& subject, const Belief& belief, const Symbol& predicate, const Symbol& object)
    {
      Symbol p(predicate, belief);
      return asString(subject, p, object);
    }
    static std::string asString(String subject, const Belief& belief, const Symbol& predicate, const Symbol& object)
    {
      Symbol s(subject), p(predicate, belief);
      return asString(s, p, object);
    }
    static std::string asString(const Symbol& subject, const Belief& belief, String predicate, const Symbol& object)
    {
      Symbol p(predicate, belief);
      return asString(subject, p, object);
    }
    static std::string asString(const Symbol& subject, const Belief& belief, const Symbol& predicate, String object)
    {
      Symbol p(predicate, belief), o(object);
      return asString(subject, p, o);
    }
    static std::string asString(String subject, const Belief& belief, String predicate, const Symbol& object)
    {
      Symbol s(subject), p(predicate, belief);
      return asString(s, p, object);
    }
    static std::string asString(String subject, const Belief& belief, const Symbol& predicate, String object)
    {
      Symbol s(subject), p(predicate, belief), o(object);
      return asString(s, p, o);
    }
    static std::string asString(const Symbol& subject, const Belief& belief, String predicate, String object)
    {
      Symbol p(predicate, belief), o(object);
      return asString(subject, p, o);
    }
    static std::string asString(String subject, const Belief& belief, String predicate, String object)
    {
      Symbol s(subject), p(predicate, belief), o(object);
      return asString(s, p, o);
    }
    /**
     * @function save
     * @memberof RelationalMap
     * @instance
     * @description Saves the triples of this relation map text file in partial [Turtle](https://en.wikipedia.org/wiki/Turtle_(syntax) syntax.
     * @param {String} filename The output file name.
     */
    void save(String filename) const;
  };
}

#endif