@@ -90,7 +90,7 @@ operator<<(ostream &out, const SymbolOrValue<T> &s)
90
90
template <typename ... Ts>
91
91
struct Relation : tuple<Ts...>
92
92
{
93
- typedef tuple<Ts...> Type ;
93
+ typedef tuple<Ts...> TupleType ;
94
94
typedef tuple<SymbolOrValue<Ts>...> Atom;
95
95
using tuple<Ts...>::tuple;
96
96
struct compare
@@ -103,6 +103,24 @@ struct Relation : tuple<Ts...>
103
103
typedef set<Relation, compare> Set;
104
104
};
105
105
106
+ #if 0
107
+ template <std::size_t I, typename ...Ts>
108
+ decltype(auto) get(Relation<Ts...>&& v)
109
+ {
110
+ return std::get<I>(static_cast<std::tuple<Ts...>&&>(v));
111
+ }
112
+ template <std::size_t I, typename ...Ts>
113
+ decltype(auto) get(Relation<Ts...>& v)
114
+ {
115
+ return std::get<I>(static_cast<std::tuple<Ts...>&>(v));
116
+ }
117
+ template <std::size_t I, typename ...Ts>
118
+ decltype(auto) get(Relation<Ts...> const& v)
119
+ {
120
+ return std::get<I>(static_cast<std::tuple<Ts...> const&>(v));
121
+ }
122
+ #endif
123
+
106
124
template <typename TUPLE_TYPE>
107
125
static ostream &
108
126
print (ostream &out, const TUPLE_TYPE &s)
@@ -150,9 +168,8 @@ bool bind(SymbolOrValue<VALUE_TYPE> &s, const VALUE_TYPE &v)
150
168
return true ;
151
169
}
152
170
153
- template <typename RELATION_TYPE, size_t ... Is>
154
- static bool bind (typename RELATION_TYPE::Atom &atom,
155
- const typename RELATION_TYPE::Type &fact, index_sequence<Is...>)
171
+ template <typename ATOM_TYPE, typename GROUND_TYPE, size_t ... Is>
172
+ static bool bind (ATOM_TYPE &atom, const GROUND_TYPE &fact, index_sequence<Is...>)
156
173
{
157
174
bool success = ((bind<Is>(get<Is>(atom), get<Is>(fact))) and ...);
158
175
if (success)
@@ -177,16 +194,23 @@ static bool bind(typename RELATION_TYPE::Atom &atom,
177
194
}
178
195
179
196
// bind 1 atom with 1 fact (of the same relation type)
197
+ #if 0
198
+ template <typename ATOM_TYPE, typename GROUND_TYPE>
199
+ static bool bind(ATOM_TYPE &atom, const GROUND_TYPE &fact)
200
+ {
201
+ //unbind<RELATION_TYPE>(atom);
202
+ constexpr size_t tupleSize = tuple_size<typename GROUND_TYPE::TupleType>::value;
203
+ return bind<ATOM_TYPE, typename GROUND_TYPE::TupleType>(atom, fact, make_index_sequence<tupleSize>{});
204
+ }
205
+ #else
180
206
template <typename RELATION_TYPE>
181
- static bool bind (typename RELATION_TYPE::Atom &atom,
182
- // const typename RELATION_TYPE::GroundType& fact
183
- const RELATION_TYPE &fact)
207
+ static bool bind (typename RELATION_TYPE::Atom &atom, const RELATION_TYPE &fact)
184
208
{
185
209
// unbind<RELATION_TYPE>(atom);
186
- // constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::GroundType::Type>::value;
187
- constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::Type>::value;
188
- return bind<RELATION_TYPE>(atom, fact, make_index_sequence<tupleSize>{});
210
+ constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::TupleType>::value;
211
+ return bind<typename RELATION_TYPE::Atom, typename RELATION_TYPE::TupleType>(atom, fact, make_index_sequence<tupleSize>{});
189
212
}
213
+ #endif
190
214
191
215
#if 0
192
216
// bind 1 atom with a relation (n facts)
@@ -211,18 +235,18 @@ struct Rule
211
235
{
212
236
typedef Rule Define;
213
237
typedef typename HEAD_RELATION::Atom HeadType;
214
- const HeadType head;
238
+ HeadType head;
215
239
typedef tuple<BODY_RELATIONs...> BodyRelations;
216
240
typedef tuple<typename BODY_RELATIONs::Atom...> BodyType;
217
- const BodyType body;
241
+ BodyType body;
218
242
};
219
243
220
244
template <typename ... RELATIONs>
221
245
struct State
222
246
{
223
247
typedef tuple<typename RELATIONs::Set...> RelationsType;
224
248
RelationsType relations;
225
- typedef tuple<RELATIONs const *...> SliceType;
249
+ typedef tuple<const RELATIONs *...> SliceType;
226
250
typedef tuple<typename RELATIONs::Set::const_iterator...> IteratorsArrayType;
227
251
228
252
template <typename TUPLE_TYPE>
@@ -231,7 +255,7 @@ struct State
231
255
{
232
256
if (p)
233
257
{
234
- datalog::print<typename TUPLE_TYPE::Type >(out, *p);
258
+ datalog::print<typename TUPLE_TYPE::TupleType >(out, *p);
235
259
}
236
260
return out;
237
261
}
@@ -252,13 +276,13 @@ struct State
252
276
private:
253
277
template <size_t I, typename RELATION_TYPE>
254
278
void pick (const typename RELATION_TYPE::Set &relation,
255
- RELATION_TYPE const *&sliceElement)
279
+ const RELATION_TYPE *&sliceElement)
256
280
{
257
281
const auto &it = get<I>(iterators);
258
282
if (it != relation.end ())
259
283
{
260
- // NASTY: fix this
261
- sliceElement = reinterpret_cast <RELATION_TYPE const *>(&*it);
284
+ // TODO: avoid cast if possible
285
+ sliceElement = reinterpret_cast <const RELATION_TYPE *>(&*it);
262
286
}
263
287
else
264
288
{
@@ -288,7 +312,6 @@ struct State
288
312
it++;
289
313
if (it == end)
290
314
{
291
- // it = get<I>(relations).tuples.begin();
292
315
it = get<I>(relations).begin ();
293
316
if (I == tuple_size<RelationsType>::value - 1 )
294
317
{
@@ -361,8 +384,7 @@ struct State
361
384
362
385
Iterator iterator () const
363
386
{
364
- Iterator it{
365
- relations};
387
+ Iterator it{relations};
366
388
return it;
367
389
}
368
390
};
@@ -381,34 +403,43 @@ static void unbind(const typename RULE_TYPE::BodyType &atoms)
381
403
make_index_sequence<tuple_size<typename RULE_TYPE::BodyType>::value>{});
382
404
}
383
405
406
+ #if 0
407
+ // bind 1 atom with 1 fact (of the same relation type)
408
+ template <typename RELATION_TYPE>
409
+ static bool bind(typename RELATION_TYPE::Atom &atom,
410
+ const RELATION_TYPE &fact)
411
+ {
412
+ //unbind<RELATION_TYPE>(atom);
413
+ //constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::Type>::value;
414
+ constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::TupleType>::value;
415
+ return bind<RELATION_TYPE>(atom, fact, make_index_sequence<tupleSize>{});
416
+ }
417
+ #endif
418
+
384
419
template <size_t I, typename RULE_TYPE, typename ... RELATIONs>
385
- static void bind (const typename RULE_TYPE::BodyType &atoms,
420
+ static void bind (typename RULE_TYPE::BodyType &atoms, // typedef tuple<typename BODY_RELATIONs::Atom...> BodyType;
386
421
const tuple<RELATIONs const *...> &slice)
387
422
{
388
- auto &atom = get<I>(atoms);
423
+ // get the fact in the slice that can potentially bind
389
424
typedef typename tuple_element<I, typename RULE_TYPE::BodyRelations>::type BodyRelationI;
390
- const auto &factPtr = get<BodyRelationI const *>(slice);
391
- // ((bind()), ...)
392
-
393
- // TODO: atom type is SymbolOrValue but ground type is not
394
- #if 0
395
- ((bind<Relation<typename tuple_element<Is, const typename RULE_TYPE::BodyType>::type>>(
396
- get<Is>(atoms),
397
- (get<typename Relation<typename tuple_element<Is, const typename RULE_TYPE::BodyType>::type>::GroundType>(slice))
398
- )), ...);
399
- #endif
425
+ auto factPtr = get<BodyRelationI const *>(slice);
426
+ const BodyRelationI& fact = *factPtr;
427
+ // get the atom
428
+ typename BodyRelationI::Atom &atom = get<I>(atoms);
429
+ // try to bind the atom with the fact
430
+ bind (atom, fact);
400
431
}
401
432
402
433
template <typename RULE_TYPE, typename ... RELATIONs, size_t ... Is>
403
- static void bind (const typename RULE_TYPE::BodyType &atoms,
434
+ static void bind (typename RULE_TYPE::BodyType &atoms,
404
435
const tuple<RELATIONs const *...> &slice, index_sequence<Is...>)
405
436
{
406
437
((bind<Is, RULE_TYPE, RELATIONs...>(atoms, slice)), ...);
407
438
}
408
439
409
440
// bind n atoms (of multiple relation types) with 1 slice (of multiple relation types)
410
441
template <typename RULE_TYPE, typename ... RELATIONs>
411
- static bool bind (const typename RULE_TYPE::BodyType &atoms,
442
+ static bool bind (typename RULE_TYPE::BodyType &atoms,
412
443
const tuple<RELATIONs const *...> &slice)
413
444
{
414
445
// unbind all the symbols
@@ -421,7 +452,7 @@ static bool bind(const typename RULE_TYPE::BodyType &atoms,
421
452
422
453
// apply a rule to state
423
454
template <typename RULE_TYPE, typename ... RELATIONs>
424
- static void apply (const RULE_TYPE &rule, const State<RELATIONs...> &state)
455
+ static void apply (RULE_TYPE &rule, const State<RELATIONs...> &state)
425
456
{
426
457
auto it = state.iterator ();
427
458
while (it.hasNext ())
0 commit comments