This is a simple SWI-Prolog environment setup.
Installations
- Open Terminal
- Install SWI-Prolog
$ arch -arm64 brew install swi-prolog
$ swipl --version
File Preparation
- Make a new folder
- Inside the folder save the Ontology file in .owl format (ex. your_ontology.owl)
- inside the folder where the .owl is located, create a new .pl file. (ex. load_ontology.pl)
% Setup
:- use_module(library(semweb/rdf11)).
:- rdf_load('skos.owl').
:- rdf_register_prefix(owl, 'http://www.w3.org/2002/07/owl#').
:- rdf_register_prefix(rdfs, 'http://www.w3.org/2000/01/rdf-schema#').
:- rdf_register_prefix(skos, 'http://www.w3.org/2004/02/skos/core#').
:- rdf_register_prefix(foaf, 'http://xmlns.com/foaf/0.1/').
% List all classes
list_classes :-
rdf(Class, rdf:type, skos:'Concept'),
writeln(Class),
fail.
list_classes :-
rdf(Class, rdf:type, skos:'ConceptScheme'),
writeln(Class),
fail.
list_classes :-
rdf(Class, rdf:type, foaf:'Person'),
writeln(Class),
fail.
list_classes.
% Find all subclasses
subclasses(Class) :-
rdf(Sub, rdfs:subClassOf, Class),
writeln(Sub),
fail.
subclasses(_).
% Find all instances
instances_of(Class) :-
rdf(Instance, rdf:type, Class),
writeln(Instance),
fail.
instances_of(_).
% List all properties
list_properties :-
rdf(Property, rdf:type, rdf:'Property'),
writeln(Property),
fail.
list_properties :-
rdf(Property, rdf:type, owl:'ObjectProperty'),
writeln(Property),
fail.
list_properties :-
rdf(Property, rdf:type, owl:'DatatypeProperty'),
writeln(Property),
fail.
list_properties.
% Find all values of a property for a specific instance
property_values(Instance, Property) :-
findall(Value, rdf(Instance, Property, Value), Values),
print_property_values(Values).
print_property_values([]).
print_property_values([Value|Rest]) :-
format('~w~n', [Value]),
print_property_values(Rest).
% Example usage
:- initialization(main).
main :-
writeln('Ontology loaded successfully.'),
writeln('Classes in the ontology:'),
list_classes,
writeln('Subclasses of owl:Thing:'),
subclasses('http://www.w3.org/2002/07/owl#Thing'),
writeln('Instances of foaf:Person:'),
instances_of('http://xmlns.com/foaf/0.1/Person'),
writeln('Properties in the ontology:'),
list_properties,
writeln('Property values for example:Person1 and example:hasName:'),
property_values('http://www.example.org#Person1', 'http://www.example.org#hasName').
- In this part of the code, use your own .owl file (ex. your_ontology.owl)
% Load the OWL file from the local file
:- rdf_load('skos.owl').
Executions
- Open Terminal
- cd ./your/ontology.owl/path
- Run swipl your_swish.pl (ex. swipl load_ontology.pl)
- The GUI will be shown like below
user@macbook ai % swipl load_ontology.pl
% Parsed "skos.owl" in 0.03 sec; 192 triples
Ontology loaded successfully.
Classes in the ontology:
http://www.w3.org/2004/02/skos/core#Concepthttp://www.w3.org/2004/02/skos/core#ConceptSchemehttp://xmlns.com/foaf/0.1/Person
Subclasses of owl:Thing:
Instances of owl:Person:
Properties in the ontology:
Property values for owl:Person1 and owl:hasName:
Welcome to SWI-Prolog (threaded, 64 bits, version 9.2.5)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit [https://www.swi-prolog.org](https://www.swi-prolog.org/)
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?-