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

Release 0.96.2 #705

Merged
merged 3 commits into from
Nov 5, 2024
Merged
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: run-explorer run-tests run-linters build-ui build-python build-docker run-docker compose-up
version="0.96.1"
version="0.96.2"
run-explorer:
@echo "Running explorer API server..."
# open "http://localhost:8000/static/index.html" || true
Expand Down
18 changes: 6 additions & 12 deletions cognite/neat/_rules/importers/_dms2rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def _create_dms_property(
property_=prop_id,
description=prop.description,
name=prop.name,
connection=self._get_connection_type(prop_id, prop, view_entity.as_id()),
connection=self._get_connection_type(prop),
value_type=str(value_type),
is_list=self._get_is_list(prop),
nullable=self._get_nullable(prop),
Expand All @@ -367,19 +367,13 @@ def _container_prop_unsafe(self, prop: dm.MappedPropertyApply) -> dm.ContainerPr
return self._all_containers_by_id[prop.container].properties[prop.container_property_identifier]

def _get_connection_type(
self, prop_id: str, prop: ViewPropertyApply, view_id: dm.ViewId
self, prop: ViewPropertyApply
) -> Literal["direct"] | ReverseConnectionEntity | EdgeEntity | None:
if isinstance(prop, SingleEdgeConnectionApply | MultiEdgeConnectionApply) and prop.direction == "outwards":
if isinstance(prop, SingleEdgeConnectionApply | MultiEdgeConnectionApply):
properties = ViewEntity.from_id(prop.edge_source) if prop.edge_source is not None else None
return EdgeEntity(properties=properties, type=DMSNodeEntity.from_reference(prop.type), direction="outwards")
elif isinstance(prop, SingleEdgeConnectionApply | MultiEdgeConnectionApply) and prop.direction == "inwards":
if reverse_prop := self._find_reverse_edge(prop_id, prop, view_id):
return ReverseConnectionEntity(property=reverse_prop)
else:
properties = ViewEntity.from_id(prop.source) if prop.edge_source is not None else None
return EdgeEntity(
properties=properties, type=DMSNodeEntity.from_reference(prop.type), direction="inwards"
)
return EdgeEntity(
properties=properties, type=DMSNodeEntity.from_reference(prop.type), direction=prop.direction
)
elif isinstance(prop, SingleReverseDirectRelationApply | MultiReverseDirectRelationApply):
return ReverseConnectionEntity(property=prop.through.property)
elif isinstance(prop, dm.MappedPropertyApply) and isinstance(
Expand Down
35 changes: 7 additions & 28 deletions cognite/neat/_rules/models/dms/_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ def _create_view_property(
return edge_cls(
type=cls._create_edge_type_from_prop(prop),
source=source_view_id,
direction="outwards",
direction=prop.connection.direction,
name=prop.name,
description=prop.description,
edge_source=edge_source,
Expand All @@ -580,7 +580,6 @@ def _create_view_property(
"If this error occurs it is a bug in NEAT, please report"
f"Debug Info, Invalid valueType reverse connection: {prop.model_dump_json()}"
)
edge_source = None
reverse_prop = next(
(
prop
Expand All @@ -589,13 +588,6 @@ def _create_view_property(
),
None,
)
if (
reverse_prop
and isinstance(reverse_prop.connection, EdgeEntity)
and reverse_prop.connection.properties is not None
):
edge_source = reverse_prop.connection.properties.as_id()

if reverse_prop is None:
warnings.warn(
PropertyNotFoundWarning(
Expand All @@ -608,30 +600,17 @@ def _create_view_property(
stacklevel=2,
)

if reverse_prop is None or isinstance(reverse_prop.connection, EdgeEntity):
inwards_edge_cls = (
dm.MultiEdgeConnectionApply if prop.is_list in [True, None] else SingleEdgeConnectionApply
)
return inwards_edge_cls(
type=cls._create_edge_type_from_prop(reverse_prop or prop),
source=source_view_id,
name=prop.name,
description=prop.description,
direction="inwards",
edge_source=edge_source,
)
elif reverse_prop and reverse_prop.connection == "direct":
reverse_direct_cls = (
dm.MultiReverseDirectRelationApply
if prop.is_list in [True, None]
else SingleReverseDirectRelationApply
)
return reverse_direct_cls(
if reverse_prop and reverse_prop.connection == "direct":
args: dict[str, Any] = dict(
source=source_view_id,
through=dm.PropertyId(source=source_view_id, property=reverse_prop_id),
name=prop.name,
description=prop.description,
)
if prop.is_list in [True, None]:
return dm.MultiReverseDirectRelationApply(**args)
else:
return SingleReverseDirectRelationApply(**args)
else:
return None

Expand Down
2 changes: 1 addition & 1 deletion cognite/neat/_session/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def verify(self) -> IssueList:
self._state.store.add_rules(output.rules)
self._state.issue_lists.append(output.issues)
if output.issues:
print("You can inspect the issues with the .inspect attribute.")
print("You can inspect the issues with the .inspect.issues(...) method.")
return output.issues

def convert(self, target: Literal["dms"]) -> None:
Expand Down
12 changes: 9 additions & 3 deletions cognite/neat/_session/_to.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import Any, overload
from typing import Any, Literal, overload

from cognite.client import CogniteClient

Expand Down Expand Up @@ -61,11 +61,17 @@ def instances(self, space: str | None = None):

return loader.load_into_cdf(self._client)

def data_model(self):
def data_model(self, existing_handling: Literal["fail", "skip", "update", "force"] = "skip"):
"""Export the verified DMS data model to CDF.

Args:
existing_handling: How to handle if component of data model exists. Defaults to "skip".

"""
if not self._state.last_verified_dms_rules:
raise ValueError("No verified DMS data model available")

exporter = exporters.DMSExporter()
exporter = exporters.DMSExporter(existing_handling=existing_handling)

if not self._client:
raise ValueError("No client provided!")
Expand Down
2 changes: 1 addition & 1 deletion cognite/neat/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.96.1"
__version__ = "0.96.2"
8 changes: 8 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Changes are grouped as follows:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [0.96.2] - 05-11-**2024**
### Added
- Can configure `neat.to.cdf.data_model` behavior for data model components that already exist in CDF

### Changed
- When reading a data model from CDF, `inwards` edges are now treated as an edge with direction inwards and
not the reverse edge.

## [0.96.1] - 04-11-**2024**
### Fixed
- `naet.show` working in a pyodide environment
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cognite-neat"
version = "0.96.1"
version = "0.96.2"
readme = "README.md"
description = "Knowledge graph transformation"
authors = [
Expand Down
2 changes: 1 addition & 1 deletion tests/data/windturbine.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
"MetMast",
"windTurbines",
"WindTurbine",
connection="reverse(property=metmasts)",
connection="edge(properties=Distance, type=distance, direction=inwards)",
is_list=True,
),
DMSInputProperty(
Expand Down
96 changes: 96 additions & 0 deletions tests/tests_unit/rules/test_importers/test_dms_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from cognite.neat._rules.importers import DMSImporter, ExcelImporter
from cognite.neat._rules.models import DMSRules, DMSSchema, RoleTypes
from cognite.neat._rules.transformers import DMSToInformation, ImporterPipeline, VerifyDMSRules
from cognite.neat._utils.cdf.data_classes import ContainerApplyDict, SpaceApplyDict, ViewApplyDict
from tests.config import DOC_RULES
from tests.data import windturbine

Expand Down Expand Up @@ -98,6 +99,18 @@ def test_import_rules_properties_with_edge_properties_units_and_enum(self) -> No
dms_recreated.containers[windturbine.DISTANCE_CONTAINER_ID].dump() == windturbine.DISTANCE_CONTAINER.dump()
)

def test_import_export_schema_with_inwards_edge_with_properties(self) -> None:
importer = DMSImporter(SCHEMA_INWARDS_EDGE_WITH_PROPERTIES)

result = ImporterPipeline.try_verify(importer)
rules = cast(DMSRules, result.rules)
issues = result.issues
assert len(issues) == 0

dms_recreated = DMSExporter().export(rules)

assert dms_recreated.views.dump() == SCHEMA_INWARDS_EDGE_WITH_PROPERTIES.views.dump()


SCHEMA_WITH_DIRECT_RELATION_NONE = DMSSchema(
data_model=dm.DataModelApply(
Expand Down Expand Up @@ -132,3 +145,86 @@ def test_import_rules_properties_with_edge_properties_units_and_enum(self) -> No
)
},
)
SCHEMA_INWARDS_EDGE_WITH_PROPERTIES = DMSSchema(
data_model=dm.DataModelApply(
space="neat",
external_id="data_model",
version="1",
description="Creator: MISSING",
views=[
dm.ViewId("neat", "EdgeView", "1"),
dm.ViewId("neat", "NodeView1", "1"),
dm.ViewId("neat", "NodeView2", "1"),
],
),
spaces=SpaceApplyDict([dm.SpaceApply(space="neat")]),
views=ViewApplyDict(
[
dm.ViewApply(
space="neat",
external_id="NodeView1",
version="1",
filter=dm.filters.Equals(
["node", "type"],
value={
"space": "neat",
"externalId": "NodeView1",
},
),
properties={
"to": dm.MultiEdgeConnectionApply(
type=dm.DirectRelationReference("neat", "myEdgeType"),
source=dm.ViewId("neat", "NodeView2", "1"),
edge_source=dm.ViewId("neat", "EdgeView", "1"),
direction="inwards",
)
},
),
dm.ViewApply(
space="neat",
external_id="NodeView2",
version="1",
filter=dm.filters.Equals(
["node", "type"],
value={
"space": "neat",
"externalId": "NodeView2",
},
),
properties={
"from": dm.MultiEdgeConnectionApply(
type=dm.DirectRelationReference("neat", "myEdgeType"),
source=dm.ViewId("neat", "NodeView1", "1"),
edge_source=dm.ViewId("neat", "EdgeView", "1"),
direction="outwards",
)
},
),
dm.ViewApply(
space="neat",
external_id="EdgeView",
version="1",
filter=dm.filters.HasData(containers=[dm.ContainerId("neat", "container")]),
properties={
"distance": dm.MappedPropertyApply(
container=dm.ContainerId("neat", "container"),
container_property_identifier="distance",
)
},
),
]
),
containers=ContainerApplyDict(
[
dm.ContainerApply(
space="neat",
external_id="container",
properties={
"distance": dm.ContainerProperty(
type=dm.data_types.Float64(),
)
},
)
]
),
)
Loading