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’, ‘jpype’. Default is ‘py4j’.

  • 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() None[source]#

Load Corese library into JVM and expose the Corese classes.

loadRDF(rdf: str, graph=None) object[source]#

Load RDF file/string into Corese graph. Supported formats are RDF/XML and Turtle.

Parameters:
  • rdf (str) – Path to the RDF file or a string with RDF content.

  • graph (object, optional) – Corese object of either fr.inria.corese.core.Graph or fr.inria.core.storage.CoreseGraphDataManager type. If an object is not provided (default), new Graph and GraphManager will be created.

Returns:

object – Corese fr.inria.core.storage.CoreseGraphDataManager object.

loadRuleEngine(graph: object, profile: object, replace: bool = False) object[source]#

Load the rule engine for a given graph.

Parameters:
  • graph (object) – Corese Graph or DataManager object

  • profile (object) – Profile object for the rule engine. Accepted values: RuleEngine.Profile.RDFS, RuleEngine.Profile.OWLRL, RuleEngine.Profile.OWLRL_LITE, RuleEngine.Profile.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 Graph or DataManager object

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.

shaclValidate(graph: object, prefixes: str | list | None = None, shacl_shape_ttl: str = '', return_dataframe=False) object[source]#

Validate RDF graph against SHACL shape.

This version supports only Turtle format to define a SHACL shape.

Parameters:
  • graph (object) – Corese Graph or DataManager object

  • prefixes (str or list, optional) – namespace prefixes. Default is None.

  • shacl_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:

object – SHACL validation report in Turtle format.

sparqlConstruct(graph: object, prefixes: str | list | 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) – Corese Graph or DataManager object

  • prefixes (str or list, optional) – namespace prefixes. Default is None.

  • query (str, optional) – SPARQL query. Default is empty string resulting in empty graph.

  • merge (bool, optional) – Option to merge the result with the existing graph. Default is False.

Returns:

object – Result of the SPARQL CONSTRUCT query in RDF/XML format.

sparqlSelect(graph: object, prefixes: str | list | None = None, query: str = 'SELECT * WHERE {?s ?p ?o} LIMIT 5', return_dataframe: bool = True) object | DataFrame[source]#

Execute SPARQL SELECT or ASK query on Corese graph. Optionally return the result as DataFrame.

Parameters:
  • graph (object) – Corese Graph or DataManager object

  • prefixes (str or list, optional) – namespace prefixes. Default is None.

  • query (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.

Returns:

object or pd.DataFrame – Result of the SPARQL query in CSV-formatted fr.inria.core.print.ResultFormat object or a DataFrame.

toDataFrame(queryResult: object, dtypes: list | dict | None = None) DataFrame[source]#

Convert Corese ResultFormat object to pandas.DataFrame.

Parameters:
Returns:

pd.DataFrame – Corese object converted to a DataFrame.

toTurtle(rdf: object) str[source]#

Convert RDF/XML to Turtle format.

Parameters:

rdf (object) – Corese RDF object

Returns:

str – RDF in Turtle format.

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.

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.

CoreseAPI.DataManager#

Corese fr.inria.corese.core.storage.api.dataManager.DataManager object.

CoreseAPI.CoreseGraphDataManager#

Corese fr.inria.corese.core.storage.CoreseGraphDataManager object.