-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 Add GetInGroups()
- Loading branch information
Showing
6 changed files
with
168 additions
and
39 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/main/java/ch/geowerkstatt/ilivalidator/extensions/functions/GetInGroupsIoxPlugin.java
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,86 @@ | ||
package ch.geowerkstatt.ilivalidator.extensions.functions; | ||
|
||
import ch.interlis.ili2c.metamodel.PathEl; | ||
import ch.interlis.ili2c.metamodel.Viewable; | ||
import ch.interlis.iom.IomObject; | ||
import ch.interlis.iox_j.validator.Value; | ||
|
||
import java.security.acl.Group; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class GetInGroupsIoxPlugin extends BaseInterlisFunction { | ||
|
||
private static Map<GroupKey, List<IomObject>> groups = new HashMap<>(); | ||
|
||
@Override | ||
public String getQualifiedIliName() { | ||
return "GeoW_FunctionsExt.GetInGroups"; | ||
} | ||
|
||
@Override | ||
public Value evaluateInternal(String validationKind, String usageScope, IomObject contextObject, Value[] arguments) { | ||
Value argObjects = arguments[0]; | ||
Value argPath = arguments[1]; | ||
|
||
if (argObjects.isUndefined() || argPath.isUndefined()) { | ||
return Value.createSkipEvaluation(); | ||
} | ||
|
||
Collection<IomObject> inputObjects = argObjects.getComplexObjects(); | ||
if (inputObjects == null) { | ||
return Value.createUndefined(); | ||
} | ||
|
||
Viewable contextClass = EvaluationHelper.getContextClass(td, contextObject, argObjects); | ||
if (contextClass == null) { | ||
throw new IllegalStateException("unknown class in " + usageScope); | ||
} | ||
PathEl[] attributePath = EvaluationHelper.getAttributePathEl(validator, contextClass, argPath); | ||
|
||
GroupKey key = new GroupKey(inputObjects, validator.getValueFromObjectPath(null, contextObject, attributePath, null)); | ||
if(!groups.containsKey(key)){ | ||
FillGroups(inputObjects, attributePath); | ||
} | ||
|
||
return new Value(groups.get(key)); | ||
} | ||
|
||
private void FillGroups(Collection<IomObject> inputObjects, PathEl[] attributePath) { | ||
for (IomObject object: inputObjects) { | ||
GroupKey key = new GroupKey(inputObjects, validator.getValueFromObjectPath(null, object, attributePath, null)); | ||
if(!groups.containsKey(key)){ | ||
groups.put(key, new ArrayList<>()); | ||
} | ||
|
||
groups.get(key).add(object); | ||
} | ||
} | ||
|
||
private static class GroupKey | ||
{ | ||
private Collection<IomObject> inputObjects; | ||
private Value groupId; | ||
public GroupKey(Collection<IomObject> inputObjects, Value groupId){ | ||
this.inputObjects = inputObjects; | ||
this.groupId = groupId; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GroupKey that = (GroupKey) o; | ||
return inputObjects.equals(that.inputObjects) && groupId.compareTo(that.groupId) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(inputObjects, groupId.getValue()); | ||
} | ||
} | ||
} |
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,29 @@ | ||
INTERLIS 2.4; | ||
|
||
MODEL TestSuite | ||
AT "mailto:[email protected]" VERSION "2022-12-02" = | ||
IMPORTS GeoW_FunctionsExt ; | ||
|
||
DOMAIN | ||
!!@CRS=EPSG:2056 | ||
CHKoord = COORD 2460000.000 .. 2870000.000 [INTERLIS.m], | ||
1045000.000 .. 1310000.000 [INTERLIS.m], | ||
ROTATION 2 -> 1; | ||
|
||
TOPIC FunctionTestTopic = | ||
|
||
CLASS BaseClass = | ||
textAttr: TEXT*16; | ||
enumAttr: (val1,val2,val3); | ||
numberAttr: 0..10; | ||
SET CONSTRAINT trueConstraintTextAttr: INTERLIS.elementCount(GeoW_FunctionsExt.GetInGroups(ALL, "textAttr")) == 3; | ||
SET CONSTRAINT trueConstraintEnumAttr: INTERLIS.elementCount(GeoW_FunctionsExt.GetInGroups(ALL, "enumAttr")) == 3; | ||
SET CONSTRAINT trueConstraintNumberAttr: INTERLIS.elementCount(GeoW_FunctionsExt.GetInGroups(ALL, "numberAttr")) == 3; | ||
SET CONSTRAINT falseConstraintTextAttr: INTERLIS.elementCount(GeoW_FunctionsExt.GetInGroups(ALL, "textAttr")) == 2; | ||
SET CONSTRAINT falseConstraintEnumAttr: INTERLIS.elementCount(GeoW_FunctionsExt.GetInGroups(ALL, "enumAttr")) == 2; | ||
SET CONSTRAINT falseConstraintNumberAttr: INTERLIS.elementCount(GeoW_FunctionsExt.GetInGroups(ALL, "numberAttr")) == 2; | ||
END BaseClass; | ||
|
||
END FunctionTestTopic; | ||
|
||
END TestSuite. |
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 was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
...test/java/ch/geowerkstatt/ilivalidator/extensions/functions/GetInGroupsIoxPluginTest.java
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,32 @@ | ||
package ch.geowerkstatt.ilivalidator.extensions.functions; | ||
|
||
import ch.interlis.ili2c.Ili2cFailure; | ||
import ch.interlis.iox.IoxException; | ||
import com.vividsolutions.jts.util.Assert; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class GetInGroupsIoxPluginTest { | ||
|
||
protected static final String TEST_DATA = "GetInGroups/TestData.xtf"; | ||
ValidationTestHelper vh = null; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
vh = new ValidationTestHelper(); | ||
vh.addFunction(new GetInGroupsIoxPlugin()); | ||
} | ||
|
||
@Test | ||
void MandatoryConstraintOnThis() throws Ili2cFailure, IoxException { | ||
vh.runValidation(new String[]{TEST_DATA}, new String[]{"GetInGroups/SetConstraintAll.ili"}); | ||
Assert.equals(3, vh.getErrs().size()); | ||
AssertionHelper.assertNoConstraintError(vh, "trueConstraintTextAttr"); | ||
AssertionHelper.assertNoConstraintError(vh, "trueConstraintEnumAttr"); | ||
AssertionHelper.assertNoConstraintError(vh, "trueConstraintNumberAttr"); | ||
AssertionHelper.assertSingleConstraintError(vh, 0, "falseConstraintTextAttr"); | ||
AssertionHelper.assertSingleConstraintError(vh, 0, "falseConstraintEnumAttr"); | ||
AssertionHelper.assertSingleConstraintError(vh, 0, "falseConstraintNumberAttr"); | ||
} | ||
} |