You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Behavior report: Element.decompile() replaces all attributes and does not roll back when validation fails (expected?) #15
Behavior report, not a feature request. This documents what 0.2.0 does so it can be marked as expected or unexpected.
Observed behavior
Element.decompile() reads attributes from the element's codec and replaces all of the element's model attributes with what the codec returned, then re-validates. Two consequences:
An attribute the codec does not return is removed from the element without any warning. (For example, a codec that covers only some of a type's attributes silently drops the others.)
If re-validation then fails, ValidationError is raised but the graph keeps the changed, invalid state. By contrast add_vertex rolls back when validation fails.
Minimal reproduction (kc only)
fromknowledgecompleximportSchemaBuilder, KnowledgeComplex, textclassDictCodec:
"""Stands in for any codec that covers only some attributes."""def__init__(self): self.store= {}
defcompile(self, element):
self.store[element["id"]] = {k: vfork, vinelement.items()
ifknotin ("id", "type", "uri")}
defdecompile(self, uri): returndict(self.store[uri])
sb=SchemaBuilder(namespace="ex")
sb.add_vertex_type("doc", attributes={"title": text(), "notes": text(required=False)})
kc=KnowledgeComplex(schema=sb)
codec=DictCodec(); kc.register_codec("doc", codec)
kc.add_vertex("d1", type="doc", title="T", notes="N", uri="d1")
print("before:", kc.element("d1").attrs)
codec.store["d1"] = {"title": "T2"} # codec returns only `title`kc.element("d1").decompile()
print("after:", kc.element("d1").attrs) # `notes` is gone, no warningcodec.store["d1"] = {"notes": "N2"} # codec omits the required `title`try:
kc.element("d1").decompile()
exceptExceptionase:
print("decompile raised", type(e).__name__)
print("graph afterwards:", kc.element("d1").attrs) # title gone, notes changedtry:
kc.add_vertex("bad", type="doc", notes="no title") # compare: add_vertexexceptExceptionase:
print("add_vertex raised", type(e).__name__)
print("'bad' present:", "bad"inkc.element_ids()) # False: rolled back
A failed write leaves the complex in a state that does not conform to its own schema. add_vertex shows the intended behavior is to roll back.
The README describes decompile as "markdown file -> KC", which reads like a refresh from the artifact. Dropping attributes the artifact does not mention is a stronger operation and is not documented.
Not checked
MarkdownCodec behaves the same way as far as I can tell, since the replacement happens in Element.decompile, but I have not run it. I also did not check compile or decompile_uri.
Related
Issue 14 (whole-graph validation on every write). It describes the validation behavior this one builds on: decompile ends with the same whole-graph validation.
The question
Is replacing all attributes, and not rolling back on a failed re-validation, intended? If so, a note in the documentation would help.
Workaround (untested beyond the first part)
Make the codec return every attribute of the element. The second part, checking the complex after a failed decompile, I have not tried.
Activity