services.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. var ivprogModule = angular.module('ivprogServices', ['ngResource']);
  2. ivprogModule.factory('Exercicio', function($resource){
  3. return $resource(':exercicioURL', {}, {
  4. query: {method:'GET', params:{}, isArray:true }});
  5. });
  6. ivprogModule.factory('IvProgSourceParts', function(){
  7. //x alert('services.js: ' + ivprogModule.getSource());
  8. return {
  9. genVars: function(){
  10. alert(-12345);
  11. }
  12. };
  13. });
  14. var outputTXT = "";
  15. var writer = function(t, testCase){
  16. if(!testCase){
  17. $('.output').append(t+'<br>');
  18. }
  19. if(outputTXT==""){
  20. outputTXT = t;
  21. }else{
  22. outputTXT+=" "+t;
  23. }
  24. }
  25. var isPutDecimalNeeded = function(startVal){
  26. return startVal == Math.floor(startVal);
  27. }
  28. var getOutput = function(){
  29. return outputTXT;
  30. }
  31. var cleanOutput = function(){
  32. outputTXT = "";
  33. currentInput = 0;
  34. }
  35. var totalTestCases = 0;
  36. var testCases = [];
  37. var totalCasesEvaluated = 0;
  38. var totalCasesPassed = 0;
  39. var strErr = "";
  40. // Run to each test case
  41. var resetTestCase = function(index){
  42. if(testCases.length>index){
  43. testCases[index].currentIndex = 0;
  44. }
  45. }
  46. // @calledby: js/controllers.js: function scope.genNode(isEvaluating, nodes, vars, testCaseIndex): if(node.type=="read")...if(!isEvaluating)...else... strCode+= 'var a=readerInput('+testCaseIndex+');';
  47. var readerInput = function(index){
  48. //alert('js/services.js: readerInput(index=' + index + ')');
  49. if(testCases.length>index){
  50. var inputTXT = testCases[index].input;
  51. var ii = inputTXT.split("\n").join(" ").split(" ").join(" ");
  52. var ii2 = ii.split(" ");
  53. if(testCases[index].currentIndex<ii2.length){
  54. //console.log(index+" - "+ii2[testCases[index].currentIndex]);
  55. return ii2[testCases[index].currentIndex++];
  56. }
  57. }
  58. //console.log(index+" - "+0);
  59. return "0";
  60. }
  61. var endTest = function(index){
  62. //alert('js/services.js: endTest('+index+'):'+outputTXT+"||"+testCases[index].output);
  63. totalCasesEvaluated++;
  64. if(outputTXT==testCases[index].output){
  65. totalCasesPassed++;
  66. }
  67. else {
  68. strErr += "Teste " + index + ": esperado = " + testCases[index].output + " : encontrado = " + outputTXT + "<br>";
  69. }
  70. if(totalCasesEvaluated==totalTestCases){
  71. // terminou a execucao de tds
  72. var apro = parseInt((totalCasesPassed/totalTestCases)*100);
  73. writer("------<br>* Total de casos de testes: "+totalTestCases+"<br>Testes corretos: "+totalCasesPassed+"<br>Aproveitamento: "+apro+"%", false);
  74. // Se precisar depurar o erro habilitar if abaixo:
  75. if (totalCasesPassed<totalCasesEvaluated) {
  76. writer("<br>" + strErr + "");
  77. }
  78. //TODO verificar se precisa retornar ou não
  79. //TOTO retornando ao iassign
  80. getEvaluationCallback(apro/100);
  81. }
  82. //console.log(testCases[index]);
  83. outputTXT = "";
  84. }
  85. var writerError = function(id, message){
  86. $('.output').append("<a class='error' href='javascript:;' onclick='highlightError(\""+id+"\")'>"+message+"</a><br>");
  87. }
  88. var highlightError = function(id){
  89. $(".node-with-error").removeClass("node-with-error");
  90. $(id).addClass("node-with-error");
  91. }
  92. var processNodes = function(nodes){
  93. var scriptStr = "";
  94. angular.forEach(nodes, function(node, key){
  95. scriptStr+= "";
  96. });
  97. return "var nodes;";
  98. }
  99. ivprogModule.factory('IvProgSource', function(IvProgSourceParts,$filter){
  100. return {
  101. generate: function(o){
  102. var scriptStr = "";
  103. angular.forEach(o.functions, function(func, key){
  104. if(func.functionName=="main"){
  105. scriptStr+="var t = function(){";
  106. scriptStr+=" ";
  107. // processing variables
  108. angular.forEach(func.vars, function(variable, keyv){
  109. scriptStr+="var var_"+variable.id+" = "+variable.initialValue+";";
  110. });
  111. // processing other stuff
  112. scriptStr+="function "+func.functionName+"(){";
  113. scriptStr+= processNodes(func.nodes);
  114. scriptStr+= "}";
  115. scriptStr += "main();}; t();";
  116. }
  117. window.eval(scriptStr);
  118. //console.log(scriptStr);
  119. });
  120. }
  121. }
  122. });
  123. ivprogModule.factory('ExercicioProcessa', function($resource){
  124. return {
  125. processa: function(d){
  126. var atributos = [];
  127. var atributosValores = [];
  128. // descobrindo os atributos
  129. angular.forEach(d.universo.elemento[0].atributo, function(value, key){
  130. atributos.push(value.nome);
  131. atributosValores[value.nome] = [];
  132. });
  133. // descobrindo os valores
  134. angular.forEach(d.universo.elemento, function(elValue, elKey){
  135. angular.forEach(elValue.atributo, function(atValue, atKey){
  136. if(atributosValores[atValue.nome].indexOf(atValue.valor)==-1){
  137. atributosValores[atValue.nome].push(atValue.valor);
  138. }
  139. if(atValue.predicado && (atributosValores[atValue.nome].indexOf(atValue.predicado)==-1)){
  140. atributosValores[atValue.nome].push(atValue.predicado);
  141. }
  142. });
  143. });
  144. return { atributos: atributos, valores: atributosValores };
  145. }
  146. };
  147. });
  148. ivprogModule.factory('Universo', function($resource){
  149. universo = $resource('universos/:universoTipo/:universoSubTipo.json', {}, {
  150. query: {method:'GET', params:{}, isArray:false }});
  151. return universo;
  152. });