Skip to content

Commit

Permalink
Also allow numeric types for IsInsideAreaByCode
Browse files Browse the repository at this point in the history
  • Loading branch information
domi-b committed Feb 26, 2024
1 parent 1a0e4f5 commit 50e1db7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.ehi.basics.types.OutParam;
import ch.interlis.ili2c.metamodel.EnumerationType;
import ch.interlis.ili2c.metamodel.NumericType;
import ch.interlis.ili2c.metamodel.PathEl;
import ch.interlis.ili2c.metamodel.Type;
import ch.interlis.ili2c.metamodel.Viewable;
Expand Down Expand Up @@ -69,19 +70,24 @@ private Value isInsideArea(String usageScope, Collection<IomObject> objects, Pat
Geometry::union
));

List<Geometry> sortedGeometries;
ValueKey firstKey = geometriesByCodeValue.keySet().iterator().next();
Type keyType = firstKey.getType();
if (!(keyType instanceof EnumerationType)) {
logger.addEvent(logger.logErrorMsg("{0}: Enumeration type expected.", usageScope));
return Value.createSkipEvaluation();
}
EnumerationType enumType = (EnumerationType) keyType;
if (!enumType.isOrdered()) {
logger.addEvent(logger.logErrorMsg("{0}: Enumeration type must be ordered.", usageScope));

if (keyType instanceof EnumerationType) {
EnumerationType enumType = (EnumerationType) keyType;
if (!enumType.isOrdered()) {
logger.addEvent(logger.logErrorMsg("{0}: Enumeration type must be ordered.", usageScope));
return Value.createSkipEvaluation();
}
sortedGeometries = sortByEnumValues(geometriesByCodeValue, enumType);
} else if (keyType instanceof NumericType) {
sortedGeometries = sortByNumericValues(geometriesByCodeValue);
} else {
logger.addEvent(logger.logErrorMsg("{0}: Unsupported type {1} for {2}.", usageScope, keyType.toString(), getQualifiedIliName()));
return Value.createSkipEvaluation();
}

List<Geometry> sortedGeometries = sortByEnumValues(geometriesByCodeValue, enumType);
for (int i = 0; i < sortedGeometries.size() - 1; i++) {
Geometry current = sortedGeometries.get(i);
Geometry next = sortedGeometries.get(i + 1);
Expand All @@ -104,6 +110,14 @@ private List<Geometry> sortByEnumValues(Map<ValueKey, Geometry> map, Enumeration
.collect(Collectors.toList());
}

private List<Geometry> sortByNumericValues(Map<ValueKey, Geometry> map) {
return map.entrySet()
.stream()
.sorted(Comparator.comparingDouble(entry -> entry.getKey().getNumericValue()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}

private ValueKey getCodeValue(IomObject object, PathEl[] enumPath) {
Value value = validator.getValueFromObjectPath(null, object, enumPath, null);
return new ValueKey(value);
Expand Down Expand Up @@ -147,6 +161,10 @@ public String getStringValue() {
return value.getValue();
}

public double getNumericValue() {
return value.getNumeric();
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
9 changes: 6 additions & 3 deletions src/test/data/IsInsideAreaByCode/SetConstraints.ili
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ MODEL TestSuite
TOPIC FunctionTestTopic =

DOMAIN
Code = (
CodeEnum = (
code_1,
code_2,
code_3,
code_4
) ORDERED;
CodeNumeric = 0 .. 999;

!!@CRS=EPSG:2056
CHKoord = COORD 2460000.000 .. 2870000.000 [INTERLIS.m],
1045000.000 .. 1310000.000 [INTERLIS.m],
ROTATION 2 -> 1;

CLASS BaseClass =
code : Code;
codeEnum : CodeEnum;
codeNumeric : CodeNumeric;
surface : SURFACE WITH (STRAIGHTS) VERTEX CHKoord WITHOUT OVERLAPS > 0.001;

SET CONSTRAINT insideAreaConstraint: NGK_SO_FunctionsExt.IsInsideAreaByCode(ALL, "surface", "code");
SET CONSTRAINT insideAreaConstraintEnum: NGK_SO_FunctionsExt.IsInsideAreaByCode(ALL, "surface", "codeEnum");
SET CONSTRAINT insideAreaConstraintNumeric: NGK_SO_FunctionsExt.IsInsideAreaByCode(ALL, "surface", "codeNumeric");
END BaseClass;

END FunctionTestTopic;
Expand Down
12 changes: 8 additions & 4 deletions src/test/data/IsInsideAreaByCode/TestData_Fail.xtf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<ili:datasection>
<TestSuite:FunctionTestTopic ili:bid="TestSuite.FunctionTestTopic">
<TestSuite:BaseClass ili:tid="1">
<TestSuite:code>code_1</TestSuite:code>
<TestSuite:codeEnum>code_1</TestSuite:codeEnum>
<TestSuite:codeNumeric>11</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down Expand Up @@ -134,7 +135,8 @@
</TestSuite:surface>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="2">
<TestSuite:code>code_2</TestSuite:code>
<TestSuite:codeEnum>code_2</TestSuite:codeEnum>
<TestSuite:codeNumeric>22</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down Expand Up @@ -289,7 +291,8 @@
</TestSuite:surface>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="3">
<TestSuite:code>code_3</TestSuite:code>
<TestSuite:codeEnum>code_3</TestSuite:codeEnum>
<TestSuite:codeNumeric>33</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down Expand Up @@ -420,7 +423,8 @@
</TestSuite:surface>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="4">
<TestSuite:code>code_4</TestSuite:code>
<TestSuite:codeEnum>code_4</TestSuite:codeEnum>
<TestSuite:codeNumeric>44</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down
11 changes: 7 additions & 4 deletions src/test/data/IsInsideAreaByCode/TestData_Ok.xtf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
</ili:headersection>
<ili:datasection>
<TestSuite:FunctionTestTopic ili:bid="TestSuite.FunctionTestTopic">
<TestSuite:BaseClass ili:tid="a"><!-- Code ordered 2, 1, 4 to test sorting and gap in codes -->
<TestSuite:code>code_2</TestSuite:code>
<TestSuite:BaseClass ili:tid="a"><!-- Code enum ordered 2, 1, 4 and numeric 1, 0, 440 to test sorting and gap in codes -->
<TestSuite:codeEnum>code_2</TestSuite:codeEnum>
<TestSuite:codeNumeric>1</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down Expand Up @@ -74,7 +75,8 @@
</TestSuite:surface>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="b">
<TestSuite:code>code_1</TestSuite:code>
<TestSuite:codeEnum>code_1</TestSuite:codeEnum>
<TestSuite:codeNumeric>0</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down Expand Up @@ -149,7 +151,8 @@
</TestSuite:surface>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="c">
<TestSuite:code>code_4</TestSuite:code>
<TestSuite:codeEnum>code_4</TestSuite:codeEnum>
<TestSuite:codeNumeric>440</TestSuite:codeNumeric>
<TestSuite:surface>
<geom:surface>
<geom:exterior>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public void setUp() {
public void setConstraintOk() throws Ili2cFailure, IoxException {
vh.runValidation(new String[]{TEST_DATA_OK}, new String[]{ILI_FILE});
Assert.equals(0, vh.getErrs().size());
AssertionHelper.assertNoConstraintError(vh, "insideAreaConstraint");
AssertionHelper.assertNoConstraintError(vh, "insideAreaConstraintEnum");
AssertionHelper.assertNoConstraintError(vh, "insideAreaConstraintNumeric");
}

@Test
public void setConstraintFail() throws Ili2cFailure, IoxException {
vh.runValidation(new String[]{TEST_DATA_FAIL}, new String[]{ILI_FILE});
Assert.equals(1, vh.getErrs().size());
AssertionHelper.assertConstraintErrors(vh, 1, "insideAreaConstraint");
Assert.equals(2, vh.getErrs().size());
AssertionHelper.assertConstraintErrors(vh, 1, "insideAreaConstraintEnum");
AssertionHelper.assertConstraintErrors(vh, 1, "insideAreaConstraintNumeric");
}
}

0 comments on commit 50e1db7

Please sign in to comment.