Implemeneted tick (basic)

This commit is contained in:
Tomáš Musil 2020-12-26 14:23:14 +01:00
parent 624b18bd79
commit 0fbe472261
12 changed files with 102 additions and 28 deletions

View File

@ -20,4 +20,10 @@
generate generate
</steps> </steps>
</scenario> </scenario>
<scenario type="Customer">
<steps>
pass
sendreq PURCHASE FRUIT jablko
</steps>
</scenario>
</scenarios> </scenarios>

View File

@ -27,12 +27,13 @@ public class FoodChain
parseProducts(); parseProducts();
parseScenarios(); parseScenarios();
Ticker.start(); // getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType()));
// getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().findAll().stream().forEach(ch -> System.out.println(ch));
// getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType() + " " + ch.getName()));
// getFactoryManager().getPoolDatabaseInstance().getScenarioPoolInstance().findAll().stream().forEach(ch -> ch.getSteps().stream().forEach(s -> System.out.println(s)));
getFactoryManager().getPoolDatabaseInstance().getChannelPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType())); Ticker.start();
getFactoryManager().getPoolDatabaseInstance().getNodePoolInstance().findAll().stream().forEach(ch -> System.out.println(ch)); while (Ticker.getTimer().isRunning());
getFactoryManager().getPoolDatabaseInstance().getProductPoolInstance().findAll().stream().forEach(ch -> System.out.println(ch.getProductType() + " " + ch.getName()));
getFactoryManager().getPoolDatabaseInstance().getScenarioPoolInstance().findAll().stream().forEach(ch -> ch.getSteps().stream().forEach(s -> System.out.println(s)));
} }
public static FactoryManager getFactoryManager() public static FactoryManager getFactoryManager()

View File

@ -6,9 +6,8 @@ import cz.cvut.fel.sit.omo.foodchain.requirement.Requirement;
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver; import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
import cz.cvut.fel.sit.omo.foodchain.util.Observable; import cz.cvut.fel.sit.omo.foodchain.util.Observable;
import java.util.PriorityQueue; import java.util.*;
import java.util.Queue; import java.util.stream.Collectors;
import java.util.Set;
public abstract class ChannelBase implements Observable { public abstract class ChannelBase implements Observable {
protected ProductType productType; protected ProductType productType;
@ -19,15 +18,17 @@ public abstract class ChannelBase implements Observable {
public ChannelBase(ProductType type) public ChannelBase(ProductType type)
{ {
this.requirements = new PriorityQueue<>(); this.requirements = new LinkedList<>();
this.transactions = new PriorityQueue<>(); this.transactions = new LinkedList<>();
this.subscribers = new HashSet<>();
this.productType = type; this.productType = type;
} }
public ChannelBase() public ChannelBase()
{ {
this.requirements = new PriorityQueue<>(); this.requirements = new LinkedList<>();
this.transactions = new PriorityQueue<>(); this.transactions = new LinkedList<>();
this.subscribers = new HashSet<>();
this.productType = ProductType.OTHER; this.productType = ProductType.OTHER;
} }
@ -48,12 +49,18 @@ public abstract class ChannelBase implements Observable {
public void addRequirement(Requirement requirement) public void addRequirement(Requirement requirement)
{ {
this.requirements.add(requirement); this.requirements.add(requirement);
}
public boolean tick() {
requirements.forEach(Requirement::tick);
requirements = requirements.stream().filter(r -> r.getTTL() > 0).collect(Collectors.toCollection(LinkedList::new));
notifyAllObservers(); notifyAllObservers();
return requirements.size() != 0;
} }
@Override @Override
public void notifyAllObservers(){ public void notifyAllObservers(){
subscribers.forEach(subscriber -> subscriber.update(requirements.peek())); subscribers.forEach(subscriber -> requirements.forEach(subscriber::update));
} }
@Override @Override

View File

@ -61,6 +61,9 @@ public class ScenarioFactory
case "sendreq": case "sendreq":
scenarioStep = new SendRequirementStep(node, TransactionType.valueOf(parts[1]), ProductType.valueOf(parts[2]), parts[3]); scenarioStep = new SendRequirementStep(node, TransactionType.valueOf(parts[1]), ProductType.valueOf(parts[2]), parts[3]);
break; break;
case "pass":
scenarioStep = new PassStep();
break;
} }
scenarioSteps.add(scenarioStep); scenarioSteps.add(scenarioStep);
} }

View File

@ -11,7 +11,6 @@ import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver; import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
import org.javamoney.moneta.Money; import org.javamoney.moneta.Money;
import javax.money.Monetary;
import javax.money.MonetaryAmount; import javax.money.MonetaryAmount;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -24,19 +23,19 @@ public class NodeBase implements ChannelObserver {
private final List<Product> products; private final List<Product> products;
private final MonetaryAmount money; private final MonetaryAmount money;
private final List<Transaction> refusedTranslations; private final List<Transaction> refusedTransactions;
public NodeBase() { public NodeBase() {
subscribedChannels = new HashMap<>(); subscribedChannels = new HashMap<>();
products = new LinkedList<>(); products = new LinkedList<>();
refusedTranslations = new LinkedList<>(); refusedTransactions = new LinkedList<>();
money = Money.of(0, "USD"); money = Money.of(0, "USD");
} }
public NodeBase(double m) { public NodeBase(double m) {
subscribedChannels = new HashMap<>(); subscribedChannels = new HashMap<>();
products = new LinkedList<>(); products = new LinkedList<>();
refusedTranslations = new LinkedList<>(); refusedTransactions = new LinkedList<>();
money = Money.of(m, "USD"); money = Money.of(m, "USD");
} }
@ -71,7 +70,7 @@ public class NodeBase implements ChannelObserver {
public void refuseTransaction(Transaction transaction) { public void refuseTransaction(Transaction transaction) {
transaction.endTransaction(); transaction.endTransaction();
refusedTranslations.add(transaction); //TODO maybe need more work on it refusedTransactions.add(transaction); //TODO maybe need more work on it
} }
public Transaction readNextTransaction(ChannelBase channel) { public Transaction readNextTransaction(ChannelBase channel) {
@ -89,12 +88,13 @@ public class NodeBase implements ChannelObserver {
} }
public void acceptRequirement(Requirement requirement) { public void acceptRequirement(Requirement requirement) {
System.out.println(this.getClass().getSimpleName() + " accepted " + requirement.toString());
} }
public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type) { public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type) {
subscribedChannels.put(channel, type); subscribedChannels.put(channel, type);
channel.attach(this);
} }

View File

@ -25,9 +25,13 @@ public class Action extends OperationBase {
product.setStateToAvailable(); product.setStateToAvailable();
} }
public void tick() { public boolean tick() {
if (duration == 0) endAction(); if (duration == 0) {
endAction();
return false;
}
else duration--; else duration--;
return true;
} }
public OperationBase getLastOperation() { public OperationBase getLastOperation() {

View File

@ -15,6 +15,7 @@ public abstract class Transaction extends OperationBase {
transactionType = type; transactionType = type;
nodeReceiver = receiver; nodeReceiver = receiver;
signature = SignatureManager.signTransaction(this); signature = SignatureManager.signTransaction(this);
System.out.println(toString());
} }
public void endTransaction() { public void endTransaction() {
@ -32,4 +33,14 @@ public abstract class Transaction extends OperationBase {
public NodeBase getNodeReceiver() { public NodeBase getNodeReceiver() {
return nodeReceiver; return nodeReceiver;
} }
@Override
public String toString() {
return "Transaction{" +
"transactionType=" + transactionType +
", signature=" + (signature != null) +
", nodeCreator=" + getNodeCreator().getClass().getSimpleName() +
", nodeReceiver=" + nodeReceiver.getClass().getSimpleName() +
'}';
}
} }

View File

@ -1,5 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.product; package cz.cvut.fel.sit.omo.foodchain.product;
import cz.cvut.fel.sit.omo.foodchain.operation.Action;
import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase; import cz.cvut.fel.sit.omo.foodchain.operation.OperationBase;
import org.javamoney.moneta.Money; import org.javamoney.moneta.Money;
@ -58,6 +59,15 @@ public class Product
this.money = Money.of(m, "USD"); this.money = Money.of(m, "USD");
} }
public boolean tick() {
try {
return ((Action) currentOperation).tick();
}
catch (Exception e) {
return false;
}
}
// "State machine methods" // "State machine methods"
/** /**

View File

@ -17,7 +17,8 @@ public class Requirement {
this.transactionType = transactionType; this.transactionType = transactionType;
this.productType = productType; this.productType = productType;
this.productName = productName; this.productName = productName;
TTL = 2; TTL = 3;
System.out.println(toString());
} }
public void tick() { public void tick() {
@ -43,4 +44,15 @@ public class Requirement {
public int getTTL() { public int getTTL() {
return TTL; return TTL;
} }
@Override
public String toString() {
return "Requirement{" +
"creator=" + creator.getClass().getSimpleName() +
", transactionType=" + transactionType +
", productType=" + productType +
", productName='" + productName + '\'' +
", TTL=" + TTL +
'}';
}
} }

View File

@ -0,0 +1,7 @@
package cz.cvut.fel.sit.omo.foodchain.scenario;
public class PassStep extends ScenarioStep {
@Override
void execute() {}
}

View File

@ -17,11 +17,17 @@ public class Scenario {
stepIterator = steps.listIterator(0); stepIterator = steps.listIterator(0);
} }
void execute() { boolean execute() {
if (!stepIterator.hasNext()) { if (!stepIterator.hasNext()) {
stepIterator = steps.listIterator(0); // stepIterator = steps.listIterator(0);
return false;
} }
stepIterator.next().execute(); stepIterator.next().execute();
return true;
}
public boolean tick() {
return execute();
} }
public List<ScenarioStep> getSteps() { public List<ScenarioStep> getSteps() {

View File

@ -1,7 +1,10 @@
package cz.cvut.fel.sit.omo.foodchain.util; package cz.cvut.fel.sit.omo.foodchain.util;
import cz.cvut.fel.sit.omo.foodchain.FoodChain; import cz.cvut.fel.sit.omo.foodchain.FoodChain;
import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase;
import cz.cvut.fel.sit.omo.foodchain.pool.PoolDatabase; import cz.cvut.fel.sit.omo.foodchain.pool.PoolDatabase;
import cz.cvut.fel.sit.omo.foodchain.product.Product;
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
import javax.swing.*; import javax.swing.*;
@ -11,11 +14,13 @@ public class Ticker {
public static void start() { public static void start() {
timer = new Timer(500, actionEvent -> { timer = new Timer(500, actionEvent -> {
System.out.println("tick");
PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance(); PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
// db.getChannelPoolInstance().findAll().forEach(ch -> ch.tick()); // change TTL of requirements boolean productBool = db.getProductPoolInstance().findAll().stream().filter(Product::tick).count() == 0; // change duration of action
// db.getNodePoolInstance().findAll().forEach(ch -> ch.tick()); // do next step in scenario boolean scenarioBool = db.getScenarioPoolInstance().findAll().stream().filter(Scenario::tick).count() == 0; // do next step in scenario
// db.getProductPoolInstance().findAll().forEach(ch -> ch.tick()); // change duration of action boolean channelBool = db.getChannelPoolInstance().findAll().stream().filter(ChannelBase::tick).count() == 0; // change TTL of requirements
// db.getScenarioPoolInstance().findAll().forEach(ch -> ch.tick()); // maybe unnecessary // db.getNodePoolInstance().findAll().forEach(n -> n.tick()); // maybe unnecessary
if (channelBool && productBool && scenarioBool) stop();
}); });
timer.start(); timer.start();
} }
@ -23,4 +28,6 @@ public class Ticker {
public static void stop() { public static void stop() {
timer.stop(); timer.stop();
} }
public static Timer getTimer() { return timer; }
} }