(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)









(*<pre>*)

BeginPackage["ManagingKnotData`",{"KnotTheory`","WikiLink`"}];

FromWikiString;



LoadInvariantRules::usage="LoadInvariantRules[pagename] loads definitions for invariants from the page pagename (using the current WikiLink` connection).";

InvariantDefinitionTable::usage="InvariantDefinitionTable[rules] generates an html table representing rules, suitable for input via LoadInvariantRules.";

Invariants::usage="Invariants[] returns a list of all invariant definitions currently known. Invariants[string_pattern] returns all invariant definitions with type matching string_pattern.";

InvariantNames::usage="InvariantNames[rules] returns a list of the names of the invariants described by rules.";

RetrieveInvariant::usage="RetrieveInvariant[invariant, knot, source] returns the value of the named invariant for the given knot, from the specified source. At present, the only sources understood are \"KnotAtlas\" and \"KnotTheory`\". More, like \"Livingston\", may come soon!";

RetrieveInvariants::usage="RetrieveInvariants[invariantList, knotList, source] returns a list of triples, each of the form {\"InvariantName\", K, value}, from the specified source. At present, the only sources understood are \"KnotAtlas\" and \"KnotTheory`\". More, like \"Livingston\", may come soon!";

StoreInvariants::usage="StoreInvariants[data, target] stores the data, given in the form produced by RetrieveInvariants, in the specified target. At present, the only target understood is \"KnotAtlas\". Perhaps soon they'll be a way to specify a Mathematica .m file as the target.";

FindDataDiscrepancies::usage=
    "FindDataDiscrepancies[data1, data2] returns a list of conflicts between the two lists of data. The conflicts are given in the form {\"InvariantName\", K, value1, value2}, where value1 is the value given in data1, and value2 is the value given in data2. See also FindMissingData.\n"<>
      "FindDataDiscrepancies[invariantList, knotList, source1, source2] first makes two calls to RetrieveInvariants to generate data1 and data2.";

FindMissingData::usage=
    "FindMissingData[data1, data2] returns a sublist of data1 consisting of items for which there is no corresponding value in data2. See also FindDataDiscrepancies.\n"<>
      "FindMissingData[invariantList, knotList, source1, source2] first makes two calls to RetrieveInvariants to generate data1 and data2.";

Begin["`Private`"];



namePattern=
    "<!-- Invariant name -->"~~WhitespaceCharacter...~~
          "<td>"~~n:ShortestMatch[__]~~"</td>"\[RuleDelayed]n;

linePattern=
    "<!-- "~~t:(WordCharacter..)~~
          " ="~~ShortestMatch[__]~~
              "<td>"~~v:ShortestMatch[___]~~"</td>"\[RuleDelayed](t\[Rule]v);

expressionTags={"ReadWiki","KnotTheory"};

ConstructInvariantRule[S_String]:=
  Module[{names=StringCases[S,namePattern]},
    If[Length[names]\[NotEqual]1,Return[$Failed]];
    names\[LeftDoubleBracket]1\[RightDoubleBracket]\[Rule]
        DeleteCases[
          StringCases[S,linePattern],_\[Rule]
            ""]/.(t_String?(MemberQ[expressionTags,#]&)\[Rule]
            s_String)\[RuleDelayed](t\[Rule]ToExpression[s])]

LoadInvariantRules[pagename_String]:=
  AllInvariants=
    ConstructInvariantRule/@
      Drop[StringSplit[WikiGetPageText[pagename],"<tr>"],2]



InvariantTags[rules_]:=Union@@(rules/.(_\[Rule]L_List)\[RuleDelayed]First/@L)

TableHeader[rules_]:=
  "<tr>\n<th>Invariant name</th>\n"<>StringJoin@@("   <th>"<>#<>"</th>\n"&/@
          InvariantTags[rules])<>"</tr>\n"

whitespaces[n_]:=StringJoin@@Table[" ",{n}]

TableRow[rules_,i_]:=
  "<tr>\n<!-- Invariant name --> <td>"<>rules\[LeftDoubleBracket]i,
      1\[RightDoubleBracket]<>"</td>\n"<>
    StringJoin@@("<!-- "<>#<>" ="<>whitespaces[20-StringLength[#]]<>"--> <td>"<>
              ToString[#/.rules\[LeftDoubleBracket]i,
                      2\[RightDoubleBracket]/.{#\[Rule]""}]<>"</td>\n"&/@
          InvariantTags[rules])<>"</tr>\n"

InvariantDefinitionTable[rules_]:=
  "{{Invariant Definition Table Warning}}\n"<>"<table width=\"100%\">\n"<>
    TableHeader[rules]<>
    StringJoin@@Table[TableRow[rules,i],{i,1,Length[rules]}]<>"</table>"



Invariants[S_]:=
  Select[AllInvariants,
    StringMatchQ[("Type"/.#\[LeftDoubleBracket]2\[RightDoubleBracket]),S]&]

Invariants[]:=Invariants[__]

Invariants["KnotTheory` Knot Invariants"]:=
  Invariants[
    "Navigation"|"Knot Presentations"|"Link Presentations"|"3D Invariant"|"Polynomial Invariant"|
      "Vassiliev Invariant"]

Invariants["KnotTheory` Link Invariants"]:=
  Invariants["Navigation"|"Link Presentations"|"Polynomial Invariant"]



FromWikiString[S_String]/;StringMatchQ[S,"<math>"~~__~~"</math>"]:=
  ToExpression[StringReplace[S,"<math>"~~X__~~"</math>"\[RuleDelayed]X],
    TeXForm]

FromWikiString[S_String]:=ToExpression[S]

InvariantNames[L_List]:=Cases[L,(S_String\[Rule]_List)\[RuleDelayed]S]

InvariantRule[I_String]:=Module[{rule},rule=I/.AllInvariants;
    If[rule\[Equal]I,Print["I don't recognise the invariant "<>I<>"."];
      Return[$Failed],rule]]

RetrieveInvariant[I_String,K_,"KnotTheory"]:=
  Module[{rule=InvariantRule[I],KnotTheory},
    If[rule\[Equal]$Failed,Return[$Failed]];
    KnotTheory="KnotTheory"/.(I/.AllInvariants);
    If[KnotTheory\[Equal]"KnotTheory",
      Print["Sorry, I don't know how to calculate the invariant "<>I<>
          " using KnotTheory`."];Return[$Failed]];
    KnotTheory[K]]

RetrieveInvariants[Is:{__Rule},Ks_List,source_String]:=
  RetrieveInvariants[InvariantNames[Is],Ks,source]

RetrieveInvariants[Is:{__String},Ks_List,source_String]:=
  Flatten[Outer[{#1,#2,RetrieveInvariant[#1,#2,source]}&,Is,Ks],1]

RetrieveInvariant[I_String,K_,"KnotAtlas"]:=
  Module[{rule=InvariantRule[I],WikiPage,WikiResult,ReadWiki},
    If[rule\[Equal]$Failed,Return[$Failed]];
    WikiPage="WikiPage"/.(I/.AllInvariants);
    If[WikiPage\[Equal]"WikiPage",
      Print["Sorry, I don't know how to retrieve the invariant "<>I<>
          " from the Knot Atlas."];Return[$Failed]];
    ReadWiki="ReadWiki"/.(I/.AllInvariants);
    If[ReadWiki\[Equal]"ReadWiki",
      Print["Sorry, I don't know how to translate the invariant "<>I<>
          " from its textual representation in the Knot Atlas."];
      Return[$Failed]];
    WikiResult=WikiGetPageText["Data:"<>NameString[K]<>"/"<>WikiPage];
    ReadWiki[WikiResult]]

RetrieveInvariants[Is:{__Rule},Ks_List,"KnotAtlas"]:=
  RetrieveInvariants[InvariantNames[Is],Ks,"KnotAtlas"]



RetrieveInvariants[Is:{__String},Ks_List,"KnotAtlas"]:=
  Flatten[Outer[{#1,#2,RetrieveInvariant[#1,#2,"KnotAtlas"]}&,Is,Ks],1]

Clear[WikiPageForInvariant];
WikiPageForInvariant[I_String]:=
  WikiPageForInvariant[I]=
    Module[{rule=InvariantRule[I],wikiPage},
      If[rule\[Equal]$Failed,Return[$Failed]];
      wikiPage="WikiPage"/.(I/.AllInvariants);
      If[wikiPage\[Equal]"WikiPage",
        Print["Sorry, I don't know how to store the invariant "<>I<>
            " in the Knot Atlas."];Return[$Failed],Return[wikiPage]];]

Options[StoreInvariants]={Write\[Rule]True};

StoreInvariants[D:{{_String,_,_}...},"KnotAtlas",opts___]:=
  Module[{invariants,unknownInvariants,wikiPages,uploadPairs},
    invariants=Union[Part[D,All,1]];
    wikiPages=WikiPageForInvariant/@invariants;
    If[MemberQ[wikiPages,$Failed],Return[$Failed]];
    uploadPairs={"Data:"<>
              NameString[#\[LeftDoubleBracket]2\[RightDoubleBracket]]<>"/"<>
              WikiPageForInvariant[#\[LeftDoubleBracket]1\[RightDoubleBracket]\
],ToString[#\[LeftDoubleBracket]3\[RightDoubleBracket],WikiForm]}&/@D;
    If[Write/.{opts}/.Options[StoreInvariants],WikiSetPageTexts[uploadPairs],
      uploadPairs]]

FindDataDiscrepancies[Is:{__Rule},Ks_List,source1_String,source2_String]:=
  FindDataDiscrepancies[InvariantNames[Is],Ks,source1,source2]

FindDataDiscrepancies[Is:{__String},Ks_List,source1_String,source2_String]:=
  FindDataDiscrepancies[RetrieveInvariants[Is,Ks,source1],
    RetrieveInvariants[Is,Ks,source2]]

FindDataDiscrepancies[D1:{{_String,_,_}...},D2:{{_String,_,_}...}]:=
  Module[{D1t,D2t,D,P,C},(*Mark the data,according to where it came from.*)
      D1t={#\[LeftDoubleBracket]1\[RightDoubleBracket],#\[LeftDoubleBracket]2\
\[RightDoubleBracket],1,#\[LeftDoubleBracket]3\[RightDoubleBracket]}&/@D1;
    D2t={#\[LeftDoubleBracket]1\[RightDoubleBracket],#\[LeftDoubleBracket]2\
\[RightDoubleBracket],2,#\[LeftDoubleBracket]3\[RightDoubleBracket]}&/@D2;
    (*Combine the data,
      and split it into doublets (or singlets) corresponding to the same \
invariant and knot.*)D=
      Split[Sort[D1t~Join~D2t],SameQ[Take[#1,2],Take[#2,2]]&];
    (*Take only the pairs.*)P=Select[D,Length[#]\[Equal]2&];
    (*Combine the pairs*)C=
      P/.{{I_,K_,1,V1_},{I_,K_,2,V2_}}\[RuleDelayed]{I,K,V1,V2};
    Select[
      C,#\[LeftDoubleBracket]3\[RightDoubleBracket]=!=#\[LeftDoubleBracket]4\
\[RightDoubleBracket]&]]

FindMissingData[D1:{{_String,_,_}...},D2:{{_String,_,_}...}]:=
  Complement[D1,D2,SameTest\[Rule]SameQ[Take[#1,2],Take[#2,2]]&]

End[];

EndPackage[];

(*</pre>[[Category:Source Code]]*)