Python API Reference#
pycorese is a Python wrapper for accessing and manipulating RDF data with Corese features connected by one of the Java bridge packages: py4j
or jpype
.
Note
pycorese is still in beta version and is under active development. The API may change in future releases.
In the following sections, you will find the documentation of the Python API of pycorese.
High-level API#
HIgh-level API is a set of convenience methods to facilitate the common tasks of working with Knowledge Graphs.
- class pycorese.api.CoreseAPI(java_bridge: str = 'py4j', corese_path: str | None = None)[source]#
Simplified API to leverage functionality of Corese Java library
corese-core
.- Parameters:
java_bridge (str, optional) – Package name to use for Java integration. Options:
py4j
,jpyp
. Default ispy4j
.corese_path (str, optional) – Path to the corese-python library. If not specified (default), the jar file that was installed with the package is used.
- coreseVersion() str | None [source]#
Get the version of the corese-core library.
Notes
Corese library must be loaded first.
- Returns:
str – The version of the
corese-core
library used. If the library is not loaded, returns None.
- loadCorese() object | None [source]#
Load Corese library into JVM and expose the Corese classes.
- Returns:
object – Java Gateway object if the library is loaded successfully. Otherwise, returns None.
- unloadCorese()[source]#
Explicitly unload Corese library.
It’s not necessary to call this method, as the library is automatically unloaded when the Python interpreter exits.
Warning
After unloading Corese bridged by
JPype
it is not possible to restart it.
- loadRDF(rdf: str, graph: object | None = None) object [source]#
Load RDF file/string into Corese graph. Supported formats are RDF/XML and Turtle.
- Parameters:
rdf (str) – Path or URL of an RDF file or a string with RDF content.
graph (object, optional) – Corese
fr.inria.corese.core.Graph
object. If an object is not provided (default), new Graph and GraphManager will be created.
- Returns:
object – Corese
fr.inria.core.Graph
object.
- loadRuleEngine(graph: object, profile: str, replace: bool = False) object [source]#
Load the rule engine for a given graph.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
objectprofile (str) – Profile the rule engine. Accepted values: Accepted values: rdfs, owlrl, owlrl_lite, owlrl_ext
replace (bool, optional) – Replace the existing rule engine. Default is False.
- Returns:
object – Corese
fr.inria.core.rule.RuleEngine
object.
- resetRuleEngine(graph: object) None [source]#
Reset the rule engine for a given graph.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
object
- parsePrefixes(query: str) dict [source]#
Parse a query string to extract a dictionary of (prefix, namespace) pairs
- Parameters:
query (str) – Query string that may contain PREFIX declarations
- Returns:
dict – Dictionary of (prefix, namespace) pairs or an empty dictionary if no prefixes are found.
- sparqlSelect(graph: object, query: str = 'SELECT * WHERE {?s ?p ?o} LIMIT 5', return_dataframe: bool = True, post_apply_prefixes: bool = True) object | DataFrame [source]#
Execute SPARQL SELECT or ASK query on Corese graph. Optionally return the result as DataFrame.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
objectquery (str, optional) – SPARQL query. By default five first triples of the graph are returned.
return_dataframe (bool, optional) – Return the result as a DataFrame. Default is True.
post_apply_prefixes (bool, optional) – Substitute long namespaces with prefixes defined in the query . Default is True.
- Returns:
object or pd.DataFrame – Result of the SPARQL query in CSV-formatted
fr.inria.core.print.ResultFormat
object or apandas.DataFrame
.
- toDataFrame(queryResult: object, dtypes: list | dict | None = None) DataFrame [source]#
Convert Corese ResultFormat object to
pandas.DataFrame
.- Parameters:
queryResult (object) – CSV-formatted
fr.inria.core.print.ResultFormat
object.dtypes (list or dict, optional) – Optional column data types for the columns in the format as in
panads.read_csv
method. https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
- Returns:
pd.DataFrame – Corese object converted to a DataFrame.
- sparqlConstruct(graph: object | None = None, query: str = '', merge: bool = False) object [source]#
Execute SPARQL CONSTRUCT query on Corese graph.
Optionally the new triples can be merged with the existing graph.
- Parameters:
graph (object, optional) – Corese
fr.inria.corese.core.Graph
object. If not provided (default), a new graph is created.query (str, optional) – SPARQL query. Defaults to an empty string, resulting in an empty graph.
merge (bool, optional) – Option to merge the result with the existing graph passed in the parameters. Default is False.
- Returns:
object – Result of the SPARQL CONSTRUCT query in RDF/XML format.
- toTurtle(rdf: object) str [source]#
Convert RDF/XML to Turtle format.
- Parameters:
rdf (object) – Corese RDF object
- Returns:
str – RDF in Turtle format.
- shaclValidate(graph: object, shacl_shape_ttl: str = '', return_dataframe=False) str [source]#
Validate RDF graph against SHACL shape.
This version supports only Turtle format to define a SHACL shape.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
objectshacl_shape_ttl (str, optional) – SHACL shape in Turtle format. If not provided, the validation will be skipped.
return_dataframe (bool, optional) – Return the validation report as a DataFrame. Default is False.
- Returns:
str – SHACL validation report in Turtle format.
- shaclReportToDataFrame(validation_report: str) DataFrame [source]#
Convert SHACL validation report to
pandas.DataFrame
.- Parameters:
validation_report (str) – SHACL validation report in Turtle format.
- Returns:
pd.DataFrame – Validation report as a DataFrame.
- addTriple(graph: object, subject: str, predicate: str, obj: str) object [source]#
Add a triple to the default Corese graph.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
objectsubject (str) – Subject of the triple. Must be a URI.
predicate (str) – Predicate of the triple. Must be a URI.
obj (str) – Object of the triple. Must be a URI or a literal.
- Returns:
object – Corese
fr.inria.corese.core.Graph
object with the new triple.
- removeTriple(graph: object, subject: str, predicate: str, obj: str) object [source]#
Remove a triple from the default Corese graph.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
objectsubject (str) – Subject of the triple. Must be a URI.
predicate (str) – Predicate of the triple. Must be a URI.
obj (str) – Object of the triple. Must be a URI or a literal.
- Returns:
object – Corese
fr.inria.corese.core.Graph
object without the triple.
- getTripleObject(graph, subject: str, predicate: str) str | None [source]#
Get the object of a triple. It can be a URI or a literal.
- Returns:
str – String representation of the object of the triple or None if the triple does not exist.
- exportRDF(graph: object, path: str, format: str = 'turtle', overwrite: bool = False) None [source]#
Export Corese graph to an RDF file. Only RDF/XML and Turtle are supported by this version.
- Parameters:
graph (object) – Corese
fr.inria.corese.core.Graph
object.path (str) – Path to the output RDF file.
format (str, optional) – RDF format. Default is Turtle. Accepted values are turtle, ttl, xml, and rdfxml.
overwrite (bool, optional) – Overwrite the file if it exists. Default is False.
Low-level API#
Low-level API is a subset of corese-core
classes exposed as Python objects. These are dynamically created classes
and can be accessed only after the Corese engine is loaded
For the details of these classes and their methods, please refer to the Corese Java documentation.
- CoreseAPI.Graph#
Corese
fr.inria.corese.core.Graph
object.
- CoreseAPI.Load#
Corese
fr.inria.corese.core.load.Load
object.
- CoreseAPI.QueryProcess#
Corese
fr.inria.corese.core.query.QueryProcess
object.
- CoreseAPI.ResultFormat#
Corese
fr.inria.corese.core.print.ResultFormat
object.
- CoreseAPI.RuleEngine#
Corese
fr.inria.corese.core.rule.RuleEngine
object.
- CoreseAPI.Transformer#
Corese
fr.inria.corese.core.transform.Transformer
object.
- CoreseAPI.Shacl#
Corese
fr.inria.corese.core.shacl.Shacl
object.