我正在尝试在 Python 中的 rdflib 中创建一个自定义函数,以便从 SPARQL 查询进行调用。
我创建了这个应该返回“test”的简单示例。它不会给出错误。它什么也没返回。
from rdflib import Graph, URIRef, Literal
from rdflib.plugins.sparql.operators import custom_function
g = Graph()
@custom_function(URIRef("http://example.org/myCustomFunction"))
def myCustomFunction(args):
return Literal("test")
query = """
SELECT ?result WHERE {
BIND(<http://example.org/myCustomFunction>() AS ?result)
}
"""
for row in g.query(query):
print(f"Result: {row.result}")
希望大家能帮忙。
from rdflib import Graph, URIRef, Literal
from rdflib.plugins.sparql.operators import custom_function
g = Graph()
# Define the custom function
@custom_function(URIRef("http://example.org/myCustomFunction"))
def myCustomFunction(args):
return Literal("test")
# Bind the custom function to the graph
g.store.query.bind("http://example.org/myCustomFunction", myCustomFunction)
# Prepare and execute the SPARQL query
query = """
SELECT ?result WHERE {
BIND(<http://example.org/myCustomFunction>() AS ?result)
}
"""
# Associate the graph with the query
result = g.query(query)
# Iterate through the results
for row in result:
print(f"Result: {row.result}")