diff --git a/clorm/orm/noclingo.py b/clorm/orm/noclingo.py index b9e5fdc..59db02b 100644 --- a/clorm/orm/noclingo.py +++ b/clorm/orm/noclingo.py @@ -8,6 +8,7 @@ import functools import enum import clingo +from clingo import SymbolType __all__ = [ 'Function', @@ -28,20 +29,19 @@ 'get_symbol_generator' ] -class SymbolType(enum.IntEnum): - Infimum = 1 - Number = 2 - String = 3 - Function = 4 - Supremum = 5 - def __str__(self): - if self.Infimum: return "Infimum" - elif self.Number: return "Number" - elif self.String: return "String" - elif self.Function: return "Function" - return "Supremum" +# -------------------------------------------------------------------------------- +# Note: the ordering between symbols is manually determined to match clingo 5.5 +# -------------------------------------------------------------------------------- + +_SYMBOLTYPE_OID = { + SymbolType.Infimum: 1, + SymbolType.Number: 2, + SymbolType.Function: 3, + SymbolType.String: 4, + SymbolType.Supremum: 5 +} class Symbol(object): """A noclingo replacement for clingo.Symbol. @@ -155,7 +155,8 @@ def __ne__(self, other): def __gt__(self, other): """Overloaded boolean operator.""" if not isinstance(other, self.__class__): return NotImplemented - if self._stype != other._stype: return self._stype > other._stype + if self._stype != other._stype: + return _SYMBOLTYPE_OID[self._stype] > _SYMBOLTYPE_OID[other._stype] if self._stype == SymbolType.Infimum: return False if self._stype == SymbolType.Supremum: return False @@ -172,7 +173,8 @@ def __le__(self, other): def __lt__(self, other): """Overloaded boolean operator.""" if not isinstance(other, self.__class__): return NotImplemented - if self._stype != other._stype: return self._stype < other._stype + if self._stype != other._stype: + return _SYMBOLTYPE_OID[self._stype] < _SYMBOLTYPE_OID[other._stype] if self._stype == SymbolType.Infimum: return False if self._stype == SymbolType.Supremum: return False @@ -252,68 +254,43 @@ def noclingo_to_clingo(nclsym): tuple(noclingo_to_clingo(t) for t in nclsym.arguments), nclsym.positive) - -# ------------------------------------------------------------------------------ -# Internal function that converts the clingo.SymbolType to noclingo.SymbolType -# ------------------------------------------------------------------------------ -def _get_symboltype(sym): - if isinstance(sym,clingo.Symbol): - if sym.type == clingo.SymbolType.Number: return SymbolType.Number - elif sym.type == clingo.SymbolType.String: return SymbolType.String - elif sym.type == clingo.SymbolType.Function: return SymbolType.Function - elif sym.type == clingo.SymbolType.Infimum: return SymbolType.Infimum - elif sym.type == clingo.SymbolType.Supremum: return SymbolType.Supremum - else: - raise ValueError(("Internal Error: unrecognised SymbolType for " - "'{}'").format(sym)) - if isinstance(sym, Symbol): return sym.type - raise TypeError("Object '{}' ({}) is not a Symbol".format(sym,type(sym))) - # ------------------------------------------------------------------------------ # Functions to test the type of Symbol irrespective of clingo.Symbol or # noclingo.Symbol. # ------------------------------------------------------------------------------ def is_Number(sym): try: - stype = _get_symboltype(sym) - return stype == SymbolType.Number + return sym.type == SymbolType.Number except: return False def is_String(sym): try: - stype = _get_symboltype(sym) - return stype == SymbolType.String + return sym.type == SymbolType.String except: return False def is_Function(sym): try: - stype = _get_symboltype(sym) - return stype == SymbolType.Function + return sym.type == SymbolType.Function except: return False def is_Supremum(sym): try: - stype = _get_symboltype(sym) - return stype == SymbolType.Supremum + return sym.type == SymbolType.Supremum except: return False def is_Infimum(sym): try: - stype = _get_symboltype(sym) - return stype == SymbolType.Infimum + return sym.type == SymbolType.Infimum except: return False def is_Symbol(sym): - try: - stype = _get_symboltype(sym) - return True - except: - return False + return (isinstance(sym, clingo.Symbol) or + isinstance(sym, noclingo.Symbol)) #------------------------------------------------------------------------------ # A mechanism to group together the symbol generator functions for clingo or diff --git a/clorm/orm/symbols_facts.py b/clorm/orm/symbols_facts.py index b956968..30aad94 100644 --- a/clorm/orm/symbols_facts.py +++ b/clorm/orm/symbols_facts.py @@ -16,7 +16,7 @@ import clingo import clingo.ast as clast from .core import * -from .noclingo import is_Function, is_Number, SymbolMode +from .noclingo import SymbolMode from .factbase import * from .core import get_field_definition, PredicatePath, kwargs_check_keys, \ get_symbol_mode diff --git a/tests/test_orm_noclingo.py b/tests/test_orm_noclingo.py index 8929e23..541d3dc 100644 --- a/tests/test_orm_noclingo.py +++ b/tests/test_orm_noclingo.py @@ -10,7 +10,7 @@ import clingo import clorm.orm.noclingo as noclingo from clorm.orm.noclingo import clingo_to_noclingo, noclingo_to_clingo, \ - SymbolMode, get_symbol_generator, _get_symboltype, \ + SymbolMode, get_symbol_generator,\ is_Number, is_String, is_Function, is_Supremum, is_Infimum clingo_version = clingo.__version__ @@ -34,34 +34,6 @@ def tearDown(self): pass - #-------------------------------------------------------------------------- - # - #-------------------------------------------------------------------------- - def test_symboltype_value_and_order(self): - self.assertTrue(str(clingo.SymbolType.Number), str(noclingo.SymbolType.Number)) - self.assertTrue(str(clingo.SymbolType.String), str(noclingo.SymbolType.String)) - self.assertTrue(str(clingo.SymbolType.Function), str(noclingo.SymbolType.Function)) - self.assertTrue(str(clingo.SymbolType.Supremum), str(noclingo.SymbolType.Supremum)) - self.assertTrue(str(clingo.SymbolType.Infimum), str(noclingo.SymbolType.Infimum)) - - # Note: ordering comparision between SymbolTypes is not supported in 5.5.0+ - if clingo_version >= "5.5.0": return - - if clingo.SymbolType.Number < clingo.SymbolType.String: - self.assertTrue(noclingo.SymbolType.Number < noclingo.SymbolType.String) - else: - self.assertTrue(noclingo.SymbolType.Number > noclingo.SymbolType.String) - - if clingo.SymbolType.Number < clingo.SymbolType.Function: - self.assertTrue(noclingo.SymbolType.Number < noclingo.SymbolType.Function) - else: - self.assertTrue(noclingo.SymbolType.Number > noclingo.SymbolType.Function) - - if clingo.SymbolType.String < clingo.SymbolType.Function: - self.assertTrue(noclingo.SymbolType.String < noclingo.SymbolType.Function) - else: - self.assertTrue(noclingo.SymbolType.String > noclingo.SymbolType.Function) - #-------------------------------------------------------------------------- # #-------------------------------------------------------------------------- @@ -221,10 +193,28 @@ def test_comparison_ops(self): nc7 = noclingo.Function("abc",[noclingo.String("45"), noclingo.Number(5)]) self.assertTrue(nc6 < nc7) - if noclingo.SymbolType.Number < noclingo.SymbolType.String: - self.assertTrue(nc1 < nc3) - else: - self.assertTrue(nc1 > nc3) + def compare_ordering(a, b): + if noclingo_to_clingo(a) < noclingo_to_clingo(b): + self.assertTrue(a < b) + elif noclingo_to_clingo(b) < noclingo_to_clingo(a): + self.assertTrue(b < a) + else: + self.assertEqual(a, b) + + compare_ordering(noclingo.String("1"), noclingo.Number(2)) + compare_ordering(noclingo.Number(1), noclingo.String("2")) + compare_ordering(noclingo.String("1"), noclingo.Function("2")) + compare_ordering(noclingo.Function("2"), noclingo.String("1")) + compare_ordering(noclingo.Number(1), noclingo.Function("2")) + compare_ordering(noclingo.Function("1"), noclingo.Number(2)) + compare_ordering(noclingo.Infimum, noclingo.Supremum) + compare_ordering(noclingo.Supremum, noclingo.Infimum) + compare_ordering(noclingo.Infimum, noclingo.Number(2)) + compare_ordering(noclingo.Infimum, noclingo.String("2")) + compare_ordering(noclingo.Infimum, noclingo.Function("2")) + compare_ordering(noclingo.Supremum, noclingo.Function("2")) + compare_ordering(noclingo.Supremum, noclingo.String("2")) + compare_ordering(noclingo.Supremum, noclingo.Number(2)) def test_clingo_noclingo_difference(self): self.assertNotEqual(clingo.String("blah"), noclingo.String("blah")) @@ -271,46 +261,6 @@ def test_clingo_to_noclingo(self): self.assertEqual(clingo_to_noclingo(cl5),ncl5) self.assertEqual(noclingo_to_clingo(ncl5),cl5) - - def test_get_symboltype(self): - cli = clingo.Infimum - cls = clingo.Supremum - cl1 = clingo.Function("const") - cl2 = clingo.Number(3) - cl3 = clingo.String("No") - cl4 = clingo.Function("",[cl1,cl2]) - - ncli = noclingo.Infimum - ncls = noclingo.Supremum - ncl1 = noclingo.Function("const") - ncl2 = noclingo.Number(3) - ncl3 = noclingo.String("No") - ncl4 = noclingo.Function("",[ncl1,ncl2]) - - self.assertEqual(_get_symboltype(cli),noclingo.SymbolType.Infimum) - self.assertEqual(_get_symboltype(cls),noclingo.SymbolType.Supremum) - self.assertEqual(_get_symboltype(cl1),noclingo.SymbolType.Function) - self.assertEqual(_get_symboltype(cl2),noclingo.SymbolType.Number) - self.assertEqual(_get_symboltype(cl3),noclingo.SymbolType.String) - self.assertEqual(_get_symboltype(cl4),noclingo.SymbolType.Function) - - self.assertEqual(_get_symboltype(ncli),noclingo.SymbolType.Infimum) - self.assertEqual(_get_symboltype(ncls),noclingo.SymbolType.Supremum) - self.assertEqual(_get_symboltype(ncl1),noclingo.SymbolType.Function) - self.assertEqual(_get_symboltype(ncl2),noclingo.SymbolType.Number) - self.assertEqual(_get_symboltype(ncl3),noclingo.SymbolType.String) - self.assertEqual(_get_symboltype(ncl4),noclingo.SymbolType.Function) - - self.assertNotEqual(_get_symboltype(cls),noclingo.SymbolType.Infimum) - self.assertNotEqual(_get_symboltype(cli),noclingo.SymbolType.Supremum) - self.assertNotEqual(_get_symboltype(cl2),noclingo.SymbolType.Function) - self.assertNotEqual(_get_symboltype(cl4),noclingo.SymbolType.Number) - self.assertNotEqual(_get_symboltype(cl4),noclingo.SymbolType.String) - self.assertNotEqual(_get_symboltype(cl3),noclingo.SymbolType.Function) - - with self.assertRaises(TypeError) as ctx: - x=_get_symboltype(4) - def test_is_functions(self): cli = clingo.Infimum cls = clingo.Supremum