Search in sources :

Example 1 with TemporaryFact

use of org.openremote.model.rules.TemporaryFact in project openremote by openremote.

the class RulesFacts method matchFact.

@SuppressWarnings("unchecked")
protected <T> Optional<T> matchFact(Object fact, Class<T> factType, Predicate<T> predicate) {
    if (fact == null)
        return Optional.empty();
    if (fact instanceof TemporaryFact) {
        TemporaryFact temporaryFact = (TemporaryFact) fact;
        fact = temporaryFact.getFact();
    }
    return Optional.ofNullable(factType.isAssignableFrom(fact.getClass()) && predicate.test((T) fact) ? (T) fact : null);
}
Also used : TemporaryFact(org.openremote.model.rules.TemporaryFact)

Example 2 with TemporaryFact

use of org.openremote.model.rules.TemporaryFact in project openremote by openremote.

the class RulesFacts method removeExpiredTemporaryFacts.

public void removeExpiredTemporaryFacts() {
    long currentTimestamp = (long) getClock().getTimestamp();
    getAssetEvents().removeIf(fact -> {
        boolean result = fact.isExpired(currentTimestamp);
        if (result && LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Fact change (DELETE EXPIRED): " + fact + " - on: " + loggingContext);
        }
        return result;
    });
    asMap().entrySet().removeIf(entry -> {
        if (entry.getKey().equals(CLOCK) || entry.getKey().equals(ASSET_STATES) || entry.getKey().equals(ASSET_EVENTS) || entry.getKey().equals(EXECUTION_VARS) || entry.getKey().equals(ANONYMOUS_FACTS) || !(entry.getValue() instanceof TemporaryFact)) {
            return false;
        }
        boolean result = ((TemporaryFact) entry.getValue()).isExpired(currentTimestamp);
        if (result && LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Fact change (DELETE EXPIRED): " + entry.getValue() + " - on: " + loggingContext);
        }
        return result;
    });
    getAnonymousFacts().removeIf(fact -> {
        boolean result = false;
        if (fact instanceof TemporaryFact) {
            TemporaryFact temporaryFact = (TemporaryFact) fact;
            result = temporaryFact.isExpired((long) getClock().getTimestamp());
        }
        if (result && LOG.isLoggable(Level.FINEST)) {
            LOG.finest("Fact change (DELETE EXPIRED): " + fact + " - on: " + loggingContext);
        }
        return result;
    });
}
Also used : TemporaryFact(org.openremote.model.rules.TemporaryFact)

Example 3 with TemporaryFact

use of org.openremote.model.rules.TemporaryFact in project openremote by openremote.

the class RulesFacts method get.

@SuppressWarnings("unchecked")
@Override
public <T> T get(String name) {
    Object fact = super.get(name);
    if (fact != null && fact instanceof TemporaryFact) {
        TemporaryFact temporaryFact = (TemporaryFact) fact;
        fact = temporaryFact.getFact();
    }
    return (T) fact;
}
Also used : TemporaryFact(org.openremote.model.rules.TemporaryFact)

Example 4 with TemporaryFact

use of org.openremote.model.rules.TemporaryFact in project openremote by openremote.

the class RulesFacts method logFacts.

public boolean logFacts(Logger logger) {
    boolean haveLog = false;
    if (getAllFacts().count() > 0) {
        logger.info("--------------------------------- CLOCK ---------------------------------");
        LOG.info(getClock().toString());
        haveLog = true;
    }
    List<AssetState> sortedAssetStates = new ArrayList<>(getAssetStates());
    sortedAssetStates.sort(Comparator.naturalOrder());
    if (sortedAssetStates.size() > 0) {
        logger.info("--------------------------------- ASSET STATES (" + sortedAssetStates.size() + ") ---------------------------------");
        for (AssetState assetState : sortedAssetStates) {
            logger.info(assetState.toString());
        }
        haveLog = true;
    }
    List<TemporaryFact<AssetState>> sortedAssetEvents = new ArrayList<>(getAssetEvents());
    if (sortedAssetEvents.size() > 0) {
        logger.info("--------------------------------- ASSET EVENTS (" + sortedAssetEvents.size() + ") ---------------------------------");
        sortedAssetEvents.sort(Comparator.comparing(TemporaryFact::getTime));
        for (TemporaryFact<AssetState> assetEvent : sortedAssetEvents) {
            logger.info(assetEvent.toString());
        }
        haveLog = true;
    }
    Map<String, Object> namedFacts = getNamedFacts();
    List<String> names = new ArrayList<>(namedFacts.keySet());
    names.sort(Comparator.naturalOrder());
    if (names.size() > 0) {
        logger.info("--------------------------------- NAMED FACTS (" + names.size() + ") ---------------------------------");
        for (String name : names) {
            logger.info(String.format("%s => %s", name, namedFacts.get(name)));
        }
        haveLog = true;
    }
    if (getAnonymousFacts().size() > 0) {
        logger.info("--------------------------------- ANONYMOUS FACTS (" + getAnonymousFacts().size() + ") ---------------------------------");
        for (Object o : getAnonymousFacts()) {
            logger.info(String.format("%s", o));
        }
        haveLog = true;
    }
    return haveLog;
}
Also used : TemporaryFact(org.openremote.model.rules.TemporaryFact) AssetState(org.openremote.model.rules.AssetState)

Aggregations

TemporaryFact (org.openremote.model.rules.TemporaryFact)4 AssetState (org.openremote.model.rules.AssetState)1