
(*basic package to generate some states for Rubi Rules*)
(*for summer 2022 CAS integration tests*)
(*Nasser M. Abbasi, Version Oct 1, 2022. Written using Mathematica 13.1 version *)
(*to use, do the following. *)
(*  
<<CAS`
db = CAS`openDB["cas_integration_tests.db"]; 
CAS`getNumberOfIntegrals[db]
CAS`generateRubiStepsStats[db]
CAS`generateRubiRulesStats[db]
CAS`generateLeafStats[db]
CAS`closeDB[db]
*)

BeginPackage["CAS`"] 
Unprotect @@ Names["CAS`*"]; 
ClearAll @@ Names["CAS`*"]; 


(* public API *)
getNumberOfIntegrals::usage = "getNumberOfIntegrals[db]  gets number of integrals" 
openDB::usage = "openDB[folderName]  opens the database"
closeDB::usage = "closeDB[db]  closes the database"
generateRubiStepsStats::usage= "generateRubiStepsStats[db]  generate stats per rubi steps"
generateRubiRulesStats::usage= "generateRubiRulesStats[db]  generate stats per rubi rules"
generateLeafStats::usage= "generateLeafStats[db]  generate histograms for leaf size"


(*----------------------------------*)

Begin["`Private`"] 
Needs["DatabaseLink`"]

(*----------------------------------*)

openDB[fullPath_String]:= Module[{db},	
   db = Check[OpenSQLConnection[JDBC["SQLite", fullPath]],$Failed];
   db	
 
];

(*----------------------------------*)
closeDB[db_]:= Module[{},Check[CloseSQLConnection[db],$Failed]];

(*----------------------------------*)
	
getNumberOfIntegrals[db_] := Module[{numberOfIntegrals},	    
    numberOfIntegrals = SQLExecute[db, "SELECT COUNT(*) FROM main;"][[1, 1]];
    numberOfIntegrals
]; 


(*------------------------------------------*)
(*Function to generate histogram of leaf size per cas*)
getHistogramForLeafSize[casName_String, db_] :=  Module[{data, binWidth = 40},
  data = SQLExecute[db, "SELECT rubi_leafsize FROM main where " <> casName <> "_pass=1;"] // Flatten;
  
  Histogram[data, {binWidth},
   Frame -> {True, True, False, False},
   FrameLabel -> {"Leaf size", "Number of integrals"},
   PlotLabel -> Style[casName, Bold],
   ImageSize -> 300, BaseStyle -> 12]
];

(*----------------------------------*)

casSystems = {"rubi", "mma", "maple", "fricas", "giac", "maxima", "sympy", "mupad"};

(*------------------------------------------*)
generateRubiStepsStats[db_]:=Module[{maxNumberOfSteps=20,dataPerStep,m,lbl,data,g,cas,nProblemsForTheseSteps,n},

dataPerStep = Table[
   nProblemsForTheseSteps = SQLExecute[db, "SELECT COUNT(*) FROM main where rubi_number_of_steps=" <> ToString@n <> ";"][[1, 1]];
   m = SQLExecute[db, "SELECT COUNT(*) FROM main where rubi_number_of_steps=" <> ToString@n <> " and " <> cas <> "_pass=1;"][[1, 1]];
   m*100./nProblemsForTheseSteps,
    {n, 1, maxNumberOfSteps}, {cas, casSystems}
   ];
   
data = Transpose@dataPerStep;
lbl   = LabelStyle -> Directive[Italic, Small, Black];
data = Callout[data[[#]], casSystems[[#]], {1.1*#, Above}, lbl] & /@  Range[Length@casSystems];
g = ListLinePlot[data,
  GridLines -> {Range[maxNumberOfSteps], Automatic},
  GridLinesStyle -> LightGray,
  ImageSize -> 600,
  Frame -> {True, True, False, False},
  FrameLabel -> {"Rubi number of steps", "% solved problems"},
  Axes -> False,
  (*PlotLegends -> casSystems,*)
  PlotRange -> {{1, maxNumberOfSteps}, Automatic},
  FrameTicks -> {{Automatic, None}, {Range[maxNumberOfSteps], None}},
  BaseStyle -> 16];
  
  g
];


(*------------------------------------------*)
generateRubiRulesStats[db_]:=Module[{maxNumberOfRules=9,m,lbl,data,g,cas,nProblemsForTheseRules,n},

data = Table[
   nProblemsForTheseRules = SQLExecute[db, "SELECT COUNT(*) FROM main where rubi_number_of_rules=" <> ToString@n <> ";"][[1, 1]];
   m = SQLExecute[db, "SELECT COUNT(*) FROM main where rubi_number_of_rules=" <> ToString@n <> " and " <> cas <> "_pass=1;"][[1, 1]];
   m*100./nProblemsForTheseRules,
    {n, 1, maxNumberOfRules}, {cas, casSystems}
   ];
   
data = Transpose@data;
lbl   = LabelStyle -> Directive[Italic, Small, Black];
data = Callout[data[[#]], casSystems[[#]], {1.1*#, Above}, lbl] & /@  Range[Length@casSystems];
g = ListLinePlot[data,
  GridLines -> {Range[maxNumberOfRules], Automatic},
  GridLinesStyle -> LightGray,
  ImageSize -> 600,
  Frame -> {True, True, False, False},
  FrameLabel -> {"Rubi number of steps", "% solved problems"},
  Axes -> False,
  (*PlotLegends -> casSystems,*)
  PlotRange -> {{1, maxNumberOfRules}, Automatic},
  FrameTicks -> {{Automatic, None}, {Range[maxNumberOfRules], None}},
  BaseStyle -> 16];
  
  g
];

(*----------------------------------*)
generateLeafStats[db_]:=Module[{p,g,lbl},

p = getHistogramForLeafSize[#, db] & /@ casSystems;
g = Grid[Partition[p, 2], Frame -> All, FrameStyle -> LightGray];
lbl = Style[Column[{"Histogram showing distribution of solved integrals", 
                    "based on leaf size using bin width of 40"}], Bold, 16];
g = Labeled[g, lbl, Top];	
g		
	
];	
(*----------------------------------*)

End[]; 
Protect @@ Names["CAS`*"]; 
EndPackage[];

