use crate::graph::Graph;
use crate::projection::ids::sanitize_qname;
use std::collections::BTreeSet;
struct ClassAxiom {
local: &'static str,
comment: &'static str,
}
struct PropAxiom {
local: &'static str,
object: bool,
domain: &'static str,
range: &'static str,
}
struct Individual {
local: String,
class: &'static str,
label: String,
}
pub struct OntologyIR {
classes: Vec<ClassAxiom>,
object_props: Vec<PropAxiom>,
data_props: Vec<PropAxiom>,
individuals: Vec<Individual>,
}
impl OntologyIR {
pub fn from_graph(graph: &Graph) -> Self {
let has_entities = !graph.all_entities().is_empty();
let has_roles = !graph.all_roles().is_empty();
let has_resources = !graph.all_resources().is_empty();
let has_flows = !graph.all_flows().is_empty();
let has_relations = !graph.all_relations().is_empty();
let has_entity_instances = !graph.all_entity_instances().is_empty();
let has_policies = !graph.all_policies().is_empty();
let mut classes = Vec::new();
if has_entities {
classes.push(ClassAxiom {
local: "Entity",
comment: "Business actor, location, or organizational unit",
});
}
if has_roles {
classes.push(ClassAxiom {
local: "Role",
comment: "Named participant in a relation",
});
}
if has_resources {
classes.push(ClassAxiom {
local: "Resource",
comment: "Quantifiable subject of value",
});
}
if has_flows {
classes.push(ClassAxiom {
local: "Flow",
comment: "Transfer of a resource between entities",
});
}
if has_relations {
classes.push(ClassAxiom {
local: "Relation",
comment: "Typed association between two roles",
});
}
if has_entity_instances {
classes.push(ClassAxiom {
local: "EntityInstance",
comment: "Declared instance of an entity with field values",
});
}
if has_policies {
classes.push(ClassAxiom {
local: "Policy",
comment: "Declared rule over the model; stated here, not enforced",
});
}
let mut object_props = Vec::new();
let mut data_props = Vec::new();
if has_flows {
object_props.push(PropAxiom {
local: "from",
object: true,
domain: "Flow",
range: "Entity",
});
object_props.push(PropAxiom {
local: "to",
object: true,
domain: "Flow",
range: "Entity",
});
object_props.push(PropAxiom {
local: "hasResource",
object: true,
domain: "Flow",
range: "Resource",
});
data_props.push(PropAxiom {
local: "quantity",
object: false,
domain: "Flow",
range: "xsd:decimal",
});
}
if has_relations {
object_props.push(PropAxiom {
local: "subjectRole",
object: true,
domain: "Relation",
range: "Role",
});
object_props.push(PropAxiom {
local: "objectRole",
object: true,
domain: "Relation",
range: "Role",
});
}
if has_entity_instances {
object_props.push(PropAxiom {
local: "instanceOf",
object: true,
domain: "EntityInstance",
range: "Entity",
});
}
if has_policies {
data_props.push(PropAxiom {
local: "expression",
object: false,
domain: "Policy",
range: "xsd:string",
});
data_props.push(PropAxiom {
local: "modality",
object: false,
domain: "Policy",
range: "xsd:string",
});
data_props.push(PropAxiom {
local: "priority",
object: false,
domain: "Policy",
range: "xsd:integer",
});
}
let mut seen = BTreeSet::new();
let mut individuals = Vec::new();
let mut push_individual = |local: String, class: &'static str, label: String| {
if seen.insert(local.clone()) {
individuals.push(Individual {
local,
class,
label,
});
}
};
for e in graph.all_entities() {
push_individual(sanitize_qname(e.name()), "Entity", e.name().to_string());
}
for r in graph.all_roles() {
push_individual(sanitize_qname(r.name()), "Role", r.name().to_string());
}
for r in graph.all_resources() {
push_individual(sanitize_qname(r.name()), "Resource", r.name().to_string());
}
for i in graph.all_entity_instances() {
push_individual(
format!("instance_{}", sanitize_qname(i.name())),
"EntityInstance",
i.name().to_string(),
);
}
for p in graph.all_policies() {
push_individual(
format!("policy_{}", sanitize_qname(&p.name)),
"Policy",
p.name.clone(),
);
}
individuals.sort_by(|a, b| (&a.label, &a.local).cmp(&(&b.label, &b.local)));
Self {
classes,
object_props,
data_props,
individuals,
}
}
pub fn to_turtle(
&self,
base: &str,
ontology_iri: &str,
header: &str,
model_hash: &str,
) -> String {
let mut s = String::new();
s.push_str(header);
s.push_str(&format!("@prefix sea: <{base}> .\n"));
s.push_str("@prefix owl: <http://www.w3.org/2002/07/owl#> .\n");
s.push_str("@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n");
s.push_str("@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n");
s.push_str("@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\n");
s.push_str(&format!("<{ontology_iri}> a owl:Ontology ;\n"));
s.push_str(&format!(" owl:versionInfo \"{model_hash}\" .\n\n"));
if !self.classes.is_empty() {
s.push_str("# Classes\n");
for c in &self.classes {
s.push_str(&format!(
"sea:{} a owl:Class ;\n rdfs:label \"{}\" ;\n rdfs:comment \"{}\" .\n\n",
c.local, c.local, c.comment
));
}
}
let all_props: Vec<&PropAxiom> = self.object_props.iter().chain(&self.data_props).collect();
if !all_props.is_empty() {
s.push_str("# Properties\n");
for p in &all_props {
let kind = if p.object {
"owl:ObjectProperty"
} else {
"owl:DatatypeProperty"
};
let range = if p.range.contains(':') {
p.range.to_string()
} else {
format!("sea:{}", p.range)
};
s.push_str(&format!(
"sea:{} a {kind} ;\n rdfs:domain sea:{} ;\n rdfs:range {range} .\n\n",
p.local, p.domain
));
}
}
if !self.individuals.is_empty() {
s.push_str("# Individuals\n");
for i in &self.individuals {
s.push_str(&format!(
"sea:{} a owl:NamedIndividual, sea:{} ;\n rdfs:label \"{}\" .\n\n",
i.local,
i.class,
escape_literal(&i.label)
));
}
}
s
}
}
fn escape_literal(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}