Commit 54b29b1f authored by Thorsten Wißmann's avatar Thorsten Wißmann
Browse files

Parse most of the owl class expressions

parent b5352205
......@@ -19,12 +19,22 @@ type class_exp =
| MAX of int * role * class_exp
(* TODO: | OneOf of * formula *)
type declaration =
| CLASS of uri
| DATATYPE of uri
| OBJECTPROPERTY of uri
| DATAPROPERTY of uri
| ANNOTATIONPROPERTY of uri
| NAMEDINDIVIDUAL of uri
type axiom =
(* class expression axioms *)
| DECLARATION of declaration
| SUBCLASS of class_exp * class_exp
| EQUIVALENTCLASSES of class_exp list
| DISJOINTCLASSES of class_exp list
| DISJOINTUNION of class_exp * (class_exp list)
type prefix = string * string (* prefix name to long substitution *)
type prefixes = prefix list
......@@ -68,15 +78,17 @@ let string_of_class_exp ce =
let string_of_axiom ax =
let soc = string_of_class_exp in
match ax with
| DECLARATION a -> "Declaration (TODO)"
| SUBCLASS (a,b) -> (soc a) ^" ⊑ "^(soc b)
| EQUIVALENTCLASSES la -> String.concat " ≡ " (L.map soc la)
| DISJOINTCLASSES la -> String.concat " ⊍ " (L.map soc la)
| DISJOINTUNION (union,parts) -> (soc union)^" ≡ "^(String.concat " ⊍ " (L.map soc parts))
let string_of_prefix (short,u) = "Prefix "^short^" ↦ "^u
let string_of_ontology (name,axioms) =
"Ontology " ^ name ^ " {\n"^(
String.concat "\n" (L.map string_of_axiom axioms)
)^"}\n"
String.concat "\n" (L.map (((^) " ") << string_of_axiom) axioms)
)^"\n}\n"
......@@ -16,11 +16,22 @@ type class_exp =
| MAX of int * role * class_exp
(* TODO: | OneOf of * formula *)
type declaration =
| CLASS of uri
| DATATYPE of uri
| OBJECTPROPERTY of uri
| DATAPROPERTY of uri
| ANNOTATIONPROPERTY of uri
| NAMEDINDIVIDUAL of uri
type axiom =
(* class expression axioms *)
| DECLARATION of declaration
| SUBCLASS of class_exp * class_exp
| EQUIVALENTCLASSES of class_exp list
| DISJOINTCLASSES of class_exp list
| DISJOINTUNION of class_exp * (class_exp list)
type prefix = string * string (* prefix name to long substitution *)
type prefixes = prefix list
......
......@@ -191,15 +191,60 @@ let parse_prefix a lst : OWL.prefix =
)
| _ -> raise (ParseError (a,("Missing Prefix-definition")))
let parse_role t : OWL.role = match t with
| INode ((str,_),[]) -> str
| INode ((_,a),_) -> raise (ParseError (a, "Only named roles without parameters are supported"))
let rec parse_class_exp (INode ((name,anno), sub): string annotated intree) : OWL.class_exp =
let pr = parse_role in
let pc = parse_class_exp in
if (String.contains name ':') then (OWL.NAMED name)
else match name, sub with
| "ObjectSomeValuesFrom", [role; obj] -> OWL.SOME (pr role, pc obj)
| "ObjectSomeValuesFrom", _ ->
raise (ParseError(anno, name^" requires exactly one role and a class expression"))
| "ObjectAllValuesFrom", [role; obj] -> OWL.ALL (pr role, pc obj)
| "ObjectAllValuesFrom", _ ->
raise (ParseError(anno, name^" requires exactly one role and a class expression"))
| _,_ ->
raise (ParseError(anno, "Unknown class expression name \""^name^"\""))
let parse_axiom (ax: string annotated intree) : OWL.axiom =
let INode ((name,anno), sub) = ax in
let pce = parse_class_exp in
match name with
| "Declaration" -> (match sub with
| [(INode ((name,anno),[INode ((child,_),[])]))] ->
(match name with
| "Class" -> OWL.DECLARATION (OWL.CLASS child)
| _ -> raise (ParseError (anno, "Unknown declaration name \""^name^"\"")))
| _ -> raise (ParseError (anno, name^" expects exactly one parameters")))
| "SubClassOf" -> (match sub with
| [csub;csuper] -> OWL.SUBCLASS (pce csub,parse_class_exp csuper)
| _ -> raise (ParseError (anno, name^" expects exactly two parameters")))
| "EquivalentClasses" -> OWL.EQUIVALENTCLASSES (L.map pce sub)
| "DisjointClasses" -> OWL.DISJOINTCLASSES (L.map pce sub)
| "DisjointUnion" -> (match sub with
| (union::parts) -> OWL.DISJOINTUNION ((pce union),(L.map pce parts))
| _ -> raise (ParseError (anno, name^" expects at least one parameter")))
| _ -> raise (ParseError (anno, "Unknown axiom name \""^name^"\""))
let parse_ontology a (axioms:string annotated intree list) : OWL.ontology =
(* the first element of axioms, i.e. of the children of the ontology node
is just the ontology name/uri *)
match axioms with
| (INode ((ontoname,_), [])::axioms) -> (ontoname, L.map parse_axiom axioms)
| _ -> raise (ParseError (a,("Missing Ontology name")))
let ap (lref:'a list ref) (el:'a): unit = (* append to 'a list ref *)
(lref := ((!lref) @ [el]))
let parse_intree_list (strAnIts: string annotated intree list) =
let prefs : OWL.prefixes ref = ref [] in
let ontos = ref [] in
let ap (lref:'a list ref) (el:'a): unit =
(lref := ((!lref) @ [el]))
in
let ontos : OWL.ontology list ref = ref [] in
foreach_l strAnIts (fun f -> match f with
| INode (("Prefix", a), lst) -> (ap prefs (parse_prefix a lst))
| INode (("Ontology", _), lst) -> ()
| INode (("Ontology", a), axioms) -> (ap ontos (parse_ontology a axioms))
| INode ((n, a), _) -> raise (ParseError (a,(
"Unknown toplevel element \""^n^"\""))));
(!prefs, !ontos)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment