Skip to content

Commit

Permalink
add feature for receiving an RDFlib graph as shapes input
Browse files Browse the repository at this point in the history
  • Loading branch information
prohde committed Nov 23, 2023
1 parent 01b41e7 commit 1dd1407
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions TravSHACL/core/ShapeSchema.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import annotations # required for typing in older versions of Python

from typing import TYPE_CHECKING

__author__ = 'Philipp D. Rohde and Monica Figuera'

from rdflib import Graph

from TravSHACL.core.GraphTraversal import GraphTraversal
from TravSHACL.core.ShapeParser import ShapeParser
from TravSHACL.rule_based_validation.Validation import Validation
from TravSHACL.utils import parse_heuristics
from TravSHACL.sparql.SPARQLEndpoint import SPARQLEndpoint
from TravSHACL.utils import parse_heuristics

if TYPE_CHECKING:
from rdflib import Graph

class ShapeSchema:
"""This class represents a SHACL shape schema."""

def __init__(self, *, schema_dir: str, schema_format: str = 'SHACL', endpoint: str | Graph,
def __init__(self, *, schema_dir: str | Graph, schema_format: str = 'SHACL', endpoint: str | Graph,
endpoint_user: str = None, endpoint_password: str = None,
graph_traversal: GraphTraversal = GraphTraversal.DFS, heuristics: dict = parse_heuristics("TARGET IN BIG"),
use_selective_queries: bool = True, max_split_size: int = 256, output_dir: str = None,
order_by_in_queries: bool = False, save_outputs: bool = False, work_in_parallel: bool = False):
"""
Creates a new shape schema instance.
:param schema_dir: path of the files including the shape definitions
:param schema_dir: path of the files including the shape definitions (or an RDFlib graph)
:param schema_format: indicates the format used for defining the shapes, this parameter should only
be used if shapes defined in the legacy JSON format are used
:param endpoint: URL for the SPARQL endpoint (or an RDFLib graph) to be evaluated against
Expand All @@ -41,8 +39,14 @@ def __init__(self, *, schema_dir: str, schema_format: str = 'SHACL', endpoint: s
:param save_outputs: indicates whether target classifications will be saved to the output path; default: False
:param work_in_parallel: indicates whether parallelization will be used; not yet implemented; default: False
"""
self.shapes = ShapeParser().parse_shapes_from_dir(schema_dir, schema_format, use_selective_queries,
max_split_size, order_by_in_queries)
if isinstance(schema_dir, Graph):
self.shapes = ShapeParser().parse_ttl(
schema_dir, use_selective_queries, max_split_size, order_by_in_queries
)
else:
self.shapes = ShapeParser().parse_shapes_from_dir(
schema_dir, schema_format, use_selective_queries, max_split_size, order_by_in_queries
)
self.shapesDict = {shape.get_id(): shape for shape in self.shapes} # TODO: use only the dict?
self.endpoint = SPARQLEndpoint(endpoint, endpoint_user, endpoint_password)
self.graphTraversal = graph_traversal
Expand Down

0 comments on commit 1dd1407

Please sign in to comment.