-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbvss_model.py
More file actions
212 lines (179 loc) · 7.82 KB
/
bvss_model.py
File metadata and controls
212 lines (179 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
BVSS (Barwise Visual Symbol System) Model Converter for OWL Ontologies
This module converts OWL ontologies to BVSS-compatible graph structures
for visualization, with special focus on BFO (Basic Formal Ontology) alignment.
"""
import logging
from owlready2 import get_ontology
from bfo_2020_definitions import BFO_2020_CLASSES, BFO_2020_RELATIONS
logger = logging.getLogger(__name__)
class BVSSConverter:
"""
Converts OWL ontologies to BVSS visualization format.
"""
def __init__(self):
self.bfo_classes = BFO_2020_CLASSES
self.bfo_relations = BFO_2020_RELATIONS
def extract_bvss_graph(self, ontology_path):
"""
Extract a BVSS-compatible graph structure from an OWL ontology.
Args:
ontology_path (str): Path to the OWL ontology file
Returns:
dict: Graph structure with nodes and edges for BVSS visualization
"""
try:
onto = get_ontology(ontology_path).load()
nodes = []
edges = []
# Extract classes as nodes
for cls in onto.classes():
if cls.name: # Skip anonymous classes
node = {
"id": str(cls.iri),
"name": cls.name,
"label": cls.name,
"type": self.classify_bfo_type(cls),
"description": self.get_class_description(cls),
"temporal": self.has_temporal_aspects(cls)
}
nodes.append(node)
# Extract inheritance relationships
for parent in cls.is_a:
if hasattr(parent, 'iri') and parent.name:
edge = {
"source": str(parent.iri),
"target": str(cls.iri),
"relation": "is_a",
"type": "inheritance"
}
edges.append(edge)
# Extract object properties as relationships
for prop in onto.object_properties():
if prop.name:
# Add property as a node if it represents a significant relationship
prop_node = {
"id": str(prop.iri),
"name": prop.name,
"label": prop.name,
"type": "Relation",
"description": self.get_property_description(prop),
"temporal": False
}
nodes.append(prop_node)
# Connect domains and ranges if specified
if prop.domain:
for domain_cls in prop.domain:
if hasattr(domain_cls, 'iri'):
edge = {
"source": str(domain_cls.iri),
"target": str(prop.iri),
"relation": "has_property",
"type": "domain"
}
edges.append(edge)
if prop.range:
for range_cls in prop.range:
if hasattr(range_cls, 'iri'):
edge = {
"source": str(prop.iri),
"target": str(range_cls.iri),
"relation": "points_to",
"type": "range"
}
edges.append(edge)
return {
"nodes": nodes,
"edges": edges,
"ontology_info": {
"name": onto.name or "Unknown",
"base_iri": onto.base_iri,
"class_count": len([n for n in nodes if n["type"] != "Relation"]),
"property_count": len([n for n in nodes if n["type"] == "Relation"])
}
}
except Exception as e:
logger.error(f"Error extracting BVSS graph from {ontology_path}: {str(e)}")
return {
"nodes": [],
"edges": [],
"error": str(e),
"ontology_info": {
"name": "Error",
"base_iri": "",
"class_count": 0,
"property_count": 0
}
}
def classify_bfo_type(self, cls):
"""
Classify a class according to BFO categories for BVSS visualization.
Args:
cls: OWL class from owlready2
Returns:
str: BFO type category
"""
class_name = cls.name.lower() if cls.name else ""
class_iri = str(cls.iri).lower()
# Check for direct BFO class matches
for bfo_class_id, bfo_info in self.bfo_classes.items():
if (bfo_class_id.lower() in class_name or
bfo_class_id.lower().replace('_', '') in class_name.replace('_', '')):
return bfo_info.get('category', 'IndependentContinuant')
# Heuristic classification based on naming patterns
if any(term in class_name for term in ['process', 'event', 'activity', 'action']):
return "Process"
elif any(term in class_name for term in ['role', 'function', 'disposition', 'quality']):
return "DependentContinuant"
elif any(term in class_name for term in ['entity', 'object', 'material', 'substance']):
return "IndependentContinuant"
elif any(term in class_name for term in ['temporal', 'time', 'moment', 'interval']):
return "TemporalRegion"
elif any(term in class_name for term in ['spatial', 'region', 'location', 'place']):
return "SpatialRegion"
else:
# Default classification
return "IndependentContinuant"
def has_temporal_aspects(self, cls):
"""
Determine if a class has temporal aspects for BVSS visualization.
Args:
cls: OWL class from owlready2
Returns:
bool: True if the class has temporal aspects
"""
class_name = cls.name.lower() if cls.name else ""
temporal_keywords = ['temporal', 'time', 'moment', 'interval', 'duration', 'period']
return any(keyword in class_name for keyword in temporal_keywords)
def get_class_description(self, cls):
"""
Extract description/comment from a class.
Args:
cls: OWL class from owlready2
Returns:
str: Description of the class
"""
if cls.comment:
return str(cls.comment[0]) if cls.comment else ""
return f"OWL class: {cls.name}"
def get_property_description(self, prop):
"""
Extract description/comment from a property.
Args:
prop: OWL property from owlready2
Returns:
str: Description of the property
"""
if prop.comment:
return str(prop.comment[0]) if prop.comment else ""
return f"OWL property: {prop.name}"
def extract_bvss_graph(ontology_path):
"""
Convenience function to extract BVSS graph from an ontology file.
Args:
ontology_path (str): Path to the OWL ontology file
Returns:
dict: BVSS graph structure
"""
converter = BVSSConverter()
return converter.extract_bvss_graph(ontology_path)