forked from apache/lucenenet
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4ef219
commit 13343c5
Showing
5 changed files
with
342 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Lucene.Net.Contrib.Spatial.Test | ||
{ | ||
public class SpatialMatchConcern | ||
{ | ||
public readonly bool orderIsImportant; | ||
public readonly bool resultsAreSuperset; // if the strategy can not give exact answers, but used to limit results | ||
|
||
private SpatialMatchConcern(bool order, bool superset) | ||
{ | ||
this.orderIsImportant = order; | ||
this.resultsAreSuperset = superset; | ||
} | ||
|
||
public static SpatialMatchConcern EXACT = new SpatialMatchConcern(true, false); | ||
public static SpatialMatchConcern FILTER = new SpatialMatchConcern(false, false); | ||
public static SpatialMatchConcern SUPERSET = new SpatialMatchConcern(false, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Spatial4n.Core.Context; | ||
using Spatial4n.Core.Io; | ||
using Spatial4n.Core.Query; | ||
|
||
namespace Lucene.Net.Contrib.Spatial.Test | ||
{ | ||
/// <summary> | ||
/// Helper class to execute queries | ||
/// </summary> | ||
public class SpatialTestQuery | ||
{ | ||
public String testname; | ||
public String line; | ||
public int lineNumber = -1; | ||
public SpatialArgs args; | ||
public List<String> ids = new List<String>(); | ||
|
||
public class SpatialTestQueryLineReader : LineReader<SpatialTestQuery> | ||
{ | ||
private readonly SpatialArgsParser parser; | ||
private readonly SpatialContext ctx; | ||
|
||
public SpatialTestQueryLineReader(Stream @in, SpatialArgsParser parser, SpatialContext ctx) | ||
: base(@in) | ||
{ | ||
this.parser = parser; | ||
this.ctx = ctx; | ||
} | ||
|
||
public SpatialTestQueryLineReader(StreamReader r, SpatialArgsParser parser, SpatialContext ctx) | ||
: base(r) | ||
{ | ||
this.parser = parser; | ||
this.ctx = ctx; | ||
} | ||
|
||
public override SpatialTestQuery ParseLine(string line) | ||
{ | ||
var test = new SpatialTestQuery {line = line, lineNumber = GetLineNumber()}; | ||
|
||
try | ||
{ | ||
// skip a comment | ||
if (line.StartsWith("[")) | ||
{ | ||
int idx0 = line.IndexOf(']'); | ||
if (idx0 > 0) | ||
{ | ||
line = line.Substring(idx0 + 1); | ||
} | ||
} | ||
|
||
int idx = line.IndexOf('@'); | ||
|
||
var pos = 0; | ||
var st = line.Substring(0, idx).Split(); | ||
while (pos< st.Length) | ||
{ | ||
test.ids.Add(st[pos++].Trim()); | ||
} | ||
test.args = parser.Parse(line.Substring(idx + 1).Trim(), ctx); | ||
return test; | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new Exception("invalid query line: " + test.line, ex); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Get Test Queries | ||
/// </summary> | ||
/// <param name="parser"></param> | ||
/// <param name="ctx"></param> | ||
/// <param name="name"></param> | ||
/// <param name="in"></param> | ||
/// <returns></returns> | ||
public static IEnumerator<SpatialTestQuery> getTestQueries( | ||
SpatialArgsParser parser, | ||
SpatialContext ctx, | ||
String name, | ||
Stream @in) | ||
{ | ||
return new SpatialTestQueryLineReader(new StreamReader(@in, Encoding.UTF8), parser, ctx); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Lucene.Net.Documents; | ||
using Lucene.Net.Spatial; | ||
using NUnit.Framework; | ||
using Spatial4n.Core.Context; | ||
using Spatial4n.Core.Io.Samples; | ||
using Spatial4n.Core.Query; | ||
using Spatial4n.Core.Shapes; | ||
|
||
namespace Lucene.Net.Contrib.Spatial.Test | ||
{ | ||
public abstract class StrategyTestCase<T> : SpatialTestCase where T : SpatialTestCase, SpatialFieldInfo | ||
{ | ||
public static readonly String DATA_STATES_POLY = "states-poly.txt"; | ||
public static readonly String DATA_STATES_BBOX = "states-bbox.txt"; | ||
public static readonly String DATA_COUNTRIES_POLY = "countries-poly.txt"; | ||
public static readonly String DATA_COUNTRIES_BBOX = "countries-bbox.txt"; | ||
public static readonly String DATA_WORLD_CITIES_POINTS = "world-cities-points.txt"; | ||
|
||
public static readonly String QTEST_States_IsWithin_BBox = "states-IsWithin-BBox.txt"; | ||
public static readonly String QTEST_States_Intersects_BBox = "states-Intersects-BBox.txt"; | ||
|
||
public static readonly String QTEST_Cities_IsWithin_BBox = "cities-IsWithin-BBox.txt"; | ||
|
||
//private Logger log = Logger.getLogger(getClass().getName()); | ||
|
||
protected readonly SpatialArgsParser argsParser = new SpatialArgsParser(); | ||
|
||
protected SpatialStrategy<T> strategy; | ||
protected SpatialContext ctx; | ||
protected T fieldInfo; | ||
protected bool storeShape = true; | ||
|
||
protected void executeQueries(SpatialMatchConcern concern, params String[] testQueryFile) | ||
{ | ||
Console.WriteLine("testing queried for strategy " + strategy); | ||
foreach (String path in testQueryFile) | ||
{ | ||
IEnumerator<SpatialTestQuery> testQueryIterator = getTestQueries(path, ctx); | ||
runTestQueries(testQueryIterator, concern); | ||
} | ||
} | ||
|
||
protected void getAddAndVerifyIndexedDocuments(String testDataFile) | ||
{ | ||
List<Document> testDocuments = getDocuments(testDataFile); | ||
addDocumentsAndCommit(testDocuments); | ||
verifyDocumentsIndexed(testDocuments.Count); | ||
} | ||
|
||
protected List<Document> getDocuments(String testDataFile) | ||
{ | ||
IEnumerator<SampleData> sampleData = getSampleData(testDataFile); | ||
var documents = new List<Document>(); | ||
while (sampleData.MoveNext()) | ||
{ | ||
SampleData data = sampleData.Current; | ||
var document = new Document(); | ||
document.Add(new Field("id", data.id, Field.Store.YES, Field.Index.ANALYZED)); | ||
document.Add(new Field("name", data.name, Field.Store.YES, Field.Index.ANALYZED)); | ||
Shape shape = ctx.ReadShape(data.shape); | ||
foreach (var f in strategy.CreateFields(fieldInfo, shape, true, storeShape)) | ||
{ | ||
if (f != null) | ||
{ // null if incompatibleGeometry && ignore | ||
document.Add(f); | ||
} | ||
} | ||
documents.Add(document); | ||
} | ||
return documents; | ||
} | ||
|
||
[Test] | ||
public void testWWW() | ||
{ | ||
Console.WriteLine(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\data", "file")); | ||
} | ||
|
||
protected IEnumerator<SampleData> getSampleData(String testDataFile) | ||
{ | ||
var stream = File.OpenRead(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\data", testDataFile)); | ||
return new SampleDataReader(stream); | ||
} | ||
|
||
protected IEnumerator<SpatialTestQuery> getTestQueries(String testQueryFile, SpatialContext ctx) | ||
{ | ||
var @in = File.OpenRead(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\", testQueryFile)); | ||
return SpatialTestQuery.getTestQueries(argsParser, ctx, testQueryFile, @in); | ||
} | ||
|
||
public void runTestQueries( | ||
IEnumerator<SpatialTestQuery> queries, | ||
SpatialMatchConcern concern) | ||
{ | ||
while (queries.MoveNext()) | ||
{ | ||
SpatialTestQuery q = queries.Current; | ||
|
||
String msg = q.line; //"Query: " + q.args.toString(ctx); | ||
SearchResults got = executeQuery(strategy.MakeQuery(q.args, fieldInfo), 100); | ||
if (concern.orderIsImportant) | ||
{ | ||
var ids = q.ids.GetEnumerator(); | ||
foreach (var r in got.results) | ||
{ | ||
String id = r.document.Get("id"); | ||
ids.MoveNext(); | ||
Assert.AreEqual("out of order: " + msg, ids.Current, id); | ||
} | ||
if (ids.MoveNext()) | ||
{ | ||
Assert.Fail(msg + " :: expect more results then we got: " + ids.Current); | ||
} | ||
} | ||
else | ||
{ | ||
// We are looking at how the results overlap | ||
if (concern.resultsAreSuperset) | ||
{ | ||
var found = new HashSet<String>(); | ||
foreach (var r in got.results) | ||
{ | ||
found.Add(r.document.Get("id")); | ||
} | ||
foreach (String s in q.ids) | ||
{ | ||
if (!found.Contains(s)) | ||
{ | ||
Assert.Fail("Results are mising id: " + s + " :: " + found); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
var found = new List<String>(); | ||
foreach (SearchResult r in got.results) | ||
{ | ||
found.Add(r.document.Get("id")); | ||
} | ||
|
||
// sort both so that the order is not important | ||
q.ids.Sort(); | ||
found.Sort(); | ||
Assert.AreEqual(msg, q.ids.ToString(), found.ToString()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters