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

ale at codespeak.net ale at codespeak.net
Thu Dec 7 08:01:11 CET 2006


Author: ale
Date: Thu Dec  7 08:01:04 2006
New Revision: 35413

Modified:
   pypy/dist/pypy/lib/pyontology/constraint_classes.py
   pypy/dist/pypy/lib/pyontology/pyontology.py
   pypy/dist/pypy/lib/pyontology/sparql_grammar.py
   pypy/dist/pypy/lib/pyontology/test/test_ontology.py
   pypy/dist/pypy/lib/pyontology/test/test_sparql.py
   pypy/dist/pypy/lib/pyontology/test/testont.rdf
   pypy/dist/pypy/lib/pyontology/test/testont2.rdf
Log:
Bugfixes, different approach to xml types

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	Thu Dec  7 08:01:04 2006
@@ -248,7 +248,6 @@
                     keep.append(key)
         sub.removeValues([v for v in sub.getValues() if not v in keep])
 
-import time
 class PropertyConstrain3(AbstractConstraint):
     cost = 1
     def __init__(self, prop, variable, cls_or_restriction):
@@ -345,14 +344,15 @@
 
 class EquivalentPropertyConstraint(SubClassConstraint):
 
-    cost = 100
+    cost = 10
     
     def narrow(self, domains):
         subdom = domains[self.variable]
         superdom = domains[self.object]
-        for value in subdom.getValues():
-            if not value in superdom:
-                superdom.addValue(value[0], value[1])
+        superset = set(superdom.getValues())
+        superset = superset.union(set(subdom.getValues()))
+        superdom.setValues(superset)
+        subdom.setValues(superset)
 
 class TypeConstraint(SubClassConstraint):
     cost = 1
@@ -367,6 +367,7 @@
 
 class FunctionalCardinality(OwlConstraint):
     """Contraint: all values must be distinct"""
+    cost = 100
 
     def narrow(self, domains):
         """narrowing algorithm for the constraint"""

Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py	(original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py	Thu Dec  7 08:01:04 2006
@@ -425,8 +425,10 @@
 def Types(typ):
     class Type(Literal):
         def __contains__(self, item):
-            #assert isinstance(item, rdflib_literal)
-            return item.datatype is None or item.datatype == self.Type
+            if isinstance(item, rdflib_literal):
+                return item.datatype is None or item.datatype == self.Type
+            else:
+                return XMLTypes[self.Type.split("#")[-1]] == type(item)
 
     datatype = Type
     datatype.Type = typ
@@ -455,9 +457,9 @@
                getUriref('rdf', 'Literal') : Literal,
 #               getUriref('rdf', 'type') : Property,
               }
-XMLTypes = ['string', 'float', 'integer', 'date']
-#XMLTypes = {'string': str, 'float': float, 'integer': int, 
-#            'date': lambda x: datetime.date(*[int(v) for v in x.split('-')])}
+#XMLTypes = ['string', 'float', 'integer', 'date']
+XMLTypes = {'string': str, 'float': float, 'integer': int, 
+            'date': lambda x: datetime.date(*[int(v) for v in x.split('-')])}
 
 for typ in XMLTypes:
     uri = getUriref('xmlschema', typ)
@@ -546,7 +548,11 @@
             trip_ = [trip.Subject[0], trip.Verb[0], trip.Object[0]]
             for item in trip_:
                 if isinstance(item[0], rdflib_literal):
-                    newtrip.append(item[0])
+                    o = item[0]
+                    if o.datatype in builtin_voc:
+                        o = XMLTypes[o.datatype.split('#')[-1]](o)
+                    self.variables['owl_Literal'].addValue(o)
+                    newtrip.append(o)
                 elif item[0].NCNAME_PREFIX:
                     uri = prefixes[item[0].NCNAME_PREFIX[0]] + item[0].NCNAME[0]
                     newtrip.append(URIRef(uri))
@@ -706,9 +712,9 @@
                    val = v.uri
                else:
                    val = v 
-               d[k] = unicode(val)
+               d[k] = (val)
                if k in query_vars:
-                   res_dict[query_vars[k]] = unicode(val)
+                   res_dict[query_vars[k]] = (val)
            res.append(res_dict)
         return res
     
@@ -743,10 +749,9 @@
             else:
                 val = Individual(obj, o)
         elif type(o) == rdflib_literal:
-            print "Literal type", repr(o.datatype)
+#            self.variables.setdefault('owl_Literal', ClassDomain('owl_Literal',u''))
             if o.datatype in builtin_voc:
-                print "XML datatype"
-                o = XMLTypes[o.datatype.split('#')[-1]](o)
+               o = XMLTypes[o.datatype.split('#')[-1]](o)
             self.variables['owl_Literal'].addValue(o)
             val = o
         else:

Modified: pypy/dist/pypy/lib/pyontology/sparql_grammar.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/sparql_grammar.py	(original)
+++ pypy/dist/pypy/lib/pyontology/sparql_grammar.py	Thu Dec  7 08:01:04 2006
@@ -24,7 +24,7 @@
     return o
 
 def replace_int(s, loc, toks):
-    return [rdfliteral(int(toks[0]), datatype="http://www.w3.org/2001/XMLSchema#int")]
+    return [rdfliteral(int(toks[0]), datatype="http://www.w3.org/2001/XMLSchema#integer")]
 
 
 def replace_string(s, loc, toks):
@@ -590,5 +590,7 @@
 
     # NCNAME ::= ( "_" | NCCHAR1 ) ((NCCHAR|".")* NCCHAR)?
 
-    NCNAME << ("_" | NCCHAR1)
+    NCNAME << ("_" | NCCHAR1) + Optional(ZeroOrMore(NCCHAR | ".") + NCCHAR)
+
+
 

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	Thu Dec  7 08:01:04 2006
@@ -35,7 +35,6 @@
     raises(ConsistencyFailure, O.consistency)
 
 def test_XMLSchema_string():
-    py.test.skip("WIP")
     O = Ontology()
     a = URIRef(u'A')
     p = URIRef(u'P')
@@ -47,7 +46,7 @@
     O.consistency()
 
 def test_XMLSchema_string_fail():
-    py.test.skip("WIP")
+#    py.test.skip("WIP")
     O = Ontology()
     a = URIRef(u'A')
     p = URIRef(u'P')

Modified: pypy/dist/pypy/lib/pyontology/test/test_sparql.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/test_sparql.py	(original)
+++ pypy/dist/pypy/lib/pyontology/test/test_sparql.py	Thu Dec  7 08:01:04 2006
@@ -118,7 +118,7 @@
     O.attach_fd()
     O.finish()
     res = O.sparql(query)
-    assert res[0]['x'] == '123'
+    assert res[0]['x'] == 123
 
 def test_case_4():
     """ search for s in p """
@@ -145,7 +145,7 @@
 
     res = O.sparql(query)
     assert res[0]['x'] == u'http://example.org/ns#sub' 
-    assert res[0]['y'] == u'123' 
+    assert res[0]['y'] == 123 
     assert res[0]['x'] == u'http://example.org/ns#sub' 
 
 def test_case_6():
@@ -164,7 +164,6 @@
 
 def test_case_7():
     """ for all p's return p[1] if p[0]==s """
-    #py.test.skip("Doesn't work yet due to changed generatorinterface")
 
     query = qt_proto % ('?x ?y ?z', '?x ?y ?z .')
     O = Ontology()
@@ -174,7 +173,16 @@
     res = O.sparql(query)
     assert list(O.variables['query_x_'].getValues())[0].uri == u'http://example.org/ns#sub' 
     assert res[0]['x'] == u'http://example.org/ns#sub' 
- 
+
+def test_filter():
+    query = qt_proto % ('?x ?y', '?x ns:p ?y  .\n FILTER(?y < 124) .')
+    O = Ontology()
+    O.add_file("testont.rdf")
+    O.attach_fd()
+    O.finish()
+    res = O.sparql(query)
+    assert res[0]['y'] == 123
+    
 query1 = """
         PREFIX ltw : <http://www.lt-world.org/ltw.owl#>
         PREFIX owl : <http://www.w3.org/2002/07/owl#>
@@ -190,13 +198,12 @@
 query2 = """
         PREFIX ltw : <http://www.lt-world.org/ltw.owl#>
         PREFIX owl : <http://www.w3.org/2002/07/owl#>
-        SELECT ?project
+        SELECT ?project ?date_begin
                 WHERE {
                         ?project ltw:funded_by ltw:BMBF .
                         ?project ltw:dateStart ?date_begin .
                         ?project ltw:dateEnd ?date_end .
-                        FILTER ( ?date_begin  < 2007 ) .
-                FILTER ( ?date_end >= 2006) .
+                        FILTER ( ?date_begin  < 2007  &&  ?date_end >= 2006) .
                                 }"""
 #which project is funded in a technological area (i.e. Semantic web),
 query3 = """
@@ -220,7 +227,7 @@
     assert res[0]['person'] == u'\nKlara Vicsi' 
 
 def test_query2():
-    py.test.skip("Doesn't work yet")
+#    py.test.skip("Doesn't work yet")
     O = Ontology()
     O.add_file("testont2.rdf")
     O.attach_fd()
@@ -228,8 +235,20 @@
     res = O.sparql(query2)
     assert len(res) == 1
     assert res[0]['activity'] == u'http://www.lt-world.org/ltw.owl#obj_59754' 
+    assert res[0]['person'] == u'\nKlara Vicsi'
+
+def test_query3():
+    #py.test.skip("Doesn't work yet")
+    O = Ontology()
+    O.add_file("testont2.rdf")
+    O.attach_fd()
+
+    res = O.sparql(query3)
+    assert len(res) == 1
+    assert res[0]['activity'] == u'http://www.lt-world.org/ltw.owl#obj_59754' 
     assert res[0]['person'] == u'\nKlara Vicsi' 
 
+
 import xmlrpclib, socket, os, signal
 
 class TestXMLRPC:
@@ -258,4 +277,6 @@
         print "test_xmlrpc"
         server = xmlrpclib.ServerProxy("http://localhost:9000", allow_none=True)
         result = server.sparql(qt_proto % ('?x', 'ns:sub ns:p ?x .'))
-        assert result[0]['x'] == '123'
+        result2 = server.sparql(qt_proto % ('?x', 'ns:test ns:p ?x .'))
+        print "Result 2",result2
+        assert result[0]['x'] == 123

Modified: pypy/dist/pypy/lib/pyontology/test/testont.rdf
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/testont.rdf	(original)
+++ pypy/dist/pypy/lib/pyontology/test/testont.rdf	Thu Dec  7 08:01:04 2006
@@ -11,7 +11,7 @@
       </owl:Class>
     <owl:Thing rdf:about="http://example.org/ns#sub">
         <ns:p rdf:datatype=
-        "http://www.w3.org/2001/XMLSchema#int">123</ns:p>
+        "http://www.w3.org/2001/XMLSchema#integer">123</ns:p>
     </owl:Thing>
     <owl:ObjectProperty rdf:about="http://example.org/ns#p" />
   </rdf:RDF>

Modified: pypy/dist/pypy/lib/pyontology/test/testont2.rdf
==============================================================================
--- pypy/dist/pypy/lib/pyontology/test/testont2.rdf	(original)
+++ pypy/dist/pypy/lib/pyontology/test/testont2.rdf	Thu Dec  7 08:01:04 2006
@@ -32,9 +32,9 @@
 joseph.dichy at univ-lyon2.fr</contact>
 <dc_keyword>
 NLP!Natural Language Processing!MT</dc_keyword>
-<dateEnd>
+<dateEnd rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
 2000-08-01</dateEnd>
-<dateStart>
+<dateStart rdf:datatype="http://www.w3.org/2001/XMLSchema#date">
 1998-02-01</dateStart>
 <dc_source>
 http://www.hltcentral.org/projects</dc_source>



More information about the Pypy-commit mailing list