[pypy-svn] r32817 - in pypy/dist/pypy/lib/pyontology: . test
ale at codespeak.net
ale at codespeak.net
Mon Oct 2 14:27:05 CEST 2006
Author: ale
Date: Mon Oct 2 14:27:03 2006
New Revision: 32817
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_sparql.py
Log:
Some progress in adding sparql queries
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 Mon Oct 2 14:27:03 2006
@@ -199,7 +199,7 @@
for item in val:
for otheritem in val:
if (otheritem == item) == False:
- raise ConsistencyFailure("FunctionalCardinality error: %s for property %s" % (cls, self.variable))
+ raise ConsistencyFailure("FunctionalCardinality error: %s for property %s with %r" % (cls, self.variable, val))
else:
return 0
Modified: pypy/dist/pypy/lib/pyontology/pyontology.py
==============================================================================
--- pypy/dist/pypy/lib/pyontology/pyontology.py (original)
+++ pypy/dist/pypy/lib/pyontology/pyontology.py Mon Oct 2 14:27:03 2006
@@ -3,6 +3,7 @@
from logilab.constraint.fd import Expression, FiniteDomain as fd
from logilab.constraint.propagation import AbstractDomain, AbstractConstraint,\
ConsistencyFailure
+from pypy.lib.pyontology.sparql_grammar import SPARQLGrammar as SP
from constraint_classes import *
import sys, py
import datetime, time
@@ -40,10 +41,12 @@
def check_format(f):
if type(f) == str:
tmp = file(f, "r")
- else:
+ elif hasattr(f, 'open'):
tmp = f.open()
+ else:
+ tmp = f
start = tmp.read(10)
- tmp.close()
+ tmp.seek(0)
if "<" in start:
format = "xml"
else:
@@ -430,6 +433,7 @@
tmp.load(f, format=format)
for triple in tmp.triples((None,)*3):
self.add(triple)
+# self.consider_triple(triple)
def load_file(self, f, format=None):
if not format:
@@ -449,7 +453,106 @@
if isinstance( self.variables[key], fd):
continue
self.variables[key].finish(self.variables, self.constraints)
+# try:
constraint.narrow(self.variables)
+# except ConsistencyFailure, e:
+# print "FAilure", e
+
+ def _sparql(self, query):
+ qe = SP.Query.parseString(query)[0]
+
+ prefixes = qe.PrefixDecl[0]
+
+ resvars = []
+ for v in qe.SelectQuery[0].VARNAME:
+ resvars.append(v[0])
+
+ where = qe.SelectQuery[0].WhereClause[0]
+
+ triples = where.GroupGraphPattern[0].Triples
+# import pdb
+# pdb.set_trace()
+ new = []
+ for trip in triples:
+ case = 1
+ inc = 0
+ newtrip = []
+ trip = list(trip)
+ for item in trip:
+ if isinstance(item, Literal):
+ print "!!!!!!!",item
+ newtrip.append(item)
+ elif item.NCNAME_PREFIX:
+ uri = prefixes[item.NCNAME_PREFIX[0]] + item.NCNAME[0]
+ newtrip.append(URIRef(uri))
+ elif item.getName() == 'VAR1':
+ newtrip.append(URIRef('query_'+item[0]))
+ case += trip.index(item) + inc
+ print "Case calc", case, inc, list(trip).index(item),item
+ if inc:
+ inc = 1
+ else:
+ inc = 2
+ else:
+ newtrip.append(item[0])
+ newtrip.append(case)
+ new.append(newtrip)
+ constrain = where.GroupGraphPattern[0].Constraint
+ return new, prefixes, resvars, constrain
+
+# There are 8 ways of having the triples in the query, if predicate is not a builtin owl predicate
+#
+# case s p o
+#
+# 0 bound bound bound ; Check if this triple entails
+# 1 var bound bound ; add a hasvalue constraint
+# 2 bound var bound ; for all p's return p if p[0]==s and p[1]==o
+# 3 bound bound var ; search for s in p
+# 4 var var bound ; for all p's return p[0] if p[1]==o
+# 5 var bound var ; return the values of p
+# 6 bound var var ; for all p's return p[1] if p[0]==s
+# 7 var var var ; for all p's return p.getvalues
+#
+
+ def sparql(self, query):
+ new, prefixes, resvars, constrain = self._sparql(query)
+ for trip in new:
+ case = trip.pop(-1)
+ if case == 0:
+ # Check if this triple entails
+ self.consider_triple(trip)
+ elif case == 1:
+ # Add a HasValue constraint
+ var = self.make_var(Restriction, URIRef(trip[0]))
+ self.onProperty(var, URIRef(trip[1]))
+ self.hasValue(var, trip[2])
+ elif case == 2:
+ # for all p's return p if p[0]==s and p[1]==o
+ prop = self.make_var(Property, URIRef(trip[1]))
+ self.type(URIRef(trip[1]), namespaces['owl']+'Property')
+ elif case == 3:
+ # search for s in p
+ prop = self.make_var(None, trip[1])
+ p_vals = self.variables[prop].getValuesPrKey(self.make_var(trip[0]))
+ print "========",p_vals
+ p_vals = p_vals[0][1]
+ var = self.make_var(Thing, trip[2])
+ self.variables[var].setValues((p_vals))
+ elif case == 4:
+ # for all p's return p[0] if p[1]==o
+ pass
+ elif case == 5:
+ # return the values of p
+ pass
+ elif case == 6:
+ # for all p's return p[1] if p[0]==s
+ pass
+ elif case == 7:
+ # for all p's return p.getvalues
+ pass
+
+ self.consistency()
+
def consider_triple(self,(s, p, o)):
if (s, p, o) in self.seen:
@@ -622,8 +725,6 @@
# the extension of s it must be in the extension of
# var
- # what I really want is to just say s.bases.append(var)
-
log("%r subClassOf %r "%(s, var))
avar = self.make_var(ClassDomain, var)
svar = self.make_var(ClassDomain, s)
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 Mon Oct 2 14:27:03 2006
@@ -5,7 +5,7 @@
TokenConverter, Empty, Suppress, NoMatch, CharsNotIn, ParseResults
from pyparsing import Literal as ppLiteral # name Literal assigned by grammar
-
+from rdflib import Literal as rdfliteral
DEBUG = 0
def punctuation(lit, d=False):
@@ -23,6 +23,15 @@
if DEBUG or d: o.setDebug()
return o
+def replace_int(s, loc, toks):
+ return [rdfliteral(int(toks[0]), datatype="http://www.w3.org/2001/XMLSchema#int")]
+
+
+def replace_string(s, loc, toks):
+ return [rdfliteral((toks[0]), datatype="http://www.w3.org/2001/XMLSchema#String")]
+
+def replace_float(s, loc, toks):
+ return [rdfliteral(float(toks[0]), datatype="http://www.w3.org/2001/XMLSchema#float")]
class SPARQLGrammar(object):
@@ -166,7 +175,7 @@
NumericLiteral = production('NumericLiteral')
RDFLiteral = production('RDFLiteral')
BooleanLiteral = production('BooleanLiteral')
- String = production('String')
+ String = production('String').setParseAction(replace_string)
IRIref = production('IRIref')
QName = production('QName')
BlankNode = production('BlankNode')
@@ -177,10 +186,10 @@
VAR1 = production('VAR1')
VAR2 = production('VAR2')
LANGTAG = production('LANGTAG')
- INTEGER = production('INTEGER')
- DECIMAL = production('DECIMAL')
- FLOATING_POINT = production('FLOATING_POINT')
- EXPONENT = production('EXPONENT')
+ INTEGER = production('INTEGER').setParseAction(replace_int)
+ DECIMAL = production('DECIMAL').setParseAction(replace_float)
+ FLOATING_POINT = production('FLOATING_POINT').setParseAction(replace_float)
+ EXPONENT = production('EXPONENT').setParseAction(replace_float)
STRING_LITERAL1 = production('STRING_LITERAL1')
STRING_LITERAL2 = production('STRING_LITERAL2')
STRING_LITERAL_LONG1 = production('STRING_LITERAL_LONG1')
@@ -324,7 +333,7 @@
# ObjectList ::= Object ( ',' ObjectList )?
- ObjectList << Group(Object + Optional(comma + ObjectList))
+ ObjectList << (Object + Optional(comma + ObjectList))
# Verb ::= VarOrBlankNodeOrIRIref | 'a'
@@ -364,7 +373,7 @@
# Var ::= VAR1 | VAR2
- Var << (VAR1 | VAR2)
+ Var << (VAR1 | VAR2)
# GraphTerm ::= RDFTerm | '(' ')'
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 Mon Oct 2 14:27:03 2006
@@ -1,10 +1,14 @@
+
try:
import pyparsing
+ import rdflib
except ImportError:
from py.test import skip
- skip("Pyparsing not installed")
+ skip("Pyparsing or Rdflib not installed")
from pypy.lib.pyontology.sparql_grammar import SPARQLGrammar as SP
+from pypy.lib.pyontology.pyontology import Ontology, ConsistencyFailure
+import py
qt = """
PREFIX ns: <http://example.org/ns#>
@@ -26,52 +30,24 @@
assert len(triples) == 2
assert triples[0][0].getName() == 'VAR1'
assert triples[1][0].getName() == 'VAR1'
- assert triples[1][0][0] == 'x'
- assert triples[0][0][0] == 'y'
+ assert triples[1][0][0][0] == 'x'
+ assert triples[0][0][0][0] == 'y'
assert triples[1][1].asList() == ['ns', 'p']
assert triples[0][1].asList() == ['ns', 'q']
- assert triples[1][2][0] == '123'
- assert triples[1][2].getName() == 'Object'
- vars = query.SelectQuery[0].VARNAME
+ assert triples[1][2] == '123'
+ assert type(triples[1][2]) == rdflib.Literal
+ vars = query.SelectQuery[0].VAR1
assert len(vars) == 2
assert 'x' in vars[0][0]
assert 'y' in vars[1][0]
-class notest:
-
- def sparql(self, query):
- qe = SP.Query.parseString(query)[0]
-
- prefixes = qe.PrefixDecl[0]
-
- resvars = []
- for v in qe.SelectQuery[0].VARNAME:
- resvars.append(v[0])
-
- where = qe.SelectQuery[0].WhereClause[0]
-
- triples = where.GroupGraphPattern[0].Triples
- new = []
- for trip in triples:
- newtrip = []
- for item in trip:
- if item.NCNAME_PREFIX:
- uri = prefixes[item.NCNAME_PREFIX[0]] + item.NCNAME[0]
- newtrip.append(uri)
- elif item.getName() == 'VAR1':
- newtrip.append(item[0])
- else:
- newtrip.append(item)
- new.append(newtrip)
- constrain = where.GroupGraphPattern[0].Constraint
- return new, prefixes, resvars, constrain
def test_sparql():
- n = notest()
- result = n.sparql(qt)
+ n = Ontology()
+ result = n._sparql(qt)
res = result[0]
assert len(res) == 2
- assert len(res[0]) == len(res[1]) == 3
+ assert len(res[0]) == len(res[1]) == 4
assert res[0][1] in ['http://example.org/ns#p', 'http://example.org/ns#q']
assert res[1][1] in ['http://example.org/ns#p', 'http://example.org/ns#q']
assert result[3][0] == 'x<2'
@@ -90,4 +66,124 @@
# var var var ; for all p's return p.getvalues
#
# If p is a builtin owl property
+Onto = """<rdf:RDF
+ xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:owl="http://www.w3.org/2002/07/owl#"
+ xmlns:ns="http://example.org/ns#" >
+
+<owl:Class rdf:about="http://www.w3.org/2002/07/owl#Thing">
+ <owl:oneOf rdf:parseType="Collection">
+ <owl:Thing rdf:about="#s"/>
+ </owl:oneOf>
+ </owl:Class>
+ <owl:Thing rdf:ID="sub">
+ <ns:p rdf:datatype=
+ "http://www.w3.org/2001/XMLSchema#int">123</ns:p>
+ </owl:Thing>
+ </rdf:RDF>
+ """
+
+qt_proto = """
+ PREFIX ns: <http://example.org/ns#>
+ SELECT %s
+ WHERE {
+ %s
+ }
+ """
+from StringIO import StringIO
+
+def test_case_0():
+ """ Check if the triple is entailed """
+ py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x', 'ns:sub ns:p 123 .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ raises(ConsistencyFailure, O.sparql, query)
+
+def test_case_1():
+ #""" add a hasvalue constraint """
+
+ query = qt_proto % ('?x', '?x ns:p 123 .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ O.attach_fd()
+ print "Attached"
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0].uri == u'#sub'
+
+def test_case_2():
+ "for all p's return p if p[0]==s and p[1]==o """
+ py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x', 'ns:sub ?x 123 .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ O.attach_fd()
+
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0] == 'ns:sub'
+
+def test_case_3():
+ """search for s in p"""
+# py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x', 'ns:sub ns:p ?x .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+
+ O.attach_fd()
+
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0] == '123'
+
+def test_case_4():
+ """ search for s in p """
+ py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x ?y', '?x ?y 123 .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ O.attach_fd()
+
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0].uri == u'#sub'
+
+def test_case_5():
+ """ for all p's return p[0] if p[1]==o """
+ py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x ?y', '?x ns:p ?y .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ O.attach_fd()
+
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0].uri == u'#sub'
+
+def test_case_6():
+ """ return the values of p """
+ py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x ?y', 'ns:sub ?x ?y .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ O.attach_fd()
+
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0].uri == u'#sub'
+
+def test_case_7():
+ """ for all p's return p[1] if p[0]==s """
+ py.test.skip("Doesn't work yet")
+
+ query = qt_proto % ('?x ?y ?z', '?x ?y ?z .')
+ O = Ontology()
+ O.add_file(StringIO(Onto))
+ O.attach_fd()
+
+ O.sparql(query)
+ assert list(O.variables['query_x_'].getValues())[0].uri == u'#sub'
+
+
More information about the Pypy-commit
mailing list