implement filter for market analyzer
This commit is contained in:
@@ -1,12 +1,17 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface Market {
|
||||
|
||||
void add(Place place);
|
||||
Place addPlace(String name, double x, double y, double z);
|
||||
void remove(Place place);
|
||||
default Place get(String name){
|
||||
Optional<Place> place = get().stream().filter(p -> name.equals(p.getName())).findFirst();
|
||||
return place.isPresent() ? place.get() : null;
|
||||
}
|
||||
|
||||
void add(Group group);
|
||||
Group addGroup(String name, GROUP_TYPE type);
|
||||
|
||||
@@ -9,7 +9,8 @@ import java.util.*;
|
||||
public class MarketAnalyzer {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(MarketAnalyzer.class);
|
||||
|
||||
private Market market;
|
||||
private final Market market;
|
||||
private MarketFilter filter;
|
||||
private double tank;
|
||||
private double maxDistance;
|
||||
private int segmentSize;
|
||||
@@ -25,221 +26,10 @@ public class MarketAnalyzer {
|
||||
this.segmentSize = 0;
|
||||
}
|
||||
|
||||
public Collection<Order> getTop(double balance){
|
||||
LOG.debug("Get top {}", limit);
|
||||
Iterable<Place> places = market.get();
|
||||
List<Order> top = new ArrayList<>(limit);
|
||||
for (Place place : places) {
|
||||
LOG.trace("Check place {}", place);
|
||||
Collection<Order> orders = getOrders(place, balance, top.isEmpty() ? 0 : top.get(top.size()-1).getProfit());
|
||||
TopList.addAllToTop(top, orders, limit, orderComparator);
|
||||
}
|
||||
return top;
|
||||
public void setFilter(MarketFilter filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Vendor vendor, double balance) {
|
||||
Collection<Place> places = market.get();
|
||||
Graph<Place> graph = new Graph<Place>(vendor.getPlace(), places, tank, maxDistance, true, jumps, Path::new);
|
||||
return getOrders(graph, Collections.singleton(vendor), balance, 0);
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Place place, double balance) {
|
||||
return getOrders(place, balance, 0);
|
||||
}
|
||||
|
||||
private Collection<Order> getOrders(Place place, double balance, double lowProfit) {
|
||||
Collection<Place> places = market.get();
|
||||
Graph<Place> graph = new Graph<>(place, places, tank, maxDistance, true, jumps, Path::new);
|
||||
return getOrders(graph, place.get(), balance, lowProfit);
|
||||
}
|
||||
|
||||
private Collection<Order> getOrders(Graph<Place> graph, Collection<Vendor> sellers, double balance, double lowProfit) {
|
||||
List<Order> res = new ArrayList<>(20);
|
||||
for (Vendor vendor : sellers) {
|
||||
for (Offer sell : vendor.getAllSellOffers()) {
|
||||
LOG.trace("Sell offer {}", sell);
|
||||
if (sell.getCount() == 0) continue;
|
||||
long count = Order.getMaxCount(sell, balance, cargo);
|
||||
LOG.trace("count = {}", count);
|
||||
if (count == 0) continue;
|
||||
Iterator<Offer> buyers = market.getStatBuy(sell.getItem()).getOffers().descendingIterator();
|
||||
while (buyers.hasNext()){
|
||||
Offer buy = buyers.next();
|
||||
if (!graph.isAccessible(buy.getVendor().getPlace())){
|
||||
LOG.trace("Is inaccessible buyer, skip");
|
||||
continue;
|
||||
}
|
||||
Order order = new Order(sell, buy, count);
|
||||
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
|
||||
if (order.getProfit() <= 0 && order.getCount() > 0) break;
|
||||
if (order.getProfit() < lowProfit && order.getCount() == count) {
|
||||
LOG.trace("Is low profit, skip");
|
||||
break;
|
||||
}
|
||||
res.add(order);
|
||||
}
|
||||
}
|
||||
}
|
||||
res.sort(orderComparator);
|
||||
return res;
|
||||
}
|
||||
|
||||
private Collection<Order> getOrders(Collection<Vendor> sellers, Collection<Vendor> buyers, double balance, double lowProfit) {
|
||||
List<Order> res = new ArrayList<>();
|
||||
for (Vendor seller : sellers) {
|
||||
for (Offer sell : seller.getAllSellOffers()) {
|
||||
if (sell.getCount() == 0) continue;
|
||||
long count = Order.getMaxCount(sell, balance, cargo);
|
||||
LOG.trace("Sell offer {}, count = {}", sell, count);
|
||||
if (count == 0) continue;
|
||||
for (Vendor buyer : buyers) {
|
||||
Offer buy = buyer.getBuy(sell.getItem());
|
||||
if (buy != null){
|
||||
Order order = new Order(sell, buy, count);
|
||||
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
|
||||
if (order.getProfit() < lowProfit) {
|
||||
LOG.trace("Is low profit, skip");
|
||||
continue;
|
||||
}
|
||||
res.add(order);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res.sort(orderComparator);
|
||||
return res;
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Vendor from, Vendor to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from.getPlace(), market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to.getPlace())){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(Collections.singleton(from), Collections.singleton(to), balance, 0);
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Place from, Place to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to)){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(from.get(), to.get(), balance, 0);
|
||||
}
|
||||
|
||||
|
||||
public Collection<Order> getOrders(Vendor from, Place to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from.getPlace(), market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to)){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(Collections.singleton(from), to.get(), balance, 0);
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Place from, Vendor to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to.getPlace())){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(from.get(), Collections.singleton(to), balance, 0);
|
||||
}
|
||||
|
||||
|
||||
public Collection<Path<Place>> getPaths(Place from, Place to){
|
||||
Graph<Place> graph = new Graph<Place>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
return graph.getPathsTo(to);
|
||||
}
|
||||
|
||||
public Path<Place> getPath(Place from, Place to){
|
||||
Graph<Place> graph = new Graph<Place>(from, market.get(), tank, maxDistance, true, jumps, Path::new);
|
||||
return graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public PathRoute getPath(Vendor from, Vendor to){
|
||||
RouteGraph graph = new RouteGraph(from, market.getVendors(), tank, maxDistance, true, jumps);
|
||||
return (PathRoute)graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
return searcher.getPaths(from, vendors, jumps, balance, cargo, limit);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
for (Vendor vendor : from.get()) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(vendor, vendors, jumps, balance, cargo, limit);
|
||||
if (paths.size()>0){
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
return searcher.getPaths(from, to, market.getVendors(), jumps, balance, cargo, limit);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, Place to, double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
Collection<Vendor> fVendors = from.get();
|
||||
Collection<Vendor> toVendors = to.get();
|
||||
int count = (int) Math.ceil(limit / fVendors.size());
|
||||
for (Vendor fromVendor : fVendors) {
|
||||
for (Vendor toVendor : toVendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(fromVendor, toVendor, vendors, jumps, balance, cargo, count);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, Place to, double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
Collection<Vendor> toVendors = to.get();
|
||||
int count = (int) Math.ceil(limit / toVendors.size());
|
||||
for (Vendor toVendor : toVendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(from, toVendor, vendors, jumps, balance, cargo, count);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, Vendor to, double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = market.getVendors();
|
||||
Collection<Vendor> fVendors = from.get();
|
||||
int count = (int) Math.ceil(limit / fVendors.size());
|
||||
for (Vendor fromVendor : fVendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(fromVendor, to, vendors, jumps, balance, cargo, count);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getTopPaths(double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = new PlacesWrapper(market.get());
|
||||
for (Vendor vendor : vendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(vendor, vendor, vendors, jumps, balance, cargo, 3);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
|
||||
public void setTank(double tank) {
|
||||
this.tank = tank;
|
||||
}
|
||||
@@ -263,4 +53,279 @@ public class MarketAnalyzer {
|
||||
public void setPathsCount(int count) {
|
||||
this.limit = count;
|
||||
}
|
||||
|
||||
public Collection<Order> getTop(double balance){
|
||||
LOG.debug("Get top {}", limit);
|
||||
Iterable<Place> places = getPlaces();
|
||||
List<Order> top = new ArrayList<>(limit);
|
||||
for (Place place : places) {
|
||||
LOG.trace("Check place {}", place);
|
||||
Collection<Order> orders = getOrders(place, balance, top.isEmpty() ? 0 : top.get(top.size()-1).getProfit());
|
||||
TopList.addAllToTop(top, orders, limit, orderComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Vendor vendor, double balance) {
|
||||
Collection<Place> places = getPlaces();
|
||||
Graph<Place> graph = new Graph<Place>(vendor.getPlace(), places, tank, maxDistance, true, jumps, Path::new);
|
||||
return getOrders(graph, Collections.singleton(vendor), balance, 0);
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Place place, double balance) {
|
||||
return getOrders(place, balance, 0);
|
||||
}
|
||||
|
||||
private Collection<Order> getOrders(Place place, double balance, double lowProfit) {
|
||||
Collection<Place> places = getPlaces();
|
||||
Graph<Place> graph = new Graph<>(place, places, tank, maxDistance, true, jumps, Path::new);
|
||||
return getOrders(graph, place.get(), balance, lowProfit);
|
||||
}
|
||||
|
||||
private Collection<Order> getOrders(Graph<Place> graph, Collection<Vendor> sellers, double balance, double lowProfit) {
|
||||
List<Order> res = new ArrayList<>(20);
|
||||
for (Vendor vendor : sellers) {
|
||||
if (isFiltered(vendor)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
for (Offer sell : vendor.getAllSellOffers()) {
|
||||
LOG.trace("Sell offer {}", sell);
|
||||
if (sell.getCount() == 0) continue;
|
||||
long count = Order.getMaxCount(sell, balance, cargo);
|
||||
LOG.trace("count = {}", count);
|
||||
if (count == 0) continue;
|
||||
Iterator<Offer> buyers = market.getStatBuy(sell.getItem()).getOffers().descendingIterator();
|
||||
while (buyers.hasNext()){
|
||||
Offer buy = buyers.next();
|
||||
if (isFiltered(buy.getVendor())){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
if (!graph.isAccessible(buy.getVendor().getPlace())){
|
||||
LOG.trace("Is inaccessible buyer, skip");
|
||||
continue;
|
||||
}
|
||||
Order order = new Order(sell, buy, count);
|
||||
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
|
||||
if (order.getProfit() <= 0 && order.getCount() > 0) break;
|
||||
if (order.getProfit() < lowProfit && order.getCount() == count) {
|
||||
LOG.trace("Is low profit, skip");
|
||||
break;
|
||||
}
|
||||
res.add(order);
|
||||
}
|
||||
}
|
||||
}
|
||||
res.sort(orderComparator);
|
||||
return res;
|
||||
}
|
||||
|
||||
private Collection<Order> getOrders(Collection<Vendor> sellers, Collection<Vendor> buyers, double balance, double lowProfit) {
|
||||
List<Order> res = new ArrayList<>();
|
||||
for (Vendor seller : sellers) {
|
||||
if (isFiltered(seller)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
for (Offer sell : seller.getAllSellOffers()) {
|
||||
if (sell.getCount() == 0) continue;
|
||||
long count = Order.getMaxCount(sell, balance, cargo);
|
||||
LOG.trace("Sell offer {}, count = {}", sell, count);
|
||||
if (count == 0) continue;
|
||||
for (Vendor buyer : buyers) {
|
||||
if (isFiltered(buyer)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
Offer buy = buyer.getBuy(sell.getItem());
|
||||
if (buy != null){
|
||||
Order order = new Order(sell, buy, count);
|
||||
LOG.trace("Buy offer {} profit = {}", buy, order.getProfit());
|
||||
if (order.getProfit() < lowProfit) {
|
||||
LOG.trace("Is low profit, skip");
|
||||
continue;
|
||||
}
|
||||
res.add(order);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res.sort(orderComparator);
|
||||
return res;
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Vendor from, Vendor to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from.getPlace(), getPlaces(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to.getPlace())){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(Collections.singleton(from), Collections.singleton(to), balance, 0);
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Place from, Place to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from, getPlaces(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to)){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(from.get(), to.get(), balance, 0);
|
||||
}
|
||||
|
||||
|
||||
public Collection<Order> getOrders(Vendor from, Place to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from.getPlace(), getPlaces(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to)){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(Collections.singleton(from), to.get(), balance, 0);
|
||||
}
|
||||
|
||||
public Collection<Order> getOrders(Place from, Vendor to, double balance) {
|
||||
Graph<Place> graph = new Graph<Place>(from, getPlaces(), tank, maxDistance, true, jumps, Path::new);
|
||||
if (!graph.isAccessible(to.getPlace())){
|
||||
LOG.trace("Is inaccessible buyer");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return getOrders(from.get(), Collections.singleton(to), balance, 0);
|
||||
}
|
||||
|
||||
|
||||
public Collection<Path<Place>> getPaths(Place from, Place to){
|
||||
Graph<Place> graph = new Graph<Place>(from, getPlaces(), tank, maxDistance, true, jumps, Path::new);
|
||||
return graph.getPathsTo(to);
|
||||
}
|
||||
|
||||
public Path<Place> getPath(Place from, Place to){
|
||||
Graph<Place> graph = new Graph<Place>(from, getPlaces(), tank, maxDistance, true, jumps, Path::new);
|
||||
return graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public PathRoute getPath(Vendor from, Vendor to){
|
||||
RouteGraph graph = new RouteGraph(from, getVendors(), tank, maxDistance, true, jumps);
|
||||
return (PathRoute)graph.getFastPathTo(to);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = getVendors();
|
||||
return searcher.getPaths(from, vendors, jumps, balance, cargo, limit);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = getVendors();
|
||||
for (Vendor vendor : from.get()) {
|
||||
if (isFiltered(vendor)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
Collection<PathRoute> paths = searcher.getPaths(vendor, vendors, jumps, balance, cargo, limit);
|
||||
if (paths.size()>0){
|
||||
return paths;
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, Vendor to, double balance){
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
return searcher.getPaths(from, to, getVendors(), jumps, balance, cargo, limit);
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, Place to, double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = getVendors();
|
||||
Collection<Vendor> fVendors = from.get();
|
||||
Collection<Vendor> toVendors = to.get();
|
||||
int count = (int) Math.ceil(limit / fVendors.size());
|
||||
for (Vendor fromVendor : fVendors) {
|
||||
if (isFiltered(fromVendor)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
for (Vendor toVendor : toVendors) {
|
||||
if (isFiltered(toVendor)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
Collection<PathRoute> paths = searcher.getPaths(fromVendor, toVendor, vendors, jumps, balance, cargo, count);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Vendor from, Place to, double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = getVendors();
|
||||
Collection<Vendor> toVendors = to.get();
|
||||
int count = (int) Math.ceil(limit / toVendors.size());
|
||||
for (Vendor toVendor : toVendors) {
|
||||
if (isFiltered(toVendor)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
Collection<PathRoute> paths = searcher.getPaths(from, toVendor, vendors, jumps, balance, cargo, count);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getPaths(Place from, Vendor to, double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = getVendors();
|
||||
Collection<Vendor> fVendors = from.get();
|
||||
int count = (int) Math.ceil(limit / fVendors.size());
|
||||
for (Vendor fromVendor : fVendors) {
|
||||
if (isFiltered(fromVendor)){
|
||||
LOG.trace("Is filtered, skip");
|
||||
continue;
|
||||
}
|
||||
Collection<PathRoute> paths = searcher.getPaths(fromVendor, to, vendors, jumps, balance, cargo, count);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
public Collection<PathRoute> getTopPaths(double balance){
|
||||
List<PathRoute> top = new ArrayList<>(limit);
|
||||
RouteSearcher searcher = new RouteSearcher(maxDistance, tank, segmentSize);
|
||||
Collection<Vendor> vendors = getVendors();
|
||||
for (Vendor vendor : vendors) {
|
||||
Collection<PathRoute> paths = searcher.getPaths(vendor, vendor, vendors, jumps, balance, cargo, 3);
|
||||
TopList.addAllToTop(top, paths, limit, RouteGraph.byProfitComparator);
|
||||
}
|
||||
return top;
|
||||
}
|
||||
|
||||
private Collection<Place> getPlaces(){
|
||||
if (filter != null){
|
||||
return filter.filtered(market.get());
|
||||
} else {
|
||||
return market.get();
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<Vendor> getVendors(){
|
||||
if (filter != null){
|
||||
Collection<Vendor> vendors = new PlacesWrapper(getPlaces());
|
||||
return filter.filteredVendors(vendors);
|
||||
} else {
|
||||
return market.getVendors();
|
||||
}
|
||||
}
|
||||
|
||||
public MarketFilter getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
private boolean isFiltered(Vendor vendor){
|
||||
return filter != null && (filter.isFiltered(vendor.getPlace()) || filter.isFiltered(vendor));
|
||||
}
|
||||
}
|
||||
|
||||
169
core/src/main/java/ru/trader/core/MarketFilter.java
Normal file
169
core/src/main/java/ru/trader/core/MarketFilter.java
Normal file
@@ -0,0 +1,169 @@
|
||||
package ru.trader.core;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MarketFilter {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(MarketFilter.class);
|
||||
|
||||
private Place center;
|
||||
private double radius;
|
||||
private double distance;
|
||||
private final EnumSet<SERVICE_TYPE> services;
|
||||
private final Collection<Vendor> excludes;
|
||||
|
||||
public MarketFilter() {
|
||||
services = EnumSet.noneOf(SERVICE_TYPE.class);
|
||||
excludes = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Place getCenter() {
|
||||
return center;
|
||||
}
|
||||
|
||||
public void setCenter(Place center) {
|
||||
this.center = center;
|
||||
}
|
||||
|
||||
public double getRadius() {
|
||||
return radius;
|
||||
}
|
||||
|
||||
public void setRadius(double radius) {
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public void add(SERVICE_TYPE service){
|
||||
services.add(service);
|
||||
}
|
||||
|
||||
public void addAll(Collection<SERVICE_TYPE> service){
|
||||
services.addAll(service);
|
||||
}
|
||||
|
||||
public void remove(SERVICE_TYPE service){
|
||||
services.remove(service);
|
||||
}
|
||||
|
||||
public boolean has(SERVICE_TYPE service){
|
||||
return services.contains(service);
|
||||
}
|
||||
|
||||
public void addExclude(Vendor vendor){
|
||||
excludes.add(vendor);
|
||||
}
|
||||
|
||||
public void removeExclude(Vendor vendor){
|
||||
excludes.remove(vendor);
|
||||
}
|
||||
|
||||
public void clearExcludes(){
|
||||
excludes.clear();
|
||||
}
|
||||
|
||||
public Collection<Vendor> getExcludes(){
|
||||
return excludes;
|
||||
}
|
||||
|
||||
public boolean isFiltered(Place place){
|
||||
return center != null && center.getDistance(place) > radius;
|
||||
}
|
||||
|
||||
public boolean isFiltered(Vendor vendor){
|
||||
if (distance > 0 && vendor.getDistance() > distance) return true;
|
||||
if (excludes.contains(vendor)) return true;
|
||||
for (SERVICE_TYPE service : services) {
|
||||
if (!vendor.has(service)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Collection<Place> filtered(Collection<Place> places){
|
||||
return places.parallelStream().filter(p -> !isFiltered(p)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Collection<Vendor> filteredVendors(Collection<Vendor> vendors){
|
||||
return vendors.parallelStream().filter(v -> !isFiltered(v)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
public static MarketFilter buildFilter(Properties values, Market market){
|
||||
MarketFilter filter = new MarketFilter();
|
||||
String v = values.getProperty("filter.center", null);
|
||||
if (v != null){
|
||||
filter.setCenter(market.get(v));
|
||||
}
|
||||
filter.setRadius(Double.valueOf(values.getProperty("filter.radius","0")));
|
||||
filter.setDistance(Double.valueOf(values.getProperty("filter.distance", "0")));
|
||||
v = values.getProperty("filter.services", "");
|
||||
if (v.length() > 0){
|
||||
for (String s : v.split(",")) {
|
||||
filter.add(SERVICE_TYPE.valueOf(s));
|
||||
}
|
||||
}
|
||||
v = values.getProperty("filter.excludes", "");
|
||||
if (v.length() > 0){
|
||||
for (String s : v.split(",")) {
|
||||
String[] st = s.split("\\|");
|
||||
Place place = market.get(st[0]);
|
||||
if (place != null) {
|
||||
Vendor vendor = place.get(st[1]);
|
||||
if (vendor != null) {
|
||||
filter.addExclude(vendor);
|
||||
} else {
|
||||
LOG.warn("Not found vendor {}", st[1]);
|
||||
}
|
||||
} else {
|
||||
LOG.warn("Not found place {}", st[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return filter;
|
||||
}
|
||||
|
||||
public void writeTo(Properties properties){
|
||||
properties.setProperty("filter.center", center != null ? center.getName() : "");
|
||||
properties.setProperty("filter.radius", String.valueOf(radius));
|
||||
properties.setProperty("filter.distance", String.valueOf(distance));
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
for (SERVICE_TYPE service: services) {
|
||||
if (s.length() > 0) s.append(",");
|
||||
s.append(service);
|
||||
}
|
||||
properties.setProperty("filter.services", s.toString());
|
||||
s = new StringBuilder();
|
||||
for (Vendor vendor : excludes) {
|
||||
if (s.length() > 0) s.append(",");
|
||||
s.append(vendor.getPlace().getName());
|
||||
s.append("|");
|
||||
s.append(vendor.getName());
|
||||
}
|
||||
properties.setProperty("filter.excludes", s.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{" +
|
||||
"center=" + center +
|
||||
", radius=" + radius +
|
||||
", distance=" + distance +
|
||||
", services=" + services +
|
||||
", excludes=" + excludes +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package ru.trader.core;
|
||||
import ru.trader.graph.Connectable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface Place extends Connectable<Place> {
|
||||
|
||||
@@ -15,6 +16,10 @@ public interface Place extends Connectable<Place> {
|
||||
void setPosition(double x, double y, double z);
|
||||
|
||||
Collection<Vendor> get();
|
||||
default Vendor get(String name){
|
||||
Optional<Vendor> vendor = get().stream().filter(p -> name.equals(p.getName())).findFirst();
|
||||
return vendor.isPresent() ? vendor.get() : null;
|
||||
}
|
||||
void add(Vendor vendor);
|
||||
Vendor addVendor(String name);
|
||||
void remove(Vendor vendor);
|
||||
|
||||
Reference in New Issue
Block a user