or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

cli.mdcore-analysis.mddocumentation-generation.mdentity-classes.mdindex.mdsparql-querying.md

core-analysis.mddocs/

0

# Core Ontology Analysis

1

2

The core analysis functionality centers around the `Ontospy` class, which provides comprehensive tools for loading, parsing, and analyzing RDF ontologies. This is the primary interface for working with ontological data and extracting structured information from RDF graphs.

3

4

## Capabilities

5

6

### Loading Ontologies

7

8

Load RDF data from various sources including files, URLs, raw data strings, file objects, and SPARQL endpoints.

9

10

```python { .api }

11

class Ontospy:

12

def __init__(self, uri_or_path=None, data=None, file_obj=None,

13

rdf_format="", verbose=False, hide_base_schemas=True,

14

hide_implicit_types=True, hide_implicit_preds=True,

15

hide_individuals=True, sparql_endpoint=None,

16

credentials=None, build_all=True, pref_title="qname",

17

pref_lang="en"):

18

"""

19

Initialize Ontospy with optional data loading.

20

21

Parameters:

22

- uri_or_path (str): File path or URL to RDF data

23

- data (str): Raw RDF data as string

24

- file_obj: Python file object containing RDF data

25

- rdf_format (str): RDF format ("turtle", "xml", "n3", "json-ld", etc.)

26

- verbose (bool): Enable verbose output during processing

27

- hide_base_schemas (bool): Filter out base schema vocabulary (RDF, RDFS, OWL)

28

- hide_implicit_types (bool): Filter implicit type declarations

29

- hide_implicit_preds (bool): Filter implicit predicate declarations

30

- hide_individuals (bool): Skip extraction of class instances

31

- sparql_endpoint (str): SPARQL endpoint URL for remote queries

32

- credentials (tuple): Authentication credentials (username, password)

33

- build_all (bool): Automatically extract all entities after loading

34

- pref_title (str): Display preference ("qname" or "label")

35

- pref_lang (str): Preferred language for labels and descriptions

36

"""

37

38

def load_rdf(self, uri_or_path=None, data=None, file_obj=None,

39

rdf_format="", verbose=False):

40

"""

41

Load RDF data from specified source.

42

43

Parameters:

44

- uri_or_path (str): File path or URL to RDF data

45

- data (str): Raw RDF data as string

46

- file_obj: Python file object containing RDF data

47

- rdf_format (str): RDF format specification

48

- verbose (bool): Enable verbose loading output

49

"""

50

51

def load_sparql(self, sparql_endpoint, verbose=False, credentials=None):

52

"""

53

Connect to SPARQL endpoint for remote ontology access.

54

55

Parameters:

56

- sparql_endpoint (str): SPARQL endpoint URL

57

- verbose (bool): Enable verbose connection output

58

- credentials (tuple): Authentication credentials if required

59

"""

60

```

61

62

Usage example:

63

64

```python

65

import ontospy

66

67

# Load from file

68

g = ontospy.Ontospy("ontology.owl")

69

70

# Load from URL with specific format

71

g = ontospy.Ontospy("http://example.org/ontology.rdf", rdf_format="xml")

72

73

# Load raw RDF data

74

rdf_data = """

75

@prefix ex: <http://example.org/> .

76

ex:Person a rdfs:Class .

77

"""

78

g = ontospy.Ontospy(data=rdf_data, rdf_format="turtle")

79

80

# Connect to SPARQL endpoint

81

g = ontospy.Ontospy(sparql_endpoint="http://dbpedia.org/sparql")

82

```

83

84

### Entity Extraction

85

86

Extract and build structured representations of ontological components from loaded RDF data.

87

88

```python { .api }

89

def build_all(self, verbose=False, hide_base_schemas=True,

90

hide_implicit_types=True, hide_implicit_preds=True,

91

hide_individuals=True):

92

"""

93

Extract all ontological entities from loaded RDF graph.

94

95

Parameters:

96

- verbose (bool): Enable detailed extraction output

97

- hide_base_schemas (bool): Filter base vocabulary elements

98

- hide_implicit_types (bool): Filter implicit type declarations

99

- hide_implicit_preds (bool): Filter implicit predicate declarations

100

- hide_individuals (bool): Skip individual/instance extraction

101

"""

102

103

def build_classes(self, hide_base_schemas=True, hide_implicit_types=True):

104

"""Extract RDFS/OWL class definitions."""

105

106

def build_properties(self, hide_implicit_preds=True):

107

"""Extract RDF/OWL property definitions."""

108

109

def build_ontologies(self, exclude_BNodes=False, return_string=False):

110

"""Extract ontology metadata and declarations."""

111

112

def build_skos_concepts(self):

113

"""Extract SKOS concept hierarchy and definitions."""

114

115

def build_shapes(self):

116

"""Extract SHACL shape definitions and constraints."""

117

118

def build_individuals(self):

119

"""Extract class instances and individual resources."""

120

```

121

122

### Entity Retrieval

123

124

Access extracted ontological entities using various lookup methods.

125

126

```python { .api }

127

def get_class(self, id=None, uri=None, match=None):

128

"""

129

Retrieve class entity by identifier.

130

131

Parameters:

132

- id (int): Internal entity ID

133

- uri (str): Full URI of the class

134

- match (str): Pattern to match against class names

135

136

Returns:

137

OntoClass: Class entity or None if not found

138

"""

139

140

def get_property(self, id=None, uri=None, match=None):

141

"""

142

Retrieve property entity by identifier.

143

144

Returns:

145

OntoProperty: Property entity or None if not found

146

"""

147

148

def get_ontology(self, id=None, uri=None, match=None):

149

"""

150

Retrieve ontology entity by identifier.

151

152

Returns:

153

Ontology: Ontology entity or None if not found

154

"""

155

156

def get_skos(self, id=None, uri=None, match=None):

157

"""

158

Retrieve SKOS concept by identifier.

159

160

Returns:

161

OntoSKOSConcept: SKOS concept or None if not found

162

"""

163

164

def get_shapes(self, id=None, uri=None, match=None):

165

"""

166

Retrieve SHACL shape by identifier.

167

168

Returns:

169

OntoShape: Shape entity or None if not found

170

"""

171

172

def get_individual(self, id=None, uri=None, match=None):

173

"""

174

Retrieve individual/instance by identifier.

175

176

Returns:

177

RdfEntity: Individual entity or None if not found

178

"""

179

180

def get_any_entity(self, id=None, uri=None, match=None):

181

"""

182

Retrieve entity of any type by identifier.

183

184

Returns:

185

RdfEntity: Found entity or None

186

"""

187

```

188

189

### Hierarchy Analysis

190

191

Analyze and extract hierarchical relationships between ontological entities.

192

193

```python { .api }

194

def ontologyClassTree(self):

195

"""

196

Generate class hierarchy tree structure.

197

198

Returns:

199

TreeStructure: Nested representation of class hierarchy

200

"""

201

202

def ontologyPropTree(self):

203

"""

204

Generate property hierarchy tree structure.

205

206

Returns:

207

TreeStructure: Nested representation of property hierarchy

208

"""

209

210

def ontologyConceptTree(self):

211

"""

212

Generate SKOS concept hierarchy tree.

213

214

Returns:

215

TreeStructure: Nested SKOS concept relationships

216

"""

217

218

def ontologyShapeTree(self):

219

"""

220

Generate SHACL shapes tree structure.

221

222

Returns:

223

TreeStructure: Nested shape hierarchy

224

"""

225

226

def getInferredPropertiesForClass(self, aClass, rel="domain_of"):

227

"""

228

Get properties applicable to class including inherited properties.

229

230

Parameters:

231

- aClass (OntoClass): Target class entity

232

- rel (str): Relationship type ("domain_of" or "range_of")

233

234

Returns:

235

list: Properties applicable to the class

236

"""

237

```

238

239

### Query and Analysis

240

241

Execute SPARQL queries and perform statistical analysis on loaded ontologies.

242

243

```python { .api }

244

def query(self, sparql_query):

245

"""

246

Execute SPARQL query against loaded RDF graph.

247

248

Parameters:

249

- sparql_query (str): SPARQL query string

250

251

Returns:

252

rdflib.plugins.sparql.processor.SPARQLResult: Query results

253

"""

254

255

def sparql(self, sparql_query):

256

"""Alias for query() method."""

257

258

def stats(self):

259

"""

260

Generate comprehensive ontology statistics.

261

262

Returns:

263

StatsResult: List of (category, count) tuples including:

264

- ('Ontologies', count)

265

- ('Triples', count)

266

- ('Classes', count)

267

- ('Properties', count)

268

- ('Annotation Properties', count)

269

- ('Object Properties', count)

270

- ('Datatype Properties', count)

271

- ('SKOS Concepts', count)

272

- ('SHACL Shapes', count)

273

- ('Data Sources', count)

274

"""

275

276

def triplesCount(self):

277

"""

278

Count total triples in loaded graph.

279

280

Returns:

281

int: Number of RDF triples

282

"""

283

```

284

285

### Serialization and Export

286

287

Export ontology data in various RDF formats and generate serialized representations.

288

289

```python { .api }

290

def serialize(self, format="turtle"):

291

"""

292

Serialize loaded RDF graph to specified format.

293

294

Parameters:

295

- format (str): Output format ("turtle", "xml", "n3", "json-ld", "nt")

296

297

Returns:

298

str: Serialized RDF data

299

"""

300

301

def rdf_source(self, format="turtle"):

302

"""Alias for serialize() method."""

303

```

304

305

### Display and Visualization

306

307

Generate textual representations and hierarchical displays of ontological structures.

308

309

```python { .api }

310

def printClassTree(self, element=None, showids=False, labels=False, showtype=False):

311

"""

312

Print formatted class hierarchy tree.

313

314

Parameters:

315

- element (OntoClass): Root element for subtree (None for full tree)

316

- showids (bool): Include entity IDs in output

317

- labels (bool): Show labels instead of URIs

318

- showtype (bool): Include RDF type information

319

"""

320

321

def printPropertyTree(self, element=None, showids=False, labels=False, showtype=False):

322

"""Print formatted property hierarchy tree."""

323

324

def printSkosTree(self, element=None, showids=False, labels=False, showtype=False):

325

"""Print formatted SKOS concept hierarchy tree."""

326

327

def printShapesTree(self, element=None, showids=False, labels=False, showtype=False):

328

"""Print formatted SHACL shapes hierarchy tree."""

329

```

330

331

### Navigation Methods

332

333

Navigate through entity collections with helper methods for iteration and traversal.

334

335

```python { .api }

336

def nextClass(self, classuri):

337

"""

338

Get next class in ordered collection.

339

340

Parameters:

341

- classuri (str): Current class URI

342

343

Returns:

344

OntoClass: Next class entity or None

345

"""

346

347

def nextProperty(self, propuri):

348

"""Get next property in collection."""

349

350

def nextConcept(self, concepturi):

351

"""Get next SKOS concept in collection."""

352

353

def nextShapes(self, shapeuri):

354

"""Get next SHACL shape in collection."""

355

356

def nextOntology(self, ontologyuri):

357

"""Get next ontology in collection."""

358

```

359

360

## Key Properties

361

362

```python { .api }

363

# Entity collections (read-only lists)

364

all_ontologies: list[Ontology] # All extracted ontology instances

365

all_classes: list[OntoClass] # All extracted class entities

366

all_properties: list[OntoProperty] # All properties (all types combined)

367

all_properties_annotation: list[OntoProperty] # Annotation properties only

368

all_properties_object: list[OntoProperty] # Object properties only

369

all_properties_datatype: list[OntoProperty] # Datatype properties only

370

all_skos_concepts: list[OntoSKOSConcept] # All SKOS concepts

371

all_shapes: list[OntoShape] # All SHACL shapes

372

all_individuals: list[RdfEntity] # All class instances

373

374

# Hierarchy top-level entities

375

toplayer_classes: list[OntoClass] # Classes with no parent classes

376

toplayer_properties: list[OntoProperty] # Properties with no parent properties

377

toplayer_skos: list[OntoSKOSConcept] # Top-level SKOS concepts

378

toplayer_shapes: list[OntoShape] # Top-level SHACL shapes

379

380

# Graph metadata

381

namespaces: dict # Namespace prefix mappings in the graph

382

sources: list[str] # Data sources that were loaded

383

```

384

385

Usage example:

386

387

```python

388

import ontospy

389

390

# Load and analyze ontology

391

g = ontospy.Ontospy("foaf.rdf", build_all=True)

392

393

# Get statistics

394

stats = g.stats()

395

print(f"Found {len(g.all_classes)} classes and {len(g.all_properties)} properties")

396

397

# Find specific class

398

person_class = g.get_class(match="Person")

399

if person_class:

400

print(f"Person class: {person_class.uri}")

401

print(f"Properties in domain: {len(person_class.domain_of)}")

402

403

# Query the graph

404

results = g.query("SELECT ?s ?p ?o WHERE {?s ?p ?o} LIMIT 10")

405

for row in results:

406

print(f"{row.s} -> {row.p} -> {row.o}")

407

408

# Export to different format

409

turtle_output = g.serialize("turtle")

410

```