Skip to content

Commit

Permalink
Merge pull request #18 Add GetInGroups()
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippluca authored Dec 14, 2022
2 parents 771cc8c + ad5876a commit 56bdb58
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 39 deletions.
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());
}
}
}
5 changes: 2 additions & 3 deletions src/model/GeoW_FunctionsExt.ili
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ MODEL GeoW_FunctionsExt

!!@ fn.description = "Gruppiert die Eingabemenge gemäss dem übergebenen Attribut-Kriterium / -Pfad. Für 'Attr' soll der Pfad zum klassierenden Attribut in INTERLIS 2 Syntax angegeben werden.";
!!@ fn.param = "Objects: Ausgangsobjekte. Attr: Pfad zum Attribut welches die Klassierung bestimmen soll";
!!@ fn.return = "Gruppierte Klasse der Eingabemenge";
!!@ fn.return = "Zugehörige Gruppe gemäss Attr des aktuellen Kontextobjekt";
!!@ fn.since = "2022-12-02";
FUNCTION GroupBy (Objects: OBJECTS OF ANYCLASS; Attr: TEXT): OBJECTS OF ANYCLASS;
FUNCTION GetInGroups (Objects: OBJECTS OF ANYCLASS; Attr: TEXT): OBJECTS OF ANYCLASS;

!!@ fn.description = "Prüft, ob ein Objekt innerhalb einer gegebenen Testgeometrie liegt, welche aus einem externen Datensatz stammt.";
!!@ fn.param = "DatasetName: Name des Transfermodells, in welchem die Objekte zur Prüfung bereitliegen. Objects: Objekt(e), über die TID identifiziert, welche zur Prüfung beigezogen werden. TestObject: Objekt, welches zu prüfen ist.TestObjectgeometry: Geometriefeld, bezogen auf das unter Testobject übergebene Objekt";
!!@ fn.return = "Boolean";
!!@ fn.since = "2022-12-05";
FUNCTION IsInsideExternalDataset (DatasetName: TEXT; Objects: TEXT; TestObject: OBJECT OF ANYCLASS; TestObjectgeometry: TEXT): BOOLEAN;

END GeoW_FunctionsExt.
29 changes: 29 additions & 0 deletions src/test/data/GetInGroups/SetConstraintAll.ili
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.
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@
<ili:datasection>
<TestSuite:FunctionTestTopic ili:bid="TestSuite.FunctionTestTopic">
<TestSuite:BaseClass ili:tid="0">
<TestSuite:attr>blaa</TestSuite:attr>
<TestSuite:criteria1>val1</TestSuite:criteria1>
<TestSuite:textAttr>aaa</TestSuite:textAttr>
<TestSuite:enumAttr>val1</TestSuite:enumAttr>
<TestSuite:numberAttr>1</TestSuite:numberAttr>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="1">
<TestSuite:attr>woot</TestSuite:attr>
<TestSuite:criteria1>val1</TestSuite:criteria1>
<TestSuite:textAttr>aaa</TestSuite:textAttr>
<TestSuite:enumAttr>val1</TestSuite:enumAttr>
<TestSuite:numberAttr>2</TestSuite:numberAttr>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="2">
<TestSuite:attr>defs</TestSuite:attr>
<TestSuite:criteria1>val3</TestSuite:criteria1>
<TestSuite:textAttr>aaa</TestSuite:textAttr>
<TestSuite:enumAttr>val2</TestSuite:enumAttr>
<TestSuite:numberAttr>2</TestSuite:numberAttr>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="3">
<TestSuite:attr>moon</TestSuite:attr>
<TestSuite:criteria1>val2</TestSuite:criteria1>
<TestSuite:textAttr>bbb</TestSuite:textAttr>
<TestSuite:enumAttr>val2</TestSuite:enumAttr>
<TestSuite:numberAttr>2</TestSuite:numberAttr>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="4">
<TestSuite:attr>soon</TestSuite:attr>
<TestSuite:criteria1>val3</TestSuite:criteria1>
<TestSuite:textAttr>bbb</TestSuite:textAttr>
<TestSuite:enumAttr>val2</TestSuite:enumAttr>
<TestSuite:numberAttr>1</TestSuite:numberAttr>
</TestSuite:BaseClass>
<TestSuite:BaseClass ili:tid="5">
<TestSuite:attr>yeah</TestSuite:attr>
<TestSuite:criteria1>val1</TestSuite:criteria1>
<TestSuite:textAttr>bbb</TestSuite:textAttr>
<TestSuite:enumAttr>val1</TestSuite:enumAttr>
<TestSuite:numberAttr>1</TestSuite:numberAttr>
</TestSuite:BaseClass>
</TestSuite:FunctionTestTopic>
</ili:datasection>
</ili:transfer>
</ili:transfer>
23 changes: 0 additions & 23 deletions src/test/data/GroupBy/MandatoryConstraintThis.ili

This file was deleted.

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");
}
}

0 comments on commit 56bdb58

Please sign in to comment.