├── example_charlesii ├── pedigree.pl ├── wiki_War_of_the_Spanish_Succession_family_tree.png ├── sex_list.csv ├── pedigree_matrix.csv └── facts.pl ├── unit_tests ├── unit_tests_run.sh └── unit_tests.pl ├── rule_variable_reference_file.txt ├── LICENSE ├── README.md ├── matrix_to_prolog_rules.R └── rules.pl /example_charlesii/pedigree.pl: -------------------------------------------------------------------------------- 1 | :- include('facts.pl'). 2 | :- include('../rules.pl'). 3 | 4 | -------------------------------------------------------------------------------- /unit_tests/unit_tests_run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | swipl -t "run_tests." -s unit_tests.pl 4 | 5 | -------------------------------------------------------------------------------- /example_charlesii/wiki_War_of_the_Spanish_Succession_family_tree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nevrome/kintreex/master/example_charlesii/wiki_War_of_the_Spanish_Succession_family_tree.png -------------------------------------------------------------------------------- /example_charlesii/sex_list.csv: -------------------------------------------------------------------------------- 1 | id,sex 2 | Philip III,male 3 | Margaret,female 4 | Maria Anna,female 5 | Anne,female 6 | Elisabeth,female 7 | Philip IV,male 8 | Mariana,female 9 | Louis XIV,male 10 | Maria Theresa,female 11 | Charles II,male 12 | Margaret Theresa,female 13 | Leopold I,male 14 | 15 | -------------------------------------------------------------------------------- /rule_variable_reference_file.txt: -------------------------------------------------------------------------------- 1 | Y = You 2 | P = Parent 3 | F = Father 4 | M = Mother 5 | C = Child 6 | So = Son 7 | Da = Daughter 8 | B = Brother 9 | S = Sister 10 | Sib = Sibling 11 | G = Grandparent 12 | Gf = Grandfather 13 | Gm = Grandmother 14 | Gc = Grandchild 15 | Gso = Grandson 16 | Gda = Granddaughter 17 | U = Uncle 18 | A = Aunt 19 | Ne = Nephew 20 | Ni = Niece 21 | Hb = Halfbrother 22 | Hs = Halfsister 23 | Hsib = Halfsibling 24 | 25 | -------------------------------------------------------------------------------- /example_charlesii/pedigree_matrix.csv: -------------------------------------------------------------------------------- 1 | ,Philip III,Margaret,Maria Anna,Anne,Elisabeth,Philip IV,Mariana,Louis XIV,Maria Theresa,Charles II,Margaret Theresa,Leopold I 2 | Philip III,100,,,,,,,,,,, 3 | Margaret,0,100,,,,,,,,,, 4 | Maria Anna,50,50,100,,,,,,,,, 5 | Anne,50,50,50,100,,,,,,,, 6 | Elisabeth,0,0,0,0,100,,,,,,, 7 | Philip IV,50,50,50,50,0,100,,,,,, 8 | Mariana,25,25,50,25,0,25,100,,,,, 9 | Louis XIV,25,25,25,50,0,25,12.5,100,,,, 10 | Maria Theresa,25,25,25,25,50,50,12.5,12.5,100,,, 11 | Charles II,37.5,37.5,50,50,0,62.5,62.5,18.75,25,100,, 12 | Margaret Theresa,37.5,37.5,50,50,0,62.5,62.5,18.75,25,62.5,100, 13 | Leopold I,25,25,50,25,0,25,50,12.5,12.5,37.5,37.5,100 14 | -------------------------------------------------------------------------------- /unit_tests/unit_tests.pl: -------------------------------------------------------------------------------- 1 | :- include('../rules.pl'). 2 | 3 | /* parents */ 4 | male(dad). 5 | female(mom). 6 | female(child). 7 | first_degree(dad, child). 8 | first_degree(child, dad). 9 | first_degree(mom, child). 10 | first_degree(child, mom). 11 | 12 | :- begin_tests(parents). 13 | test('father_of') :- father_of(dad, child). 14 | test('father_of') :- father_of(X, child), X == dad. 15 | test('mother_of') :- mother_of(mom, child). 16 | test('mother_of') :- mother_of(X, child), X == mom. 17 | test('parent_of', [true, nondet]) :- parent_of(dad, child). 18 | test('parent_of', [true, nondet]) :- parent_of(mom, child). 19 | test('parents_of') :- parents_of(X, Y, child), X == mom, Y == dad. 20 | :- end_tests(parents). 21 | 22 | run_tests(parents). 23 | 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Clemens Schmid 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kintreex 2 | 3 | kintreex is an early prototype of a pedigree exploration [expert system](https://en.wikipedia.org/wiki/Expert_system) written in [prolog](https://en.wikipedia.org/wiki/Prolog). 4 | 5 | It contains an [R script](matrix_to_prolog_rules.R) to translate percental relationship matrizes to prolog facts of the form `first_degree(personA, personB), second_degree(personA, personC), ...` and a prolog file [`rules.pl`](rules.pl) with a set of (untested) rules like `father_of(personA,personB):- male(personA), first_degree(personA,personB).` that should apply in this system. 6 | 7 | To use it you can load your facts file and the kintreex rules file in an interactive prolog session and explore the possible pedigrees by asking the expert system questions. For this you have to install a prolog implementation like e.g. [SWI-Prolog](https://www.swi-prolog.org/download/stable). 8 | 9 | ### Charles II example 10 | 11 | [`example_charlesii`](example_charlesii) contains an example application prepared by Joscha Gretzinger based on the complex pedigree of [Charles II of Spain](https://en.wikipedia.org/wiki/Charles_II_of_Spain). You can start the prolog interpreter in this directory with 12 | 13 | ``` 14 | swipl -s pedigree.pl 15 | ``` 16 | 17 | and then start to ask questions 18 | 19 | ``` 20 | father_of(X, anne). 21 | father_of(philipiii, anne). 22 | distinct(sister_of(X,philipiv)). % only distinct solutions 23 | findall(X,distinct(sister_of(X,philipiv)),L). % show all solutions 24 | ``` 25 | 26 | and finally exit the interpreter console with 27 | 28 | ``` 29 | halt. 30 | ``` 31 | -------------------------------------------------------------------------------- /matrix_to_prolog_rules.R: -------------------------------------------------------------------------------- 1 | library(magrittr) 2 | 3 | #setwd("/home/schmid/agora/kintreex") 4 | 5 | #### sex #### 6 | 7 | fix_names <- function(x) { 8 | gsub("[[:space:]]", "", tolower(x)) 9 | } 10 | 11 | sl <- read.csv("sex_list.csv") 12 | 13 | sl_fixed <- sl %>% 14 | dplyr::mutate( 15 | id = fix_names(id) 16 | ) %>% 17 | dplyr::arrange( 18 | sex, id 19 | ) 20 | 21 | individuals_facts <- sapply( 22 | 1:nrow(sl_fixed), function (i) { 23 | paste0(sl_fixed$sex[i], "(", sl_fixed$id[i], ").") 24 | }) 25 | 26 | 27 | 28 | #### relationships #### 29 | 30 | pm <- read.csv("pedigree_matrix.csv", check.names = F, row.names = 1) 31 | pm[upper.tri(pm)] <- t(pm)[upper.tri(pm)] 32 | 33 | pm_long <- reshape2::melt(as.matrix(pm)) %>% 34 | setNames(c('id.a', 'id.b', 'value')) %>% 35 | dplyr::filter(value != 100) 36 | 37 | pm_long_fixed <- pm_long %>% 38 | dplyr::mutate( 39 | id.a = fix_names(id.a), 40 | id.b = fix_names(id.b), 41 | value_cut = cut( 42 | value, 43 | breaks = c(101, 50, 25, 12.5, 6.25, -1), 44 | labels = c( 45 | "first_degree", 46 | "second_degree", 47 | "third_degree", 48 | "fourth_degree", 49 | "unrelated" 50 | ) %>% rev(), 51 | include.lowest = TRUE, 52 | right = FALSE 53 | ) 54 | ) %>% 55 | dplyr::arrange( 56 | value, id.a 57 | ) 58 | 59 | relationship_facts <- sapply( 60 | 1:nrow(pm_long_fixed), function (i) { 61 | paste0( 62 | pm_long_fixed$value_cut[i], "(", 63 | pm_long_fixed$id.a[i], ", ", pm_long_fixed$id.b[i], 64 | ")." 65 | ) 66 | }) 67 | 68 | 69 | 70 | #### write facts to file #### 71 | 72 | writeLines( 73 | text = c( 74 | "/* Facts */", 75 | "", 76 | individuals_facts, 77 | "", 78 | relationship_facts 79 | ), 80 | con = "facts.pl" 81 | ) 82 | -------------------------------------------------------------------------------- /rules.pl: -------------------------------------------------------------------------------- 1 | /* Rules */ 2 | 3 | all_relations(F,M,B,S,So,Da,Gf,Gm,U,A,Ne,Ni,Hb,Hs,Gso,Gda,Y):- 4 | %% first degree %% 5 | % male 6 | (father_of(F,Y), F \== B, F \== So; 7 | brother_of(B,Y), B \== F, B \== So; 8 | son_of(So,Y), So \== F, So \== B; 9 | false), 10 | % female 11 | (mother_of(M,Y), M \== B, M \== Da; 12 | sister_of(S,Y), S \== F, S \== Da; 13 | daughter_of(Da,Y), Da \== M, Da \== S; 14 | false), 15 | %% second degree %% 16 | % male 17 | (grandfather_of(Gf,Y), Gf \== U, Gf \== Ne, Gf \== Hb, Gf \== Gso; 18 | uncle_of(U,Y), U \== Gf, U \== Ne, U \== Hb, U \== Gso; 19 | nephew_of(Ne,Y), Ne \== Gf, Ne \== U, Ne \== Hb, Ne \== Gso; 20 | halfbrother_of(Hb,Y), Hb \== Gf, Hb \== U, Hb \== Ne, Hb \== Gso; 21 | grandson_of(Gso,Y), Gso \== Gf, Gso \== U, Gso\== Ne, Gso \== Hb; 22 | false), 23 | % female 24 | (grandmother_of(Gm,Y), Gm \== A, Gm \== Ni, Gm \== Hs, Gm \== Gda; 25 | aunt_of(A,Y), A \== Gm, A \== Ni, A \== Hs, A \== Gda; 26 | niece_of(Ni,Y), Ni \== Gm, Ni \== A, Ni \== Hs, Ni \== Gda; 27 | halfsister_of(Hs,Y), Hs \== Gm, Hs \== A, Hs \== Ni, Hs \== Gda; 28 | granddaughter_of(Gda,Y), Gda \== Gm, Gda \== A, Gda\== Ni, Gda \== Hs; 29 | false). 30 | 31 | % parents 32 | father_of(F,Y):- male(F), first_degree(F,Y). 33 | mother_of(M,Y):- female(M), first_degree(M,Y). 34 | 35 | parent_of(P,Y):- father_of(P,Y). 36 | parent_of(P,Y):- mother_of(P,Y). 37 | 38 | parents_of(F,M,Y):- father_of(F,Y), mother_of(M,Y). 39 | 40 | % children 41 | child_of(Y,P):- parent_of(P,Y). 42 | child_of(Y,F,M):- parents_of(F,M,Y). 43 | son_of(Y,P):- male(Y), child_of(Y,P). 44 | daughter_of(Y,P):- female(Y), child_of(Y,P). 45 | 46 | % siblings 47 | brother_of(B,Y):- 48 | male(B), 49 | first_degree(B,Y), 50 | parents_of(F,M,B), parents_of(F,M,Y), F \= B. 51 | 52 | sister_of(S,Y):- 53 | female(S), 54 | first_degree(S,Y), 55 | parents_of(F,M,S), parents_of(F,M,Y), M \= S. 56 | 57 | sibling_of(Sib,Y):- brother_of(Sib, Y). 58 | sibling_of(Sib,Y):- sister_of(Sib, Y). 59 | 60 | % grandparents 61 | grandfather_of(Gf,Y):- 62 | male(Gf), 63 | second_degree(Gf,Y), 64 | father_of(Gf,P), parent_of(P,Y). 65 | grandmother_of(Gm,Y):- 66 | female(Gm), 67 | second_degree(Gm,Y), 68 | mother_of(Gm,P), parent_of(P,Y). 69 | 70 | grandparent_of(G,Y):- grandfather_of(G,Y). 71 | grandparent_of(G,Y):- grandmother_of(G,Y). 72 | 73 | grandparents_of(Gf,Gm,Z):- grandfather_of(Gf,Z), grandmother_of(Gm,Z). 74 | 75 | % grandchildren 76 | grandchild_of(Y,G):- grandparent_of(G,Y). 77 | grandchild_of(Y,Gf,Gm):- grandparents_of(Gf,Gm,Y). 78 | grandson_of(Y,G):- male(Y), grandchild_of(Y,G). 79 | granddaughter_of(Y,G):- female(Y), grandchild_of(Y,G). 80 | 81 | % uncles and aunts 82 | uncle_of(U, Y):- 83 | male(U), 84 | second_degree(U, Y), 85 | parent_of(P,Y), sibling_of(P,U), 86 | grandparent_of(G,Y), parent_of(G,U). 87 | 88 | aunt_of(A, Y):- 89 | female(A), 90 | second_degree(A, Y), 91 | parent_of(P,Y), sibling_of(P,A), 92 | grandparent_of(G,Y), parent_of(G,A). 93 | 94 | % nephews and nieces 95 | nephew_of(Ne, Y):- 96 | male(Ne), 97 | second_degree(Ne,Y), 98 | parent_of(P,Ne), sibling_of(P,Y). 99 | 100 | niece_of(Ni, Y):- 101 | female(Ni), 102 | second_degree(Ni,Y), 103 | parent_of(P,Ni), sibling_of(P,Y). 104 | 105 | % half siblings 106 | halfbrother_of(Hb,Y):- 107 | male(Hb), 108 | second_degree(Hb,Y), 109 | parent_of(P,Hb), parent_of(P,Y). 110 | 111 | halfsister_of(Hs,Y):- 112 | female(Hs), 113 | second_degree(Hs,Y), 114 | parent_of(P,Hs), parent_of(P,Y). 115 | 116 | halfsibling_of(Hsib,Y):- halfbrother_of(Hsib, Y). 117 | halfsibling_of(Hsib,Y):- halfsister_of(Hsib, Y). 118 | 119 | -------------------------------------------------------------------------------- /example_charlesii/facts.pl: -------------------------------------------------------------------------------- 1 | /* Facts */ 2 | 3 | female(anne). 4 | female(elisabeth). 5 | female(margaret). 6 | female(margarettheresa). 7 | female(mariaanna). 8 | female(mariana). 9 | female(mariatheresa). 10 | male(charlesii). 11 | male(leopoldi). 12 | male(louisxiv). 13 | male(philipiii). 14 | male(philipiv). 15 | 16 | unrelated(anne, elisabeth). 17 | unrelated(charlesii, elisabeth). 18 | unrelated(elisabeth, philipiii). 19 | unrelated(elisabeth, margaret). 20 | unrelated(elisabeth, mariaanna). 21 | unrelated(elisabeth, anne). 22 | unrelated(elisabeth, philipiv). 23 | unrelated(elisabeth, mariana). 24 | unrelated(elisabeth, louisxiv). 25 | unrelated(elisabeth, charlesii). 26 | unrelated(elisabeth, margarettheresa). 27 | unrelated(elisabeth, leopoldi). 28 | unrelated(leopoldi, elisabeth). 29 | unrelated(louisxiv, elisabeth). 30 | unrelated(margaret, philipiii). 31 | unrelated(margaret, elisabeth). 32 | unrelated(margarettheresa, elisabeth). 33 | unrelated(mariaanna, elisabeth). 34 | unrelated(mariana, elisabeth). 35 | unrelated(philipiii, margaret). 36 | unrelated(philipiii, elisabeth). 37 | unrelated(philipiv, elisabeth). 38 | third_degree(leopoldi, louisxiv). 39 | third_degree(leopoldi, mariatheresa). 40 | third_degree(louisxiv, mariana). 41 | third_degree(louisxiv, mariatheresa). 42 | third_degree(louisxiv, leopoldi). 43 | third_degree(mariana, louisxiv). 44 | third_degree(mariana, mariatheresa). 45 | third_degree(mariatheresa, mariana). 46 | third_degree(mariatheresa, louisxiv). 47 | third_degree(mariatheresa, leopoldi). 48 | third_degree(charlesii, louisxiv). 49 | third_degree(louisxiv, charlesii). 50 | third_degree(louisxiv, margarettheresa). 51 | third_degree(margarettheresa, louisxiv). 52 | second_degree(anne, mariana). 53 | second_degree(anne, mariatheresa). 54 | second_degree(anne, leopoldi). 55 | second_degree(charlesii, mariatheresa). 56 | second_degree(leopoldi, philipiii). 57 | second_degree(leopoldi, margaret). 58 | second_degree(leopoldi, anne). 59 | second_degree(leopoldi, philipiv). 60 | second_degree(louisxiv, philipiii). 61 | second_degree(louisxiv, margaret). 62 | second_degree(louisxiv, mariaanna). 63 | second_degree(louisxiv, philipiv). 64 | second_degree(margaret, mariana). 65 | second_degree(margaret, louisxiv). 66 | second_degree(margaret, mariatheresa). 67 | second_degree(margaret, leopoldi). 68 | second_degree(margarettheresa, mariatheresa). 69 | second_degree(mariaanna, louisxiv). 70 | second_degree(mariaanna, mariatheresa). 71 | second_degree(mariana, philipiii). 72 | second_degree(mariana, margaret). 73 | second_degree(mariana, anne). 74 | second_degree(mariana, philipiv). 75 | second_degree(mariatheresa, philipiii). 76 | second_degree(mariatheresa, margaret). 77 | second_degree(mariatheresa, mariaanna). 78 | second_degree(mariatheresa, anne). 79 | second_degree(mariatheresa, charlesii). 80 | second_degree(mariatheresa, margarettheresa). 81 | second_degree(philipiii, mariana). 82 | second_degree(philipiii, louisxiv). 83 | second_degree(philipiii, mariatheresa). 84 | second_degree(philipiii, leopoldi). 85 | second_degree(philipiv, mariana). 86 | second_degree(philipiv, louisxiv). 87 | second_degree(philipiv, leopoldi). 88 | second_degree(charlesii, philipiii). 89 | second_degree(charlesii, margaret). 90 | second_degree(charlesii, leopoldi). 91 | second_degree(leopoldi, charlesii). 92 | second_degree(leopoldi, margarettheresa). 93 | second_degree(margaret, charlesii). 94 | second_degree(margaret, margarettheresa). 95 | second_degree(margarettheresa, philipiii). 96 | second_degree(margarettheresa, margaret). 97 | second_degree(margarettheresa, leopoldi). 98 | second_degree(philipiii, charlesii). 99 | second_degree(philipiii, margarettheresa). 100 | first_degree(anne, philipiii). 101 | first_degree(anne, margaret). 102 | first_degree(anne, mariaanna). 103 | first_degree(anne, philipiv). 104 | first_degree(anne, louisxiv). 105 | first_degree(anne, charlesii). 106 | first_degree(anne, margarettheresa). 107 | first_degree(charlesii, mariaanna). 108 | first_degree(charlesii, anne). 109 | first_degree(elisabeth, mariatheresa). 110 | first_degree(leopoldi, mariaanna). 111 | first_degree(leopoldi, mariana). 112 | first_degree(louisxiv, anne). 113 | first_degree(margaret, mariaanna). 114 | first_degree(margaret, anne). 115 | first_degree(margaret, philipiv). 116 | first_degree(margarettheresa, mariaanna). 117 | first_degree(margarettheresa, anne). 118 | first_degree(mariaanna, philipiii). 119 | first_degree(mariaanna, margaret). 120 | first_degree(mariaanna, anne). 121 | first_degree(mariaanna, philipiv). 122 | first_degree(mariaanna, mariana). 123 | first_degree(mariaanna, charlesii). 124 | first_degree(mariaanna, margarettheresa). 125 | first_degree(mariaanna, leopoldi). 126 | first_degree(mariana, mariaanna). 127 | first_degree(mariana, leopoldi). 128 | first_degree(mariatheresa, elisabeth). 129 | first_degree(mariatheresa, philipiv). 130 | first_degree(philipiii, mariaanna). 131 | first_degree(philipiii, anne). 132 | first_degree(philipiii, philipiv). 133 | first_degree(philipiv, philipiii). 134 | first_degree(philipiv, margaret). 135 | first_degree(philipiv, mariaanna). 136 | first_degree(philipiv, anne). 137 | first_degree(philipiv, mariatheresa). 138 | first_degree(charlesii, philipiv). 139 | first_degree(charlesii, mariana). 140 | first_degree(charlesii, margarettheresa). 141 | first_degree(margarettheresa, philipiv). 142 | first_degree(margarettheresa, mariana). 143 | first_degree(margarettheresa, charlesii). 144 | first_degree(mariana, charlesii). 145 | first_degree(mariana, margarettheresa). 146 | first_degree(philipiv, charlesii). 147 | first_degree(philipiv, margarettheresa). 148 | --------------------------------------------------------------------------------