[pypy-svn] r27617 - pypy/dist/pypy/lib/pyontology

ale at codespeak.net ale at codespeak.net
Tue May 23 11:50:09 CEST 2006


Author: ale
Date: Tue May 23 11:50:08 2006
New Revision: 27617

Modified:
   pypy/dist/pypy/lib/pyontology/constraint_classes.py
   pypy/dist/pypy/lib/pyontology/pyontology.py
Log:
bugfixes and simplifications

Modified: pypy/dist/pypy/lib/pyontology/constraint_classes.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/constraint_classes.py	(original)
+++ pypy/dist/pypy/lib/pyontology/constraint_classes.py	Tue May 23 11:50:08 2006
@@ -23,61 +23,17 @@
            card = 0
         return card 
 
-class MaxCardinality(AbstractConstraint):
-    """Contraint: all values must be distinct"""
-
-    def __init__(self, variable, cardinality):
-        AbstractConstraint.__init__(self, [variable])
-        self.cost = 80
-        self.variable = variable
-        self.cardinality = cardinality
+class CardinalityConstraint(AbstractConstraint):
 
-    def __repr__(self):
-        return '<%s  %s %i>' % (self.__class__.__name__, str(self._variables[0]), self.cardinality)
+    def __init__(self, prop, cls_name, var, comp):
+        AbstractConstraint.__init__(self, [prop])
+        self.check_individual = "domains['%s'].getValues() != []" % prop
+        self.formula = "len(domains['%s'].getValuesPrKey('%s')) %s int(%s)"% (prop, cls_name, comp, var)
 
-    def estimateCost(self, domains):
-        return self.cost
-    
     def narrow(self, domains):
-        """narrowing algorithm for the constraint"""
-        prop = domains[self.variable].property
-        props = Linkeddict(domains[prop].getValues())
-        dom = domains[self.variable].getValues()
-        if not dom:
-            return 0
-        cls = dom[0]
-        card = get_cardinality(props, cls)
-        if card > self.cardinality:
-            raise ConsistencyFailure("Maxcardinality of %i exceeded by the value %i" %(self.cardinality,len(props[cls])))
-        else:
-            return 1
-
-class MinCardinality(MaxCardinality):
-
-    def narrow(self, domains):
-        """narrowing algorithm for the constraint"""
-        prop = domains[self.variable].property
-        props = Linkeddict(domains[prop].getValues())
-        cls = domains[self.variable].getValues()[0]
-        card = get_cardinality(props, cls)
-        if card < self.cardinality:
-            raise ConsistencyFailure("MinCardinality of %i not achieved by the value %i" %(self.cardinality,len(props[cls])))
-        else:
-            return 1
-        
-class Cardinality(MaxCardinality):
-    
-    def narrow(self, domains):
-        """narrowing algorithm for the constraint"""
-        prop = domains[self.variable].property
-        props = Linkeddict(domains[prop].getValues())
-        cls = domains[self.variable].getValues()[0]
-        card = get_cardinality(props, cls)
-        if card != self.cardinality:
-            raise ConsistencyFailure("Cardinality of %i exceeded by the value %r for %r" %
-                                     (self.cardinality, props[cls], prop))
-        else:
-            return 1
+        if eval(self.check_individual):
+            if not eval(self.formula):
+                raise ConsistencyFailure
 
 class SubClassConstraint(AbstractConstraint):
 

Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Tue May 23 11:50:08 2006
@@ -312,7 +312,8 @@
         else:
             avar = self.make_var(Property, p)
             # Set the values of the property p to o
-            sub = self.make_var(ClassDomain, s)
+            self.type(s, Thing_uri)
+            sub = self.make_var(Thing, s)
             obj = self.make_var(Thing, o)
             propdom = self.variables[avar]
             res = propdom.addValue(sub, obj)
@@ -328,8 +329,8 @@
             self.consider_triple(triple)
 
     def get_individuals_of(self, item):
-        item_as_subject = self.graph.triples(( None, rdf_type, item))
-        for triple in item_as_subject:
+        item_as_object = self.graph.triples(( None, rdf_type, item))
+        for triple in item_as_object:
             self.consider_triple(triple)
 
     def make_var(self, cls=fd, a=''):
@@ -562,6 +563,7 @@
     
     def subPropertyOf(self, s, var):
         # s is a subproperty of var
+        self.resolve_item(var)
         avar = self.make_var(Property, var)
         svar = self.make_var(Property, s)
         avals = self.variables[avar].getValues()
@@ -599,16 +601,12 @@
         self.resolve_item(s)
         svar =self.make_var(Restriction, s)
         cls = list(self.graph.subjects(None,s))[0]
+        self.resolve_item(cls)
         cls_name = self.make_var(ClassDomain, cls)
         prop = self.variables[svar].property
         self.variables[svar].TBox[prop] = {'Cardinality': [( '<', int(var))]}
-        formula = "len(%s[1]) < int(%s)" %(prop, var)
-        constrain = Expression([prop], formula)
-        self.constraints.append(constrain)
 
-        for cls,vals in self.variables[prop].getValuesPrKey():
-            if len(vals) < int(var):
-                self.variables[svar].addValue(cls)
+        self.constraints.append(CardinalityConstraint(prop, cls_name, var, '<='))
 
     def minCardinality(self, s, var):
         """ Len of finite domain of the property shall be greater than or equal to var"""
@@ -618,10 +616,8 @@
         cls_name = self.make_var(ClassDomain, cls)
         prop = self.variables[svar].property
         self.variables[svar].TBox[prop] = {'Cardinality': [( '>', int(var))]}
-        for cls,vals in self.variables[prop].getValuesPrKey():
-            if len(vals) > int(var):
-                self.variables[svar].addValue(cls)
 
+        self.constraints.append(CardinalityConstraint(prop, cls_name, var, '>='))
     
     def cardinality(self, s, var):
         """ Len of finite domain of the property shall be equal to var"""
@@ -631,10 +627,8 @@
         cls_name = self.make_var(ClassDomain, cls)
         prop = self.variables[svar].property
         self.variables[svar].TBox[prop] = {'Cardinality': [( '=', int(var))]}
-        for cls,vals in self.variables[prop].getValuesPrKey():
-            if len(vals) == int(var):
-                self.variables[svar].addValue(cls)
-
+        
+        self.constraints.append(CardinalityConstraint(prop, cls_name, var, '=='))
     
     def hasValue(self, s, var):
         self.resolve_item(s)



More information about the Pypy-commit mailing list