Implemeneted tick (basic)
This commit is contained in:
parent
624b18bd79
commit
0fbe472261
@ -20,4 +20,10 @@
|
||||
generate
|
||||
</steps>
|
||||
</scenario>
|
||||
<scenario type="Customer">
|
||||
<steps>
|
||||
pass
|
||||
sendreq PURCHASE FRUIT jablko
|
||||
</steps>
|
||||
</scenario>
|
||||
</scenarios>
|
||||
@ -27,12 +27,13 @@ public class FoodChain
|
||||
parseProducts();
|
||||
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()));
|
||||
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)));
|
||||
Ticker.start();
|
||||
while (Ticker.getTimer().isRunning());
|
||||
}
|
||||
|
||||
public static FactoryManager getFactoryManager()
|
||||
|
||||
@ -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.Observable;
|
||||
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class ChannelBase implements Observable {
|
||||
protected ProductType productType;
|
||||
@ -19,15 +18,17 @@ public abstract class ChannelBase implements Observable {
|
||||
|
||||
public ChannelBase(ProductType type)
|
||||
{
|
||||
this.requirements = new PriorityQueue<>();
|
||||
this.transactions = new PriorityQueue<>();
|
||||
this.requirements = new LinkedList<>();
|
||||
this.transactions = new LinkedList<>();
|
||||
this.subscribers = new HashSet<>();
|
||||
this.productType = type;
|
||||
}
|
||||
|
||||
public ChannelBase()
|
||||
{
|
||||
this.requirements = new PriorityQueue<>();
|
||||
this.transactions = new PriorityQueue<>();
|
||||
this.requirements = new LinkedList<>();
|
||||
this.transactions = new LinkedList<>();
|
||||
this.subscribers = new HashSet<>();
|
||||
this.productType = ProductType.OTHER;
|
||||
}
|
||||
|
||||
@ -48,12 +49,18 @@ public abstract class ChannelBase implements Observable {
|
||||
public void addRequirement(Requirement 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();
|
||||
return requirements.size() != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void notifyAllObservers(){
|
||||
subscribers.forEach(subscriber -> subscriber.update(requirements.peek()));
|
||||
subscribers.forEach(subscriber -> requirements.forEach(subscriber::update));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -61,6 +61,9 @@ public class ScenarioFactory
|
||||
case "sendreq":
|
||||
scenarioStep = new SendRequirementStep(node, TransactionType.valueOf(parts[1]), ProductType.valueOf(parts[2]), parts[3]);
|
||||
break;
|
||||
case "pass":
|
||||
scenarioStep = new PassStep();
|
||||
break;
|
||||
}
|
||||
scenarioSteps.add(scenarioStep);
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@ import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager;
|
||||
import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver;
|
||||
import org.javamoney.moneta.Money;
|
||||
|
||||
import javax.money.Monetary;
|
||||
import javax.money.MonetaryAmount;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
@ -24,19 +23,19 @@ public class NodeBase implements ChannelObserver {
|
||||
private final List<Product> products;
|
||||
private final MonetaryAmount money;
|
||||
|
||||
private final List<Transaction> refusedTranslations;
|
||||
private final List<Transaction> refusedTransactions;
|
||||
|
||||
public NodeBase() {
|
||||
subscribedChannels = new HashMap<>();
|
||||
products = new LinkedList<>();
|
||||
refusedTranslations = new LinkedList<>();
|
||||
refusedTransactions = new LinkedList<>();
|
||||
money = Money.of(0, "USD");
|
||||
}
|
||||
|
||||
public NodeBase(double m) {
|
||||
subscribedChannels = new HashMap<>();
|
||||
products = new LinkedList<>();
|
||||
refusedTranslations = new LinkedList<>();
|
||||
refusedTransactions = new LinkedList<>();
|
||||
money = Money.of(m, "USD");
|
||||
}
|
||||
|
||||
@ -71,7 +70,7 @@ public class NodeBase implements ChannelObserver {
|
||||
|
||||
public void refuseTransaction(Transaction transaction) {
|
||||
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) {
|
||||
@ -89,12 +88,13 @@ public class NodeBase implements ChannelObserver {
|
||||
}
|
||||
|
||||
public void acceptRequirement(Requirement requirement) {
|
||||
|
||||
System.out.println(this.getClass().getSimpleName() + " accepted " + requirement.toString());
|
||||
}
|
||||
|
||||
|
||||
public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type) {
|
||||
subscribedChannels.put(channel, type);
|
||||
channel.attach(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -25,9 +25,13 @@ public class Action extends OperationBase {
|
||||
product.setStateToAvailable();
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
if (duration == 0) endAction();
|
||||
public boolean tick() {
|
||||
if (duration == 0) {
|
||||
endAction();
|
||||
return false;
|
||||
}
|
||||
else duration--;
|
||||
return true;
|
||||
}
|
||||
|
||||
public OperationBase getLastOperation() {
|
||||
|
||||
@ -15,6 +15,7 @@ public abstract class Transaction extends OperationBase {
|
||||
transactionType = type;
|
||||
nodeReceiver = receiver;
|
||||
signature = SignatureManager.signTransaction(this);
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
public void endTransaction() {
|
||||
@ -32,4 +33,14 @@ public abstract class Transaction extends OperationBase {
|
||||
public NodeBase getNodeReceiver() {
|
||||
return nodeReceiver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Transaction{" +
|
||||
"transactionType=" + transactionType +
|
||||
", signature=" + (signature != null) +
|
||||
", nodeCreator=" + getNodeCreator().getClass().getSimpleName() +
|
||||
", nodeReceiver=" + nodeReceiver.getClass().getSimpleName() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
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 org.javamoney.moneta.Money;
|
||||
|
||||
@ -58,6 +59,15 @@ public class Product
|
||||
this.money = Money.of(m, "USD");
|
||||
}
|
||||
|
||||
public boolean tick() {
|
||||
try {
|
||||
return ((Action) currentOperation).tick();
|
||||
}
|
||||
catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// "State machine methods"
|
||||
|
||||
/**
|
||||
|
||||
@ -17,7 +17,8 @@ public class Requirement {
|
||||
this.transactionType = transactionType;
|
||||
this.productType = productType;
|
||||
this.productName = productName;
|
||||
TTL = 2;
|
||||
TTL = 3;
|
||||
System.out.println(toString());
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
@ -43,4 +44,15 @@ public class Requirement {
|
||||
public int getTTL() {
|
||||
return TTL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Requirement{" +
|
||||
"creator=" + creator.getClass().getSimpleName() +
|
||||
", transactionType=" + transactionType +
|
||||
", productType=" + productType +
|
||||
", productName='" + productName + '\'' +
|
||||
", TTL=" + TTL +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.scenario;
|
||||
|
||||
public class PassStep extends ScenarioStep {
|
||||
|
||||
@Override
|
||||
void execute() {}
|
||||
}
|
||||
@ -17,11 +17,17 @@ public class Scenario {
|
||||
stepIterator = steps.listIterator(0);
|
||||
}
|
||||
|
||||
void execute() {
|
||||
boolean execute() {
|
||||
if (!stepIterator.hasNext()) {
|
||||
stepIterator = steps.listIterator(0);
|
||||
// stepIterator = steps.listIterator(0);
|
||||
return false;
|
||||
}
|
||||
stepIterator.next().execute();
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean tick() {
|
||||
return execute();
|
||||
}
|
||||
|
||||
public List<ScenarioStep> getSteps() {
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
package cz.cvut.fel.sit.omo.foodchain.util;
|
||||
|
||||
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.product.Product;
|
||||
import cz.cvut.fel.sit.omo.foodchain.scenario.Scenario;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
@ -11,11 +14,13 @@ public class Ticker {
|
||||
|
||||
public static void start() {
|
||||
timer = new Timer(500, actionEvent -> {
|
||||
System.out.println("tick");
|
||||
PoolDatabase db = FoodChain.getFactoryManager().getPoolDatabaseInstance();
|
||||
// db.getChannelPoolInstance().findAll().forEach(ch -> ch.tick()); // change TTL of requirements
|
||||
// db.getNodePoolInstance().findAll().forEach(ch -> ch.tick()); // do next step in scenario
|
||||
// db.getProductPoolInstance().findAll().forEach(ch -> ch.tick()); // change duration of action
|
||||
// db.getScenarioPoolInstance().findAll().forEach(ch -> ch.tick()); // maybe unnecessary
|
||||
boolean productBool = db.getProductPoolInstance().findAll().stream().filter(Product::tick).count() == 0; // change duration of action
|
||||
boolean scenarioBool = db.getScenarioPoolInstance().findAll().stream().filter(Scenario::tick).count() == 0; // do next step in scenario
|
||||
boolean channelBool = db.getChannelPoolInstance().findAll().stream().filter(ChannelBase::tick).count() == 0; // change TTL of requirements
|
||||
// db.getNodePoolInstance().findAll().forEach(n -> n.tick()); // maybe unnecessary
|
||||
if (channelBool && productBool && scenarioBool) stop();
|
||||
});
|
||||
timer.start();
|
||||
}
|
||||
@ -23,4 +28,6 @@ public class Ticker {
|
||||
public static void stop() {
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
public static Timer getTimer() { return timer; }
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user