Archived
0

add callbacks to crawler

This commit is contained in:
iMoHax
2015-07-31 10:58:01 +03:00
parent 40b0f66d8d
commit eb9f42ddb6
18 changed files with 180 additions and 297 deletions

View File

@@ -13,9 +13,15 @@ import java.util.*;
public class RouteSearcher {
private final static Logger LOG = LoggerFactory.getLogger(RouteSearcher.class);
private final Scorer scorer;
private final AnalysisCallBack callback;
public RouteSearcher(Scorer scorer) {
this(scorer, new AnalysisCallBack());
}
public RouteSearcher(Scorer scorer, AnalysisCallBack callback) {
this.scorer = scorer;
this.callback = callback;
}
public List<Edge<Place>> getPath(Place from, Place to, Collection<Place> places){
@@ -34,12 +40,12 @@ public class RouteSearcher {
private List<List<Edge<Place>>> search(Place source, Place target, Collection<Place> places, int count, RouteSpecification<Place> specification){
Profile profile = scorer.getProfile();
LOG.trace("Start search path from {} to {} ", source, target);
ConnectibleGraph<Place> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Place> graph = new ConnectibleGraph<>(profile, callback);
LOG.trace("Build connectible graph");
graph.build(source, places);
LOG.trace("Graph is builds");
List<List<Edge<Place>>> paths = new ArrayList<>();
Crawler<Place> crawler = specification != null ? new CCrawler<>(graph, specification, paths::add) : new CCrawler<>(graph, paths::add);
Crawler<Place> crawler = specification != null ? new CCrawler<>(graph, specification, paths::add, callback) : new CCrawler<>(graph, paths::add, callback);
crawler.setMaxSize(profile.getJumps());
if (profile.getPathPriority() == Profile.PATH_PRIORITY.FAST){
crawler.findFast(target, count);
@@ -78,12 +84,12 @@ public class RouteSearcher {
private List<Route> search(Vendor source, Vendor target, Collection<Vendor> vendors, int count, RouteSpecification<Vendor> specification){
LOG.trace("Start search route from {} to {}", source, target);
VendorsGraph vGraph = new VendorsGraph(scorer);
VendorsGraph vGraph = new VendorsGraph(scorer, callback);
LOG.trace("Build vendors graph");
vGraph.build(source, vendors);
LOG.trace("Graph is builds");
RouteCollector collector = new RouteCollector();
Crawler<Vendor> crawler = specification != null ? vGraph.crawler(specification, collector::add) : vGraph.crawler(collector::add);
Crawler<Vendor> crawler = specification != null ? vGraph.crawler(specification, collector::add, callback) : vGraph.crawler(collector::add, callback);
crawler.setMaxSize(scorer.getProfile().getLands());
crawler.findMin(target, count);
return collector.get();
@@ -96,11 +102,10 @@ public class RouteSearcher {
private class RouteCollector {
private List<Route> routes = new ArrayList<>();
public boolean add(List<Edge<Vendor>> edges){
public void add(List<Edge<Vendor>> edges){
Route route = toRoute(edges);
route.setBalance(scorer.getProfile().getBalance());
routes.add(route);
return true;
}
public List<Route> get() {

View File

@@ -7,7 +7,7 @@ import ru.trader.analysis.graph.*;
import ru.trader.core.*;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -17,22 +17,17 @@ public class VendorsGraph extends ConnectibleGraph<Vendor> {
private final Scorer scorer;
private final List<VendorsGraphBuilder> deferredTasks = new ArrayList<>();
public VendorsGraph(Scorer scorer) {
super(scorer.getProfile());
this.scorer = scorer;
}
public VendorsGraph(Scorer scorer, AnalysisCallBack callback) {
super(scorer.getProfile(), callback);
this.scorer = scorer;
}
public VendorsCrawler crawler(Predicate<List<Edge<Vendor>>> onFoundFunc){
return new VendorsCrawler(onFoundFunc);
public VendorsCrawler crawler(Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback){
return new VendorsCrawler(onFoundFunc, callback);
}
public VendorsCrawler crawler(RouteSpecification<Vendor> specification, Predicate<List<Edge<Vendor>>> onFoundFunc){
return new VendorsCrawler(specification, onFoundFunc);
public VendorsCrawler crawler(RouteSpecification<Vendor> specification, Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback){
return new VendorsCrawler(specification, onFoundFunc, callback);
}
@Override
@@ -61,6 +56,7 @@ public class VendorsGraph extends ConnectibleGraph<Vendor> {
private void runDeferredTasks(){
deferredTasks.sort((b1,b2) -> Integer.compare(b2.getDeep(), b1.getDeep()));
for (VendorsGraphBuilder task : deferredTasks) {
if (callback.isCancel()) break;
task.compute();
}
deferredTasks.clear();
@@ -154,6 +150,7 @@ public class VendorsGraph extends ConnectibleGraph<Vendor> {
VendorsGraphBuilder h = this;
Path<Vendor> path = new Path<>(Collections.singleton(lastEdge));
while (h != null){
if (callback.isCancel()) break;
BuildEdge cEdge = h.edge;
Vertex<Vendor> source = cEdge.getSource();
if (source.equals(vertex)){
@@ -204,8 +201,10 @@ public class VendorsGraph extends ConnectibleGraph<Vendor> {
int i = 1;
Path<Vendor> path = paths != null ? paths.get(0) : new Path<>(Collections.singleton((BuildEdge)lastEdge));
while (path != null){
if (callback.isCancel()) break;
VendorsGraphBuilder h = this;
while (h != null){
if (callback.isCancel()) break;
if (h.limit >= path.getMinFuel() && h.limit <= path.getMaxFuel()){
BuildEdge cEdge = h.edge;
Vertex<Vendor> source = cEdge.getSource();
@@ -464,14 +463,14 @@ public class VendorsGraph extends ConnectibleGraph<Vendor> {
private double startFuel;
private double startBalance;
protected VendorsCrawler(Predicate<List<Edge<Vendor>>> onFoundFunc) {
super(VendorsGraph.this, onFoundFunc);
protected VendorsCrawler(Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback) {
super(VendorsGraph.this, onFoundFunc, callback);
startFuel = getShip().getTank();
startBalance = getProfile().getBalance();
}
protected VendorsCrawler(RouteSpecification<Vendor> specification, Predicate<List<Edge<Vendor>>> onFoundFunc) {
super(VendorsGraph.this, specification, onFoundFunc);
protected VendorsCrawler(RouteSpecification<Vendor> specification, Consumer<List<Edge<Vendor>>> onFoundFunc, AnalysisCallBack callback) {
super(VendorsGraph.this, specification, onFoundFunc, callback);
startFuel = getShip().getTank();
startBalance = getProfile().getBalance();
}

View File

@@ -17,10 +17,6 @@ public abstract class AbstractGraph<T> implements Graph<T> {
protected int minJumps;
private final ReentrantLock lock = new ReentrantLock();
protected AbstractGraph() {
this(new AnalysisCallBack());
}
protected AbstractGraph(AnalysisCallBack callback) {
this.callback = new GraphCallBack(callback);
vertexes = new ArrayList<>();

View File

@@ -2,6 +2,7 @@ package ru.trader.analysis.graph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.analysis.AnalysisCallBack;
import ru.trader.analysis.RouteSpecification;
import ru.trader.core.Profile;
import ru.trader.core.Ship;
@@ -9,23 +10,26 @@ import ru.trader.graph.Connectable;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class CCrawler<T extends Connectable<T>> extends Crawler<T> {
private final static Logger LOG = LoggerFactory.getLogger(CCrawler.class);
private double startFuel;
public CCrawler(ConnectibleGraph<T> graph, Predicate<List<Edge<T>>> onFoundFunc) {
super(graph, onFoundFunc);
startFuel = getShip().getTank();
public CCrawler(ConnectibleGraph<T> graph, Consumer<List<Edge<T>>> onFoundFunc, AnalysisCallBack callback) {
super(graph, onFoundFunc, callback);
init();
}
public CCrawler(ConnectibleGraph<T> graph, RouteSpecification<T> specification, Predicate<List<Edge<T>>> onFoundFunc) {
super(graph, specification, onFoundFunc);
startFuel = getShip().getTank();
public CCrawler(ConnectibleGraph<T> graph, RouteSpecification<T> specification, Consumer<List<Edge<T>>> onFoundFunc, AnalysisCallBack callback) {
super(graph, specification, onFoundFunc, callback);
init();
}
private void init(){
startFuel = getShip().getTank();
}
protected Ship getShip(){
return ((ConnectibleGraph<T>)graph).getShip();

View File

@@ -14,11 +14,6 @@ public class ConnectibleGraph<T extends Connectable<T>> extends AbstractGraph<T>
protected final Profile profile;
public ConnectibleGraph(Profile profile) {
super();
this.profile = profile;
}
public ConnectibleGraph(Profile profile, AnalysisCallBack callback) {
super(callback);
this.profile = profile;

View File

@@ -3,13 +3,14 @@ package ru.trader.analysis.graph;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.analysis.AnalysisCallBack;
import ru.trader.analysis.LimitedQueue;
import ru.trader.analysis.RouteSpecification;
import java.util.*;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.function.Predicate;
import java.util.function.Consumer;
public class Crawler<T> {
private final static Logger LOG = LoggerFactory.getLogger(Crawler.class);
@@ -18,20 +19,23 @@ public class Crawler<T> {
private final static int SPLIT_SIZE = 3;
protected final Graph<T> graph;
private final Predicate<List<Edge<T>>> onFoundFunc;
protected final CrawlerCallBack callback;
private final Consumer<List<Edge<T>>> onFoundFunc;
private final RouteSpecification<T> specification;
private T target;
private int maxSize;
public Crawler(Graph<T> graph, Predicate<List<Edge<T>>> onFoundFunc) {
public Crawler(Graph<T> graph, Consumer<List<Edge<T>>> onFoundFunc, AnalysisCallBack callback) {
this.graph = graph;
this.callback = new CrawlerCallBack(callback);
maxSize = graph.getRoot().getLevel();
this.onFoundFunc = onFoundFunc;
this.specification = (edge, entry) -> isTarget(edge);
}
public Crawler(Graph<T> graph, RouteSpecification<T> specification, Predicate<List<Edge<T>>> onFoundFunc) {
public Crawler(Graph<T> graph, RouteSpecification<T> specification, Consumer<List<Edge<T>>> onFoundFunc, AnalysisCallBack callback) {
this.graph = graph;
this.callback = new CrawlerCallBack(callback);
maxSize = graph.getRoot().getLevel();
this.onFoundFunc = onFoundFunc;
this.specification = specification;
@@ -67,6 +71,12 @@ public class Crawler<T> {
return updateStates ? specification.updateSpecified(edge, head) : specification.specified(edge, head);
}
private void found(List<Edge<T>> res){
callback.found();
onFoundFunc.accept(res);
}
public int getMaxSize() {
return maxSize;
}
@@ -95,6 +105,7 @@ public class Crawler<T> {
}
private void findFast(Vertex<T> s, T target, int count){
callback.startSearch(s.getEntry(), target, count);
Optional<Vertex<T>> t = graph.getVertex(target);
int found = 0;
if (t.isPresent()) {
@@ -107,6 +118,7 @@ public class Crawler<T> {
found = dfs(start(s), t.get().getLevel() + 1, count);
}
}
callback.endSearch();
LOG.debug("Found {} paths", found);
setTarget(null);
}
@@ -131,6 +143,7 @@ public class Crawler<T> {
}
public void findMin(Vertex<T> s, T target, int count){
callback.startSearch(s.getEntry(), target, count);
Optional<Vertex<T>> t = graph.getVertex(target);
int found = 0;
if (t.isPresent()) {
@@ -144,6 +157,7 @@ public class Crawler<T> {
}
}
LOG.debug("Found {} paths", found);
callback.endSearch();
setTarget(null);
}
@@ -159,24 +173,23 @@ public class Crawler<T> {
int found = 0;
Vertex<T> source = entry.vertex;
LOG.trace("DFS from {} to {}, deep {}, count {}, entry {}", source, target, deep, count, entry);
boolean stop = false;
if (deep == source.getLevel()){
for (Edge<T> next : entry.getEdges()) {
if (isFound(next, entry, true)){
List<Edge<T>> res = getCopyList(entry, next);
LOG.debug("Last edge found, path {}", res);
found++;
if (!onFoundFunc.test(res)){
stop = true;
}
found(res);
break;
}
if (callback.isCancel()) break;
}
}
if (!stop && found < count){
if (found < count){
if (deep < source.getLevel() && entry.size() < maxSize-1) {
LOG.trace("Search around");
for (Edge<T> edge : entry.getEdges()) {
if (callback.isCancel()) break;
if (entry.isSkipped()){
LOG.trace("Is skipped");
break;
@@ -197,6 +210,7 @@ public class Crawler<T> {
root.sort();
queue.add(root);
while (!queue.isEmpty() && count > found){
if (callback.isCancel()) break;
CostTraversalEntry entry = queue.poll();
if (entry.isSkipped()){
LOG.trace("Is skipped");
@@ -210,14 +224,13 @@ public class Crawler<T> {
LOG.trace("Search from {} to {}, entry {}", source, target, entry);
Iterator<Edge<T>> iterator = entry.iterator();
while (iterator.hasNext()){
if (callback.isCancel()) break;
Edge<T> edge = iterator.next();
if (isFound(edge, entry, true)){
List<Edge<T>> res = getCopyList(entry, edge);
LOG.debug("Last edge found, path {}", res);
found++;
if (!onFoundFunc.test(res)){
break;
}
found(res);
}
if (entry.isSkipped()){
LOG.trace("Is skipped");
@@ -241,6 +254,7 @@ public class Crawler<T> {
PriorityQueue<CostTraversalEntry> queue = new PriorityQueue<>();
queue.add(root);
while (!queue.isEmpty() && count > found){
if (callback.isCancel()) break;
CostTraversalEntry entry = queue.poll();
if (entry.isSkipped()){
LOG.trace("Is skipped");
@@ -253,9 +267,7 @@ public class Crawler<T> {
List<Edge<T>> res = entry.toEdges();
LOG.debug("Path found {}", res);
found++;
if (!onFoundFunc.test(res)){
break;
}
found(res);
if (found >= count) break;
}
if (entry.isSkipped()){
@@ -272,6 +284,7 @@ public class Crawler<T> {
}
Iterator<Edge<T>> iterator = entry.iterator();
while (iterator.hasNext()){
if (callback.isCancel()) break;
edge = iterator.next();
boolean canDeep = !entry.getTarget().isSingle() && deep < entry.getTarget().getLevel();
if (canDeep || isFound(edge, entry)){
@@ -292,6 +305,7 @@ public class Crawler<T> {
root.sort();
queue.add(new CTEntrySupport(root));
while (!(queue.isEmpty() && targetsQueue.isEmpty()) && count > found){
if (callback.isCancel()) break;
int alreadyFound = targetsQueue.size();
CTEntrySupport curr = targetsQueue.peek();
boolean isTarget = curr != null && (queue.isEmpty() || alreadyFound + found >= count || Comparator.<CTEntrySupport>naturalOrder().compare(curr, queue.peek()) <= 0);
@@ -306,9 +320,7 @@ public class Crawler<T> {
List<Edge<T>> res = entry.toEdges();
LOG.trace("Path found {}", res);
found++;
if (!onFoundFunc.test(res)){
break;
}
found(res);
if (found >= count) break;
CTEntrySupport next = targetsQueue.peek();
limit = next != null ? next.entry.getWeight() : Double.NaN;
@@ -437,7 +449,7 @@ public class Crawler<T> {
}
private boolean cancel(){
if (isCancelled()) return true;
if (isCancelled() || callback.isCancel()) return true;
if (root.entry.isSkipped()){
LOG.trace("Root skipped");
if (isSubTask){

View File

@@ -0,0 +1,30 @@
package ru.trader.analysis.graph;
import ru.trader.analysis.AnalysisCallBack;
public class CrawlerCallBack {
private final AnalysisCallBack parent;
public final String SEARCH_STAGE = "crawler.stage.search";
public CrawlerCallBack(AnalysisCallBack parent) {
this.parent = parent;
}
public void startSearch(Object from, Object to, int count){
parent.startStage(SEARCH_STAGE);
parent.print(String.format(parent.getMessage(SEARCH_STAGE), from.toString(), to.toString()));
parent.setMax(count);
}
public void found(){
parent.inc();
}
public void endSearch(){
parent.endStage(SEARCH_STAGE);
}
public boolean isCancel(){
return parent.isCancel();
}
}

View File

@@ -21,18 +21,14 @@ public class MarketAnalyzer {
private final static Comparator<Order> orderComparator = (o1, o2) -> o2.compareTo(o1);
public MarketAnalyzer(FilteredMarket market, Profile profile) {
this(market, profile, new MarketAnalyzerCallBack());
this(market, profile, new AnalysisCallBack());
}
public MarketAnalyzer(FilteredMarket market, Profile profile, MarketAnalyzerCallBack callback) {
public MarketAnalyzer(FilteredMarket market, Profile profile, AnalysisCallBack callback) {
this.market = market;
this.callback = callback;
this.callback = new MarketAnalyzerCallBack(callback);
this.profile = profile;
this.searcher = new RouteSearcher(new Scorer(market, profile));
}
public void setCallback(MarketAnalyzerCallBack callback) {
this.callback = callback;
this.searcher = new RouteSearcher(new Scorer(market, profile), callback);
}
public Profile getProfile() {
@@ -51,7 +47,7 @@ public class MarketAnalyzer {
LOG.debug("Get top {}", limit);
Collection<Place> places = getPlaces();
LimitedQueue<Order> top = new LimitedQueue<>(limit, orderComparator);
callback.setMax(places.size());
callback.start(places.size());
for (Place place : places) {
if (callback.isCancel()) break;
LOG.trace("Check place {}", place);
@@ -59,7 +55,7 @@ public class MarketAnalyzer {
top.addAll(orders);
callback.inc();
}
callback.onEnd();
callback.end();
return top;
}
@@ -105,10 +101,10 @@ public class MarketAnalyzer {
}
private Collection<Order> getOrders(Place place, Collection<Vendor> sellers, double lowProfit) {
ConnectibleGraph<Place> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Place> graph = new ConnectibleGraph<>(profile, callback.getParent());
graph.build(place, getPlaces());
List<Order> res = new ArrayList<>(20);
callback.setMax(sellers.size());
callback.start(sellers.size());
for (Vendor vendor : sellers) {
if (callback.isCancel()) break;
for (Offer sell : vendor.getAllSellOffers()) {
@@ -139,12 +135,13 @@ public class MarketAnalyzer {
callback.inc();
}
res.sort(orderComparator);
callback.end();
return res;
}
private Collection<Order> getOrders(Collection<Vendor> sellers, Collection<Vendor> buyers, double lowProfit) {
List<Order> res = new ArrayList<>();
callback.setMax(sellers.size());
callback.start(sellers.size());
for (Vendor seller : sellers) {
if (callback.isCancel()) break;
for (Offer sell : seller.getAllSellOffers()) {
@@ -170,6 +167,7 @@ public class MarketAnalyzer {
callback.inc();
}
res.sort(orderComparator);
callback.end();
return res;
}
@@ -189,7 +187,7 @@ public class MarketAnalyzer {
LOG.debug("Get top {}", limit);
LimitedQueue<Route> top = new LimitedQueue<>(limit);
Collection<Vendor> vendors = getVendors();
callback.setMax(vendors.size());
callback.start(vendors.size());
Iterator<Vendor> iterator = market.getMarkets(false).iterator();
while (iterator.hasNext()){
Vendor vendor = iterator.next();
@@ -198,7 +196,7 @@ public class MarketAnalyzer {
top.addAll(paths);
callback.inc();
}
callback.onEndSearch();
callback.end();
return top;
}
@@ -233,7 +231,7 @@ public class MarketAnalyzer {
public Route getRoute(Collection<Vendor> vendors) {
Route res = null;
callback.setMax(vendors.size());
callback.start(vendors.size());
for (Vendor from : vendors) {
//TODO: implement search with constant length
Collection<Route> paths = searcher.getRoutes(from, vendors);
@@ -243,12 +241,12 @@ public class MarketAnalyzer {
}
callback.inc();
}
callback.onEndSearch();
callback.end();
return res;
}
private boolean isInaccessible(Place from, Place to){
ConnectibleGraph<Place> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Place> graph = new ConnectibleGraph<>(profile, callback.getParent());
graph.build(from, getPlaces());
if (!graph.isAccessible(to)){
LOG.trace("Is inaccessible buyer");
@@ -268,4 +266,8 @@ public class MarketAnalyzer {
private List<Vendor> getVendors(Place place){
return market.getVendors(place).collect(Collectors.toList());
}
public MarketAnalyzer changeCallBack(AnalysisCallBack callback){
return new MarketAnalyzer(market, profile, callback);
}
}

View File

@@ -1,62 +1,34 @@
package ru.trader.core;
import ru.trader.graph.GraphCallBack;
import ru.trader.graph.RouteSearcherCallBack;
import ru.trader.analysis.AnalysisCallBack;
public class MarketAnalyzerCallBack {
private volatile boolean cancel = false;
private RouteSearcherCallBack callbackRoute;
private GraphCallBack<Place> callbackGraph;
private final AnalysisCallBack parent;
public final String ANALYSIS_STAGE = "market.stage.analysis";
protected RouteSearcherCallBack getRouteSearcherCallBackInstance(){
return new RouteSearcherCallBack();
public MarketAnalyzerCallBack(AnalysisCallBack parent) {
this.parent = parent;
}
protected GraphCallBack<Place> getGraphCallBackInstance(){
return new GraphCallBack<>();
public void start(int count){
parent.startStage(ANALYSIS_STAGE);
parent.print(String.format(parent.getMessage(ANALYSIS_STAGE)));
parent.setMax(count);
}
public final GraphCallBack<Place> onStartGraph(){
callbackGraph = getGraphCallBackInstance();
return callbackGraph;
public void inc(){
parent.inc();
}
public final void onEndGraph(){
callbackGraph = null;
public void end(){
parent.endStage(ANALYSIS_STAGE);
}
public final RouteSearcherCallBack onStartSearch(){
callbackRoute = getRouteSearcherCallBackInstance();
return callbackRoute;
public boolean isCancel(){
return parent.isCancel();
}
public final void onEndSearch(){
callbackRoute = null;
onEnd();
public AnalysisCallBack getParent() {
return parent;
}
protected void onEnd(){}
public void setMax(long max){}
public void inc(){}
public final boolean isCancel() {
return cancel;
}
public final void cancel(){
if (cancel) return;
this.cancel = true;
if (callbackRoute != null){
callbackRoute.cancel();
callbackRoute = null;
}
if (callbackGraph != null){
callbackGraph.cancel();
callbackGraph = null;
}
}
}

View File

@@ -1,29 +0,0 @@
package ru.trader.graph;
import ru.trader.analysis.graph.Vertex;
public class GraphCallBack<T extends Connectable<T>> {
private volatile boolean cancel = false;
public void onStartBuild(T from){}
public void onEndBuild(){}
public void onStartFind(Vertex<T> from, Vertex<T> to){}
public void onFound(){}
public void onEndFind(){}
public void setMax(long count){}
public void inc(){}
public final boolean isCancel() {
return cancel;
}
public final void cancel(){
this.cancel = true;
}
}

View File

@@ -1,44 +0,0 @@
package ru.trader.graph;
import ru.trader.core.Vendor;
import java.util.LinkedList;
import java.util.List;
public class RouteSearcherCallBack {
private volatile boolean cancel = false;
private final List<GraphCallBack<Vendor>> callbacks = new LinkedList<>();
public final GraphCallBack<Vendor> onStart(){
GraphCallBack<Vendor> callback = getGraphCallBackInstance();
if (cancel) return callback;
synchronized (callbacks) {
callbacks.add(callback);
}
return callback;
}
public final void onEnd( GraphCallBack<Vendor> callback){
synchronized (callbacks) {
callbacks.remove(callback);
}
}
protected GraphCallBack<Vendor> getGraphCallBackInstance(){
return new GraphCallBack<>();
}
public final boolean isCancel() {
return cancel;
}
public final void cancel(){
if (cancel) return;
this.cancel = true;
synchronized (callbacks){
callbacks.forEach(GraphCallBack::cancel);
callbacks.clear();
}
}
}

View File

@@ -58,11 +58,11 @@ public class VendorsGraphTest extends Assert {
profile.setBalance(100000); profile.setJumps(6);
Scorer scorer = new Scorer(fWorld, profile);
LOG.info("Build vendors graph");
VendorsGraph vGraph = new VendorsGraph(scorer);
VendorsGraph vGraph = new VendorsGraph(scorer, new AnalysisCallBack());
vGraph.build(cabreraDock, fWorld.getMarkets(true).collect(Collectors.toList()));
LOG.info("Search");
SimpleCollector<Vendor> paths = new SimpleCollector<>();
Crawler<Vendor> crawler = vGraph.crawler(paths::add);
Crawler<Vendor> crawler = vGraph.crawler(paths::add, new AnalysisCallBack());
// Cabrera Dock -> Transit Wolf 1323 -> Transit Wolf 1325 -> Quimper Ring -> Transit Bhadaba -> Transit Wolf 1325 -> Cabrera Dock]
crawler.findMin(cabreraDock, 100);
assertEquals(100, paths.get().size());
@@ -189,7 +189,7 @@ public class VendorsGraphTest extends Assert {
LOG.info("Start build test");
profile.setBalance(100000); profile.setJumps(6);
LOG.info("Build connectible graph");
ConnectibleGraph<Vendor> vGraph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Vendor> vGraph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
vGraph.build(cabreraDock, fWorld.getMarkets(true).collect(Collectors.toList()));
for (Vertex<Vendor> vertex : vGraph.vertexes()) {
assertEquals(edgesStat.get(vertex.getEntry().toString()).intValue(), vertex.getEdges().size());
@@ -197,7 +197,7 @@ public class VendorsGraphTest extends Assert {
LOG.info("Build vendors graph");
Scorer scorer = new Scorer(fWorld, profile);
vGraph = new VendorsGraph(scorer);
vGraph = new VendorsGraph(scorer, new AnalysisCallBack());
vGraph.build(cabreraDock, fWorld.getMarkets(true).collect(Collectors.toList()));
for (Vertex<Vendor> vertex : vGraph.vertexes()) {
switch (vertex.getEntry().getName()){
@@ -224,11 +224,11 @@ public class VendorsGraphTest extends Assert {
profile.setBalance(100000); profile.setJumps(6);
Scorer scorer = new Scorer(fWorld, profile);
LOG.info("Build vendors graph");
VendorsGraph vGraph = new VendorsGraph(scorer);
VendorsGraph vGraph = new VendorsGraph(scorer, new AnalysisCallBack());
vGraph.build(cabreraDock, fWorld.getMarkets(true).collect(Collectors.toList()));
LOG.info("Search");
SimpleCollector<Vendor> paths = new SimpleCollector<>();
Crawler<Vendor> crawler = vGraph.crawler(new LoopRouteSpecification<>(true), paths::add);
Crawler<Vendor> crawler = vGraph.crawler(new LoopRouteSpecification<>(true), paths::add, new AnalysisCallBack());
crawler.findMin(cabreraDock, 100);
assertEquals(60, paths.get().size());
Collection<Vendor> vendors = new ArrayList<>(60);

View File

@@ -7,6 +7,7 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.TestUtil;
import ru.trader.analysis.AnalysisCallBack;
import ru.trader.core.Profile;
import ru.trader.core.Ship;
@@ -68,12 +69,12 @@ public class CrawlerTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(2); profile.setRefill(false);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4, x5 <-> x6
SimpleCollector<Point> paths = new SimpleCollector<>();
Crawler<Point> crawler = new CCrawler<>(graph, paths::add);
Crawler<Point> crawler = new CCrawler<>(graph, paths::add, new AnalysisCallBack());
crawler.findMin(x4, 10);
TestUtil.assertPaths(paths.get(), PPath.of(x5, x4));
paths.clear();
@@ -97,12 +98,12 @@ public class CrawlerTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(3); profile.setRefill(false);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <-> x3 <-> x2, x5 <-> x6 <-> x7 <-> x8
// x5 <-> x3, x4 <-> x2, x3 <-> x6, x4 <-> x6
SimpleCollector<Point> paths = new SimpleCollector<>();
Crawler<Point> crawler = new CCrawler<>(graph, paths::add);
Crawler<Point> crawler = new CCrawler<>(graph, paths::add, new AnalysisCallBack());
crawler.findMin(x8, 10);
TestUtil.assertPaths(paths.get(), PPath.of(x5, x6, x7, x8));
@@ -161,12 +162,12 @@ public class CrawlerTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(3); profile.setRefill(false);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <- refill -> x3 <- refill -> x2, x5 <-> x6
// x5 <-> x3 <- refill -> x2, x5 <-> x4 <- refill -> x6
SimpleCollector<Point> paths = new SimpleCollector<>();
Crawler<Point> crawler = new CCrawler<>(graph, paths::add);
Crawler<Point> crawler = new CCrawler<>(graph, paths::add, new AnalysisCallBack());
crawler.findMin(x1, 10);
assertTrue(paths.get().isEmpty());
@@ -202,14 +203,14 @@ public class CrawlerTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(4);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <-> x3 - refill -> x2,
// x5 <-> x6 <-> x4 <-refill -> x2
// x5 <-> x3 <- refill -> x2
// x5 <-> x4 <- refill -> x6
SimpleCollector<Point> paths = new SimpleCollector<>();
Crawler<Point> crawler = new CCrawler<>(graph, paths::add);
Crawler<Point> crawler = new CCrawler<>(graph, paths::add, new AnalysisCallBack());
crawler.findMin(x1, 10);
assertTrue(paths.get().isEmpty());
@@ -255,14 +256,14 @@ public class CrawlerTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(4);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <-> x3 - refill -> x2,
// x5 <-> x6 <-> x4 <-refill -> x2
// x5 <-> x3 <- refill -> x2
// x5 <-> x4 <- refill -> x6
SimpleCollector<Point> paths = new SimpleCollector<>();
CCrawler<Point> crawler = new CCrawler<>(graph, paths::add);
CCrawler<Point> crawler = new CCrawler<>(graph, paths::add, new AnalysisCallBack());
crawler.setStartFuel(0.3);
crawler.findMin(x3, x2);

View File

@@ -6,6 +6,7 @@ import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.trader.analysis.AnalysisCallBack;
import ru.trader.core.Profile;
import ru.trader.core.Ship;
@@ -51,7 +52,7 @@ public class GraphTest extends Assert {
profile.setJumps(10);
profile.setRefill(false);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5
assertFalse(graph.isAccessible(x1));
@@ -76,7 +77,7 @@ public class GraphTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(2);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <-refill-> x3, x5 -> x6
assertFalse(graph.isAccessible(x1));
@@ -121,7 +122,7 @@ public class GraphTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(3);profile.setRefill(false);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <-> x3, x5 <-> x6
assertFalse(graph.isAccessible(x1));
@@ -158,7 +159,7 @@ public class GraphTest extends Assert {
Profile profile = new Profile(ship);
profile.setJumps(3); profile.setRefill(false);
LOG.info("Ship = {}, Jumps = {}", profile.getShip(), profile.getJumps());
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile);
ConnectibleGraph<Point> graph = new ConnectibleGraph<>(profile, new AnalysisCallBack());
graph.build(x5, entrys);
// x5 <-> x4 <-> x3 -> x2, x5 <-> x6 <-> x7 -> x8
// x5 <-> x3, x5 <-> x4 <-> x2, x3 <-> x6, x4 <-> x6

View File

@@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
@@ -12,9 +13,8 @@ import java.util.stream.Collectors;
public class SimpleCollector<T> {
private List<List<Edge<T>>> paths = new ArrayList<>();
public boolean add(List<Edge<T>> path){
public void add(List<Edge<T>> path){
paths.add(path);
return true;
}
public List<List<Edge<T>>> get() {

View File

@@ -4,4 +4,4 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p: %d{dd.MM.yyyy HH:mm:ss} (%F:%L) - %m%n
#log4j.logger.ru.trader.analysis.graph = TRACE
log4j.logger.ru.trader.analysis.VendorsGraph = DEBUG