Relationship Management System in Python
This challenge focuses on building a simplified relationship management system using Python. You'll be designing and implementing classes to represent entities (like people or organizations) and the relationships between them. This is a fundamental concept in many applications, from social networks to knowledge graphs.
Problem Description
You are tasked with creating a system to manage entities and their relationships. The system should allow you to:
- Define Entities: Create entities with a name and an optional description.
- Establish Relationships: Define relationships between entities, specifying the type of relationship (e.g., "friend", "works_at", "parent_of"). Relationships should be directional (A is related to B, but B is not necessarily related to A in the same way).
- Query Relationships: Retrieve all relationships an entity has, and retrieve all entities related to a given entity through a specific relationship type.
Key Requirements:
- Implement two classes:
EntityandRelationshipManager. - The
Entityclass should have attributes forname(string) anddescription(string, optional). - The
RelationshipManagerclass should manage entities and relationships between them. It should have methods for:add_entity(entity): Adds an entity to the system.add_relationship(source_entity, target_entity, relationship_type): Creates a relationship between two entities.source_entityandtarget_entityareEntityobjects.relationship_typeis a string.get_relationships(entity): Returns a list of tuples, where each tuple represents a relationship and contains(target_entity, relationship_type).get_related_entities(entity, relationship_type): Returns a list ofEntityobjects that are related to the given entity through the specifiedrelationship_type.
Expected Behavior:
The system should handle adding entities, creating relationships, and querying relationships correctly. It should gracefully handle cases where an entity doesn't exist or a relationship type is invalid.
Edge Cases to Consider:
- Adding the same entity multiple times (should only store one instance).
- Adding the same relationship between the same entities multiple times (should only store one instance).
- Querying for relationships or related entities when no relationships exist.
- Handling invalid entity names (e.g., empty strings).
- Case sensitivity of relationship types (consider whether they should be treated as case-insensitive).
Examples
Example 1:
Input:
entity1 = Entity("Alice", "Software Engineer")
entity2 = Entity("Bob", "Project Manager")
entity3 = Entity("Charlie", "Data Scientist")
manager = RelationshipManager()
manager.add_entity(entity1)
manager.add_entity(entity2)
manager.add_entity(entity3)
manager.add_relationship(entity1, entity2, "works_with")
manager.add_relationship(entity2, entity3, "manages")
Output:
manager.get_relationships(entity1) # Output: [(entity2, 'works_with')]
manager.get_related_entities(entity2, "manages") # Output: [entity3]
Explanation: Alice works with Bob, and Bob manages Charlie. The queries correctly retrieve these relationships.
Example 2:
Input:
entity1 = Entity("David")
entity2 = Entity("Eve")
manager = RelationshipManager()
manager.add_entity(entity1)
manager.add_entity(entity2)
manager.add_relationship(entity1, entity2, "friend")
Output:
manager.get_relationships(entity1) # Output: [(entity2, 'friend')]
manager.get_related_entities(entity1, "friend") # Output: [entity2]
manager.get_related_entities(entity1, "sibling") # Output: []
Explanation: David is a friend of Eve. Querying for siblings returns an empty list because there's no such relationship.
Example 3: (Edge Case)
Input:
entity1 = Entity("Frank")
entity2 = Entity("Frank") # Same name, different object
manager = RelationshipManager()
manager.add_entity(entity1)
manager.add_entity(entity2) # Should only add one "Frank" entity
Output:
manager.get_entities() # Output: [entity1] (entity2 is not added)
Explanation: Adding the same entity twice should result in only one instance of that entity being stored.
Constraints
- Entity names must be unique within the
RelationshipManager. - Relationship types must be strings.
- The number of entities and relationships can be up to 1000.
- The length of entity names and relationship types should not exceed 50 characters.
- Performance:
add_entity,add_relationship,get_relationships, andget_related_entitiesshould complete within a reasonable time (e.g., less than 0.1 seconds) for the given constraints.
Notes
- Consider using dictionaries or other data structures to efficiently store and retrieve entities and relationships.
- Think about how to handle duplicate relationships. Should they be allowed, or should the system prevent them? The current specification implies preventing duplicates.
- You can choose to implement a
get_entities()method to retrieve all entities in the system, but it is not strictly required. - Error handling (e.g., raising exceptions for invalid input) is encouraged but not mandatory. Focus on the core functionality first.
- Consider the implications of case sensitivity for relationship types. You might want to normalize them to lowercase for consistency.