0

是否可以在 Logicblox 中对集合进行建模。具体来说,我想要一个set of Integers,比如说{1,4,9},并想检查该集合是否为空集,以及某个元素是否属于该集合。

4

1 回答 1

1

谓词实际上自然地为集合建模,所以如果你想为一个集合建模,你可以为它创建一个谓词,如下例所示:

create --unique
addblock <doc>
  my_set(x) -> int(x).
  my_set_not_empty() <- my_set(_).
  my_set_empty() <- !my_set_not_empty().
</doc>
echo 'my_set'
print my_set
echo 'my_set_empty'
print my_set_empty
echo 'my_set_not_empty'
print my_set_not_empty
echo 'adding an element'
exec '+my_set(1).'
echo 'my_set'
print my_set
echo 'my_set_empty'
print my_set_empty
echo 'my_set_not_empty'
print my_set_not_empty
close --destroy

如果你想将集合建模为实体,只需要一个额外的键:

create --unique
addblock <doc>
  integer_set(set),integer_set:name(set:name) -> string(name).
  contains(set,integer) -> integer_set(set),int(integer).
  not_empty(set) <- contains(set,_).
  empty(set) <- !not_empty(set), integer_set(set).
</doc>
exec <doc>
  +integer_set(my_set),+integer_set:name(my_set:"my_set").
</doc>
echo "empty"
print empty
echo "not empty"
print not_empty
echo "adding elements and creating another set"
exec <doc>
  +integer_set(my_set),+integer_set:name(my_set:"other_set").
  +contains(my_set,1) <- integer_set:name(my_set:"my_set").
  +contains(my_set,4) <- integer_set:name(my_set:"my_set").
  +contains(my_set,9) <- integer_set:name(my_set:"my_set").
</doc>
echo "contains"
print contains
echo "empty"
print empty
echo "not empty"
print not_empty

close --destroy
于 2016-12-30T16:23:59.760 回答