From eb05c410d4496227946662a129b87e852a48d1e0 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 19:20:45 +0100 Subject: [PATCH 01/13] Refactor: drawTick() for Entity --- .../applications/crossing/Entity.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index fa3dba9..6083718 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -75,19 +75,8 @@ public boolean isAnomaly() { return normalness < 0.3 && hasMoved(); } - public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { - applet.pushMatrix(); - //float posXDiscrete = (((int) this.posX)/Util.discretization * Util.discretization); - //float posYDiscrete = (((int) this.posY)/Util.discretization * Util.discretization); - applet.translate((float) posX, (float) posY); - applet.rotate((float) angle); - if(truth == null) { - applet.rect(0, 0, Util.discretization*scale, Util.discretization/2*scale); - } - applet.ellipse(2.5f, 2.5f, Util.discretization*scale, Util.discretization*scale); - applet.popMatrix(); - applet.fill(0); - applet.text(String.valueOf(id), (float)posX, (float)posY); + // mess of draw and tick for refactoring + private void drawTick(List streets, List trafficLights, List entities, TruthValue truth, long time) { if(truth != null) { return; } @@ -145,4 +134,21 @@ public void draw(PApplet applet, List streets, List traffi //this.id = entityID++; } } + + public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { + applet.pushMatrix(); + //float posXDiscrete = (((int) this.posX)/Util.discretization * Util.discretization); + //float posYDiscrete = (((int) this.posY)/Util.discretization * Util.discretization); + applet.translate((float) posX, (float) posY); + applet.rotate((float) angle); + if(truth == null) { + applet.rect(0, 0, Util.discretization*scale, Util.discretization/2*scale); + } + applet.ellipse(2.5f, 2.5f, Util.discretization*scale, Util.discretization*scale); + applet.popMatrix(); + applet.fill(0); + applet.text(String.valueOf(id), (float)posX, (float)posY); + + drawTick(streets, trafficLights, entities, truth, time); + } } From d9307a8d0dc5fe7dc32a9831bace13ac75b2d47f Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 19:37:41 +0100 Subject: [PATCH 02/13] Refactor: seperation of tick and drawing! --- .../opennars/applications/crossing/Car.java | 6 ++++++ .../applications/crossing/Crossing.java | 2 +- .../applications/crossing/Entity.java | 6 ++---- .../applications/crossing/Pedestrian.java | 20 +++++++++++++------ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Car.java b/src/main/java/com/opennars/applications/crossing/Car.java index b825d33..30aa5de 100644 --- a/src/main/java/com/opennars/applications/crossing/Car.java +++ b/src/main/java/com/opennars/applications/crossing/Car.java @@ -34,6 +34,12 @@ public Car(int id, double posX, double posY, double velocity, double angle) { maxSpeed = 2; } + public void tick(List streets, List trafficLights, List entities, TruthValue truth, long time) { + super.tick(streets, trafficLights, entities, truth, time); + + super.drawTickInternal(streets, trafficLights, entities, truth, time); + } + public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { float mul = Util.truthToValue(truth) * Util.timeToValue(time); applet.fill(255, 0, 255, mul*255.0f); diff --git a/src/main/java/com/opennars/applications/crossing/Crossing.java b/src/main/java/com/opennars/applications/crossing/Crossing.java index 5e04c6f..930da58 100644 --- a/src/main/java/com/opennars/applications/crossing/Crossing.java +++ b/src/main/java/com/opennars/applications/crossing/Crossing.java @@ -126,7 +126,7 @@ public void draw() { // tick for (Entity ie : entities) { - ie.tick(); + ie.tick(streets, trafficLights, entities, null, 0); } diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index 6083718..bcf8bf5 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -57,7 +57,7 @@ public Entity(int id, double posX, double posY, double velocity, double angle) { this.angle = angle; } - public void tick() { + public void tick(List streets, List trafficLights, List entities, TruthValue truth, long time) { // decay normalness // 0.96 is to slow @@ -76,7 +76,7 @@ public boolean isAnomaly() { } // mess of draw and tick for refactoring - private void drawTick(List streets, List trafficLights, List entities, TruthValue truth, long time) { + protected void drawTickInternal(List streets, List trafficLights, List entities, TruthValue truth, long time) { if(truth != null) { return; } @@ -148,7 +148,5 @@ public void draw(PApplet applet, List streets, List traffi applet.popMatrix(); applet.fill(0); applet.text(String.valueOf(id), (float)posX, (float)posY); - - drawTick(streets, trafficLights, entities, truth, time); } } diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java index 96c74db..a445a6e 100644 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ b/src/main/java/com/opennars/applications/crossing/Pedestrian.java @@ -40,12 +40,11 @@ public Pedestrian(int id, double posX, double posY, double velocity, double angl maxSpeed = 1; } - public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { - prevX = posX; - prevY = posY; - float mul = Util.truthToValue(truth) * Util.timeToValue(time); - applet.fill(0, 255, 255, mul*255.0f); - super.draw(applet, streets, trafficLights, entities, truth, time); + public void tick(List streets, List trafficLights, List entities, TruthValue truth, long time) { + super.tick(streets, trafficLights, entities, truth, time); + + super.drawTickInternal(streets, trafficLights, entities, truth, time); + angle+=(Util.rnd.nextFloat()*0.1-0.05); //ok pedestrian, don't go on grass boolean forPedestrians = false; @@ -61,4 +60,13 @@ public void draw(PApplet applet, List streets, List traffi this.posY = prevY; } } + + public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { + prevX = posX; + prevY = posY; + float mul = Util.truthToValue(truth) * Util.timeToValue(time); + applet.fill(0, 255, 255, mul*255.0f); + super.draw(applet, streets, trafficLights, entities, truth, time); + + } } From 8e9705698e974d5340f6d2c2668b18e1ab16f3f3 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 19:41:48 +0100 Subject: [PATCH 03/13] Add: BehaviourComponent --- .../crossing/BehaviourComponent.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/com/opennars/applications/crossing/BehaviourComponent.java diff --git a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java new file mode 100644 index 0000000..60a1f64 --- /dev/null +++ b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java @@ -0,0 +1,31 @@ +/* + * The MIT License + * + * Copyright 2018 The OpenNARS authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.opennars.applications.crossing; + +/** + * Component for the behaviour of an entity + */ +public class BehaviourComponent { + +} From 27898b1a0cf475a8b5a982e15c2f5c5503755310 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 19:53:39 +0100 Subject: [PATCH 04/13] Refactor: moved most behaviour into BehaviourComponent --- .../crossing/BehaviourComponent.java | 104 ++++++++++++++++++ .../applications/crossing/Entity.java | 60 ---------- .../applications/crossing/Pedestrian.java | 21 ---- 3 files changed, 104 insertions(+), 81 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java index 60a1f64..5346da4 100644 --- a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java +++ b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java @@ -23,9 +23,113 @@ */ package com.opennars.applications.crossing; +import org.opennars.entity.TruthValue; + +import java.util.List; + /** * Component for the behaviour of an entity */ public class BehaviourComponent { + private final EnumType type; + + + public BehaviourComponent(final EnumType type) { + this.type = type; + } + + public void tick(Entity entity, List streets, List trafficLights, List entities, TruthValue truth, long time) { + if (type == EnumType.CAR) { + carTick(entity, streets, trafficLights, entities, truth, time); + } + else if (type == EnumType.PEDESTRIAN) { + carTick(entity, streets, trafficLights, entities, truth, time); + + + + entity.angle+=(Util.rnd.nextFloat()*0.1-0.05); + //ok pedestrian, don't go on grass + boolean forPedestrians = false; + for(Street street : streets) { + if(!street.forCarsOnly && entity.posX > street.startX && entity.posX < street.endX && entity.posY > street.startY && entity.posY < street.endY) { + forPedestrians = true; + break; + } + } + + /* TODO + if(!forPedestrians) { + entity.angle = initialAngle; + entity.posX = prevX; + entity.posY = prevY; + } + */ + } + } + + + static protected void carTick(Entity entity, List streets, List trafficLights, List entities, TruthValue truth, long time) { + if(truth != null) { + return; + } + + boolean accelerate = true; + for (TrafficLight l : trafficLights) { + if (Util.distance(entity.posX, entity.posY, l.posX, l.posY) < l.radius) { + if (l.colour == l.RED) { + if (Util.rnd.nextFloat() > 0.3 && ((entity instanceof Car && !entity.carIgnoreTrafficLight) || (entity instanceof Pedestrian && !entity.pedestrianIgnoreTrafficLight))) { + entity.velocity *= 0.5; + accelerate = false; + } + } + } + } + for (Entity e : entities) { + boolean collidable = !(entity instanceof Pedestrian && e instanceof Pedestrian); + if (e != entity && collidable) { + double nearEnough = 10; + for (double k = 0; k < nearEnough; k += 0.1) { + double pXNew = entity.posX + k * Math.cos(entity.angle); + double pYNew = entity.posY + k * Math.sin(entity.angle); + if (Util.distance(pXNew, pYNew, e.posX, e.posY) < nearEnough) { + entity.velocity *= 0.8; + accelerate = false; + } + } + } + } + + if (accelerate && entity.velocity < entity.maxSpeed) { + entity.velocity += 0.02; + } + + double aX = Math.cos(entity.angle); + double aY = Math.sin(entity.angle); + entity.posX += aX * entity.velocity; + entity.posY += aY * entity.velocity; + + double epsilon = 1; + if (entity.posY < 0) { + entity.posY = 1000 - epsilon; + //this.id = entityID++; + } + if (entity.posY > 1000) { + entity.posY = epsilon; + //this.id = entityID++; + } + if (entity.posX < 0) { + entity.posX = 1000 - epsilon; + //this.id = entityID++; + } + if (entity.posX > 1000) { + entity.posX = epsilon; + //this.id = entityID++; + } + } + + public enum EnumType { + CAR, + PEDESTRIAN, + } } diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index bcf8bf5..b3e377a 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -75,66 +75,6 @@ public boolean isAnomaly() { return normalness < 0.3 && hasMoved(); } - // mess of draw and tick for refactoring - protected void drawTickInternal(List streets, List trafficLights, List entities, TruthValue truth, long time) { - if(truth != null) { - return; - } - - boolean accelerate = true; - for (TrafficLight l : trafficLights) { - if (Util.distance(posX, posY, l.posX, l.posY) < l.radius) { - if (l.colour == l.RED) { - if (Util.rnd.nextFloat() > 0.3 && ((this instanceof Car && !carIgnoreTrafficLight) || (this instanceof Pedestrian && !pedestrianIgnoreTrafficLight))) { - velocity *= 0.5; - accelerate = false; - } - } - } - } - for (Entity e : entities) { - boolean collidable = !(this instanceof Pedestrian && e instanceof Pedestrian); - if (e != this && collidable) { - double nearEnough = 10; - for (double k = 0; k < nearEnough; k += 0.1) { - double pXNew = posX + k * Math.cos(angle); - double pYNew = posY + k * Math.sin(angle); - if (Util.distance(pXNew, pYNew, e.posX, e.posY) < nearEnough) { - velocity *= 0.8; - accelerate = false; - } - } - } - } - - if (accelerate && velocity < maxSpeed) { - velocity += 0.02; - } - - double aX = Math.cos(angle); - double aY = Math.sin(angle); - posX += aX * velocity; - posY += aY * velocity; - - double epsilon = 1; - if (posY < 0) { - posY = 1000 - epsilon; - //this.id = entityID++; - } - if (posY > 1000) { - posY = epsilon; - //this.id = entityID++; - } - if (posX < 0) { - posX = 1000 - epsilon; - //this.id = entityID++; - } - if (posX > 1000) { - posX = epsilon; - //this.id = entityID++; - } - } - public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { applet.pushMatrix(); //float posXDiscrete = (((int) this.posX)/Util.discretization * Util.discretization); diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java index a445a6e..1f699f4 100644 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ b/src/main/java/com/opennars/applications/crossing/Pedestrian.java @@ -40,27 +40,6 @@ public Pedestrian(int id, double posX, double posY, double velocity, double angl maxSpeed = 1; } - public void tick(List streets, List trafficLights, List entities, TruthValue truth, long time) { - super.tick(streets, trafficLights, entities, truth, time); - - super.drawTickInternal(streets, trafficLights, entities, truth, time); - - angle+=(Util.rnd.nextFloat()*0.1-0.05); - //ok pedestrian, don't go on grass - boolean forPedestrians = false; - for(Street street : streets) { - if(!street.forCarsOnly && this.posX > street.startX && this.posX < street.endX && this.posY > street.startY && this.posY < street.endY) { - forPedestrians = true; - break; - } - } - if(!forPedestrians) { - this.angle = this.initialAngle; - this.posX = prevX; - this.posY = prevY; - } - } - public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { prevX = posX; prevY = posY; From 5abfe14f4b17b10daa367d05ac8ac12fb25091ba Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:01:45 +0100 Subject: [PATCH 05/13] Refactor: use BehaviourComponent - Pedestriants are buggy --- .../java/com/opennars/applications/crossing/Car.java | 8 +------- .../java/com/opennars/applications/crossing/Entity.java | 9 ++++++++- .../com/opennars/applications/crossing/Pedestrian.java | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Car.java b/src/main/java/com/opennars/applications/crossing/Car.java index 30aa5de..11ee3b5 100644 --- a/src/main/java/com/opennars/applications/crossing/Car.java +++ b/src/main/java/com/opennars/applications/crossing/Car.java @@ -30,16 +30,10 @@ public class Car extends Entity { public Car(int id, double posX, double posY, double velocity, double angle) { - super(id, posX, posY, velocity, angle); + super(id, posX, posY, velocity, angle, new BehaviourComponent(BehaviourComponent.EnumType.CAR)); maxSpeed = 2; } - public void tick(List streets, List trafficLights, List entities, TruthValue truth, long time) { - super.tick(streets, trafficLights, entities, truth, time); - - super.drawTickInternal(streets, trafficLights, entities, truth, time); - } - public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { float mul = Util.truthToValue(truth) * Util.timeToValue(time); applet.fill(255, 0, 255, mul*255.0f); diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index b3e377a..0e812ec 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -30,6 +30,9 @@ public class Entity { public static int entityID = 0; + + public BehaviourComponent behaviour; + public double posX, posY; public double velocity; public double angle; @@ -49,12 +52,13 @@ public class Entity { public Entity() { } - public Entity(int id, double posX, double posY, double velocity, double angle) { + public Entity(int id, double posX, double posY, double velocity, double angle, final BehaviourComponent behaviour) { this.id = id; this.posX = posX; this.posY = posY; this.velocity = velocity; this.angle = angle; + this.behaviour = behaviour; } public void tick(List streets, List trafficLights, List entities, TruthValue truth, long time) { @@ -63,6 +67,9 @@ public void tick(List streets, List trafficLights, List Date: Wed, 21 Nov 2018 20:06:07 +0100 Subject: [PATCH 06/13] Refactor: moved behaviour of Pedestriant --- .../applications/crossing/BehaviourComponent.java | 11 +++++++---- .../opennars/applications/crossing/Pedestrian.java | 3 +-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java index 5346da4..8510c5e 100644 --- a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java +++ b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java @@ -33,6 +33,10 @@ public class BehaviourComponent { private final EnumType type; + // initial angle (for the pedestriant) + public double initialAngle; + + public BehaviourComponent(final EnumType type) { this.type = type; @@ -57,13 +61,12 @@ else if (type == EnumType.PEDESTRIAN) { } } - /* TODO + if(!forPedestrians) { entity.angle = initialAngle; - entity.posX = prevX; - entity.posY = prevY; + entity.posX = entity.prevX; + entity.posY = entity.prevY; } - */ } } diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java index 8013f88..8c3ccd3 100644 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ b/src/main/java/com/opennars/applications/crossing/Pedestrian.java @@ -29,13 +29,12 @@ public class Pedestrian extends Entity { - double initialAngle; double prevX = 0; double prevY = 0; public final static float pedestrianScale = 0.75f; public Pedestrian(int id, double posX, double posY, double velocity, double angle) { super(id, posX, posY, velocity, angle, new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN)); - initialAngle = angle; + behaviour.initialAngle = angle; scale = pedestrianScale; maxSpeed = 1; } From 3289ba2159389a81a240facac7940b78ba7690a9 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:10:41 +0100 Subject: [PATCH 07/13] Fix: behaviour of Pedestriant --- .../java/com/opennars/applications/crossing/Entity.java | 8 ++++++++ .../com/opennars/applications/crossing/Pedestrian.java | 6 ------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index 0e812ec..92fb694 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -33,6 +33,10 @@ public class Entity { public BehaviourComponent behaviour; + + public double prevX = 0; + public double prevY = 0; + public double posX, posY; public double velocity; public double angle; @@ -68,6 +72,10 @@ public void tick(List streets, List trafficLights, List streets, List trafficLights, List entities, TruthValue truth, long time) { - prevX = posX; - prevY = posY; float mul = Util.truthToValue(truth) * Util.timeToValue(time); applet.fill(0, 255, 255, mul*255.0f); super.draw(applet, streets, trafficLights, entities, truth, time); - } } From e89fb664bdae9f3e458eafbed0d5513022da9b1e Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:13:24 +0100 Subject: [PATCH 08/13] Refactor: moved maxSpeed into Behaviour --- .../opennars/applications/crossing/BehaviourComponent.java | 4 +++- src/main/java/com/opennars/applications/crossing/Car.java | 2 +- src/main/java/com/opennars/applications/crossing/Entity.java | 1 - .../java/com/opennars/applications/crossing/Pedestrian.java | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java index 8510c5e..fe43d07 100644 --- a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java +++ b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java @@ -36,6 +36,8 @@ public class BehaviourComponent { // initial angle (for the pedestriant) public double initialAngle; + public float maxSpeed = 2.0f; + public BehaviourComponent(final EnumType type) { @@ -102,7 +104,7 @@ static protected void carTick(Entity entity, List streets, List streets, List trafficLights, List entities, TruthValue truth, long time) { diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index 92fb694..52cf37b 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -42,7 +42,6 @@ public class Entity { public double angle; public int id; public float scale = 1.0f; - public float maxSpeed = 2.0f; public static boolean pedestrianIgnoreTrafficLight = false; public static boolean carIgnoreTrafficLight = false; diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java index 0fe0d9d..8e1f79a 100644 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ b/src/main/java/com/opennars/applications/crossing/Pedestrian.java @@ -32,8 +32,8 @@ public class Pedestrian extends Entity { public Pedestrian(int id, double posX, double posY, double velocity, double angle) { super(id, posX, posY, velocity, angle, new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN)); behaviour.initialAngle = angle; + behaviour.maxSpeed = 1; scale = pedestrianScale; - maxSpeed = 1; } public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { From 1281e9cecbb341f520a98b7743d295a194532bc7 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:16:37 +0100 Subject: [PATCH 09/13] Add: Entity: added tag --- src/main/java/com/opennars/applications/crossing/Car.java | 2 +- src/main/java/com/opennars/applications/crossing/Entity.java | 5 ++++- .../java/com/opennars/applications/crossing/Pedestrian.java | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Car.java b/src/main/java/com/opennars/applications/crossing/Car.java index b43572c..6f6ea19 100644 --- a/src/main/java/com/opennars/applications/crossing/Car.java +++ b/src/main/java/com/opennars/applications/crossing/Car.java @@ -30,7 +30,7 @@ public class Car extends Entity { public Car(int id, double posX, double posY, double velocity, double angle) { - super(id, posX, posY, velocity, angle, new BehaviourComponent(BehaviourComponent.EnumType.CAR)); + super(id, posX, posY, velocity, angle, "car", new BehaviourComponent(BehaviourComponent.EnumType.CAR)); behaviour.maxSpeed = 2; } diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index 52cf37b..56f2082 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -52,15 +52,18 @@ public class Entity { public double lastPosX = 0; public double lastPosY = 0; + public String tag; // tag used to identify the type of the entity + public Entity() { } - public Entity(int id, double posX, double posY, double velocity, double angle, final BehaviourComponent behaviour) { + public Entity(int id, double posX, double posY, double velocity, double angle, final String tag, final BehaviourComponent behaviour) { this.id = id; this.posX = posX; this.posY = posY; this.velocity = velocity; this.angle = angle; + this.tag = tag; this.behaviour = behaviour; } diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java index 8e1f79a..c8dd152 100644 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ b/src/main/java/com/opennars/applications/crossing/Pedestrian.java @@ -30,7 +30,7 @@ public class Pedestrian extends Entity { public final static float pedestrianScale = 0.75f; public Pedestrian(int id, double posX, double posY, double velocity, double angle) { - super(id, posX, posY, velocity, angle, new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN)); + super(id, posX, posY, velocity, angle, "pedestrian", new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN)); behaviour.initialAngle = angle; behaviour.maxSpeed = 1; scale = pedestrianScale; From e1ed76e1994f911e0f703ae10456b582d4926e58 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:20:28 +0100 Subject: [PATCH 10/13] Refactor: just one drawing functionality --- .../com/opennars/applications/crossing/Car.java | 3 ++- .../opennars/applications/crossing/Entity.java | 17 +++++++++++++++++ .../applications/crossing/Pedestrian.java | 4 ++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Car.java b/src/main/java/com/opennars/applications/crossing/Car.java index 6f6ea19..010ce49 100644 --- a/src/main/java/com/opennars/applications/crossing/Car.java +++ b/src/main/java/com/opennars/applications/crossing/Car.java @@ -34,6 +34,7 @@ public Car(int id, double posX, double posY, double velocity, double angle) { behaviour.maxSpeed = 2; } + /* public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { float mul = Util.truthToValue(truth) * Util.timeToValue(time); applet.fill(255, 0, 255, mul*255.0f); @@ -48,5 +49,5 @@ public void draw(PApplet applet, List streets, List traffi super.draw(applet, streets, trafficLights, entities, truth, time); applet.stroke(127); - } + }*/ } diff --git a/src/main/java/com/opennars/applications/crossing/Entity.java b/src/main/java/com/opennars/applications/crossing/Entity.java index 56f2082..8486f70 100644 --- a/src/main/java/com/opennars/applications/crossing/Entity.java +++ b/src/main/java/com/opennars/applications/crossing/Entity.java @@ -92,7 +92,24 @@ public boolean isAnomaly() { return normalness < 0.3 && hasMoved(); } + public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { + float mul = Util.truthToValue(truth) * Util.timeToValue(time); + applet.fill(255, 0, 255, mul*255.0f); + + if (!isPredicted && isAnomaly()) { + applet.stroke(255,0,0); + } + else { + applet.stroke(127); + } + + drawInternal(applet, streets, trafficLights, entities, truth, time); + + applet.stroke(127); + } + + public void drawInternal(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { applet.pushMatrix(); //float posXDiscrete = (((int) this.posX)/Util.discretization * Util.discretization); //float posYDiscrete = (((int) this.posY)/Util.discretization * Util.discretization); diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java index c8dd152..bd8ef22 100644 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ b/src/main/java/com/opennars/applications/crossing/Pedestrian.java @@ -35,10 +35,10 @@ public Pedestrian(int id, double posX, double posY, double velocity, double angl behaviour.maxSpeed = 1; scale = pedestrianScale; } - + /* public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { float mul = Util.truthToValue(truth) * Util.timeToValue(time); applet.fill(0, 255, 255, mul*255.0f); super.draw(applet, streets, trafficLights, entities, truth, time); - } + }*/ } From bf548636bdfd09cf07d4c197a1eec471ee506c7b Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:22:53 +0100 Subject: [PATCH 11/13] Refactor: BehaviourComponent: use tag --- .../com/opennars/applications/crossing/BehaviourComponent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java index fe43d07..0b67eb4 100644 --- a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java +++ b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java @@ -82,7 +82,7 @@ static protected void carTick(Entity entity, List streets, List 0.3 && ((entity instanceof Car && !entity.carIgnoreTrafficLight) || (entity instanceof Pedestrian && !entity.pedestrianIgnoreTrafficLight))) { + if (Util.rnd.nextFloat() > 0.3 && ((entity.tag.equals("car") && !entity.carIgnoreTrafficLight) || (entity.tag.equals("pedestrian") && !entity.pedestrianIgnoreTrafficLight))) { entity.velocity *= 0.5; accelerate = false; } From e3b1d80f20a2e65e7fb1d28f982a166d3c25bbd3 Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:30:57 +0100 Subject: [PATCH 12/13] Refactor: compose entities --- .../applications/crossing/Crossing.java | 29 ++++++++++++++++--- .../applications/crossing/NarListener.java | 4 +-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/opennars/applications/crossing/Crossing.java b/src/main/java/com/opennars/applications/crossing/Crossing.java index 930da58..0ce4ab4 100644 --- a/src/main/java/com/opennars/applications/crossing/Crossing.java +++ b/src/main/java/com/opennars/applications/crossing/Crossing.java @@ -65,13 +65,13 @@ public void setup() { trafficLights.add(new TrafficLight(trafficLightID++, trafficLightRadius/2, 500, 500 - trafficLightRadius, 1)); int cars = 4; //cars and pedestrians for (float i = 0; i < cars/2; i += 1.05) { - entities.add(new Car(entityID++, 500 + streetWidth - Util.discretization+1, 900 - i * 100, 0.3, -PI / 2)); - entities.add(new Car(entityID++, 500 + Util.discretization, 900 - i * 100, 0.3, PI / 2)); + entities.add(makeEntity("car", entityID++, 500 + streetWidth - Util.discretization+1, 900 - i * 100, 0.3, -PI / 2)); + entities.add(makeEntity("car", entityID++, 500 + Util.discretization, 900 - i * 100, 0.3, PI / 2)); } int pedestrians = 4;//4; for (float i = 0; i < pedestrians/2; i += 1.05) { - entities.add(new Pedestrian(entityID++, 900 - i * 100, 500 + streetWidth - Util.discretization, 0.3, 0)); - entities.add(new Pedestrian(entityID++, 900 - i * 100, 500 + Util.discretization, 0.3, -PI)); + entities.add(makeEntity("pedestrian", entityID++, 900 - i * 100, 500 + streetWidth - Util.discretization, 0.3, 0)); + entities.add(makeEntity("pedestrian", entityID++, 900 - i * 100, 500 + Util.discretization, 0.3, -PI)); } /*for (TrafficLight l : trafficLights) { //it can't move anyway, so why would the coordinates matter to NARS? String pos = Util.positionToTerm(l.posX, l.posY); @@ -190,6 +190,27 @@ public void mouseDragged() { viewport.mouseDragged(); } + public static Entity makeEntity(final String tag, int id, double posX, double posY, double velocity, double angle) { + if (tag.equals("car")) { + Entity entity = new Entity(id, posX, posY, velocity, angle, "car", new BehaviourComponent(BehaviourComponent.EnumType.CAR)); + entity.behaviour.maxSpeed = 2; + return entity; + } + else if (tag.equals("pedestrian")) { + final float pedestrianScale = 0.75f; + + Entity entity = new Entity(id, posX, posY, velocity, angle, "pedestrian", new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN)); + entity.behaviour.initialAngle = angle; + entity.behaviour.maxSpeed = 1; + entity.scale = pedestrianScale; + return entity; + } + else { + return null; + } + } + + public static void main(String[] args) { /* Set the Nimbus look and feel */ // diff --git a/src/main/java/com/opennars/applications/crossing/NarListener.java b/src/main/java/com/opennars/applications/crossing/NarListener.java index d0c211e..327cc7a 100644 --- a/src/main/java/com/opennars/applications/crossing/NarListener.java +++ b/src/main/java/com/opennars/applications/crossing/NarListener.java @@ -115,14 +115,14 @@ public Prediction predictionFromTask(Task t) { Entity pred; if(type.toString().startsWith(car.toString())) { String id = type.toString().substring(car.toString().length(), type.toString().length()); - pred = new Car(Integer.valueOf(id), posX, posY, 0, 0); + pred = Crossing.makeEntity("car", Integer.valueOf(id), posX, posY, 0, 0); pred.isPredicted = true; prediction = new Prediction(pred, t.sentence.truth, t.sentence.getOccurenceTime(), "car"); } else if(type.toString().startsWith(pedestrian.toString())) { String id = type.toString().substring(pedestrian.toString().length(), type.toString().length()); - pred = new Pedestrian(Integer.valueOf(id), posX, posY, 0, 0); + pred = Crossing.makeEntity("pedestrian", Integer.valueOf(id), posX, posY, 0, 0); pred.isPredicted = true; prediction = new Prediction(pred, t.sentence.truth, t.sentence.getOccurenceTime(), "pedestrian"); } From 19248cc052ab2d7b596d3f5cdd24652cfb06327b Mon Sep 17 00:00:00 2001 From: PtrMan Date: Wed, 21 Nov 2018 20:38:44 +0100 Subject: [PATCH 13/13] Refactor: removed specializations of Entity --- .../crossing/BehaviourComponent.java | 2 +- .../opennars/applications/crossing/Car.java | 53 ------------------- .../applications/crossing/Crossing.java | 4 +- .../applications/crossing/InformNARS.java | 4 +- .../applications/crossing/Pedestrian.java | 44 --------------- 5 files changed, 5 insertions(+), 102 deletions(-) delete mode 100644 src/main/java/com/opennars/applications/crossing/Car.java delete mode 100644 src/main/java/com/opennars/applications/crossing/Pedestrian.java diff --git a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java index 0b67eb4..7ec0f5f 100644 --- a/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java +++ b/src/main/java/com/opennars/applications/crossing/BehaviourComponent.java @@ -90,7 +90,7 @@ static protected void carTick(Entity entity, List streets, List streets, List trafficLights, List entities, TruthValue truth, long time) { - float mul = Util.truthToValue(truth) * Util.timeToValue(time); - applet.fill(255, 0, 255, mul*255.0f); - - if (!isPredicted && isAnomaly()) { - applet.stroke(255,0,0); - } - else { - applet.stroke(127); - } - - super.draw(applet, streets, trafficLights, entities, truth, time); - - applet.stroke(127); - }*/ -} diff --git a/src/main/java/com/opennars/applications/crossing/Crossing.java b/src/main/java/com/opennars/applications/crossing/Crossing.java index 0ce4ab4..508e944 100644 --- a/src/main/java/com/opennars/applications/crossing/Crossing.java +++ b/src/main/java/com/opennars/applications/crossing/Crossing.java @@ -141,10 +141,10 @@ public void draw() { if(showAnomalies) { for (Prediction pred : disappointments) { Entity e = pred.ent; - if(e instanceof Car) { + if(e.tag.equals("car")) { fill(255,0,0); } - if(e instanceof Pedestrian) { + if(e.tag.equals("pedestrian")) { fill(0,0,255); } this.text("ANOMALY", (float)e.posX, (float)e.posY); diff --git a/src/main/java/com/opennars/applications/crossing/InformNARS.java b/src/main/java/com/opennars/applications/crossing/InformNARS.java index a6aebf5..56d3741 100644 --- a/src/main/java/com/opennars/applications/crossing/InformNARS.java +++ b/src/main/java/com/opennars/applications/crossing/InformNARS.java @@ -39,11 +39,11 @@ public void informAboutEntity(Nar nar, Entity ent, int minX, int minY) { id = "0"; } String pos = Util.positionToTerm((int) ent.posX-minX, (int) ent.posY-minY); - if (ent instanceof Car) { + if (ent.tag.equals("car")) { inputs.add("<(*,car" + id + ","+ pos + ") --> at>. :|:"); input += inputs.get(inputs.size()-1); } - if (ent instanceof Pedestrian) { + if (ent.tag.equals("pedestrian")) { inputs.add("<(*,pedestrian" + id + "," + pos + ") --> at>. :|:"); input += inputs.get(inputs.size()-1); } diff --git a/src/main/java/com/opennars/applications/crossing/Pedestrian.java b/src/main/java/com/opennars/applications/crossing/Pedestrian.java deleted file mode 100644 index bd8ef22..0000000 --- a/src/main/java/com/opennars/applications/crossing/Pedestrian.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * The MIT License - * - * Copyright 2018 The OpenNARS authors. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package com.opennars.applications.crossing; - -import java.util.List; -import org.opennars.entity.TruthValue; -import processing.core.PApplet; - -public class Pedestrian extends Entity { - public final static float pedestrianScale = 0.75f; - public Pedestrian(int id, double posX, double posY, double velocity, double angle) { - super(id, posX, posY, velocity, angle, "pedestrian", new BehaviourComponent(BehaviourComponent.EnumType.PEDESTRIAN)); - behaviour.initialAngle = angle; - behaviour.maxSpeed = 1; - scale = pedestrianScale; - } - /* - public void draw(PApplet applet, List streets, List trafficLights, List entities, TruthValue truth, long time) { - float mul = Util.truthToValue(truth) * Util.timeToValue(time); - applet.fill(0, 255, 255, mul*255.0f); - super.draw(applet, streets, trafficLights, entities, truth, time); - }*/ -}