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

es.uam.eps.ir.relison.diffusion.sight.AllTrainSightMechanism Maven / Gradle / Ivy

The newest version!
/* 
 *  Copyright (C) 2020 Information Retrieval Group at Universidad Autónoma
 *  de Madrid, http://ir.ii.uam.es
 * 
 *  This Source Code Form is subject to the terms of the Mozilla Public
 *  License, v. 2.0. If a copy of the MPL was not distributed with this
 *  file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package es.uam.eps.ir.relison.diffusion.sight;

import es.uam.eps.ir.relison.diffusion.data.Data;
import es.uam.eps.ir.relison.diffusion.data.PropagatedInformation;
import es.uam.eps.ir.relison.diffusion.simulation.SimulationEdgeTypes;
import es.uam.eps.ir.relison.diffusion.simulation.UserState;
import es.uam.eps.ir.relison.graph.Graph;
import es.uam.eps.ir.relison.graph.edges.EdgeOrientation;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Sees the pieces of information that come from training users and the user has not previously propagated.
 *
 * @author Javier Sanz-Cruzado ([email protected])
 * @author Pablo Castells ([email protected])
 *
 * @param  type of the users
 * @param  type of the information pieces
 * @param 

type of the parameters */ public class AllTrainSightMechanism extends IndividualSightMechanism { /** * For each user, the set of users they are allowed to see information from */ private final Map> allowed; /** * Indicates if the selections have been initialized or not. */ private boolean initialized = false; /** * Orientation for indicating whih neighbors of the user propagate the information. */ private final EdgeOrientation orientation; /** * Constructor. * @param orient orientation for indicating which neighbors of the user propagate the information. */ public AllTrainSightMechanism(EdgeOrientation orient) { this.allowed = new HashMap<>(); this.orientation = orient; } @Override public void resetSelections(Data data) { // Store the information for speeding up the simulation if(!this.initialized) { Graph graph = data.getGraph(); graph.getAllNodes().forEach(u -> { Set set = new HashSet<>(); graph.getNeighbourhood(u, orientation).filter(v -> { if(orientation == EdgeOrientation.IN) return graph.getEdgeType(v, u) == SimulationEdgeTypes.TRAINING; else if(orientation == EdgeOrientation.OUT) return graph.getEdgeType(u, v) == SimulationEdgeTypes.TRAINING; else return (graph.containsEdge(u,v) && graph.getEdgeType(u,v) == SimulationEdgeTypes.TRAINING) || (graph.containsEdge(v,u) && graph.getEdgeType(v,u) == SimulationEdgeTypes.TRAINING); }).forEach(set::add); this.allowed.put(u, set); }); this.initialized = true; } } @Override public boolean seesInformation(UserState user, Data data, PropagatedInformation prop) { U u = user.getUserId(); long count = prop.getCreators() .stream() .filter(cidx -> this.allowed.get(u).contains(data.getUserIndex().idx2object(cidx))) .count(); return (count > 0) && !user.containsPropagatedInformation(prop.getInfoId()); } }