amalgame/commit
split up descdendent matcher
author | Jacco van Ossenbruggen |
---|---|
Sat Aug 30 15:57:13 2014 +0200 | |
committer | Jacco van Ossenbruggen |
Sat Aug 30 15:57:13 2014 +0200 | |
commit | 92177556ba33515cbfc9d90d03a9788a9b97bf9c |
tree | 22cc27b0e25c26ff199ff937a2e89fe32d065013 |
parent | 134f9f6e406e617b6e8bfcfddc9eec98e53f93c8 |
Diff style: patch stat
diff --git a/config-available/ag_modules.pl b/config-available/ag_modules.pl index 755a122..93ef516 100644 --- a/config-available/ag_modules.pl +++ b/config-available/ag_modules.pl @@ -20,6 +20,7 @@ :- use_module(library(ag_modules/most_labels)). :- use_module(library(ag_modules/most_generic)). :- use_module(library(ag_modules/ancestor_selecter)). +:- use_module(library(ag_modules/descendent_selecter)). :- use_module(library(ag_modules/related_selecter)). :- use_module(library(ag_modules/sibling_selecter)). diff --git a/lib/ag_modules/descendent.pl b/lib/ag_modules/descendent.pl new file mode 100644 index 0000000..efd4346 --- /dev/null +++ b/lib/ag_modules/descendent.pl @@ -0,0 +1,22 @@ +:- module(descendent_match_util, + [ descendent_match/4 + ]). + +:- use_module(library(semweb/rdf_db)). +:- use_module(library(skos/util)). + +descendent_match(align(S, T, Prov0), BackgroundMatches, align(S, T, [Prov|Prov0]), Options) :- + option(steps(MaxSteps), Options), + descendent(S, MaxSteps, DesS, R1, Steps1), + descendent(T, MaxSteps, DesT, R2, Steps2), + get_assoc(DesS-DesT, BackgroundMatches, _), + Prov = [method(descendent_match), + source(DesS), + target(DesT), + steps(Steps1/Steps2), + graph([R1,R2]) + ]. + +descendent(R, MaxSteps, Descendent, rdf(R, Prop, Descendent), Steps) :- + skos_descendant_of(R, Descendent, MaxSteps, Steps), + rdf_equal(amalgame:descendant, Prop). diff --git a/lib/ag_modules/descendent_match.pl b/lib/ag_modules/descendent_match.pl index ebb744e..2a8da66 100644 --- a/lib/ag_modules/descendent_match.pl +++ b/lib/ag_modules/descendent_match.pl @@ -1,29 +1,26 @@ :- module(descendent_match, []). -:- use_module(library(semweb/rdf_db)). -:- use_module(library(skos/util)). +:- use_module(library(assoc)). +:- use_module(library(lists)). +:- use_module(library(option)). :- use_module(library(amalgame/vocabulary)). -:- use_module(library(amalgame/map)). +:- use_module(descendent). :- public amalgame_module/1. :- public filter/3. :- public matcher/4. -:- public selecter/5. :- public parameter/4. amalgame_module(amalgame:'DescendentMatcher'). amalgame_module(amalgame:'DescendentFilter'). -amalgame_module(amalgame:'DescendentSelecter'). -parameter(steps, integer, 1, 'depth of search, defaults to 1, e.g. direct children only'). -parameter(type, - oneof([source, target]), source, - 'Discard other matches with the same source/target'). +parameter(steps, integer, 1, + 'depth of search, defaults to 1, e.g. direct children only'). %% filter(+MappingsIn, -MappingsOut, +Options) % -% Filter mappings based on exact matching of labels. +% Filter mappings based on descendent matches in snd_input. filter(In, Out, Options) :- option(snd_input(SecList), Options), @@ -32,20 +29,6 @@ filter(In, Out, Options) :- ord_list_to_assoc(Deduped, BackgroundMatches), filter_(In, BackgroundMatches, Out, Options). -filter_([], _, [], _). -filter_([align(S,T,P)|Cs], BackgroundMatches, [C|Mappings], Options) :- - ( T = scheme(_) - -> match(align(S,_,P), BackgroundMatches, C, Options), - C=align(_,T2,_), - vocab_member(T2, T) - ; match(align(S,T,P), BackgroundMatches, C, Options) - ), - !, - filter_(Cs, BackgroundMatches, Mappings, Options). -filter_([_|Cs], BackgroundMatches, Mappings, Options) :- - filter_(Cs, BackgroundMatches, Mappings, Options). - - %% matcher(+Source, +Target, -Mappings, +Options) % % Mappings is a list of matches between instances of Source and @@ -59,72 +42,21 @@ matcher(Source, Target, Mappings, Options) :- findall(M, align(Source, Target, BackgroundMatches, M, Options), Mappings0), sort(Mappings0, Mappings). +filter_([], _, [], _). +filter_([align(S,T,P)|Cs], BackgroundMatches, [C|Mappings], Options) :- + ( T = scheme(_) + -> descendent_match(align(S,_,P), BackgroundMatches, C, Options), + C=align(_,T2,_), + vocab_member(T2, T) + ; descendent_match(align(S,T,P), BackgroundMatches, C, Options) + ), + !, + filter_(Cs, BackgroundMatches, Mappings, Options). +filter_([_|Cs], BackgroundMatches, Mappings, Options) :- + filter_(Cs, BackgroundMatches, Mappings, Options). + + align(Source, Target, BackgroundMatches, Match, Options) :- vocab_member(S, Source), vocab_member(T, Target), - match(align(S,T,[]), BackgroundMatches, Match, Options). - - -match(align(S, T, Prov0), BackgroundMatches, align(S, T, [Prov|Prov0]), Options) :- - option(steps(MaxSteps), Options), - descendent(S, MaxSteps, DescS, R1, Steps1), - descendent(T, MaxSteps, DescT, R2, Steps2), - get_assoc(DescS-DescT, BackgroundMatches, _), - Prov = [method(descendent_match), - source(DescS), - target(DescT), - steps(Steps1-Steps2), - graph([R1,R2]) - ]. - -descendent(R, MaxSteps, Child, rdf(R, Prop, Child), Steps) :- - skos_descendant_of(R, Child, MaxSteps, Steps), - rdf_equal(amalgame:descendant, Prop). - -selecter(In, Sel, Dis, Und, Options) :- - option(snd_input(SecList), Options), - option(type(SourceOrTarget), Options, source), - findall(S-T-P, member(align(S,T,P), SecList), KeyValueList), - keysort(KeyValueList, Deduped), - ord_list_to_assoc(Deduped, BackgroundMatches), - ( SourceOrTarget = source - -> selecter_(SourceOrTarget, In, BackgroundMatches, Sel, Dis, Und, Options) - ; predsort(ag_map:compare_align(target), In, InT), - selecter_(SourceOrTarget, InT, BackgroundMatches, Sel0, Dis0, Und0, Options), - predsort(ag_map:compare_align(source), Sel0, Sel), - predsort(ag_map:compare_align(source), Dis0, Dis), - predsort(ag_map:compare_align(source), Und0, Und) - ). - -selecter_(_, [], _, [], [], [], _). -selecter_(Type, [Head|Tail], BackgroundMatches, Sel, Dis, Und, Options) :- - Head = align(S,T,_), - ( Type == source - -> same_source(Tail, S, Same, Rest) - ; same_target(Tail, T, Same, Rest) - ), - selecter_(Type, Rest, BackgroundMatches, TailSel, TailDis, TailUnd, Options), - Candidates = [Head|Same], - maplist(descendent_count(BackgroundMatches, Options), Candidates, Counts0), - keysort(Counts0, Counts), - partition(=(0-_), Counts, Zero, NonZero), - ( NonZero \= [] - -> pairs_values(Zero, DisgardedSame), - pairs_values(NonZero, SelectedSame), - append([SelectedSame, TailSel], Sel), - append([DisgardedSame, TailDis], Dis), - Und = TailUnd - ; Sel = TailSel, - Dis = TailDis, - append([Candidates, TailUnd], Und) - ). - -descendent_count(BackgroundMatches, Options, Corr, Count-Merged) :- - findall(Match, - match(Corr, BackgroundMatches, Match, Options), - Matches), - length(Matches, Count), - ( Count > 0 - -> merge_provenance(Matches, [Merged]) - ; Merged = Corr - ). + descendent_match(align(S,T,[]), BackgroundMatches, Match, Options). diff --git a/lib/ag_modules/descendent_selecter.pl b/lib/ag_modules/descendent_selecter.pl new file mode 100644 index 0000000..bd1ce21 --- /dev/null +++ b/lib/ag_modules/descendent_selecter.pl @@ -0,0 +1,74 @@ +:- module(descedent_selecter, + []). + +:- use_module(library(apply)). +:- use_module(library(assoc)). +:- use_module(library(lists)). +:- use_module(library(option)). +:- use_module(library(pairs)). +:- use_module(library(sort)). + +:- use_module(library(amalgame/map)). +:- use_module(descendent). + +:- public amalgame_module/1. +:- public selecter/5. +:- public parameter/4. + +amalgame_module(amalgame:'DescendentSelecter'). + +parameter(steps, integer, 1, + 'depth of search, defaults to 1, e.g. direct parents only'). +parameter(type, + oneof([source, target]), source, + 'Discard other matches with the same source/target'). + +selecter(In, Sel, Dis, Und, Options) :- + option(snd_input(SecList), Options), + option(type(SourceOrTarget), Options, source), + findall(S-T-P, member(align(S,T,P), SecList), KeyValueList), + keysort(KeyValueList, Deduped), + ord_list_to_assoc(Deduped, BackgroundMatches), + ( SourceOrTarget = source + -> selecter_(SourceOrTarget, In, BackgroundMatches, Sel, Dis, Und, Options) + ; predsort(ag_map:compare_align(target), In, InT), + selecter_(SourceOrTarget, InT, BackgroundMatches, Sel0, Dis0, Und0, Options), + predsort(ag_map:compare_align(source), Sel0, Sel), + predsort(ag_map:compare_align(source), Dis0, Dis), + predsort(ag_map:compare_align(source), Und0, Und) + ). + +selecter_(_, [], _, [], [], [], _). +selecter_(Type, [Head|Tail], BackgroundMatches, Sel, Dis, Und, Options) :- + Head = align(S,T,_), + ( Type == source + -> same_source(Tail, S, Same, Rest) + ; same_target(Tail, T, Same, Rest) + ), + selecter_(Type, Rest, BackgroundMatches, TailSel, TailDis, TailUnd, Options), + Candidates = [Head|Same], + maplist(descedent_count(BackgroundMatches, Options), Candidates, Counts0), + keysort(Counts0, Counts), + partition(zero_key, Counts, Zero, NonZero), + ( NonZero \= [] + -> pairs_values(Zero, DisgardedSame), + pairs_values(NonZero, SelectedSame), + append([SelectedSame, TailSel], Sel), + append([DisgardedSame, TailDis], Dis), + Und = TailUnd + ; Sel = TailSel, + Dis = TailDis, + append([Candidates, TailUnd], Und) + ). + +zero_key(0-_Value). + +descedent_count(BackgroundMatches, Options, Corr, Count-Merged) :- + findall(Match, + descendent_match(Corr, BackgroundMatches, Match, Options), + Matches), + length(Matches, Count), + ( Count > 0 + -> merge_provenance(Matches, [Merged]) + ; Merged = Corr + ).