List of usage examples for org.apache.commons.collections15 ListUtils union
public static <E> List<E> union(final List<? extends E> list1, final List<? extends E> list2)
From source file:mulavito.algorithms.shortestpath.disjoint.SuurballeTarjan.java
/** * Combines two disjoint paths from two SuurballeTarjan input paths. * /*from ww w . j a va 2 s. c o m*/ * @param path1 * Dijkstra shortest path * @param path2 * Dijkstra shortest path in partly reverted graph * @return the two disjoint paths */ private List<List<E>> findTwoWays(List<E> path1, List<E> path2) { final V source = graph.getSource(path1.get(0)); final V target = graph.getDest(path1.get(path1.size() - 1)); // Remove common links. Iterator<E> it1 = path1.iterator(); while (it1.hasNext()) { E e1 = it1.next(); Iterator<E> it2 = path2.iterator(); while (it2.hasNext()) { E e2 = it2.next(); // ensure disjointness if (comparator.compare(e1, e2) == 0) { // for multigraph if (graph.isSource(source, e1) || graph.isSource(source, e2) || graph.isDest(target, e1) || graph.isDest(target, e2)) return null; // Removing required edge it1.remove(); it2.remove(); break; // inner loop } } } if (path1.isEmpty() || path2.isEmpty()) return null; // no disjoint solution found // Now recombine the two paths. List<E> union = ListUtils.union(path1, path2); // concatenate List<E> p1 = recombinePaths(path1, target, union); if (p1 == null) return null; List<E> p2 = recombinePaths(path2, target, union); if (p2 == null) return null; if (!union.isEmpty()) throw new AssertionError("BUG"); List<List<E>> solution = new LinkedList<List<E>>(); solution.add(p1); solution.add(p2); return solution; }
From source file:org.dllearner.algorithms.qtl.experiments.PRConvergenceExperiment.java
private ExampleCandidates generateExamples(String sparqlQuery, boolean posOnly, boolean noiseEnabled) throws Exception { logger.info("Generating examples for query ..." + sparqlQuery); // generate hash for the query String queryHash = hash(sparqlQuery); // create examples folder File examplesDirectory = new File(cacheDirectory, "examples"); examplesDirectory.mkdirs();/*from w w w . ja va2 s .c o m*/ // create sub-folder for the query examplesDirectory = new File(examplesDirectory, queryHash); examplesDirectory.mkdirs(); // get all pos. examples, i.e. resources returned by the query List<String> posExamples; File file = new File(examplesDirectory, "examples.tp"); if (file.exists()) { posExamples = Files.readLines(file, Charsets.UTF_8); } else { posExamples = getResult(sparqlQuery, false); Files.write(Joiner.on("\n").join(posExamples), file, Charsets.UTF_8); } Collections.sort(posExamples); logger.info("#Pos. examples: " + posExamples.size()); List<String> negExamples = new ArrayList<>(); List<String> noiseCandidates = new ArrayList<>(); if (!posOnly) { // get some neg. examples, i.e. resources not returned by the query int maxNrOfNegExamples = 100; file = new File(examplesDirectory, "examples-" + maxNrOfNegExamples + ".tn"); if (file.exists()) { negExamples = Files.readLines(file, Charsets.UTF_8); } else { negExamples = new NegativeExampleSPARQLQueryGenerator(qef).getNegativeExamples(sparqlQuery, maxNrOfNegExamples); Files.write(Joiner.on("\n").join(negExamples), file, Charsets.UTF_8); } Collections.sort(negExamples); logger.info("#Neg. examples: " + negExamples.size()); if (noiseEnabled) { // get some noise candidates, i.e. resources used as false pos. examples int maxNrOfNoiseCandidates = 100; file = new File(examplesDirectory, "examples-" + maxNrOfNoiseCandidates + ".fp"); if (file.exists()) { noiseCandidates = Files.readLines(file, Charsets.UTF_8); } else { noiseCandidates = noiseGenerator.generateNoiseCandidates(sparqlQuery, noiseMethod, ListUtils.union(posExamples, negExamples), maxNrOfNoiseCandidates); Files.write(Joiner.on("\n").join(noiseCandidates), file, Charsets.UTF_8); } logger.info("#False pos. example candidates: " + noiseCandidates.size()); } } return new ExampleCandidates(posExamples, negExamples, noiseCandidates); }
From source file:org.dllearner.algorithms.qtl.experiments.QTLEvaluation.java
private ExampleCandidates generateExamples(String sparqlQuery) throws Exception { logger.info("Generating examples for query ..." + sparqlQuery); // generate hash for the query String queryHash = hash(sparqlQuery); // create examples folder File examplesDirectory = new File(cacheDirectory, "examples"); examplesDirectory.mkdirs();/* w w w . jav a 2 s . com*/ // create sub-folder for the query examplesDirectory = new File(examplesDirectory, queryHash); examplesDirectory.mkdirs(); // get all pos. examples, i.e. resources returned by the query List<String> posExamples; File file = new File(examplesDirectory, "examples.tp"); if (file.exists()) { posExamples = Files.readLines(file, Charsets.UTF_8); } else { posExamples = getResult(sparqlQuery, false); Files.write(Joiner.on("\n").join(posExamples), file, Charsets.UTF_8); } Collections.sort(posExamples); logger.info("#Pos. examples: " + posExamples.size()); // get some neg. examples, i.e. resources not returned by the query int maxNrOfNegExamples = 100; List<String> negExamples; file = new File(examplesDirectory, "examples-" + maxNrOfNegExamples + ".tn"); if (file.exists()) { negExamples = Files.readLines(file, Charsets.UTF_8); } else { negExamples = new NegativeExampleSPARQLQueryGenerator(qef).getNegativeExamples(sparqlQuery, maxNrOfNegExamples); Files.write(Joiner.on("\n").join(negExamples), file, Charsets.UTF_8); } Collections.sort(negExamples); logger.info("#Neg. examples: " + negExamples.size()); // get some noise candidates, i.e. resources used as false pos. examples int maxNrOfNoiseCandidates = 100; List<String> noiseCandidates; file = new File(examplesDirectory, "examples-" + maxNrOfNoiseCandidates + ".fp"); if (file.exists()) { noiseCandidates = Files.readLines(file, Charsets.UTF_8); } else { noiseCandidates = generateNoiseCandidates(sparqlQuery, noiseMethod, ListUtils.union(posExamples, negExamples), maxNrOfNoiseCandidates); Files.write(Joiner.on("\n").join(noiseCandidates), file, Charsets.UTF_8); } logger.info("#False pos. example candidates: " + noiseCandidates.size()); return new ExampleCandidates(posExamples, negExamples, noiseCandidates); }
From source file:org.dllearner.algorithms.qtl.QTL.java
private List<String> getKnownResources() { return ListUtils.union(posExamples, negExamples); }
From source file:org.opendaylight.nic.of.renderer.algorithm.SuurballeAlgorithm.java
public final List<List<E>> findShortestPath(final V initial, final V destination) { final List<E> shortestPath = dijkstraAlgorithm.getPath(initial, destination); initialCostMap = dijkstraAlgorithm.getDistanceMap(initial); final List<E> reversedShortestPath = reverseUpdateEdgesWeight(graph, MapTransformer.getInstance(initialCostMap), shortestPath, initial, destination); discardCommonReversedEdges(graph, shortestPath, reversedShortestPath); final List<E> unitedPaths = ListUtils.union(shortestPath, reversedShortestPath); final List<E> resultPath1 = restorePaths(shortestPath, destination, unitedPaths); final List<E> resultPath2 = restorePaths(reversedShortestPath, destination, unitedPaths); List<List<E>> result = mergePaths(resultPath1, resultPath2); if ((result == null) || (result.size() == 0)) { result = new ArrayList<>(); result.add(shortestPath);// w w w . j av a 2 s .co m } return result; }
From source file:org.opendaylight.topoprocessing.utils.SuurballeAlgorithm.java
/** * Concatenates two disjoint paths from two SuurballeTarjan input paths. * * @param path1 Dijkstra shortest path//from www .j a v a 2s. c o m * @param path2 Dijkstra shortest path in partly reverted graph * @return the two disjoint paths */ private List<List<E>> getDisjointPaths(List<E> path1, List<E> path2) { final V source = graph.getSource(path1.get(0)); final V target = graph.getDest(path1.get(path1.size() - 1)); // Remove common links Iterator<E> path1_iterator = path1.iterator(); while (path1_iterator.hasNext()) { E edge1 = path1_iterator.next(); Iterator<E> path2_iterator = path2.iterator(); while (path2_iterator.hasNext()) { E edge2 = path2_iterator.next(); if (edge1.equals(edge2)) { if (graph.isSource(source, edge1) || graph.isSource(source, edge2) || graph.isDest(target, edge1) || graph.isDest(target, edge2)) return null; path1_iterator.remove(); path2_iterator.remove(); break; } } } // No disjoint path found if (path1.isEmpty() || path2.isEmpty()) { return null; } // Concatenate the two paths List<E> union = ListUtils.union(path1, path2); List<E> pathh1 = mergePaths(path1, target, union); if (pathh1 == null) { return null; } List<E> pathh2 = mergePaths(path2, target, union); if (pathh2 == null) { return null; } List<List<E>> solution = new LinkedList<List<E>>(); double path1_cost = 0; for (E edge : pathh1) { path1_cost += nev.transform(edge); } double path2_cost = 0; for (E edge : pathh2) { path2_cost += nev.transform(edge); } if (path1_cost <= path2_cost) { solution.add(pathh1); solution.add(pathh2); } else { solution.add(pathh2); solution.add(pathh1); } return solution; }