All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.tsl2.nano.modelkit.impl.Fact Maven / Gradle / Ivy

Go to download

TSL2 Framework to provide and use a structure of elements referenced by unique names - to declare a kit of logic in a json/yaml/xml text file

The newest version!
/*
 * File: $HeadURL$
 * Id  : $Id$
 * 
 * created by: Tom
 * created on: 31.03.2017
 * 
 * Copyright: (c) Thomas Schneider 2017, all rights reserved
 */
package de.tsl2.nano.modelkit.impl;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;

import com.fasterxml.jackson.annotation.JsonIgnore;

import de.tsl2.nano.modelkit.Identified;
import lombok.Getter;
import lombok.Setter;

/**
 * rules to check agreements. accessing definitions of its owning configuration. a name, starting with '!' will negate the rule
 * result.
 */
public class Fact extends AIdentified {
    static {
        ModelKitLoader.registereElement(Fact.class);
    }

    static final String PREF_NEGATION = "!";
    @JsonIgnore
    BiFunction rule;

	@Getter @Setter
    private List andFacts;

    Fact() {
    }

    public Fact(String name, BiFunction rule, String... andFacts) {
        super(name);
        this.rule = rule;
        this.andFacts = Arrays.asList(andFacts);
    }

    @Override
    public void validate() {
        checkExistence(Fact.class, andFacts);
    }

    @Override
    public void tagNames(String parent) {
        super.tagNames(parent);
        tag(parent, andFacts);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public boolean ask(T question) {
        visited(question);
        List facts = get(Fact.class);
        return (rule == null || ifNegate(rule.apply(config, question)))
            && andFacts.stream().allMatch(n -> Identified.get(facts, n).ask(question));
    }

    private boolean ifNegate(boolean result) {
        return negate() ? !result : result;
    }

    private boolean negate() {
        return negate(name);
    }

    static boolean negate(String factName) {
        return factName.startsWith(PREF_NEGATION);
    }

    public Fact setNegate() {
        name = negate() ? name.substring(1) : PREF_NEGATION + name;
        return this;
    }

    public static final String not(String factName) {
        return negate(factName) ? factName.substring(1) : PREF_NEGATION + factName;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy