1
0
Fork 0
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:
Stefan Schroeder 2013-06-06 07:41:45 +02:00
parent 0a8cde17cd
commit 405e837615
6 changed files with 80 additions and 4 deletions

View file

@ -20,8 +20,14 @@
******************************************************************************/ ******************************************************************************/
package algorithms; package algorithms;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import util.Resource;
import basics.VehicleRoutingAlgorithm; import basics.VehicleRoutingAlgorithm;
import basics.VehicleRoutingProblem; import basics.VehicleRoutingProblem;
import basics.io.AlgorithmConfig; import basics.io.AlgorithmConfig;
@ -57,9 +63,11 @@ public class SchrimpfFactory {
*/ */
public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp){ public VehicleRoutingAlgorithm createAlgorithm(VehicleRoutingProblem vrp){
AlgorithmConfig algorithmConfig = new AlgorithmConfig(); AlgorithmConfig algorithmConfig = new AlgorithmConfig();
URL resource = this.getClass().getClassLoader().getResource("schrimpf.xml"); URL resource = Resource.getAsURL("schrimpf.xml");
new AlgorithmConfigXmlReader(algorithmConfig).read(resource.getPath()); new AlgorithmConfigXmlReader(algorithmConfig).read(resource.getPath());
return VehicleRoutingAlgorithms.createAlgorithm(vrp, algorithmConfig); return VehicleRoutingAlgorithms.createAlgorithm(vrp, algorithmConfig);
} }
} }

View file

@ -26,6 +26,7 @@ import java.util.Collection;
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import util.Resource;
import util.Solutions; import util.Solutions;
import algorithms.VehicleRoutingAlgorithms; import algorithms.VehicleRoutingAlgorithms;
import basics.VehicleRoutingAlgorithm; import basics.VehicleRoutingAlgorithm;
@ -115,7 +116,7 @@ public class SchrimpfAcceptance implements SolutionAcceptor, IterationStartsList
*/ */
final double[] results = new double[nOfRandomWalks]; final double[] results = new double[nOfRandomWalks];
URL resource = this.getClass().getClassLoader().getResource("randomWalk.xml"); URL resource = Resource.getAsURL("randomWalk.xml");
AlgorithmConfig algorithmConfig = new AlgorithmConfig(); AlgorithmConfig algorithmConfig = new AlgorithmConfig();
new AlgorithmConfigXmlReader(algorithmConfig).read(resource.getPath()); new AlgorithmConfigXmlReader(algorithmConfig).read(resource.getPath());
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.createAlgorithm(problem, algorithmConfig); VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.createAlgorithm(problem, algorithmConfig);

View file

@ -29,6 +29,8 @@ import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import util.Resource;
public class AlgorithmConfigXmlReader { public class AlgorithmConfigXmlReader {
private static Logger log = Logger.getLogger(AlgorithmConfigXmlReader.class); private static Logger log = Logger.getLogger(AlgorithmConfigXmlReader.class);
@ -55,7 +57,7 @@ public class AlgorithmConfigXmlReader {
algorithmConfig.getXMLConfiguration().setDelimiterParsingDisabled(true); algorithmConfig.getXMLConfiguration().setDelimiterParsingDisabled(true);
if(schemaValidation){ if(schemaValidation){
final URL resource = this.getClass().getClassLoader().getResource("algorithm_schema.xsd"); final URL resource = Resource.getAsURL("algorithm_schema.xsd");
if(resource != null) { if(resource != null) {
EntityResolver resolver = new EntityResolver() { EntityResolver resolver = new EntityResolver() {

View file

@ -37,6 +37,7 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import util.Coordinate; import util.Coordinate;
import util.Resource;
import basics.Service; import basics.Service;
import basics.VehicleRoutingProblem; import basics.VehicleRoutingProblem;
import basics.VehicleRoutingProblem.FleetComposition; import basics.VehicleRoutingProblem.FleetComposition;
@ -97,7 +98,7 @@ public class VrpXMLReader{
xmlConfig.setDelimiterParsingDisabled(true); xmlConfig.setDelimiterParsingDisabled(true);
if(schemaValidation){ 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) { if(resource != null) {
EntityResolver resolver = new EntityResolver() { EntityResolver resolver = new EntityResolver() {

View 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;
}
}

View file

@ -0,0 +1,17 @@
package algorithms;
import org.junit.Test;
import basics.VehicleRoutingAlgorithm;
import basics.VehicleRoutingProblem;
public class TestSchrimpf {
@Test
public void whenUsingSchrimpfFactory_itFindsTheConfig(){
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(VehicleRoutingProblem.newBuilderInstance().build());
}
}