|
@@ -4,6 +4,9 @@ import co.paralleluniverse.fibers.Fiber;
|
|
|
import co.paralleluniverse.fibers.SuspendExecution;
|
|
|
import in.ocsf.dialij.app.obj.Processor;
|
|
|
import in.ocsf.dialij.app.srv.CompilerService;
|
|
|
+import in.ocsf.dialij.app.srv.PopulationService;
|
|
|
+import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
import org.apache.log4j.Logger;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
@@ -11,6 +14,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@RestController
|
|
|
public class DevController {
|
|
@@ -18,24 +24,49 @@ public class DevController {
|
|
|
@Inject
|
|
|
private CompilerService compiler;
|
|
|
|
|
|
+ @Inject
|
|
|
+ private PopulationService population;
|
|
|
+
|
|
|
private Logger log = Logger.getLogger(getClass());
|
|
|
|
|
|
- @RequestMapping("/dev/bf")
|
|
|
- public ResponseEntity<String> bf() {
|
|
|
- Processor pr = compiler.compile("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++\n" +
|
|
|
- " .>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.\n" +
|
|
|
- " ------.--------.>+.>.");
|
|
|
-
|
|
|
- for (int i = 0; i < 10000; i++) {
|
|
|
- new Fiber<String>() {
|
|
|
- @Override
|
|
|
- protected String run() throws SuspendExecution, InterruptedException {
|
|
|
- String out = pr.process();
|
|
|
- log.info(out);
|
|
|
- return out;
|
|
|
- }
|
|
|
- }.start();
|
|
|
- }
|
|
|
- return new ResponseEntity<>(HttpStatus.OK);
|
|
|
+ @RequestMapping("/dev/population/initial")
|
|
|
+ public ResponseEntity<String> initPopulation() throws Exception{
|
|
|
+ List<Fiber> alive = new ArrayList<>();
|
|
|
+
|
|
|
+ Map<UUID, String> code = population.getInitialPopulation();
|
|
|
+ code.forEach((k, v) -> {
|
|
|
+ alive.add(compiler.compile(v).setName(k.toString()).start());
|
|
|
+ });
|
|
|
+
|
|
|
+ List<Pair<UUID, Object>> procs = new Fiber<List<Pair<UUID, Object>>>(){
|
|
|
+ @Override
|
|
|
+ protected List<Pair<UUID, Object>> run() throws SuspendExecution, InterruptedException {
|
|
|
+ return (List) alive.parallelStream().map(c -> {
|
|
|
+ try {
|
|
|
+ return ImmutablePair.of(UUID.fromString(c.getName()), c.get());
|
|
|
+ } catch (Exception e){
|
|
|
+ return ImmutablePair.of(UUID.fromString(c.getName()), e);
|
|
|
+ }
|
|
|
+ }).filter(o -> !(o.getValue() instanceof Throwable)).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ }.start().get();
|
|
|
+
|
|
|
+ procs.stream().forEach(p -> {
|
|
|
+ Processor processor = compiler.proxify(p.getValue());
|
|
|
+ try {
|
|
|
+ new Fiber<Void>() {
|
|
|
+ @Override
|
|
|
+ protected Void run() throws SuspendExecution, InterruptedException {
|
|
|
+ log.info(code.get(p.getKey()));
|
|
|
+ log.info(processor.process());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }.start().get(1, TimeUnit.SECONDS);
|
|
|
+ } catch (Exception e){
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return ResponseEntity.ok("ok");
|
|
|
}
|
|
|
}
|