22
33module StrMap = Map. Make (String )
44
5- (* Should I put this in the module ? *)
5+ (* Should I put this in a 'common' file ? *)
66let find_list s map =
77 match StrMap. find_opt s map with
88 | Some l -> l
@@ -38,20 +38,29 @@ type individual = {
3838 * Can deduce or assert more specific class. *)
3939 mutable myclass : concept ;
4040 (* just an idea, don't know if this is the final representation. *)
41- mutable iroles : role * [`Con of concept | `Ind of individual ] list
41+ (* List might be better than map because anyway we can have
42+ multiple targets for a given role. *)
43+ mutable iroles : (role * [`Con of concept | `Ind of individual ]) list
4244 }
4345
46+ (* * Get list of all roles of a given type for an individual. *)
47+ let individual_get_roles ind rtype =
48+ List. filter
49+ (fun (r , _ ) -> r.rid = rtype)
50+ ind.iroles
4451
4552(* will the system support adding relation types? *)
4653
47- (* Assumes roles named "superclass" and "subclass" exist in ontology. *)
54+ (* Assumes roles named "superclass" and "subclass" exist in
55+ ontology. *)
4856let rec concept_is_subclass_of c1 c2 =
4957 c1 == c2 ||
50- (* Depth-first search in 2 lines! *)
51- find_list " subclass" c1.roles
52- |> List. exists (fun {role =_ ; other; default =_ } ->
53- (* Need physical equality here, because circular references. *)
54- other == c2 || concept_is_subclass_of other c2)
58+ (* Depth-first search in 2 lines! *)
59+ find_list " subclass" c1.roles
60+ |> List. exists
61+ (fun {role =_ ; other; default =_ } ->
62+ (* Need physical equality here, because circular references. *)
63+ other == c2 || concept_is_subclass_of other c2)
5564
5665(* * Check that a role is allowed by domain/range restrictions. *)
5766let concept_is_valid_role c1 (r : role ) c2 =
@@ -61,69 +70,66 @@ let concept_is_valid_role c1 (r: role) c2 =
6170(* this non-validity-checking version can be called by the checking version. *)
6271let concept_add_role c1 role c2 =
6372 c1.roles < - (
64- let old_roles = find_list role.rid c1.roles
65- in
66- StrMap. add role.rid
73+ let old_roles = find_list role.rid c1.roles in
74+ StrMap. add
75+ role.rid
6776 ({role= role; other= c2; default= false } :: old_roles)
6877 c1.roles
6978 )
7079
7180(* * Get list of roles of a given type for a concept. *)
72- let concept_get_roles c rname =
73- match StrMap. find_opt rname c.roles with
74- | Some rlist -> rlist
75- | None -> []
81+ let concept_get_roles c rname = find_list rname c.roles
7682
7783
7884module ConceptGraph = struct
7985 type t = {
86+ (* Eventually make it mutable *)
8087 cmap : concept StrMap .t ;
8188 rmap : role StrMap .t ;
8289 }
8390
8491 let create clist rlist = {
8592 cmap = List. fold_left
86- (fun map concept ->
87- StrMap. add concept.cid concept map)
93+ (fun map concept -> StrMap. add concept.cid concept map)
8894 StrMap. empty
8995 clist;
9096 rmap = List. fold_left
91- (fun map role ->
92- StrMap. add role.rid role map)
97+ (fun map role -> StrMap. add role.rid role map)
9398 StrMap. empty
9499 rlist;
95100 }
96101
97102 let get_concept graph cname = StrMap. find_opt cname graph.cmap
98103 let get_role graph rname = StrMap. find_opt rname graph.rmap
99- (* Yay, now I can add inferences corresponding to questions. *)
100- (* Could probably use "getc" and "getr" also *)
101- (* function to add concept, check for duplicate name *)
102- (* function to add relation between concepts, check restrictions *)
103- (* let add_concept (g: t) c =
104- c :: g *)
104+ (* Should inference function corresponding to questions. go here? *)
105+ (* function to add concept, check for duplicate name *)
106+ (* function to add relation between concepts, check restrictions *)
107+ (* let add_concept_under graph concept parent = *)
108+ (* let get_concepts_with_role graph role target *)
105109end
106110
107- (* need a concept-maker and a relation-adder that checks everything.
108- * ...and makes indexes of relations by type *)
109-
111+ (* */**)
112+
113+ (* need a concept-maker and a relation-adder that checks everything.
114+ * ...and makes indexes of relations by type *)
110115
111- (* Can i encode in the type system that superclass/subclass relations are
112- * only allowed between node of same category? *)
113- (* What if I could make properties be functions and the domain and
114- range be the types of categories they apply to? *)
115- (* Seems like a need subclassing; I need to distinguish categories in the
116- * type system, but have them be treated as same types also *)
116+ (* Can i encode in the type system that superclass/subclass relations
117+ are * only allowed between node of same category? *) (* What if
118+ I could make properties be functions and the domain and range
119+ be the types of categories they apply to? *) (* Seems like a
120+ need subclassing; I need to distinguish categories in the *
121+ type system, but have them be treated as same types also *)
117122
118- (* Maybe I should *not* try to use the metalanguage's type system. *)
119- (* Of course, the runtime can't add new functions to the code! *)
123+ (* Maybe I should *not* try to use the metalanguage's type
124+ system. *) (* Of course, the runtime can't add new functions to
125+ the code! *)
120126
121- (* category errors should be detected in the inference and flagged for
122- * feedback to the user! yeah! *)
127+ (* category errors should be detected in the inference and flagged for
128+ * feedback to the user! yeah! *)
123129
124130 (* need a default and a universal form of (some) relations. *)
125131
126132(* default reasoning; a property holds by default for members of a
127- class, * so unless a subclass has a negation of that property it
128- goes up the * hierarchy and sees if it's true. *)
133+ class, * so unless a subclass has a negation of that property
134+ it goes up the * hierarchy and sees if it's true. *)
129135
0 commit comments