LGF (Labeled Graph Format) Documentation¶
The Labeled Graph Format (LGF) is a human-readable text format for representing graphs with nodes, edges, and attributes. IronWeaver provides comprehensive support for parsing LGF files with both programmatic and file-based interfaces.
Quick Start¶
from ironweaver import parse_lgf, parse_lgf_file
lgf_text = """
alice Person
name = "Alice"
-knows-> bob
bob Person
"""
# Parse from string
graph = parse_lgf(lgf_text)
graph["alice"].attr # {'labels': ['Person'], 'name': 'Alice'}
# Parse from file
graph = parse_lgf_file("graph.lgf")
LGF Syntax¶
Node Declaration¶
Nodes are declared with an identifier followed by optional labels:
node_id Label1 Label2
attribute1 = "value"
attribute2 = 42
attribute3 = true
Example:
alice Person Employee
name = "Alice Johnson"
age = 30
is_manager = true
salary = 75000
Edge Declaration¶
IronWeaver supports the modern LGF syntax for edge declarations:
Forward Relationships¶
Create an edge from the current node to a target node:
node_id NodeType
-relationship_type-> target_node
Example:
alice Person
name = "Alice"
-knows-> bob
-works_at-> tech_corp
-manages-> charlie
start_date = "2023-01-15"
Inverse Relationships¶
Create an edge from a target node to the current node:
node_id NodeType
<-relationship_type- source_node
Example:
alice Person
name = "Alice"
<-reports_to- bob
<-founded_by- tech_corp
Edge Attributes¶
Edges can have attributes specified with indented lines after the edge declaration:
alice Person
name = "Alice"
-knows-> bob
since = "2020-03-15"
strength = 0.8
context = "work"
Data Types¶
LGF supports several data types with automatic parsing:
Strings¶
name = "Alice Johnson" # Double quotes
title = 'Software Engineer' # Single quotes
description = Unquoted text # Unquoted (treated as string)
Numbers¶
age = 30 # Integer
salary = 75000.50 # Float
score = -5 # Negative numbers
Integer literals, including those with a leading + or -, are parsed as
Python integers and retain their exact value.
Booleans¶
is_active = true
is_deleted = false
Special Values¶
Empty or missing values are handled gracefully:
optional_field = # Empty value (empty string)
Advanced Features¶
Import Support¶
LGF files can import other LGF files using the import() directive:
import("shared_nodes.lgf")
import("relationships.lgf")
# Additional nodes and edges specific to this file
alice Person
name = "Alice"
-knows-> bob # bob might be defined in shared_nodes.lgf
Import Rules: - Import paths are relative to the importing file - Imported files are processed before the current file content - Nodes defined in imported files can be referenced in the current file - If a node is defined in multiple files, attributes are merged
Complex Example¶
Here's a comprehensive example showing various LGF features:
# Define persons
alice Person Employee
name = "Alice Johnson"
age = 30
department = "Engineering"
is_manager = true
-knows-> bob
since = "2020-01-15"
relationship_type = "colleague"
-knows-> charlie
since = "2019-06-20"
relationship_type = "friend"
-manages-> charlie
start_date = "2022-03-01"
-works_at-> tech_corp
position = "Senior Developer"
start_date = "2019-01-10"
bob Person Employee
name = "Bob Smith"
age = 28
department = "Engineering"
is_manager = false
-works_at-> tech_corp
position = "Developer"
start_date = "2020-01-15"
<-reports_to- alice
charlie Person Employee
name = "Charlie Brown"
age = 26
department = "Engineering"
is_manager = false
-works_at-> tech_corp
position = "Junior Developer"
start_date = "2021-03-01"
tech_corp Company
name = "Tech Corporation"
founded = "2015-05-20"
industry = "Software"
size = "medium"
<-founded_by- alice
role = "Co-founder"
equity = 0.25
Python API¶
Function Reference¶
parse_lgf(text, graph=None, base_path=None)¶
Parse LGF content from a string.
Parameters:
- text (str): LGF content as string
- graph (Vertex, optional): Existing graph to add nodes/edges to
- base_path (str, optional): Base path for resolving import statements
Returns:
- Vertex: The parsed graph
Example:
from ironweaver import parse_lgf
lgf_content = """
alice Person
name = "Alice"
-knows-> bob
bob Person
name = "Bob"
"""
graph = parse_lgf(lgf_content)
print(f"Nodes: {list(graph.keys())}") # ['alice', 'bob']
parse_lgf_file(path)¶
Parse LGF content from a file.
Parameters:
- path (str): Path to the LGF file
Returns:
- Vertex: The parsed graph
Example:
from ironweaver import parse_lgf_file
graph = parse_lgf_file("my_graph.lgf")
print(f"Loaded {graph.node_count()} nodes")
Working with Parsed Graphs¶
Once parsed, LGF graphs are standard IronWeaver Vertex objects with full API support:
Assuming social_network.lgf contains the complex example above:
# Parse LGF
graph = parse_lgf_file("social_network.lgf")
# Access nodes
alice = graph.get_node("alice")
print(f"Alice's attributes: {dict(alice.attr)}")
# Access edges
for edge in alice.edges:
print(f"Alice {edge.attr['type']} {edge.to_node.id}")
# Use graph algorithms
shortest_path = graph.shortest_path("alice", "charlie", method="bfs")
print(f"Path from Alice to Charlie: {list(shortest_path.keys())}")
# Convert to NetworkX for visualization
import networkx as nx
import matplotlib.pyplot as plt
nx_graph = graph.to_networkx()
pos = nx.spring_layout(nx_graph)
nx.draw(nx_graph, pos, with_labels=True, node_color='lightblue')
plt.show()
Migration Notes¶
Deprecated Syntax¶
The old LGF syntax -> target relationship is no longer supported as of version X.X.X. Please update your LGF files to use the new syntax:
Old (Deprecated):
alice Person
-> bob knows
New (Supported):
alice Person
-knows-> bob
Breaking Changes¶
- Version X.X.X: Removed support for old arrow syntax (
-> target relationship) - The new syntax provides better readability and consistency
- Edge attributes are now properly supported with the new syntax
Best Practices¶
File Organization¶
- Use imports for shared node definitions:
# people.lgf alice Person name = "Alice" bob Person name = "Bob"
# relationships.lgf
import("people.lgf")
alice Person
-knows-> bob
- Group related nodes together in the same file
- Use meaningful node IDs that reflect the domain
Naming Conventions¶
- Node IDs: Use snake_case or camelCase consistently
- Attributes: Use descriptive names
- Relationships: Use verb phrases (
knows,works_at,reports_to)
Performance Tips¶
- Large graphs: Consider breaking into multiple files with imports
- Attribute types: Use appropriate data types (numbers vs strings)
- File size: LGF parsing is efficient, but very large files may benefit from binary formats for production use
Error Handling¶
Common parsing errors and solutions:
Syntax Errors¶
# Error: Missing arrow
alice Person
knows bob # ❌ Invalid
alice Person
-knows-> bob # ✅ Correct
Import Errors¶
# Handle missing files gracefully
try:
graph = parse_lgf_file("missing.lgf")
except FileNotFoundError:
print("LGF file not found")
Invalid Data Types¶
# All these are valid and will be parsed appropriately
alice Person
age = not_a_number # Parsed as string "not_a_number"
score = "42" # Parsed as string "42"
count = 42 # Parsed as integer 42
Writing LGF¶
to_lgf serializes a graph back to LGF; parsing the result yields the same
nodes, labels, attributes and edges:
from ironweaver import parse_lgf, to_lgf, to_lgf_file
text = to_lgf(graph, order=["start", "end"], banners={"start": "Header"})
assert parse_lgf(text).node_count() == graph.node_count()
to_lgf_file(graph, "graph.lgf")
Lists are written in the multi-line form. Values LGF cannot represent raise
ValueError: multi-line strings, list items containing ], nested dicts,
and node IDs, labels or edge types containing whitespace.
Examples Repository¶
More examples can be found in the examples/ directory:
- social_network.lgf - Social media relationships
- organization.lgf - Company hierarchy
- knowledge_graph.lgf - Domain knowledge representation