Skip to content
Snippets Groups Projects
Commit 25507419 authored by Anjali Aggarwal's avatar Anjali Aggarwal
Browse files

python script for owl file generation from schema

parent 1644f259
No related branches found
No related tags found
1 merge request!14Resolve "pizza example"
Pipeline #123285 failed
import yaml
from linkml_runtime.linkml_model.meta import SchemaDefinition, ClassDefinition, SlotDefinition, EnumDefinition
from linkml_runtime.utils.schemaview import SchemaView
from rdflib import Graph, Literal, RDF, RDFS, OWL, URIRef, Namespace
def load_schema(schema_file: str) -> SchemaDefinition:
with open(schema_file, 'r') as f:
schema_dict = yaml.safe_load(f)
return SchemaDefinition(**schema_dict)
def convert_to_owl(schema: SchemaDefinition, output_file: str):
g = Graph()
base_uri = URIRef(schema.id)
ns = Namespace(base_uri)
g.bind('owl', OWL)
g.bind('rdfs', RDFS)
g.bind('ex', ns)
# Convert classes
for cls_name, cls in schema.classes.items():
class_uri = URIRef(base_uri + cls_name)
g.add((class_uri, RDF.type, OWL.Class))
g.add((class_uri, RDFS.label, Literal(cls_name)))
if cls.description:
g.add((class_uri, RDFS.comment, Literal(cls.description)))
if cls.is_a:
parent_class_uri = URIRef(base_uri + cls.is_a)
g.add((class_uri, RDFS.subClassOf, parent_class_uri))
# Convert slots (properties)
for slot_name, slot in schema.slots.items():
slot_uri = URIRef(base_uri + slot_name)
g.add((slot_uri, RDF.type, OWL.ObjectProperty if slot.range == 'uriorcurie' else OWL.DatatypeProperty))
g.add((slot_uri, RDFS.label, Literal(slot_name)))
if slot.description:
g.add((slot_uri, RDFS.comment, Literal(slot.description)))
if slot.domain:
domain_class_uri = URIRef(base_uri + slot.domain)
g.add((slot_uri, RDFS.domain, domain_class_uri))
if slot.range:
range_class_uri = URIRef(base_uri + slot.range)
g.add((slot_uri, RDFS.range, range_class_uri))
# Convert enums
for enum_name, enum in schema.enums.items():
enum_uri = URIRef(base_uri + enum_name)
g.add((enum_uri, RDF.type, OWL.Class))
g.add((enum_uri, RDFS.label, Literal(enum_name)))
for pv_name, pv in enum.permissible_values.items():
pv_uri = URIRef(base_uri + pv_name)
g.add((pv_uri, RDF.type, OWL.NamedIndividual))
g.add((pv_uri, RDF.type, enum_uri))
g.add((pv_uri, RDFS.label, Literal(pv_name)))
if pv.description:
g.add((pv_uri, RDFS.comment, Literal(pv.description)))
# Save the graph to a file
g.serialize(destination=output_file, format='xml')
# Main function
def main():
schema_file = 'pizza.yaml'
output_file = 'pizza_schema.owl'
schema = load_schema(schema_file)
convert_to_owl(schema, output_file)
print(f'Converted {schema_file} to {output_file}')
if __name__ == '__main__':
main()
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>
<rdf:Description rdf:about="http://example.org/pizzaSpicinessLevel">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
<rdfs:label>SpicinessLevel</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaHot">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaSpicinessLevel"/>
<rdfs:label>Hot</rdfs:label>
<rdfs:comment>Hot level of spiciness</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaStuffedCrustBase">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaPizzaBase"/>
<rdfs:label>StuffedCrustBase</rdfs:label>
<rdfs:comment>Thick Base</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaWholeWheatBase">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaPizzaBase"/>
<rdfs:label>WholeWheatBase</rdfs:label>
<rdfs:comment>Thick Base</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaMild">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaSpicinessLevel"/>
<rdfs:label>Mild</rdfs:label>
<rdfs:comment>Mild level of spiciness</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaDeepPanBase">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaPizzaBase"/>
<rdfs:label>DeepPanBase</rdfs:label>
<rdfs:comment>Thick Base</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaThinAndCrispyBase">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaPizzaBase"/>
<rdfs:label>ThinAndCrispyBase</rdfs:label>
<rdfs:comment>Thin Base</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaMedium">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaSpicinessLevel"/>
<rdfs:label>Medium</rdfs:label>
<rdfs:comment>Medium level of spiciness</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaPizza">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
<rdfs:label>Pizza</rdfs:label>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaNone">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
<rdf:type rdf:resource="http://example.org/pizzaSpicinessLevel"/>
<rdfs:label>None</rdfs:label>
<rdfs:comment>No spiciness</rdfs:comment>
</rdf:Description>
<rdf:Description rdf:about="http://example.org/pizzaPizzaBase">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
<rdfs:label>PizzaBase</rdfs:label>
</rdf:Description>
</rdf:RDF>
Example/pizza_schema.png

352 KiB

import rdflib
import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_pydot import graphviz_layout
# Load the OWL file into an rdflib graph
def load_owl(file_path):
g = rdflib.Graph()
g.parse(file_path, format='xml')
return g
# Convert rdflib graph to NetworkX graph with safe node names
def rdflib_to_networkx(graph):
nx_graph = nx.DiGraph()
for s, p, o in graph:
safe_s = str(s).replace(':', '_')
safe_o = str(o).replace(':', '_')
safe_p = str(p).replace(':', '_')
nx_graph.add_edge(safe_s, safe_o, label=safe_p)
return nx_graph
# Visualize the graph using Matplotlib and Graphviz, and save as PNG
def visualize_graph(nx_graph, output_file):
pos = graphviz_layout(nx_graph, prog='dot')
plt.figure(figsize=(12, 12))
labels = nx.get_edge_attributes(nx_graph, 'label')
nx.draw(nx_graph, pos, with_labels=True, node_size=3000, node_color='skyblue', font_size=10, font_weight='bold', arrows=True)
nx.draw_networkx_edge_labels(nx_graph, pos, edge_labels=labels, font_size=8)
plt.savefig(output_file, format='png')
plt.show()
def main():
owl_file = 'pizza_schema.owl'
output_file = 'pizza_schema.png'
rdf_graph = load_owl(owl_file)
nx_graph = rdflib_to_networkx(rdf_graph)
visualize_graph(nx_graph, output_file)
print(f'Graph saved as {output_file}')
if __name__ == '__main__':
main()
import yaml
import networkx as nx
import matplotlib.pyplot as plt
from networkx.drawing.nx_pydot import graphviz_layout
# Load the YAML schema
def load_yaml(file_path):
with open(file_path, 'r') as f:
return yaml.safe_load(f)
# Convert YAML schema to NetworkX graph
def yaml_to_networkx(schema):
nx_graph = nx.DiGraph()
classes = schema.get('classes', {})
enums = schema.get('enums', {})
# Add classes and attributes
for class_name, class_info in classes.items():
nx_graph.add_node(class_name, type='class')
attributes = class_info.get('attributes', {})
for attr_name, attr_info in attributes.items():
attr_range = attr_info.get('range', 'unknown')
nx_graph.add_node(attr_name, type='attribute', range=attr_range)
nx_graph.add_edge(class_name, attr_name, label='has attribute')
nx_graph.add_edge(attr_name, attr_range, label='range')
# Add enums and their values
for enum_name, enum_info in enums.items():
nx_graph.add_node(enum_name, type='enum')
permissible_values = enum_info.get('permissible_values', {})
for value_name, value_info in permissible_values.items():
nx_graph.add_node(value_name, type='enum_value', description=value_info.get('description', ''))
nx_graph.add_edge(enum_name, value_name, label='has value')
return nx_graph
# Visualize the graph using Matplotlib and Graphviz, and save as PNG
def visualize_graph(nx_graph, output_file):
pos = graphviz_layout(nx_graph, prog='dot')
plt.figure(figsize=(12, 12))
node_labels = {node: f"{node}\n({data['type']})" for node, data in nx_graph.nodes(data=True)}
edge_labels = nx.get_edge_attributes(nx_graph, 'label')
nx.draw(nx_graph, pos, labels=node_labels, with_labels=True, node_size=3000, node_color='skyblue', font_size=10, font_weight='bold', arrows=True)
nx.draw_networkx_edge_labels(nx_graph, pos, edge_labels=edge_labels, font_size=8)
plt.savefig(output_file, format='png')
plt.show()
def main():
yaml_file = 'pizza_schema.yaml'
output_file = 'pizza_schema.png'
schema = load_yaml(yaml_file)
nx_graph = yaml_to_networkx(schema)
visualize_graph(nx_graph, output_file)
print(f'Graph saved as {output_file}')
if __name__ == '__main__':
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment