potassco / clorm

🗃️ A Python ORM-like interface for the Clingo Answer Set Programming (ASP) reasoner
https://clorm.readthedocs.io
MIT License
52 stars 5 forks source link

combine_fields cannot be used to combine ComplexTerm subclasses #103

Closed namcsi closed 2 years ago

namcsi commented 2 years ago

In the clorm documentation, under Predicates and Fields -> Combining Field Definitions, there is a code snippet demonstrating how to use combine_fields to combine several ComplexTerm subclasses:

from clorm import Predicate, ComplexTerm, IntegerField, ConstantField combine_fields

class Light(ComplexTerm):
   status=ConstantField

class RobotLocation(ComplexTerm):
   robot=ConstantField
   location=ConstantField
   class Meta: name = "robotlocation"

class True(Predicate):
   fluent = combine_fields([Light,RobotLocation])
   time = IntegerField

However, after correcting some syntax errors, the code snippet still throws a TypeError:

Traceback (most recent call last):
  File "/home/amade/src/cogsys/cavas/semplan/encoding/temp.py", line 11, in <module>
    class _True(Predicate):
  File "/home/amade/src/cogsys/cavas/semplan/encoding/temp.py", line 12, in _True
    fluent = combine_fields([Light, RobotLocation])
  File "/home/amade/.conda/envs/semplan/lib/python3.9/site-packages/clorm/orm/core.py", line 1696, in combine_fields
    raise TypeError("{} is not BaseField or a sub-class".format(f))
TypeError: <class '__main__.Light'> is not BaseField or a sub-class

My clorm version is 1.3.8. Is the documentation out of date, is this a bug, or am I severely misunderstanding something?:)

daveraja commented 2 years ago

Thanks for finding this. It is a bug in the documentation.

Light and RobotLocation are not fields but they do have an associated field class that can be accessed with the .Field class property. So the True predicate definition should look like:

class True(Predicate):
     fluent = combine_fields([Light.Field, RobotLocation.Field])
     time = IntegerField

I will update the documentation.

daveraja commented 2 years ago

Fixed with commit https://github.com/potassco/clorm/commit/3569a91daa1d691f0a7f5a9534db925e027cdbf9