changeset 10:4b608ce0c483

add local repeat test
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Sat, 30 Dec 2017 18:30:04 +0900
parents 50a2df302920
children 4e5f6db22033
files src/main/java/christie/codegear/CodeGear.java src/main/java/christie/codegear/InputDataGear.java src/main/java/christie/datagear/LocalDataGearManager.java src/main/java/christie/test/StartTest.java src/main/java/christie/test/TestCodeGear.java
diffstat 5 files changed, 33 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/christie/codegear/CodeGear.java	Thu Dec 28 19:38:02 2017 +0900
+++ b/src/main/java/christie/codegear/CodeGear.java	Sat Dec 30 18:30:04 2017 +0900
@@ -21,6 +21,8 @@
     public DataGearManager localDGM;
     public CodeGearExecutor cge;
 
+    protected abstract void run(CodeGearManager cgm);
+
     public void setup(CodeGearManager cgm){
         this.cgm = cgm;
         this.cge = new CodeGearExecutor(this, this.cgm);
@@ -28,28 +30,34 @@
         odg.initODG(cgm);
 
         for (Field field : this.getClass().getDeclaredFields()) {//AnnotationからInputDataGearをセット
-            DataGear dg = null;
-            try {
-                dg = (DataGear) field.get(this);
-            } catch (IllegalAccessException e) {
-                e.printStackTrace();
-            }
-
             if (field.isAnnotationPresent(Take.class)) {
                 Take ano = field.getAnnotation(Take.class);
-                commandList.add(new Command(this, dg, "local", ano.value(), CommandType.TAKE));
+                checkAndSetCommand(field, ano.value());
             } else if (field.isAnnotationPresent(Peek.class)) {
                 Peek ano = field.getAnnotation(Peek.class);
-                commandList.add(new Command(this, dg, "local", ano.value(), CommandType.PEEK));
+                checkAndSetCommand(field, ano.value());
             }
         }
 
         idg.finishInput(cgm, commandList);
     }
 
-    protected abstract void run(CodeGearManager cgm);
-
     public DataGearManager dgm(String dest) {
         return cgm.getDGM(dest);
     }
+
+    public void checkAndSetCommand(Field field, String name){
+        if (!field.getName().equals(name)){
+            throw new IllegalArgumentException("key and DataGearName do not match");
+        }
+
+        DataGear dg = null;
+        try {
+            dg = (DataGear) field.get(this);
+        } catch (IllegalAccessException e) {
+            e.printStackTrace();
+        }
+
+        commandList.add(new Command(this, dg, "local", name, CommandType.TAKE));
+    }
 }
--- a/src/main/java/christie/codegear/InputDataGear.java	Thu Dec 28 19:38:02 2017 +0900
+++ b/src/main/java/christie/codegear/InputDataGear.java	Sat Dec 30 18:30:04 2017 +0900
@@ -53,14 +53,14 @@
             if (field.isAnnotationPresent(Take.class)){
                 Take ano = field.getAnnotation(Take.class);
                 try {
-                    field.set(cg, nullCheck(ano.value()));
+                    field.set(cg, typeCheck(ano.value()));
                 } catch (IllegalAccessException e) {
                     e.printStackTrace();
                 }
             } else if (field.isAnnotationPresent(Peek.class)){
                 Peek ano = field.getAnnotation(Peek.class);
                 try {
-                    field.set(cg, nullCheck(ano.value()));
+                    field.set(cg, typeCheck(ano.value()));
                 } catch (IllegalAccessException e) {
                     e.printStackTrace();
                 }
@@ -68,7 +68,7 @@
         }
     }
 
-    public Object nullCheck(String key){
+    public Object typeCheck(String key){
         if (inputValue.get(key).getData() != null){
             return inputValue.get(key);
         } else {
--- a/src/main/java/christie/datagear/LocalDataGearManager.java	Thu Dec 28 19:38:02 2017 +0900
+++ b/src/main/java/christie/datagear/LocalDataGearManager.java	Sat Dec 30 18:30:04 2017 +0900
@@ -2,7 +2,6 @@
 
 import christie.codegear.Command;
 
-import java.util.Queue;
 import java.util.concurrent.LinkedBlockingQueue;
 
 /**
@@ -12,6 +11,7 @@
 
     public void take(Command cm) {
         if (dataGears.containsKey(cm.key)) {
+            System.out.println(dataGears);
             runCommand(cm);
         } else {
             addWaitList(cm);
@@ -40,6 +40,9 @@
             case TAKE:
                     cm.cs.idg.setInputs(cm.key, cm.dg);
                     dataGears.get(cm.key).poll();
+                    if (dataGears.get(cm.key).isEmpty()){
+                        dataGears.remove(cm.key);
+                    }
                     break;
             case PEEK:
                     cm.cs.idg.setInputs(cm.key, cm.dg);
--- a/src/main/java/christie/test/StartTest.java	Thu Dec 28 19:38:02 2017 +0900
+++ b/src/main/java/christie/test/StartTest.java	Sat Dec 30 18:30:04 2017 +0900
@@ -19,13 +19,7 @@
         TestCodeGear cg = new TestCodeGear();
         cgm.setup(cg);
 
-        localDGM.put("hoge", "hogehogehoge");
+        localDGM.put("hoge", 1);
 
-        int array[][] = {{1},{2},{3},{4}};
-        localDGM.put("huga", array);
-
-        HashMap<String, Integer> piyo= new HashMap<>();
-        piyo.put("piyoyo", 100);
-        localDGM.put("piyo", piyo);
     }
 }
--- a/src/main/java/christie/test/TestCodeGear.java	Thu Dec 28 19:38:02 2017 +0900
+++ b/src/main/java/christie/test/TestCodeGear.java	Sat Dec 30 18:30:04 2017 +0900
@@ -15,23 +15,16 @@
 public class TestCodeGear extends CodeGear {
 
     @Take("hoge")
-    public DataGear hoge = new DataGear<String>();
-
-    @Take("huga")
-    public DataGear huga = new DataGear<int[][]>();
-
-    @Peek("piyo")
-    public DataGear piyo = new DataGear<HashMap<String, Integer>>();
+    public DataGear<Integer> hoge = new DataGear<>();
 
     public void run(CodeGearManager cgm){
-        System.out.println("in run!");
+        System.out.println(hoge.getData());
 
-        System.out.println(hoge.getData());
-        System.out.println(huga.getData());
-        System.out.println(piyo.getData());
+        if (hoge.getData()!= 10){
+            cgm.setup(new TestCodeGear());
+        }
 
-        localDGM.put("hoge", "hoge2");//Queueにしないといけない
-        //dgm("remote").put("hoge", "hoge3");
+        localDGM.put("hoge", hoge.getData() + 1);
     }
 
 }