Ejemplo n.º 1
0
 def testLabelDotAccess(self):
     s = "'work' ^^<work:>"
     r = SPARQLParser.RDFLiteral(s)
     assert str(r.lexical_form) == "'work'", r.lexical_form
     r_copy = r.copy()
     try:
         r_copy.lexical_form = SPARQLParser.String("'work2'")
     except AttributeError as e:
         assert str(e) == 'Direct setting of attributes not allowed. To change an element e, try e.updateWith() instead.'
         
     s = '<c:check#22?> ( $var, ?var )'
     r = SPARQLParser.PrimaryExpression(s, postParseCheck=False)
     assert r.iriOrFunction.iri == SPARQLParser.iri('<c:check#22?>', postParseCheck=False)
Ejemplo n.º 2
0
 def testGetChildOrAncestors(self):
     s = '<c:check#22?> ( $var, ?var )'
     r = SPARQLParser.PrimaryExpression(s, postParseCheck=False)
     found = r.searchElements(element_type=SPARQLParser.ArgList)
     arglist = found[0]
     assert(len(arglist.getChildren())) == 4, len(arglist.getChildren())
      
     ancestors = arglist.getAncestors()
     assert str(ancestors) == '[iriOrFunction("<c:check#22?> ( $var , ?var )"), PrimaryExpression("<c:check#22?> ( $var , ?var )")]', str(ancestors)
Ejemplo n.º 3
0
 def testDescend(self):
     
     s = '(DISTINCT "*Expression*",  "*Expression*",   "*Expression*" )'
     r = SPARQLParser.ArgList(s)
     assert r.descend() == r        
     v = r.searchElements(element_type=SPARQLParser.STRING_LITERAL2)
     assert v[0].isAtom()
     assert not v[0].isBranch()
     e = r.searchElements(element_type=SPARQLParser.Expression)[0]
     d = e.descend()
     assert d.isAtom()
Ejemplo n.º 4
0
 def testSearchElements(self):
     
     s = '<c:check#22?> ( $var, ?var )'
     r = SPARQLParser.PrimaryExpression(s, postParseCheck=False)
     
     found = r.searchElements()
     assert len(found) == 32, len(found)
     
     found = r.searchElements(labeledOnly=False)
     assert len(found) == 32, len(found)
     
     found = r.searchElements(labeledOnly=True)
     assert len(found) == 4, len(found)
     
     found = r.searchElements(value='<c:check#22?>') 
     assert len(found) == 2, len(found)
     assert type(found[0]) == SPARQLParser.iri
     assert found[0].getLabel() == 'iri'
     assert found[0].__str__() == '<c:check#22?>'
Ejemplo n.º 5
0
    def testUpdateWith(self):
        s = "'work' ^^<work:>"
        r = SPARQLParser.RDFLiteral(s)
        r_copy = r.copy()
        r_copy.lexical_form.updateWith("'work2'")
        assert r_copy != r
        r_copy.lexical_form.updateWith("'work'")
        assert r_copy == r
        
        q = '''
PREFIX foaf:   <http://xmlns.com/foaf/0.1/>

SELECT ?p WHERE 
    {
        ?p a foaf:Person
    } 
'''
        r = parseQuery(q)
        r.expandIris()
        subjpath = r.searchElements(element_type=SPARQLParser.IRIREF, value=None)[1]
        assert str(subjpath.getParent()) == '<http://xmlns.com/foaf/0.1/Person>'
        assert str(subjpath.getAncestors()) == '[iri("<http://xmlns.com/foaf/0.1/Person>"), GraphTerm("<http://xmlns.com/foaf/0.1/Person>"), VarOrTerm("<http://xmlns.com/foaf/0.1/Person>"), GraphNodePath("<http://xmlns.com/foaf/0.1/Person>"), ObjectPath("<http://xmlns.com/foaf/0.1/Person>"), ObjectListPath("<http://xmlns.com/foaf/0.1/Person>"), PropertyListPathNotEmpty("a <http://xmlns.com/foaf/0.1/Person>"), TriplesSameSubjectPath("?p a <http://xmlns.com/foaf/0.1/Person>"), TriplesBlock("?p a <http://xmlns.com/foaf/0.1/Person>"), GroupGraphPatternSub("?p a <http://xmlns.com/foaf/0.1/Person>"), GroupGraphPattern("{ ?p a <http://xmlns.com/foaf/0.1/Person> }"), WhereClause("WHERE { ?p a <http://xmlns.com/foaf/0.1/Person> }"), SelectQuery("SELECT ?p WHERE { ?p a <http://xmlns.com/foaf/0.1/Person> }"), Query("PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?p WHERE { ?p a <http://xmlns.com/foaf/0.1/Person> }"), QueryUnit("PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?p WHERE { ?p a <http://xmlns.com/foaf/0.1/Person> }")]'
        assert r.hasParentPointers()
Ejemplo n.º 6
0
    l.split() for l in open('manifest.ttl')
    if ('mf:PositiveSyntaxTest11' in l or 'mf:NegativeSyntaxTest11' in l
        or 'mf:action' in l) and not l.startswith('#')
]

# for l in lines: print(l)

for i in range(len(lines) // 2):
    #     print(i)
    actions[lines[2 * i][2]].append(lines[2 * i + 1][1][1:-1])

posNum = len(actions['mf:PositiveSyntaxTest11'])
negNum = len(actions['mf:NegativeSyntaxTest11'])

print('Testing {} positive and {} negative testcases'.format(posNum, negNum))

for fname in actions['mf:PositiveSyntaxTest11']:
    try:
        s = stripComments(open(fname).readlines())
        r = SPARQLParser.QueryUnit(s, postParseCheck=False)
    except ParseException as e:
        print('\n*** {} should not raise exception? Check\n'.format(fname))

for fname in actions['mf:NegativeSyntaxTest11']:
    try:
        s = open(fname).read()
        r = SPARQLParser.UpdateUnit(s, postParseCheck=False)
        print('\n*** {} should raise exception? Check\n'.format(fname))
    except ParseException as e:
        pass
print('\nPassed')
Ejemplo n.º 7
0
# parseQuery above is actually a convenience function. It does some preprocessing (such as stripping
# comments from the query) and some postprocessing (to perform additional checks on the query that are
# part of the SPARQL specification outside the EBNF grammar). In between the query string is parsed against 
# a top level production of the grammar.
# For this, an actual SPARQL SPARQLParser object is used. It has attributes for every production in the grammar (except for
# the "whitespace" production which is not needed with pyparsing).
# Such an attribute can be used to parse a valid string for that production. This is the basic mode of parsing.

# As an example, below a string is parsed against the RDFLiteral production.
# For this, we need to import sparqlparser.

from parsertools.parsers.sparqlparser import SPARQLParser

rdfliteral = '"work"@en-bf'

rdf = SPARQLParser.RDFLiteral(rdfliteral)

print(rdf.dump())

# A parsed element can be searched for elements conforming to a specified pattern.
# Any of the following can be matched, in any combination:
# - type (class)
# - label
# - string content
# Any of these conditions can be None, which acts as a wildcard.
# In addition, a "labeledOnly" flag can be specified. If True, only elements with an assigned label will qualify.
# The default for this flag is False (all elements considered).

wheres = query.searchElements(label = None, element_type=SPARQLParser.WhereClause, value=None)
for w in wheres:
    print()
Ejemplo n.º 8
0
 def testBranchAndAtom(self):
     s = "'work' ^^<work:>"
     r = SPARQLParser.RDFLiteral(s)
     assert r.isBranch()
     assert not r.isAtom()
     assert (SPARQLParser.IRIREF('<ftp://test>')).isAtom()
Ejemplo n.º 9
0
 def testStr(self):
     s = "'work' ^^<work:>"
     r = SPARQLParser.RDFLiteral(s)
     assert r.__str__() == "'work' ^^ <work:>" 
Ejemplo n.º 10
0
 def testCopy(self):
     s = "'work' ^^<work:>"
     r = SPARQLParser.RDFLiteral(s)
     r_copy = r.copy()
     assert r_copy == r
     assert not r_copy is r
Ejemplo n.º 11
0
 def testParse(self):
     s = "'work' ^^<work:>"
     r = SPARQLParser.RDFLiteral(s)
     assert r.check()
Ejemplo n.º 12
0
    def testDump(self):
        s = '(DISTINCT "*Expression*",  "*Expression*",   "*Expression*" )'
        s_dump = '''
[ArgList] /( DISTINCT "*Expression*" , "*Expression*" , "*Expression*" )/
|  [LPAR] /(/
|  |  (
|  > distinct:
|  [DISTINCT] /DISTINCT/
|  |  DISTINCT
|  > argument:
|  [Expression] /"*Expression*"/
|  |  [ConditionalOrExpression] /"*Expression*"/
|  |  |  [ConditionalAndExpression] /"*Expression*"/
|  |  |  |  [ValueLogical] /"*Expression*"/
|  |  |  |  |  [RelationalExpression] /"*Expression*"/
|  |  |  |  |  |  [NumericExpression] /"*Expression*"/
|  |  |  |  |  |  |  [AdditiveExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  [MultiplicativeExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  [UnaryExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  [PrimaryExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  [RDFLiteral] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  > lexical_form:
|  |  |  |  |  |  |  |  |  |  |  |  [String] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  |  [STRING_LITERAL2] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  |  |  "*Expression*"
|  ,
|  > argument:
|  [Expression] /"*Expression*"/
|  |  [ConditionalOrExpression] /"*Expression*"/
|  |  |  [ConditionalAndExpression] /"*Expression*"/
|  |  |  |  [ValueLogical] /"*Expression*"/
|  |  |  |  |  [RelationalExpression] /"*Expression*"/
|  |  |  |  |  |  [NumericExpression] /"*Expression*"/
|  |  |  |  |  |  |  [AdditiveExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  [MultiplicativeExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  [UnaryExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  [PrimaryExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  [RDFLiteral] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  > lexical_form:
|  |  |  |  |  |  |  |  |  |  |  |  [String] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  |  [STRING_LITERAL2] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  |  |  "*Expression*"
|  ,
|  > argument:
|  [Expression] /"*Expression*"/
|  |  [ConditionalOrExpression] /"*Expression*"/
|  |  |  [ConditionalAndExpression] /"*Expression*"/
|  |  |  |  [ValueLogical] /"*Expression*"/
|  |  |  |  |  [RelationalExpression] /"*Expression*"/
|  |  |  |  |  |  [NumericExpression] /"*Expression*"/
|  |  |  |  |  |  |  [AdditiveExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  [MultiplicativeExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  [UnaryExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  [PrimaryExpression] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  [RDFLiteral] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  > lexical_form:
|  |  |  |  |  |  |  |  |  |  |  |  [String] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  |  [STRING_LITERAL2] /"*Expression*"/
|  |  |  |  |  |  |  |  |  |  |  |  |  |  "*Expression*"
|  [RPAR] /)/
|  |  )
'''[1:]
 
        r = SPARQLParser.ArgList(s)
        assert r.dump() == s_dump