org.integratedmodelling.engine.modelling.runtime.mediators.AbstractMediator Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (C) 2007, 2015:
*
* - Ferdinando Villa
* - integratedmodelling.org
* - any other authors listed in @author annotations
*
* All rights reserved. This file is part of the k.LAB software suite,
* meant to enable modular, collaborative, integrated
* development of interoperable data and model components. For
* details, see http://integratedmodelling.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Affero General Public License
* Version 3 or any later version.
*
* This program is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* merchantability or fitness for a particular purpose. See the
* Affero General Public License for more details.
*
* You should have received a copy of the Affero General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* The license is also available at: https://www.gnu.org/licenses/agpl.html
*******************************************************************************/
package org.integratedmodelling.engine.modelling.runtime.mediators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.integratedmodelling.api.knowledge.IConcept;
import org.integratedmodelling.api.metadata.IMetadata;
import org.integratedmodelling.api.modelling.IClassifyingObserver;
import org.integratedmodelling.api.modelling.INumericObserver;
import org.integratedmodelling.api.modelling.IObserver;
import org.integratedmodelling.api.modelling.IPresenceObserver;
import org.integratedmodelling.api.modelling.IState;
import org.integratedmodelling.base.HashableObject;
import org.integratedmodelling.collections.Pair;
import org.integratedmodelling.common.data.TableFactory.MaxAggregator;
import org.integratedmodelling.common.data.TableFactory.MeanAggregator;
import org.integratedmodelling.common.data.TableFactory.MinAggregator;
import org.integratedmodelling.common.data.TableFactory.SumAggregator;
import org.integratedmodelling.common.vocabulary.NS;
/**
* Holds aggregation methods for all mediators to use.
*
* @author ferdinando.villa
*
*/
public abstract class AbstractMediator extends HashableObject implements IState.Mediator {
private IObserver observer;
private IConcept dataReductionTrait;
protected AbstractMediator(IObserver observer, IConcept dataReductionTrait) {
this.observer = observer;
this.dataReductionTrait = dataReductionTrait;
}
@Override
public Object reduce(Collection> toReduce, IMetadata metadata) {
if (observer instanceof INumericObserver) {
return reduceNumbers(toReduce, metadata);
} else if (observer instanceof IClassifyingObserver) {
return reduceConcepts(toReduce, metadata);
} else if (observer instanceof IPresenceObserver) {
return reduceBooleans(toReduce, metadata);
}
return null;
}
/*
* CHECK - TODO: this mediator takes as true anything that mediates at least
* one true value with a weight > 0. We should enable configuration with truth
* values too.
*/
private Object reduceBooleans(Collection> toReduce, IMetadata metadata) {
double truth = Double.NaN;
int total = 0, tottrue = 0;
boolean ret = false;
for (Pair