Merge branch 'feature/channel_impl' into develop

# Conflicts:
#	src/main/java/cz/cvut/fel/sit/omo/foodchain/FoodChain.java
#	src/main/java/cz/cvut/fel/sit/omo/foodchain/node/NodeBase.java
This commit is contained in:
Nikita Iryupin 2020-12-25 18:56:21 +01:00
commit 270654e5ea
6 changed files with 110 additions and 9 deletions

5
conf/channels.xml Normal file
View File

@ -0,0 +1,5 @@
<channels>
<channel type="MEET"></channel>
<channel type="VEGETABLE"></channel>
<channel type="FRUIT"></channel>
</channels>

18
conf/nodes.xml Normal file
View File

@ -0,0 +1,18 @@
<nodes>
<node type = "Farmer">
<money>100</money>
<channels>MEET PURCHASE;VEGETABLE PURCHASE;FRUIT PURCHASE</channels>
</node>
<node type = "Processor">
<money>0</money>
<channels>MEET SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
</node>
<node type = "Distributor">
<money>1500</money>
<channels>MEET SALE_AND_PURCHASE;VEGETABLE SALE_AND_PURCHASE;FRUIT SALE_AND_PURCHASE</channels>
</node>
<node type = "Customer">
<money>250</money>
<channels>MEET SALE;VEGETABLE SALE;FRUIT SALE</channels>
</node>
</nodes>

47
conf/products.xml Normal file
View File

@ -0,0 +1,47 @@
<products>
<product>
<name>jablko</name>
<type>FRUIT</type>
<price>5</price>
</product>
<product>
<name>hruška</name>
<type>FRUIT</type>
<price>7</price>
</product>
<product>
<name>banán</name>
<type>FRUIT</type>
<price>10</price>
</product>
<product>
<name>okurka</name>
<type>VEGETABLE</type>
<price>3</price>
</product>
<product>
<name>paprika</name>
<type>VEGETABLE</type>
<price>5</price>
</product>
<product>
<name>rajče</name>
<type>VEGETABLE</type>
<price>3</price>
</product>
<product>
<name>vepřové</name>
<type>MEET</type>
<price>20</price>
</product>
<product>
<name>hovězí</name>
<type>MEET</type>
<price>50</price>
</product>
<product>
<name>kuřecí</name>
<type>MEET</type>
<price>30</price>
</product>
</products>

15
conf/scenarios.xml Normal file
View File

@ -0,0 +1,15 @@
<scenarios>
<scenario type="Farmer">
<steps>
generate jablko
generate hovězí
generate hruška
generate rajče
generate paprika
generate kuřecí
generate vepřové
generate banán
generate okurka
</steps>
</scenario>
</scenarios>

View File

@ -0,0 +1,6 @@
package cz.cvut.fel.sit.omo.foodchain.channel;
public enum ChannelSubscriptionType {
SALE, PURCHASE, SALE_AND_PURCHASE
}

View File

@ -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<ChannelBase> subscribedChannels;
private final Map<ChannelBase, ChannelSubscriptionType> subscribedChannels;
private final List<Product> products;
private MonetaryAmount money;
private final List<Transaction> 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))