SPARQL.js icon indicating copy to clipboard operation
SPARQL.js copied to clipboard

FunctionCallExpression doesn't work with string function

Open SteinerPascal opened this issue 2 years ago • 1 comments

Hi I'm using sparql.js to create a filter pattern with function call in SPARQL it should look like this:

FILTER (geof:sfWithin(?fWKT, '''
        <http://www.opengis.net/def/crs/OGC/1.3/CRS84>
            Polygon ((-83.4 34.0, -83.1 34.0,
                      -83.1 34.2, -83.4 34.2,
                      -83.4 34.0))
        '''^^geo:wktLiteral))

I followed as an example here: https://github.com/RubenVerborgh/SPARQL.js/blob/48363ab7c4bb54cf98287a715a8c1800a2e0fc78/test/parsedQueries/sparql/sparql-16-2-2a.json#L49-L63

my example:

{
  type: "filter",
  expression: {
    type: "functionCall",
    function: "geof:sfWithin",
    args: [
      {
        id: "?aWKT",
      },
      {
        id: "\"'''<http://www.opengis.net/def/crs/OGC/1.3/CRS84> \n        Polygon(( 46.19325970519137 6.127480741124601, 46.21737666278269 6.127480741124601, 46.21737666278269 6.158036466222257, 46.19325970519137 6.158036466222257, LatLng(46.19326, 6.127481)))\n        '''\"^^http://www.opengis.net/ont/geosparql#wktLiteral",
      },
    ],
  },
}

But i don't think this still works? Because i get the following error for it:

Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'map')
    at Generator.toEntity (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:332:1)
    at Generator.toExpression (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:223:1)
    at Generator.filter (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:165:1)
    at Generator.toPattern (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:98:1)
    at mapJoin (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:440:1)
    at Generator.array (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:106:1)
    at Generator.toPattern (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:98:1)
    at Generator.group (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:156:1)
    at Generator.toQuery (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:49:1)
    at stringify (/home/pascal/Sparnatural/node_modules/sparqljs/lib/SparqlGenerator.js:455:1)

I think it is due to the fact that the function:"geof:sfWithin" is wrongly tried to be parsed as an entity here (line 223): https://github.com/RubenVerborgh/SPARQL.js/blob/7f2ffd21341377fa4e8f2bc85d4b8366222c6bfe/lib/SparqlGenerator.js#L213-L226

It then fails on this place: https://github.com/RubenVerborgh/SPARQL.js/blob/7f2ffd21341377fa4e8f2bc85d4b8366222c6bfe/lib/SparqlGenerator.js#L332

Is this a bug or am I doing something wrong?

SteinerPascal avatar Jun 22 '22 11:06 SteinerPascal

Okay i think i figured it out. The example from the repo won't work i think. It works when I use the function: <NamedNode> and not a string.

{
  type: "functionCall",
  function: {
    id: "geof:sfWithin",
  },
  args: [
    {
      id: "?aWKT",
    },
    {
      id: "\"'''<http://www.opengis.net/def/crs/OGC/1.3/CRS84>       Polygon(( 46.1952797580882 6.131257291417569, 46.21856441662584 6.131257291417569, 46.21856441662584 6.161469693761319, 46.1952797580882 6.161469693761319, LatLng(46.19528, 6.131257)))      '''\"^^http://www.opengis.net/ont/geosparql#wktLiteral",
    },
  ],
}

My suggestion: 2. change type definition in https://github.com/DefinitelyTyped/DefinitelyTyped from: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/b453d9d1b99c48c8711c31c2a64e9dffb6ce729d/types/sparqljs/index.d.ts#L298-L302

to:

export interface FunctionCallExpression extends BaseExpression {
    type: 'functionCall';
    function: Term;
    args: Expression[];
}

Can you confirm that?

SteinerPascal avatar Jun 22 '22 12:06 SteinerPascal

closing this since it is resolved

SteinerPascal avatar Jan 25 '23 15:01 SteinerPascal