diff --git a/src/main/java/org/opennars/web/multinar/NarNode.java b/src/main/java/org/opennars/web/multinar/NarNode.java index a99a9ab1f3924be09de7da81745fdf18a1c11e56..210c34bc882cc3da35009c12d3af379ae9d066a5 100644 --- a/src/main/java/org/opennars/web/multinar/NarNode.java +++ b/src/main/java/org/opennars/web/multinar/NarNode.java @@ -160,7 +160,7 @@ public class NarNode extends Nar implements EventObserver { } } - public class TargetNar { + public static class TargetNar { /** * The target Nar node, specifying under which conditions the current Nar node redirects tasks to it. @@ -198,7 +198,10 @@ public class NarNode extends Nar implements EventObserver { * @throws UnknownHostException */ public void addRedirectionTo(final String targetIP, final int targetPort, final float taskThreshold, Term mustContainTerm) throws SocketException, UnknownHostException { - targets.add(new TargetNar(targetIP, targetPort, taskThreshold, mustContainTerm)); + addRedirectionTo(new TargetNar(targetIP, targetPort, taskThreshold, mustContainTerm)); + } + public void addRedirectionTo(TargetNar target) throws SocketException, UnknownHostException { + targets.add(target); } /*** @@ -231,27 +234,20 @@ public class NarNode extends Nar implements EventObserver { public static void main(String[] args) throws SocketException, UnknownHostException, IOException, InterruptedException, InstantiationException, InvocationTargetException, ParserConfigurationException, NoSuchMethodException, SAXException, ClassNotFoundException, IllegalAccessException, ParseException { - int nar1port = 64001; - int nar2port = 64002; - String localIP = "127.0.0.1"; + if((args.length-2) % 4 != 0) { //args length check, it has to be 2+4*k, with k in N0 + System.out.println("expected arguments: minCyclePeriodMS listenPort targetIP1 targetPort1 prioThres1 mustContainTerm1 ... targetIPN targetPortN prioThresN mustContainTermN"); + System.exit(0); + } + int nar1port = Integer.parseInt(args[1]); NarNode nar1 = new NarNode(nar1port); - NarNode nar2 = new NarNode(nar2port); - nar1.addRedirectionTo(localIP, nar2port, 0.5f, null); - //nar2.connectTo(localIP, nar1port, 0.5f); - nar2.event(new EventObserver() { - @Override - public void event(Class event, Object[] args) { - if(event == EventReceivedTask.class) { - Task task = (Task) args[0]; - System.out.println("received task event triggered in nar2: " + task); - System.out.println("success"); - } - } - }, true, EventReceivedTask.class); - System.out.println("High priority task occurred in nar1"); - nar1.addInput("<{task1} --> [great]>."); - Thread.sleep(5000); - System.exit(0); + List<TargetNar> redirections = new ArrayList<TargetNar>(); + for(int i=2; i<args.length; i+=4) { + Term T = args[i+3].equals("null") ? null : new Term(args[i+3]); + redirections.add(new TargetNar(args[i], Integer.parseInt(args[i+1]), Float.parseFloat(args[i+2]), T)); + } + for(TargetNar target : redirections) { + nar1.addRedirectionTo(target); + } + nar1.start(Integer.parseInt(args[0])); } - } diff --git a/src/test/java/NarNodeTest.java b/src/test/java/NarNodeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..521619f723f92ca892ff71d3d02a8841872c11a0 --- /dev/null +++ b/src/test/java/NarNodeTest.java @@ -0,0 +1,64 @@ +/** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.text.ParseException; +import javax.xml.parsers.ParserConfigurationException; +import org.junit.Test; +import org.opennars.entity.Task; +import org.opennars.io.events.EventEmitter; +import org.opennars.web.multinar.NarNode; +import org.xml.sax.SAXException; + + +public class NarNodeTest { + static Integer a = 0; + @Test + public void example1() throws UnknownHostException, IOException, SocketException, InstantiationException, InvocationTargetException, + NoSuchMethodException, ParserConfigurationException, IllegalAccessException, SAXException, ClassNotFoundException, ParseException, InterruptedException { + int nar1port = 64001; + int nar2port = 64002; + String localIP = "127.0.0.1"; + NarNode nar1 = new NarNode(nar1port); + NarNode nar2 = new NarNode(nar2port); + nar1.addRedirectionTo(localIP, nar2port, 0.5f, null); + //nar2.connectTo(localIP, nar1port, 0.5f); + nar2.event(new EventEmitter.EventObserver() { + @Override + public void event(Class event, Object[] args) { + if(event == NarNode.EventReceivedTask.class) { + Task task = (Task) args[0]; + System.out.println("received task event triggered in nar2: " + task); + System.out.println("success"); + synchronized(a) { + a++; + } + } + } + }, true, NarNode.EventReceivedTask.class); + System.out.println("High priority task occurred in nar1"); + nar1.addInput("<{task1} --> [great]>."); + while(true) { + synchronized(a) { + if(a != 0) { + break; + } + } + } + assert(true); + } +}