mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add resource.java to fix resourceNotFoundInJar
This commit is contained in:
parent
0a8cde17cd
commit
405e837615
6 changed files with 80 additions and 4 deletions
|
|
@ -20,8 +20,14 @@
|
|||
******************************************************************************/
|
||||
package algorithms;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import util.Resource;
|
||||
|
||||
|
||||
|
||||
import basics.VehicleRoutingAlgorithm;
|
||||
import basics.VehicleRoutingProblem;
|
||||
import basics.io.AlgorithmConfig;
|
||||
|
|
@ -57,9 +63,11 @@ public class SchrimpfFactory {
|
|||
*/
|
||||
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp){
|
||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||
URL resource = this.getClass().getClassLoader().getResource("schrimpf.xml");
|
||||
URL resource = Resource.getAsURL("schrimpf.xml");
|
||||
new AlgorithmConfigXmlReader(algorithmConfig).read(resource.getPath());
|
||||
return VehicleRoutingAlgorithms.createAlgorithm(vrp, algorithmConfig);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import java.util.Collection;
|
|||
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import util.Resource;
|
||||
import util.Solutions;
|
||||
import algorithms.VehicleRoutingAlgorithms;
|
||||
import basics.VehicleRoutingAlgorithm;
|
||||
|
|
@ -115,7 +116,7 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
|
|||
*/
|
||||
final double[] results = new double[nOfRandomWalks];
|
||||
|
||||
URL resource = this.getClass().getClassLoader().getResource("randomWalk.xml");
|
||||
URL resource = Resource.getAsURL("randomWalk.xml");
|
||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||
new AlgorithmConfigXmlReader(algorithmConfig).read(resource.getPath());
|
||||
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.createAlgorithm(problem, algorithmConfig);
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ import org.xml.sax.EntityResolver;
|
|||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import util.Resource;
|
||||
|
||||
public class AlgorithmConfigXmlReader {
|
||||
|
||||
private static Logger log = Logger.getLogger(AlgorithmConfigXmlReader.class);
|
||||
|
|
@ -55,7 +57,7 @@ public class AlgorithmConfigXmlReader {
|
|||
algorithmConfig.getXMLConfiguration().setDelimiterParsingDisabled(true);
|
||||
|
||||
if(schemaValidation){
|
||||
final URL resource = this.getClass().getClassLoader().getResource("algorithm_schema.xsd");
|
||||
final URL resource = Resource.getAsURL("algorithm_schema.xsd");
|
||||
if(resource != null) {
|
||||
EntityResolver resolver = new EntityResolver() {
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import org.xml.sax.InputSource;
|
|||
import org.xml.sax.SAXException;
|
||||
|
||||
import util.Coordinate;
|
||||
import util.Resource;
|
||||
import basics.Service;
|
||||
import basics.VehicleRoutingProblem;
|
||||
import basics.VehicleRoutingProblem.FleetComposition;
|
||||
|
|
@ -97,7 +98,7 @@ public class VrpXMLReader{
|
|||
xmlConfig.setDelimiterParsingDisabled(true);
|
||||
|
||||
if(schemaValidation){
|
||||
final URL resource = this.getClass().getClassLoader().getResource("vrp_xml_schema.xsd");
|
||||
final URL resource = Resource.getAsURL("vrp_xml_schema.xsd");
|
||||
if(resource != null) {
|
||||
EntityResolver resolver = new EntityResolver() {
|
||||
|
||||
|
|
|
|||
47
jsprit-core/src/main/java/util/Resource.java
Normal file
47
jsprit-core/src/main/java/util/Resource.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package util;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import algorithms.SchrimpfFactory;
|
||||
|
||||
/**
|
||||
* This is a copy of MatsimResource.java (see matsim.org).
|
||||
*
|
||||
* It makes sure that resources can also be located within jar files, since it looks for resources in several places.
|
||||
*
|
||||
* @author stefan schroeder
|
||||
*
|
||||
*/
|
||||
public class Resource {
|
||||
|
||||
private static Logger log = Logger.getLogger(Resource.class);
|
||||
|
||||
/**
|
||||
* Returns URL from the relative path of a resource.
|
||||
*
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
public final static URL getAsURL(final String filename) {
|
||||
// look for the file locally
|
||||
File file = new File("/" + filename);
|
||||
if (file.exists()) {
|
||||
try {
|
||||
return file.toURI().toURL();
|
||||
} catch (MalformedURLException e) {
|
||||
log.warn("Found resource-file, but could not return URL for it.", e); // just continue, maybe we have more luck in the classpath
|
||||
}
|
||||
}
|
||||
// maybe we find the file in the classpath, possibly inside a jar-file
|
||||
URL url = SchrimpfFactory.class.getResource("/" + filename);
|
||||
if (url == null) {
|
||||
log.warn("Resource '" + filename + "' not found!");
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue