Skip to content

Commit

Permalink
fixed bug occuring when no node/relation is specified for an entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jkminder committed Mar 22, 2022
1 parent e701f15 commit c68117d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
73 changes: 37 additions & 36 deletions rel2graph/core/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,42 +389,43 @@ def compile(self, entity_type, config_data):

precompiled_entity = []

for element_config, attributes in config_data.items():
# Grab id
match = re.search("[\)]\s*(\w+)\s*$", element_config)
id = None
if match is not None:
id = match.group(1)
self._ids.add(id)

# Precompile attributes
precompiled_attributes = []
primary_key = "&None"
if attributes is not None:
for attribute in attributes:
config, key, primary = self._precompile_attribute(attribute)
precompiled_attributes.append(config)
# if an id exist we save this attribute for later reference
if id is not None:
self._saved_attributes[f"{id}.{key}"] = config

# if primary key we save it
if primary:
if primary_key == "&None":
primary_key = key
else:
raise ConfigError(f"Error in config for entity '{self._entity_type}'{' in graph element with identifier - ' + id if id is not None else ''}: Only 1 primary key allowed")
precompiled_graph_element = self._precompile_graph_element(element_config)

# Add attributes to the string
precompiled_attributes_str = "[" + ",".join(precompiled_attributes) + "]"
precompiled_graph_element = precompiled_graph_element.format(attributes=precompiled_attributes_str, parent=self._entity_type, id=id, primary_key = primary_key)

# Remove compile markers
precompiled_graph_element = precompiled_graph_element.replace("&", "")

# append to list
precompiled_entity.append(precompiled_graph_element)
if config_data is not None:
for element_config, attributes in config_data.items():
# Grab id
match = re.search("[\)]\s*(\w+)\s*$", element_config)
id = None
if match is not None:
id = match.group(1)
self._ids.add(id)

# Precompile attributes
precompiled_attributes = []
primary_key = "&None"
if attributes is not None:
for attribute in attributes:
config, key, primary = self._precompile_attribute(attribute)
precompiled_attributes.append(config)
# if an id exist we save this attribute for later reference
if id is not None:
self._saved_attributes[f"{id}.{key}"] = config

# if primary key we save it
if primary:
if primary_key == "&None":
primary_key = key
else:
raise ConfigError(f"Error in config for entity '{self._entity_type}'{' in graph element with identifier - ' + id if id is not None else ''}: Only 1 primary key allowed")
precompiled_graph_element = self._precompile_graph_element(element_config)

# Add attributes to the string
precompiled_attributes_str = "[" + ",".join(precompiled_attributes) + "]"
precompiled_graph_element = precompiled_graph_element.format(attributes=precompiled_attributes_str, parent=self._entity_type, id=id, primary_key = primary_key)

# Remove compile markers
precompiled_graph_element = precompiled_graph_element.replace("&", "")

# append to list
precompiled_entity.append(precompiled_graph_element)

# Convert to instructions
node_instructions, relation_instructions = _parse_to_instructions(precompiled_entity)
Expand Down
1 change: 1 addition & 0 deletions rel2graph/core/factories/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def construct(self, resource: Resource) -> Subgraph:
type = self._type.construct(resource)
logger.debug(f"For relation type {type.value} matched {len(from_nodes)} from_nodes and {len(to_nodes)} to nodes")
attributes = [attr_factory.construct(resource) for attr_factory in self._attributes]
attributes = [attr for attr in attributes if attr is not None]
relations = Subgraph()
for from_node in from_nodes:
for to_node in to_nodes:
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/core/resources/duplicated_nodes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ENTITY("entity"):
NODE("label"):
+ uid = "First"

NODE("label"):
+ uid = "Second"

NODE("label"):
+ uid = "Third"
1 change: 1 addition & 0 deletions tests/unit/core/resources/empty_entity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENTITY("entity"):
7 changes: 6 additions & 1 deletion tests/unit/core/test_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ def test_dynkeys():
for rf in relation_supplychain.factories:
assert(rf._type._entity_attribute == "dynamic_key")
for attr in nf._attributes:
assert(attr._entity_attribute == "dynamic_key")
assert(attr._entity_attribute == "dynamic_key")

def test_empty_entity():
node_supplychain, relation_supplychain = parse(get_filepath("empty_entity"))["entity"]
assert(len(node_supplychain.factories) == 0)
assert(len(relation_supplychain.factories) == 0)

0 comments on commit c68117d

Please sign in to comment.