Search in sources :

Example 11 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class LinkInstanceFacade method runRule.

@SuppressWarnings("unchecked")
public void runRule(final String linkTypeId, String linkInstanceId, String attributeId, final String actionName) {
    LinkType linkType = linkTypeDao.getLinkType(linkTypeId);
    Constraint constraint = ResourceUtils.findConstraint(linkType.getAttributes(), attributeId);
    if (constraint != null) {
        var config = (Map<String, Object>) constraint.getConfig();
        var rule = config.get("rule").toString();
        if (!linkType.getRules().containsKey(rule)) {
            throw new IllegalStateException("Rule not found");
        }
        var linkInstance = checkReadLink(linkType, linkInstanceId);
        taskProcessingFacade.runRule(linkType, rule, linkInstance, actionName);
    }
}
Also used : Constraint(io.lumeer.api.model.Constraint) LinkType(io.lumeer.api.model.LinkType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class CollectionPurposeUtils method isDoneState.

public static boolean isDoneState(final DataDocument data, final Collection collection) {
    final String stateAttributeId = collection.getPurpose().getStateAttributeId();
    final List<Object> finalStates = collection.getPurpose().getFinalStatesList();
    if (finalStates != null && data != null) {
        Attribute stateAttribute = findAttribute(collection.getAttributes(), stateAttributeId);
        if (stateAttribute != null) {
            final Object states = data.getObject(stateAttributeId);
            ConstraintType constraintType = Utils.computeIfNotNull(stateAttribute.getConstraint(), Constraint::getType);
            if (constraintType == ConstraintType.Boolean) {
                var finalState = finalStates.isEmpty() ? null : finalStates.get(0);
                return toBoolean(finalState) == toBoolean(states);
            } else if (states instanceof List) {
                final List<Object> stringStates = data.getArrayList(stateAttributeId);
                return stringStates.stream().anyMatch(finalStates::contains);
            } else {
                return finalStates.contains(states);
            }
        }
    }
    return false;
}
Also used : ResourceUtils.findAttribute(io.lumeer.api.util.ResourceUtils.findAttribute) Attribute(io.lumeer.api.model.Attribute) Constraint(io.lumeer.api.model.Constraint) ConstraintType(io.lumeer.api.model.ConstraintType) List(java.util.List)

Example 13 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class AttributeCodec method convertFromDocument.

public static Attribute convertFromDocument(final Document document) {
    String id = document.getString(ID);
    String name = document.getString(NAME);
    String description = document.getString(DESCRIPTION);
    Constraint constraint = ConstraintCodec.convertFromDocument(document.get(CONSTRAINT, Document.class));
    AttributeLock lock = AttributeLockCodec.convertFromDocument(document.get(LOCK, Document.class));
    Function function = FunctionCodec.convertFromDocument(document.get(FUNCTION, Document.class));
    Integer usageCount = document.getInteger(USAGE_COUNT);
    Boolean suggestValues = document.getBoolean(SUGGEST_VALUES);
    return new Attribute(id, name, description, constraint, lock, function, usageCount, suggestValues);
}
Also used : Function(io.lumeer.api.model.function.Function) Constraint(io.lumeer.api.model.Constraint) Attribute(io.lumeer.api.model.Attribute) AttributeLock(io.lumeer.api.model.AttributeLock) Document(org.bson.Document)

Example 14 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class ConstraintManagerTest method testCoordinatesConstraint.

@Test
public void testCoordinatesConstraint() {
    final Constraint coordinatesConstraint = new Constraint(ConstraintType.Coordinates, null);
    final ConstraintManager cm = new ConstraintManager();
    cm.setLocale(l);
    Object encoded = cm.encode("40.123, -74.123", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
    encoded = cm.encode("40.123°N 74.123°W", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
    encoded = cm.encode("40°7´22.8\"N 74°7´22.8\"W", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
    encoded = cm.encode("40°7.38'N, 74°7.38'W", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
    encoded = cm.encode("N40°7’22.8, W74°7’22.8\"", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
    encoded = cm.encode("40 7 22.8, W74 7 22.8", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
    encoded = cm.encode("40.123N 74.123W", coordinatesConstraint);
    assertThat(encoded).isEqualTo(new Point(NamedCoordinateReferenceSystem.EPSG_4326, new Position(40.123, -74.123)));
}
Also used : Constraint(io.lumeer.api.model.Constraint) Position(com.mongodb.client.model.geojson.Position) Point(com.mongodb.client.model.geojson.Point) Test(org.junit.Test)

Example 15 with Constraint

use of io.lumeer.api.model.Constraint in project engine by Lumeer.

the class ConstraintManagerTest method testBooleanConstraint.

@Test
public void testBooleanConstraint() {
    final Constraint booleanConstraint = new Constraint(ConstraintType.Boolean, null);
    final ConstraintManager cm = new ConstraintManager();
    cm.setLocale(l);
    Object encoded = cm.encode("true", booleanConstraint);
    assertThat(encoded).isEqualTo(Boolean.TRUE);
    encoded = cm.encode("yes", booleanConstraint);
    assertThat(encoded).isEqualTo("yes");
    encoded = cm.encode("   fAlse    ", booleanConstraint);
    assertThat(encoded).isEqualTo(Boolean.FALSE);
}
Also used : Constraint(io.lumeer.api.model.Constraint) Test(org.junit.Test)

Aggregations

Constraint (io.lumeer.api.model.Constraint)25 Attribute (io.lumeer.api.model.Attribute)17 Test (org.junit.Test)17 DataDocument (io.lumeer.engine.api.data.DataDocument)15 Document (io.lumeer.api.model.Document)13 Query (io.lumeer.api.model.Query)9 Collection (io.lumeer.api.model.Collection)8 HashMap (java.util.HashMap)5 BigDecimal (java.math.BigDecimal)4 ConstraintType (io.lumeer.api.model.ConstraintType)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 CollectionPurpose (io.lumeer.api.model.CollectionPurpose)2 LinkType (io.lumeer.api.model.LinkType)2 NumberFormat (java.text.NumberFormat)2 ZonedDateTime (java.time.ZonedDateTime)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2