diff --git a/src/main/java/org/opennars/web/multinar/NarNode.java b/src/main/java/org/opennars/web/multinar/NarNode.java index a99a9ab1f3924be09de7da81745fdf18a1c11e56..08baafc61d46490e66aba00693ac27d1dc173227 100644 --- a/src/main/java/org/opennars/web/multinar/NarNode.java +++ b/src/main/java/org/opennars/web/multinar/NarNode.java @@ -44,7 +44,6 @@ public class NarNode extends Nar implements EventObserver { /* An extra event for received tasks*/ public class EventReceivedTask {} - public class EventReceivedNarsese {} /* The socket the Nar listens from */ private DatagramSocket receiveSocket; @@ -78,8 +77,7 @@ public class NarNode extends Nar implements EventObserver { THIS.memory.event.emit(EventReceivedTask.class, new Object[]{ret}); THIS.addInput((Task) ret, THIS); } else - if(ret instanceof String) { - THIS.memory.event.emit(EventReceivedNarsese.class, new Object[]{ret}); + if(ret instanceof String) { //emits IN.class anyway THIS.addInput((String) ret); } } @@ -145,7 +143,7 @@ public class NarNode extends Nar implements EventObserver { * @param t * @throws IOException */ - private void sendNarsese(String input, TargetNar target) throws IOException { + public static void sendNarsese(String input, TargetNar target) throws IOException { ByteArrayOutputStream bStream = new ByteArrayOutputStream(); ObjectOutput oo = new ObjectOutputStream(bStream); oo.writeObject(input); @@ -159,8 +157,11 @@ public class NarNode extends Nar implements EventObserver { //System.out.println("narsese sent:" + input); } } + public static void sendNarsese(String input, final String targetIP, final int targetPort, final float taskThreshold, Term mustContainTerm) throws IOException { + sendNarsese(input, new TargetNar(targetIP, targetPort, taskThreshold, mustContainTerm)); + } - 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 +199,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 +235,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..ac3702a77dadc0242a3d43014ce5d452b688eb32 --- /dev/null +++ b/src/test/java/NarNodeTest.java @@ -0,0 +1,67 @@ +/** + * 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.io.events.OutputHandler.IN; +import org.opennars.web.multinar.NarNode; +import org.opennars.web.multinar.NarNode.TargetNar; +import org.xml.sax.SAXException; + + +public class NarNodeTest { + static Integer a = 0; + @Test + public void testNarToNar() 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); + TargetNar nar2_connection = new TargetNar(localIP, nar2port, 0.5f, null); + nar1.addRedirectionTo(nar2_connection); + nar2.event(new EventEmitter.EventObserver() { + @Override + public void event(Class event, Object[] args) { + if(event == NarNode.EventReceivedTask.class || event == IN.class) { + Task task = (Task) args[0]; + System.out.println("received task event triggered in nar2: " + task); + synchronized(a) { + a++; + } + } + } + }, true, NarNode.EventReceivedTask.class, IN.class); + System.out.println("High priority task occurred in nar1"); + NarNode.sendNarsese("<{task1} --> [great]>.", nar2_connection); + nar1.addInput("<{task1} --> [great]>."); + while(true) { + synchronized(a) { + if(a == 2) { + System.out.println("success"); + break; + } + } + } + assert(true); + } +}