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

io.mindmaps.graph.internal.ConceptLog Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
/*
 * MindmapsDB - A Distributed Semantic Database
 * Copyright (C) 2016  Mindmaps Research Ltd
 *
 * MindmapsDB is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MindmapsDB 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MindmapsDB. If not, see .
 */

package io.mindmaps.graph.internal;

import io.mindmaps.util.Schema;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 *
 *  This package keeps track of changes to the rootGraph that need to be validated. This includes:
 *      new concepts,
 *      concepts that have had edges added/deleted,
 *      edge cases, for example, relationship where a new role player is added.
 *
 */
class ConceptLog {
    private Set modifiedConcepts;
    private Set modifiedCastings;
    private Set modifiedResources;

    ConceptLog() {
        modifiedCastings = new HashSet<>();
        modifiedConcepts = new HashSet<>();
        modifiedResources = new HashSet<>();
    }

    /**
     * Removes all the concepts from the transaction tracker
     */
    public void clearTransaction(){
        modifiedConcepts.clear();
        modifiedCastings.clear();
        modifiedResources.clear();
    }

    /**
     *
     * @param concept The concept to be later validated
     */
    public void putConcept(ConceptImpl concept) {
        if(!modifiedConcepts.contains(concept)) {
            modifiedConcepts.add(concept);
            if (Schema.BaseType.CASTING.name().equals(concept.getBaseType()))
                modifiedCastings.add(concept);
            if (Schema.BaseType.RESOURCE.name().equals(concept.getBaseType()))
                modifiedResources.add(concept);
        }
    }

    /**
     *
     * @return All the concepts which have been affected within the transaction in some way
     */
    public Set getModifiedConcepts () {
        modifiedConcepts = modifiedConcepts.stream().filter(ConceptImpl::isAlive).collect(Collectors.toSet());
        return modifiedConcepts;
    }

    /**
     *
     * @return All the castings which have been affected within the transaction in some way
     */
    public Set getModifiedCastingIds() {
        return modifiedCastings.stream().filter(ConceptImpl::isAlive).map(concept -> concept.getBaseIdentifier().toString()).collect(Collectors.toSet());
    }

    /**
     *
     * @return All the castings which have been affected within the transaction in some way
     */
    public Set getModifiedResourceIds() {
        return modifiedResources.stream().filter(ConceptImpl::isAlive).map(concept -> concept.getBaseIdentifier().toString()).collect(Collectors.toSet());
    }

    /**
     *
     * @param c The concept to nio longer track
     */
    public void removeConcept(ConceptImpl c){
        modifiedConcepts.remove(c);
        modifiedCastings.remove(c);
        modifiedResources.remove(c);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy