This commit is contained in:
Alexander Munch-Hansen 2019-10-21 16:22:00 +02:00
commit f75a9a7b4f
3 changed files with 101 additions and 148 deletions

View File

@ -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) 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" 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
if extra_compare.length > 4 steffan = extra_compare == ".equals" ? ".equals(#{extra_param}" : " #{extra_compare} #{extra_param}"
# 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;
}
}
return result; "#{type} result = null;
" \t\tint result_maggie = #{result_init};
else \t\tfor (var pls : #{container_obj}) {
" \t\t\tif (pls.get#{extra_based_on}()#{steffan}) && pls.get#{based_on}() #{compare} result_maggie) {
#{type} result = null; \t\t\t\tresult = pls;
#{helper_variable} \t\t\t\tresult_maggie = pls.get#{based_on}();
for (var pls : #{container_obj}) { \t\t\t}
if ((pls.get#{extra_based_on}()#{extra_compare}#{extra_param} && pls.get#{based_on}() #{compare} #{result_init}) { \t\t}
result = pls; \t\treturn result;"
}
}
return result;
"
end
end end
def find_all(container_obj, based_on, compare, type, parameter) def find_all(container_obj, based_on, compare, type, parameter)
alex = compare=='.equals' ? ".equals(#{parameter})" : " #{compare} #{parameter}"
if compare.length > 4 "ArrayList<#{type}> result = new ArrayList<>();
\t\tfor (var pls : #{container_obj}) {
" \t\t\tif (pls.get#{based_on}()#{alex}) {
ArrayList<#{type}> result = new ArrayList<>(); \t\t\t\tresult.add(pls);
for (var pls : #{container_obj}) { \t\t\t}
if (pls.get#{based_on}#{compare}(#{parameter}) { \t\t}
result.add(pls); \t\treturn result;"
}
}
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
end end
def add_method(container_name, name) def add_method(container_name, name)
" "#{container_name}.add(#{name});"
#{container_name}.add(#{name});
"
end end

136
lol.rb
View File

@ -24,121 +24,115 @@ def construct_class(name_of_class, parameters, to_string_template, sorting_filte
sorting_string = compare_to(sorting_filters) sorting_string = compare_to(sorting_filters)
fields = "" fields = parameters.map do |x|
parameters.each do |x| "\tprivate #{x[:type]} #{x[:name]};"
fields += "private #{x[:type]} #{x[:name]}; \n"
end end
constructor_parameters = parameters.map { |x| "#{x[:type]} #{x[:name]}"}.join "," constructor_parameters = parameters.map { |x| "#{x[:type]} #{x[:name]}"}.join ","
getters = parameters.map do |x| getters = parameters.map do |x|
"public #{x[:type]} get#{x[:name]}(){ "\tpublic #{x[:type]} get#{x[:name]}(){
return #{x[:name]}; \t\treturn #{x[:name]};
}" \t}"
end.join "\n" end.join "\n"
lol = parameters.map do |x| lol = parameters.map do |x|
"this.#{x[:name]} = #{x[:name]};" "\t\tthis.#{x[:name]} = #{x[:name]};"
end end
(0...parameters.length).each do |x| (0...parameters.length).each do |x|
pls = "\" + #{parameters[x][:name]} + \"" pls = "\" + #{parameters[x][:name]} + \""
to_string_template.sub! "#{x}", pls to_string_template.sub! "#{x}", pls
end end
" "
public class #{name_of_class} implements Comparable<#{name_of_class}> { public class #{name_of_class} implements Comparable<#{name_of_class}> {
#{fields} #{fields.join "\n"}
public #{name_of_class}(#{constructor_parameters}) {
\tpublic #{name_of_class}(#{constructor_parameters}) {
#{lol.join("\n")} #{lol.join("\n")}
} \t}
#{getters} #{getters}
public String toString() { \tpublic String toString() {
return " + '"' + "#{to_string_template}" + '"' + "; \t\treturn " + '"' + "#{to_string_template}" + '"' + ";
} \t}
public int compareTo(#{name_of_class} other) { \tpublic int compareTo(#{name_of_class} other) {
#{sorting_string} #{sorting_string}
} \t}
} }"
"
end end
def construct_driver(class_name, parameters, methods, container_class, container_params) def construct_driver(class_name, parameters, methods, container_class, container_params)
def args(parameters) args = lambda {|parameters|
parameters.map do |x| parameters.map do |x|
type = x[:type] type = x[:type]
type == "int" ? "#{rand(max=100)}" : "\"#{(0...10).map { ('a'..'z').to_a[rand(26)] }.join}\"" type == "int" ? "#{rand(max=100)}" : "\"#{(0...10).map { ('a'..'z').to_a[rand(26)] }.join}\""
end.join "," end.join ","
end }
maggie = methods.map do |x| maggie = methods.map do |x|
if x[:print] if x[:print]
"System.out.println(lol.#{x[:name]}(#{args x[:parameters]}));" "System.out.println(lol.#{x[:name]}(#{args.call x[:parameters]}));"
else else
"lol.#{x[:name]}(#{args x[:parameters]});" "lol.#{x[:name]}(#{args.call x[:parameters]});"
end end
end end
" "public class TestDriver {
public class TestDriver {
public TestDriver(){} \tpublic TestDriver(){}
public static void test() { \tpublic static void test() {
var tmp1 = new #{class_name}(#{args parameters}); \t\tvar tmp1 = new #{class_name}(#{args.call parameters});
var tmp2 = new #{class_name}(#{args parameters}); \t\tvar tmp2 = new #{class_name}(#{args.call parameters});
var tmp3 = new #{class_name}(#{args parameters}); \t\tvar tmp3 = new #{class_name}(#{args.call parameters});
var tmp4 = new #{class_name}(#{args parameters}); \t\tvar tmp4 = new #{class_name}(#{args.call parameters});
var tmp5 = new #{class_name}(#{args parameters}); \t\tvar tmp5 = new #{class_name}(#{args.call parameters});
System.out.println(tmp1); \t\tSystem.out.println(tmp1);
System.out.println(tmp2); \t\tSystem.out.println(tmp2);
System.out.println(tmp3); \t\tSystem.out.println(tmp3);
System.out.println(tmp4); \t\tSystem.out.println(tmp4);
System.out.println(tmp5); \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); \t\tlol.add#{class_name}(tmp1);
lol.add#{class_name}(tmp2); \t\tlol.add#{class_name}(tmp2);
lol.add#{class_name}(tmp3); \t\tlol.add#{class_name}(tmp3);
lol.add#{class_name}(tmp4); \t\tlol.add#{class_name}(tmp4);
lol.add#{class_name}(tmp5); \t\tlol.add#{class_name}(tmp5);
System.out.println(\"\"); \t\tSystem.out.println(\"\");
System.out.println(\"Some checkpoint\"); \t\tSystem.out.println(\"Some checkpoint\");
#{maggie.join "\n\n\tSystem.out.println(\"Some Checkpoint\"); \n\t"} \t\t#{maggie.join "\n\n\t\tSystem.out.println(\"Some Checkpoint\"); \n\t\t"}
} \t}
public static void main(String[] args) { \tpublic static void main(String[] args) {
TestDriver.test(); \t\tTestDriver.test();
} \t}
} }"
"
end end
def construct_container(name, parameters, given_fields, methods, arraylist_name) def construct_container(name, parameters, given_fields, methods, arraylist_name)
fields = "" fields = ""
parameters.each do |x| parameters.each do |x|
fields += "private #{x[:type]} #{x[:name]};\n" fields += "\tprivate #{x[:type]} #{x[:name]};\n"
end end
given_fields.each do |x| given_fields.each do |x|
fields += " private #{x[:type]} #{x[:name]};\n" fields += "\tprivate #{x[:type]} #{x[:name]};\n"
end end
maggie = methods.map do |x| maggie = methods.map do |x|
" "
public #{x[:return_type]} #{x[:name]} (#{x[:params].map {|y| "#{y[:type]} #{y[:name]}"}.join ","}) { \tpublic #{x[:return_type]} #{x[:name]} (#{x[:params].map {|y| "#{y[:type]} #{y[:name]}"}.join ","}) {
#{x[:method_type]} \t\t#{x[:method_type]}
} \t}
" "
end end
@ -151,28 +145,26 @@ def construct_container(name, parameters, given_fields, methods, arraylist_name)
"this.#{x[:name]} = #{x[:name]};" "this.#{x[:name]} = #{x[:name]};"
end end
" "import java.util.*;
import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class #{name} { public class #{name} {
#{fields} #{fields}
public #{name}(#{construtor_params.join ","}) { \tpublic #{name}(#{construtor_params.join ","}) {
#{constructor_handlers.join "\n"} \t\t#{constructor_handlers.join "\n"}
this.#{arraylist_name} = new ArrayList<>(); \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}() { \t\tfor (var i : #{arraylist_name}) {
System.out.println(\"#{name}\"); \t\t\tSystem.out.println(i);
Collections.sort(#{arraylist_name}); \t\t}
\t}
for (var i : #{arraylist_name}) {
System.out.println(i);
}
}
}" }"
end end

View File

@ -3,23 +3,22 @@
def compare_to(sortings) def compare_to(sortings)
first, second, third = *sortings first, second, third = *sortings
def return_stat sorting return_stat = lambda{ |sorting|
if sorting.type == "String" if sorting.type == "String"
{:ret => "this.#{sorting.variable}.compareTo(other.#{sorting.variable})"} {:ret => "this.#{sorting.variable}.compareTo(other.#{sorting.variable})"}
else else
{:ret => sorting.comparator == ">" ? "this.#{sorting.variable} - other.#{sorting.variable}" : "other.#{sorting.variable} - this.#{sorting.variable}"} {: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 sorting.type == "String"
{:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})", {:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})",
:ret => (return_stat sorting)[:ret]} :ret => (return_stat.call sorting)[:ret]}
else else
{:if => "this.#{sorting.variable} != other.#{sorting.variable}", {:if => "this.#{sorting.variable} != other.#{sorting.variable}",
:ret => (return_stat sorting)[:ret]} :ret => (return_stat.call sorting)[:ret]}
end end}
end
pls = sortings.slice(1,sortings.length - 2).map do |ele| pls = sortings.slice(1,sortings.length - 2).map do |ele|