在 Logicblox 中设置

Sets in Logicblox

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

谓词实际上自然而然地为模型集建模,因此如果您想为一组模型建模,您可以为其创建一个谓词,如本例所示:

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