Search code examples
logicblox

Sets in Logicblox


Is it possible to model sets in Logicblox. Specifically, I want to have a set of Integers, say {1,4,9}, and would like to check if the set is an empty-set, and if some element belongs to the set.


Solution

  • Predicates actually naturally model set so if you want to model one set you can create a predicate for it as in this example:

    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
    

    If you want to model sets as entities all it takes is an extra key:

    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