services.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Run to each test case
  40. var resetTestCase = function(index){
  41. if(testCases.length>index){
  42. testCases[index].currentIndex = 0;
  43. }
  44. }
  45. // @calledby: js/controllers.js: function scope.genNode(isEvaluating, nodes, vars, testCaseIndex): if(node.type=="read")...if(!isEvaluating)...else... strCode+= 'var a=readerInput('+testCaseIndex+');';
  46. var readerInput = function(index){
  47. //alert('js/services.js: readerInput(index=' + index + ')');
  48. if(testCases.length>index){
  49. var inputTXT = testCases[index].input;
  50. var ii = inputTXT.split("\n").join(" ").split(" ").join(" ");
  51. var ii2 = ii.split(" ");
  52. if(testCases[index].currentIndex<ii2.length){
  53. //console.log(index+" - "+ii2[testCases[index].currentIndex]);
  54. return ii2[testCases[index].currentIndex++];
  55. }
  56. }
  57. //console.log(index+" - "+0);
  58. return "0";
  59. }
  60. var endTest = function(index){
  61. //alert('js/services.js: endTest('+index+'):'+outputTXT+"||"+testCases[index].output);
  62. totalCasesEvaluated++;
  63. if(outputTXT==testCases[index].output){
  64. totalCasesPassed++;
  65. }
  66. if(totalCasesEvaluated==totalTestCases){
  67. // terminou a execucao de tds
  68. var apro = parseInt((totalCasesPassed/totalTestCases)*100);
  69. writer("------<br>Total de casos de testes: "+totalTestCases+"<br>Testes corretos: "+totalCasesPassed+"<br>Aproveitamento: "+apro+"%", false);
  70. //TODO verificar se precisa retornar ou não
  71. //TOTO retornando ao iassign
  72. getEvaluationCallback(apro/100);
  73. }
  74. //console.log(testCases[index]);
  75. outputTXT = "";
  76. }
  77. var writerError = function(id, message){
  78. $('.output').append("<a class='error' href='javascript:;' onclick='highlightError(\""+id+"\")'>"+message+"</a><br>");
  79. }
  80. var highlightError = function(id){
  81. $(".node-with-error").removeClass("node-with-error");
  82. $(id).addClass("node-with-error");
  83. }
  84. var processNodes = function(nodes){
  85. var scriptStr = "";
  86. angular.forEach(nodes, function(node, key){
  87. scriptStr+= "";
  88. });
  89. return "var nodes;";
  90. }
  91. ivprogModule.factory('IvProgSource', function(IvProgSourceParts,$filter){
  92. return {
  93. generate: function(o){
  94. var scriptStr = "";
  95. angular.forEach(o.functions, function(func, key){
  96. if(func.functionName=="main"){
  97. scriptStr+="var t = function(){";
  98. scriptStr+=" ";
  99. // processing variables
  100. angular.forEach(func.vars, function(variable, keyv){
  101. scriptStr+="var var_"+variable.id+" = "+variable.initialValue+";";
  102. });
  103. // processing other stuff
  104. scriptStr+="function "+func.functionName+"(){";
  105. scriptStr+= processNodes(func.nodes);
  106. scriptStr+= "}";
  107. scriptStr += "main();}; t();";
  108. }
  109. window.eval(scriptStr);
  110. //console.log(scriptStr);
  111. });
  112. }
  113. }
  114. });
  115. ivprogModule.factory('ExercicioProcessa', function($resource){
  116. return {
  117. processa: function(d){
  118. var atributos = [];
  119. var atributosValores = [];
  120. // descobrindo os atributos
  121. angular.forEach(d.universo.elemento[0].atributo, function(value, key){
  122. atributos.push(value.nome);
  123. atributosValores[value.nome] = [];
  124. });
  125. // descobrindo os valores
  126. angular.forEach(d.universo.elemento, function(elValue, elKey){
  127. angular.forEach(elValue.atributo, function(atValue, atKey){
  128. if(atributosValores[atValue.nome].indexOf(atValue.valor)==-1){
  129. atributosValores[atValue.nome].push(atValue.valor);
  130. }
  131. if(atValue.predicado && (atributosValores[atValue.nome].indexOf(atValue.predicado)==-1)){
  132. atributosValores[atValue.nome].push(atValue.predicado);
  133. }
  134. });
  135. });
  136. return { atributos: atributos, valores: atributosValores };
  137. }
  138. };
  139. });
  140. ivprogModule.factory('Universo', function($resource){
  141. universo = $resource('universos/:universoTipo/:universoSubTipo.json', {}, {
  142. query: {method:'GET', params:{}, isArray:false }});
  143. return universo;
  144. });