From 0a85827eb22ac755fabe3509c138a60b1b00886b Mon Sep 17 00:00:00 2001 From: Magdalena Kalin Date: Thu, 17 Oct 2019 13:44:22 +0200 Subject: [PATCH] Fix Ferry, Harbour and TestDriver --- find_algos.rb | 78 ++++++------------------ lol.rb | 154 ++++++++++++++++++++++------------------------- sorting_algos.rb | 35 +++++------ 3 files changed, 109 insertions(+), 158 deletions(-) diff --git a/find_algos.rb b/find_algos.rb index 45900f8..dc31074 100644 --- a/find_algos.rb +++ b/find_algos.rb @@ -14,72 +14,34 @@ end def find_best_with_extra_parameter(container_obj, based_on, compare, type, extra_based_on, extra_compare, extra_param, based_on_type) - result_init = extra_compare == " > " ? "Integer.MIN_VALUE" : "Integer.MAX_VALUE" - - if based_on_type == "int" - helper_variable = "int result#{based_on} = #{result_init};" - else - helper_variable = "String result#{based_on};" - end + result_init = extra_compare == ' > ' ? 'Integer.MIN_VALUE' : 'Integer.MAX_VALUE' - if extra_compare.length > 4 - # TODO: Check if this case is required - " - #{type} result = null; - #{helper_variable} - for (var pls : #{container_obj}) { - if ((pls.get#{extra_based_on}()#{extra_compare}(#{extra_param}) && pls.get#{based_on}() #{compare} result.get#{based_on}())) { - result = pls; - } - } + steffan = extra_compare == ".equals" ? ".equals(#{extra_param}" : " #{extra_compare} #{extra_param}" - return result; - " - else - " - #{type} result = null; - #{helper_variable} - for (var pls : #{container_obj}) { - if ((pls.get#{extra_based_on}()#{extra_compare}#{extra_param} && pls.get#{based_on}() #{compare} #{result_init}) { - result = pls; - } - } - - return result; - " - end + "#{type} result = null; +\t\tint result_maggie = #{result_init}; +\t\tfor (var pls : #{container_obj}) { +\t\t\tif (pls.get#{extra_based_on}()#{steffan}) && pls.get#{based_on}() #{compare} result_maggie) { +\t\t\t\tresult = pls; +\t\t\t\tresult_maggie = pls.get#{based_on}(); +\t\t\t} +\t\t} +\t\treturn result;" end def find_all(container_obj, based_on, compare, type, parameter) - - if compare.length > 4 - - " - ArrayList<#{type}> result = new ArrayList<>(); - for (var pls : #{container_obj}) { - if (pls.get#{based_on}#{compare}(#{parameter}) { - result.add(pls); - } - } - return result; - " - else - " - ArrayList<#{type}> result = new ArrayList<>(); - for (var pls : #{container_obj}) { - if (pls.get#{based_on}() #{compare} #{parameter}) { - result.add(pls); - } - } - return result; - " - end + alex = compare=='.equals' ? ".equals(#{parameter})" : " #{compare} #{parameter}" + "ArrayList<#{type}> result = new ArrayList<>(); +\t\tfor (var pls : #{container_obj}) { +\t\t\tif (pls.get#{based_on}()#{alex}) { +\t\t\t\tresult.add(pls); +\t\t\t} +\t\t} +\t\treturn result;" end def add_method(container_name, name) - " - #{container_name}.add(#{name}); - " + "#{container_name}.add(#{name});" end diff --git a/lol.rb b/lol.rb index 6becdfa..f61f6b1 100644 --- a/lol.rb +++ b/lol.rb @@ -24,122 +24,116 @@ def construct_class(name_of_class, parameters, to_string_template, sorting_filte sorting_string = compare_to(sorting_filters) - fields = "" - parameters.each do |x| - fields += "private #{x[:type]} #{x[:name]}; \n" + fields = parameters.map do |x| + "\tprivate #{x[:type]} #{x[:name]};" end constructor_parameters = parameters.map { |x| "#{x[:type]} #{x[:name]}"}.join "," getters = parameters.map do |x| - "public #{x[:type]} get#{x[:name]}(){ - return #{x[:name]}; - }" + "\tpublic #{x[:type]} get#{x[:name]}(){ +\t\treturn #{x[:name]}; +\t}" end.join "\n" lol = parameters.map do |x| - "this.#{x[:name]} = #{x[:name]};" + "\t\tthis.#{x[:name]} = #{x[:name]};" end (0...parameters.length).each do |x| pls = "\" + #{parameters[x][:name]} + \"" to_string_template.sub! "#{x}", pls end - " - public class #{name_of_class} implements Comparable<#{name_of_class}> { - #{fields} +public class #{name_of_class} implements Comparable<#{name_of_class}> { +#{fields.join "\n"} - public #{name_of_class}(#{constructor_parameters}) { +\tpublic #{name_of_class}(#{constructor_parameters}) { +#{lol.join("\n")} +\t} - #{lol.join("\n")} - } +#{getters} - #{getters} +\tpublic String toString() { +\t\treturn " + '"' + "#{to_string_template}" + '"' + "; +\t} - public String toString() { - return " + '"' + "#{to_string_template}" + '"' + "; - } - - public int compareTo(#{name_of_class} other) { - #{sorting_string} - } - } - " +\tpublic int compareTo(#{name_of_class} other) { +#{sorting_string} +\t} +}" end def construct_driver(class_name, parameters, methods, container_class, container_params) - def args(parameters) + args = lambda {|parameters| parameters.map do |x| type = x[:type] type == "int" ? "#{rand(max=100)}" : "\"#{(0...10).map { ('a'..'z').to_a[rand(26)] }.join}\"" end.join "," - end + } maggie = methods.map do |x| if x[:print] - "System.out.println(lol.#{x[:name]}(#{args x[:parameters]}));" + "System.out.println(lol.#{x[:name]}(#{args.call x[:parameters]}));" else - "lol.#{x[:name]}(#{args x[:parameters]});" + "lol.#{x[:name]}(#{args.call x[:parameters]});" end end - " - public class TestDriver { + "public class TestDriver { - public TestDriver(){} +\tpublic TestDriver(){} - public static void test() { - var tmp1 = new #{class_name}(#{args parameters}); - var tmp2 = new #{class_name}(#{args parameters}); - var tmp3 = new #{class_name}(#{args parameters}); - var tmp4 = new #{class_name}(#{args parameters}); - var tmp5 = new #{class_name}(#{args parameters}); +\tpublic static void test() { +\t\tvar tmp1 = new #{class_name}(#{args.call parameters}); +\t\tvar tmp2 = new #{class_name}(#{args.call parameters}); +\t\tvar tmp3 = new #{class_name}(#{args.call parameters}); +\t\tvar tmp4 = new #{class_name}(#{args.call parameters}); +\t\tvar tmp5 = new #{class_name}(#{args.call parameters}); - System.out.println(tmp1); - System.out.println(tmp2); - System.out.println(tmp3); - System.out.println(tmp4); - System.out.println(tmp5); +\t\tSystem.out.println(tmp1); +\t\tSystem.out.println(tmp2); +\t\tSystem.out.println(tmp3); +\t\tSystem.out.println(tmp4); +\t\tSystem.out.println(tmp5); - #{container_class} lol = new #{container_class}(#{args container_params}); +\t\t#{container_class} lol = new #{container_class}(#{args.call container_params}); - lol.add#{class_name}(tmp1); - lol.add#{class_name}(tmp2); - lol.add#{class_name}(tmp3); - lol.add#{class_name}(tmp4); - lol.add#{class_name}(tmp5); - System.out.println(\"\"); - System.out.println(\"Some checkpoint\"); - #{maggie.join "\n\n\tSystem.out.println(\"Some Checkpoint\"); \n\t"} - } +\t\tlol.add#{class_name}(tmp1); +\t\tlol.add#{class_name}(tmp2); +\t\tlol.add#{class_name}(tmp3); +\t\tlol.add#{class_name}(tmp4); +\t\tlol.add#{class_name}(tmp5); +\t\tSystem.out.println(\"\"); +\t\tSystem.out.println(\"Some checkpoint\"); +\t\t#{maggie.join "\n\n\t\tSystem.out.println(\"Some Checkpoint\"); \n\t\t"} +\t} - public static void main(String[] args) { - TestDriver.test(); - } -} - " +\tpublic static void main(String[] args) { +\t\tTestDriver.test(); +\t} +}" end def construct_container(name, parameters, given_fields, methods, arraylist_name) fields = "" parameters.each do |x| - fields += "private #{x[:type]} #{x[:name]};\n" + fields += "\tprivate #{x[:type]} #{x[:name]};\n" end given_fields.each do |x| - fields += " private #{x[:type]} #{x[:name]};\n" + fields += "\tprivate #{x[:type]} #{x[:name]};\n" end maggie = methods.map do |x| " - public #{x[:return_type]} #{x[:name]} (#{x[:params].map {|y| "#{y[:type]} #{y[:name]}"}.join ","}) { - #{x[:method_type]} - } - " +\tpublic #{x[:return_type]} #{x[:name]} (#{x[:params].map {|y| "#{y[:type]} #{y[:name]}"}.join ","}) { +\t\t#{x[:method_type]} +\t} +" end construtor_params = parameters.map do |x| @@ -151,29 +145,27 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name) "this.#{x[:name]} = #{x[:name]};" end - " - import java.util.*; - import java.util.stream.Collectors; + "import java.util.*; +import java.util.stream.Collectors; - public class #{name} { - #{fields} +public class #{name} { +#{fields} - public #{name}(#{construtor_params.join ","}) { - #{constructor_handlers.join "\n"} - this.#{arraylist_name} = new ArrayList<>(); - } + \tpublic #{name}(#{construtor_params.join ","}) { + \t\t#{constructor_handlers.join "\n"} + \t\tthis.#{arraylist_name} = new ArrayList<>(); + \t} +#{maggie.join ""} - #{maggie.join "\n"} +\tpublic void print#{name}() { +\t\tSystem.out.println(\"#{name}\"); +\t\tCollections.sort(#{arraylist_name}); - public void print#{name}() { - System.out.println(\"#{name}\"); - Collections.sort(#{arraylist_name}); - - for (var i : #{arraylist_name}) { - System.out.println(i); - } - } - }" +\t\tfor (var i : #{arraylist_name}) { +\t\t\tSystem.out.println(i); +\t\t} +\t} +}" end diff --git a/sorting_algos.rb b/sorting_algos.rb index e9611e2..1b8a032 100644 --- a/sorting_algos.rb +++ b/sorting_algos.rb @@ -3,37 +3,34 @@ def compare_to(sortings) first, second, third = *sortings - def return_stat sorting + return_stat = lambda{ |sorting| if sorting.type == "String" {:ret => "this.#{sorting.variable}.compareTo(other.#{sorting.variable})"} else {:ret => sorting.comparator == ">" ? "this.#{sorting.variable} - other.#{sorting.variable}" : "other.#{sorting.variable} - this.#{sorting.variable}"} - end - end + end} - def comp sorting + comp = lambda {|sorting| if sorting.type == "String" {:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})", - :ret => (return_stat sorting)[:ret]} + :ret => (return_stat.call sorting)[:ret]} else {:if => "this.#{sorting.variable} != other.#{sorting.variable}", - :ret => (return_stat sorting)[:ret]} - end - end + :ret => (return_stat.call sorting)[:ret]} + end} if defined? third - "if (#{(comp first)[:if]}) { - return #{(comp first)[:ret]}; - } else if (#{(comp second)[:if]}) { - return #{(comp second)[:ret]}; - } - return #{(comp third)[:ret]};" + "\t\tif (#{(comp.call first)[:if]}) { +\t\t\treturn #{(comp.call first)[:ret]}; +\t\t} else if (#{(comp.call second)[:if]}) { +\t\t\treturn #{(comp.call second)[:ret]}; +\t\t} +\t\treturn #{(comp.call third)[:ret]};" else - "if (#{(comp first)[:if]}) { - return #{(comp first)[:ret]}; - } - return #{(comp second)[:ret]}; - " + "\t\tif (#{(comp.call first)[:if]}) { +\t\t\treturn #{(comp.call first)[:ret]}; +\t\t} +\t\treturn #{(comp.call second)[:ret]};" end end \ No newline at end of file