Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEAT-734]🤼 Increase usability #1036

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cognite/neat/_session/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def convert(self, reserved_properties: Literal["error", "warning"] = "warning")
neat.convert()
```
"""
self._state._raise_exception_if_condition_not_met(
"Convert to physical", has_dms_rules=False, has_information_rules=True
)
converter = InformationToDMS(reserved_properties=reserved_properties)

issues = self._state.rule_transform(converter)
Expand Down
13 changes: 13 additions & 0 deletions cognite/neat/_session/_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ def __call__(self, io: Any, enable_manual_edit: bool = False) -> IssueList:
if enable_manual_edit:
warnings.filterwarnings("default")
AlphaFlags.manual_rules_edit.warn()
else:
self._state._raise_exception_if_condition_not_met(
"Read Excel Rules",
empty_rules_store_required=True,
)

return self._state.rule_import(importers.ExcelImporter(path), enable_manual_edit)

Expand All @@ -381,6 +386,10 @@ def __call__(self, io: Any, format: Literal["neat", "toolkit"] = "neat") -> Issu
neat.read.yaml("path_to_toolkit_yamls")
```
"""
self._state._raise_exception_if_condition_not_met(
"Read YAML data model",
empty_rules_store_required=True,
)
reader = NeatReader.create(io)
path = reader.materialize_path()
importer: BaseImporter
Expand Down Expand Up @@ -634,6 +643,10 @@ def imf(self, io: Any) -> IssueList:
return self._state.rule_import(importer)

def instances(self, io: Any) -> IssueList:
self._state._raise_exception_if_condition_not_met(
"Read RDF Instances",
empty_rules_store_required=True,
)
reader = NeatReader.create(io)
self._state.instances.store.write(extractors.RdfFileExtractor(reader.materialize_path()))
return IssueList()
Expand Down
16 changes: 14 additions & 2 deletions cognite/neat/_session/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,23 @@ def _raise_exception_if_condition_not_met(
empty_instances_store_required: bool = False,
instances_required: bool = False,
client_required: bool = False,
has_information_rules: bool | None = None,
has_dms_rules: bool | None = None,
) -> None:
"""Set conditions for raising an error in the session that are used by various methods in the session."""
condition = set()
suggestion = set()

try_again = True
if client_required and not self.client:
condition.add(f"{activity} expects a client in NEAT session")
suggestion.add("Please provide a client")
if has_information_rules is True and self.rule_store.try_get_last_information_rules is None:
condition.add(f"{activity} expects information rules in NEAT session")
suggestion.add("Read in information rules to neat session")
if has_dms_rules is False and self.rule_store.try_get_last_dms_rules is not None:
condition.add(f"{activity} expects no DMS data model in NEAT session")
suggestion.add("You already have a DMS data model in the session")
try_again = False
if empty_rules_store_required and not self.rule_store.empty:
condition.add(f"{activity} expects no data model in NEAT session")
suggestion.add("Start new session")
Expand All @@ -91,7 +100,10 @@ def _raise_exception_if_condition_not_met(
suggestion.add("Read in instances to neat session")

if condition:
raise NeatSessionError(". ".join(condition) + ". " + ". ".join(suggestion) + ". And try again.")
message = ". ".join(condition) + ". " + ". ".join(suggestion) + "."
if try_again:
message += " And try again."
raise NeatSessionError(message)


class InstancesState:
Expand Down
14 changes: 14 additions & 0 deletions cognite/neat/_store/_rules_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ def _create_id(self, info: InformationRules, dms: DMSRules | None) -> rdflib.URI
self._iteration_by_id[identifier] += 1
return identifier + f"/Iteration_{self._iteration_by_id[identifier]}"

@property
def try_get_last_dms_rules(self) -> DMSRules | None:
if not self.provenance:
return None
if self.provenance[-1].target_entity.dms is None:
return None
return self.provenance[-1].target_entity.dms

@property
def try_get_last_information_rules(self) -> InformationRules | None:
if not self.provenance:
return None
return self.provenance[-1].target_entity.information

@property
def last_verified_dms_rules(self) -> DMSRules:
if not self.provenance:
Expand Down
Loading