diff --git a/conf/channels.xml b/conf/channels.xml new file mode 100644 index 0000000..dfa0429 --- /dev/null +++ b/conf/channels.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/conf/nodes.xml b/conf/nodes.xml new file mode 100644 index 0000000..b5a2081 --- /dev/null +++ b/conf/nodes.xml @@ -0,0 +1,18 @@ + + + 100 + MEET PURCHASE;VEGETABLE PURCHASE;FRUIT PURCHASE + + + 0 + MEET SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE + + + 1500 + MEET SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE + + + 250 + MEET SALE;VEGETABLE SALE;FRUIT SALE + + \ No newline at end of file diff --git a/conf/products.xml b/conf/products.xml new file mode 100644 index 0000000..ac863d4 --- /dev/null +++ b/conf/products.xml @@ -0,0 +1,47 @@ + + + jablko + FRUIT + 5 + + + hruška + FRUIT + 7 + + + banán + FRUIT + 10 + + + okurka + VEGETABLE + 3 + + + paprika + VEGETABLE + 5 + + + rajče + VEGETABLE + 3 + + + vepřové + MEET + 20 + + + hovězí + MEET + 50 + + + kuřecí + MEET + 30 + + \ No newline at end of file diff --git a/conf/scenarios.xml b/conf/scenarios.xml new file mode 100644 index 0000000..fb74da6 --- /dev/null +++ b/conf/scenarios.xml @@ -0,0 +1,15 @@ + + + + generate jablko + generate hovězí + generate hruška + generate rajče + generate paprika + generate kuřecí + generate vepřové + generate banán + generate okurka + + + \ No newline at end of file diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/channel/ChannelSubscriptionType.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/channel/ChannelSubscriptionType.java new file mode 100644 index 0000000..dca4809 --- /dev/null +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/channel/ChannelSubscriptionType.java @@ -0,0 +1,6 @@ +package cz.cvut.fel.sit.omo.foodchain.channel; + +public enum ChannelSubscriptionType { + + SALE, PURCHASE, SALE_AND_PURCHASE +} diff --git a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java index 8680acc..68d05b1 100644 --- a/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java +++ b/src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java @@ -1,6 +1,7 @@ package cz.cvut.fel.sit.omo.foodchain.node; import cz.cvut.fel.sit.omo.foodchain.channel.ChannelBase; +import cz.cvut.fel.sit.omo.foodchain.channel.ChannelSubscriptionType; import cz.cvut.fel.sit.omo.foodchain.operation.*; import cz.cvut.fel.sit.omo.foodchain.product.Product; import cz.cvut.fel.sit.omo.foodchain.product.ProductType; @@ -9,29 +10,40 @@ import cz.cvut.fel.sit.omo.foodchain.signature.Signature; import cz.cvut.fel.sit.omo.foodchain.signature.SignatureManager; import cz.cvut.fel.sit.omo.foodchain.util.ChannelObserver; +import javax.money.Monetary; import javax.money.MonetaryAmount; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; public class NodeBase implements ChannelObserver { - private final List subscribedChannels; + private final Map subscribedChannels; private final List products; private MonetaryAmount money; private final List refusedTranslations; public NodeBase() { - subscribedChannels = new LinkedList<>(); + subscribedChannels = new HashMap<>(); products = new LinkedList<>(); refusedTranslations = new LinkedList<>(); + money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(0).create(); + } + + public NodeBase(int m) { + subscribedChannels = new HashMap<>(); + products = new LinkedList<>(); + refusedTranslations = new LinkedList<>(); + money = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(m).create(); } public void sendTransaction(Product product, NodeBase node) { Transaction transaction = new ProductTransaction(this, node, TransactionType.SALE, product); - subscribedChannels.stream() + subscribedChannels.keySet().stream() .filter(ch -> ch.getProductType().equals(product.getProductType())) .findAny().ifPresent(channel -> channel.addTransaction(transaction)); } @@ -40,7 +52,7 @@ public class NodeBase implements ChannelObserver { public void sendTransaction(MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction) { Transaction transaction = new FinancialTransaction(this, node, TransactionType.PURCHASE, amount, prevTransaction); - subscribedChannels.stream() + subscribedChannels.keySet().stream() .filter(ch -> ch.getProductType().equals(prevTransaction.getProduct().getProductType())) .findAny().ifPresent(channel -> channel.addTransaction(transaction)); } @@ -70,7 +82,7 @@ public class NodeBase implements ChannelObserver { public void createRequirement(TransactionType tType, ProductType pType, String pName) { Requirement req = new Requirement(this, tType, pType, pName); - subscribedChannels.stream() + subscribedChannels.keySet().stream() .filter(ch -> ch.getProductType().equals(pType)) .findAny().ifPresent(channel -> channel.addRequirement(req)); } @@ -80,8 +92,8 @@ public class NodeBase implements ChannelObserver { } - public void subscribeToChannel(ChannelBase channel) { - subscribedChannels.add(channel); + public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type) { + subscribedChannels.put(channel, type); } @@ -127,8 +139,6 @@ public class NodeBase implements ChannelObserver { return SignatureManager.validateSignature(transaction); } - public MonetaryAmount getMoney() { return money; } - @Override public void update(Requirement requirement) { if (!requirement.getCreator().equals(this))