Skip to content

Commit

Permalink
renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ssssarah committed Oct 25, 2023
1 parent 962d5cf commit c1e1ad7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
16 changes: 8 additions & 8 deletions kgforge/specializations/models/rdf/rdf_model_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@


class RdfModelService:
schema_to_source: Dict[URIRef, str]
classes_to_shapes: Dict[str, URIRef]
shape_to_source: Dict[URIRef, str]
class_to_shape: Dict[str, URIRef]

def __init__(self, graph: Graph, context_iri: Optional[str] = None) -> None:

if context_iri is None:
raise ConfigurationError("RdfModel requires a context")
self._graph = graph
self._context_cache = dict()
self.schema_to_source, self.classes_to_shapes = self._build_shapes_map()
self.shape_to_source, self.class_to_shape = self._build_shapes_map()
self.context = Context(self.resolve_context(context_iri), context_iri)
self.types_to_shapes: Dict[str, URIRef] = self._build_types_to_shapes()

def schema_source(self, schema_iri: URIRef) -> str:
return self.schema_to_source[schema_iri]
def shape_source(self, schema_iri: URIRef) -> str:
return self.shape_to_source[schema_iri]

def sparql(self, query: str) -> List[Resource]:
e = self._graph.query(query)
Expand Down Expand Up @@ -108,7 +108,7 @@ def _build_types_to_shapes(self) -> Dict[str, URIRef]:
"""Iterates the classes_to_shapes dictionary to create a term to shape dictionary filtering
the terms available in the context """
types_to_shapes: Dict = dict()
for k, v in self.classes_to_shapes.items():
for k, v in self.class_to_shape.items():
term = self.context.find_term(str(k))
if term:
if term.name not in types_to_shapes:
Expand Down Expand Up @@ -164,15 +164,15 @@ def traverse_properties(properties) -> Tuple[Dict, Dict]:
return l_prefixes, l_terms

target_classes = list()
for k in self.classes_to_shapes.keys():
for k in self.class_to_shape.keys():
key = as_term(k)
if key not in target_classes:
target_classes.append(key)
else:
# TODO: should this raise an error?
print("duplicated term", key, k)

for type_, shape in self.classes_to_shapes.items():
for type_, shape in self.class_to_shape.items():
t_prefix, t_namespace, t_name = self._graph.compute_qname(type_)
prefixes.update({t_prefix: str(t_namespace)})
types_.update({t_name: {"@id": ":".join((t_prefix, t_name))}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def resolve_context(self, iri: str) -> Dict:
def generate_context(self) -> Dict:
return self._generate_context()



def _build_shapes_map(self) -> Tuple[Dict[URIRef, str], Dict[str, URIRef]]:
query = """
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Expand All @@ -76,18 +74,18 @@ def _build_shapes_map(self) -> Tuple[Dict[URIRef, str], Dict[str, URIRef]]:
"""
res = self._graph.query(query)

class_being_shaped_id_to_shape_uri: Dict[str, URIRef] = {
class_to_shape: Dict[str, URIRef] = {
row["type"]: URIRef(row["shape"])
for row in res
}

# FIXME should return the file path where the schema is in
schema_to_file = dict(
shape_to_file = dict(
(e, "") # TODO file source
for e in class_being_shaped_id_to_shape_uri.values()
for e in class_to_shape.values()
)

return schema_to_file, class_being_shaped_id_to_shape_uri
return shape_to_file, class_to_shape


def load_rdf_files_into_graph(path: Path, memory_graph: Graph) -> Graph:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def resolve_context(self, iri: str) -> Dict:
return self._context_cache[iri]

def generate_context(self) -> Dict:
for v in self.schema_to_source.values():
for v in self.shape_to_source.values():
self._load_shape_and_reload_shapes_graph(v)

return self._generate_context()
Expand All @@ -96,19 +96,19 @@ def _build_shapes_map(self) -> Tuple[Dict[URIRef, str], Dict[str, URIRef]]:
limit = 100
offset = 0
count = limit
class_being_shaped_id_to_shape_uri = dict()
schema_to_resource: Dict[URIRef, URIRef] = dict()
class_to_shape = dict()
shape_to_resource: Dict[URIRef, URIRef] = dict()

while count == limit:
resources = self.context_store.sparql(query, debug=False, limit=limit, offset=offset)
for r in resources:
shape_uri = URIRef(r.shape)
class_being_shaped_id_to_shape_uri[r.type] = shape_uri
schema_to_resource[shape_uri] = URIRef(r.resource_id)
class_to_shape[r.type] = shape_uri
shape_to_resource[shape_uri] = URIRef(r.resource_id)
count = len(resources)
offset += count

return schema_to_resource, class_being_shaped_id_to_shape_uri
return shape_to_resource, class_to_shape

def recursive_resolve(self, context: Union[Dict, List, str]) -> Dict:
document = dict()
Expand Down Expand Up @@ -163,7 +163,7 @@ def _load_and_get_type_shape(self, iri: URIRef) -> ShapeWrapper:
try:
return self._shapes_graph.lookup_shape_from_node(iri)
except KeyError:
shape_resource_id = self.schema_to_source[iri]
shape_resource_id = self.shape_to_source[iri]
self._load_shape_and_reload_shapes_graph(shape_resource_id)
return self._shapes_graph.lookup_shape_from_node(iri)

Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/models/rdf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_shape_from_type(self, type: str) -> URIRef:

def schema_id(self, type: str) -> URIRef:
shape_iri: URIRef = self.get_shape_from_type(type)
return self.service.schema_source(shape_iri)
return self.service.shape_source(shape_iri)

# Validation.

Expand Down

0 comments on commit c1e1ad7

Please sign in to comment.