causality inference done

This commit is contained in:
2026-07-22 18:04:07 +02:00
parent 32154386e8
commit c918a6a428
13 changed files with 911 additions and 59 deletions

View File

@@ -44,6 +44,7 @@ from bedit.core.bond_graph import infer_causality
from bedit.core.simulation import Simulation
from bedit.core.port_types import PortTypeRegistry
from bedit.core.serializer import DocumentSerializer
from bedit.gui.preferences import application_settings
class DocumentController(QObject):
@@ -479,7 +480,9 @@ class DocumentController(QObject):
error_callback,
)
def _infer_active_graph_causality(self, component: Component) -> None:
def _infer_active_graph_causality(
self, component: Component, *, emit_reset: bool = True
) -> None:
"""Infer causality and copy the derived values into the live model."""
inferred = infer_causality(component.to_dict())
@@ -505,7 +508,8 @@ class DocumentController(QObject):
if changed:
if self.document is not None:
self.document.validate()
self.documentReset.emit()
if emit_reset:
self.documentReset.emit()
def set_route_waypoints(self, item_kind: str, item_id: str, waypoints: list[QPointF]) -> None:
item = (
@@ -1175,6 +1179,10 @@ class DocumentController(QObject):
def _insert_connection(self, owner_id: str, connection: Connection) -> None:
self._graph_for(owner_id).connections[connection.id] = connection
if self.document is not None and self._infer_causality_on_connection_change():
owner = self.document.find_component(owner_id)
if owner is not None:
self._infer_active_graph_causality(owner, emit_reset=False)
if owner_id == self.active_component_id:
self.connectionAdded.emit(connection.id)
self.documentReset.emit()
@@ -1211,10 +1219,20 @@ class DocumentController(QObject):
def _remove_connection(self, owner_id: str, connection_id: str) -> None:
self._graph_for(owner_id).connections.pop(connection_id, None)
if self.document is not None and self._infer_causality_on_connection_change():
owner = self.document.find_component(owner_id)
if owner is not None:
self._infer_active_graph_causality(owner, emit_reset=False)
if owner_id == self.active_component_id:
self.connectionRemoved.emit(connection_id)
self.documentReset.emit()
@staticmethod
def _infer_causality_on_connection_change() -> bool:
return application_settings().value(
"bondGraph/inferCausalityOnConnectionChange", True, type=bool
)
def _insert_annotation(self, owner_id: str, annotation: Annotation) -> None:
self._graph_for(owner_id).annotations[annotation.id] = annotation
if owner_id == self.active_component_id: