[pypy-svn] r21790 - in pypy/dist/pypy/lib/pyontology: . test

ale at codespeak.net ale at codespeak.net
Sat Jan 7 18:27:07 CET 2006


Author: ale
Date: Sat Jan  7 18:27:06 2006
New Revision: 21790

Modified:
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
Log:
Added support for rdf:List using rdf:first and rdf:rest

Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Sat Jan  7 18:27:06 2006
@@ -67,8 +67,10 @@
         self.values = values
 
 class List(ClassDomain):
-    pass
-
+    def __init__(self, name='', values=[], bases = []):
+        ClassDomain.__init__(self, name, values, bases)
+        self.constraint = ListConstraint(name)
+        
 class Property(ClassDomain):
     # Property contains the relationship between a class instance and a value
     # - a pair. To accomodate global assertions like 'range' and 'domain' attributes
@@ -217,8 +219,8 @@
         return Solver().solve(rep, verbose)
 
     def consistency(self, verbose=0):
-        rep = Repository(self.variables.keys(), self.variables, self.constraints)
-        rep.consistency(verbose)
+        self.rep = Repository(self.variables.keys(), self.variables, self.constraints[:])
+        self.rep.consistency(verbose)
  
     def get_list(self, subject):
         res = []
@@ -244,7 +246,9 @@
             if ns not in uris.keys():
                 uris[ns] = ns.split('/')[-1]
             a = uris[ns] + '_' + name    
-        var = str(a.replace('-','_'))
+            var = str(a.replace('-','_'))
+        else:
+            var = a
         if not cls:
             return var
         if not var in self.variables.keys():
@@ -301,10 +305,23 @@
                 self.constraints.append(cls.constraint)
 
     def first(self, s, var):
-        pass
+        avar = self.make_var(None, var)
+        svar = self.make_var(List, s)
+        vals = []
+        vals += self.variables[svar].getValues()
+        vals.insert(0, avar)
+        self.variables[svar].setValues(vals)
 
     def rest(self, s, var):
-        pass
+        if var == URIRef(namespaces['rdf']+'#nil'):
+            return 
+        else:
+            avar = self.make_var(List, var)
+        svar = self.make_var(List, s)
+        vals = []
+        vals += self.variables[svar].getValues()
+        vals.append( avar)
+        self.variables[svar].setValues(vals)
 
     def onProperty(self, s, var):
         pass
@@ -473,12 +490,12 @@
         return self.__cost
 
 
-class MaxCardinality(OwlConstraint):
+class MaxCardinality(AbstractConstraint):
     """Contraint: all values must be distinct"""
 
     def __init__(self, variable, cls, cardinality):
-        OwlConstraint.__init__(self, variable)
-        self.__cost = 1
+        AbstractConstraint.__init__(self, [variable, cls])
+        self.__cost = 2
         self.cardinality = cardinality
         self.cls = cls
 
@@ -526,10 +543,10 @@
             res.extend(get_values(domains[val], domains, attr))
     return res
 
-class SubClassConstraint(OwlConstraint):
+class SubClassConstraint(AbstractConstraint):
 
     def __init__(self, variable, cls_or_restriction):
-        OwlConstraint.__init__(self, variable)
+        AbstractConstraint.__init__(self, [variable, cls_or_restriction])
         self.object = cls_or_restriction
         self.variable = variable
 
@@ -569,7 +586,6 @@
         propdom = domains[self.variable]
         rangedom = domains[self.object]
         newrange = get_values(rangedom, domains, 'getValues')  
-        print rangedom
         range = []
         oldrange = propdom.range
         if oldrange:
@@ -702,8 +718,28 @@
         for cls, val in sub_domain:
             if not (val,cls) in obj_domain:
                 raise ConsistencyFailure("Inverseof failed") 
-##            res.append((val, cls))
-##        domains[self.variable].setValues(res)
+
+class ListConstraint(AbstractConstraint):
+    """Contraint: all values must be distinct"""
+    def __init__(self, variable):
+        AbstractConstraint.__init__(self, [variable])
+        self.variable = variable
+        self.__cost = 10
+
+    def estimateCost(self, domains):
+        return self.__cost
+
+    def narrow(self, domains):
+        """narrowing algorithm for the constraint"""
+        vals =[]
+        vals += domains[self.variable].getValues()
+        while True:
+            if vals[-1] in domains.keys() and isinstance(domains[vals[-1]], List):
+                vals = vals[:-1] + domains[vals[-1]].getValues()
+            else:
+                break
+        domains[self.variable].setValues(vals)
+        return 1
 
 class HasvalueConstraint:
-    pass
\ No newline at end of file
+    pass

Modified: pypy/dist/pypy/lib/pyontology/test/test_ontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/test_ontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/test/test_ontology.py	Sat Jan  7 18:27:06 2006
@@ -317,3 +317,18 @@
     #Make class
     O.variables['owner_'].setValues([('Fiat_', 42)])
     O.consistency()
+
+def test_List():
+    O = Ontology()
+    own = URIRef('favlist')
+    obj = URIRef(namespaces['rdf']+'#List')
+    O.type(own, obj)
+    O.first(own, 0)
+    O.rest(own,  URIRef('1'))
+    O.first( URIRef('1'), 1)
+    O.rest( URIRef('1'),  URIRef('2'))
+    O.first( URIRef('2'), 2)
+    O.rest( URIRef('2'),  URIRef(namespaces['rdf']+'#nil'))
+    assert len(O.constraints) == 1
+    O.consistency(5)
+    assert O.rep._domains['favlist_'].getValues() == [0,1,2]
\ No newline at end of file



More information about the Pypy-commit mailing list