<commit_before>
define([
    'service/vertex',
    'util/vertex/formatters'
], function(VertexService, F) {

    var PROPERTIES_TO_INSPECT_FOR_CHANGES = [
        'visibilitySource',
        'properties',
        'sandboxStatus',
        'edgeLabels'
    ];

    return withVertexCache;

    function withVertexCache() {

        this.cachedVertices = {};
        this.workspaceVertices = {};
        if (!this.vertexService) {
            this.vertexService = new VertexService();
        }

        this.resemblesVertices = function(val) {
            return val && val.length && val[0].id && val[0].properties;
        };

        this.verticesInWorkspace = function() {
            return _.values(_.pick(this.cachedVertices, _.keys(this.workspaceVertices)));
        };

        this.copy = function(obj) {
            return JSON.parse(JSON.stringify(obj));
        };

        this.workspaceOnlyVertexCopy = function(vertex) {
            return {
                id: vertex.id,
                workspace: this.copy(vertex.workspace || this.workspaceVertices[vertex.id] || {})
            };
        };

        this.workspaceOnlyVertex = function(id) {
            return {
                id: id,
                workspace: this.workspaceVertices[id] || {}
            };
        };

        this.inWorkspace = function(vertex) {
            return !!this.workspaceVertices[_.isString(vertex) ? vertex : vertex.id];
        };

        this.vertex = function(id) {
            return this.cachedVertices['' + id];
        };

        this.vertices = function(ids) {
            if (ids.toArray) {
                ids = ids.toArray();
            }

            ids = ids.map(function(id) {
                return id.id ? id.id : '' + id;
            });

            return _.values(_.pick(this.cachedVertices, ids));
        };

        this.refresh = function(vertex, forceReload) {
            var self = this,
                deferred = $.Deferred(),
                request = null,
                cachedVertex;

            if (_.isArray(vertex)) {
                var vertices = [], toRequest = [];
                vertex.forEach(function(v) {
                    cachedVertex = self.vertex(v);
                    if (forceReload !== true && cachedVertex) {
                        vertices.push(cachedVertex);
                    } else {
                        toRequest.push(v);
                    }
                })

                if (toRequest.length) {
                    request = this.vertexService.getMultiple(toRequest)
                        .done(function(data) {
                            deferred.resolve(vertices.concat(data.vertices));
                        })
                } else {
                    deferred.resolve(vertices);
                }

            } else if (_.isString(vertex) || _.isNumber(vertex)) {
                cachedVertex = this.vertex(vertex);
                if (forceReload !== true && cachedVertex) {
                    deferred.resolve(cachedVertex);
                } else {
                    request = this.vertexService.getVertexProperties(vertex)
                        .done(function(data) {
                            deferred.resolve(data);
                        })
                }
            } else {
                cachedVertex = this.vertex(vertex.id);
                if (forceReload !== true && cachedVertex) {
                    deferred.resolve(cachedVertex);
                } else {
                    request = this.vertexService.getVertexProperties(vertex.id)
                        .done(function(data) {
                            deferred.resolve(data[0]);
                        })
                }
            }

            deferred.abort = function() {
                if (request) {
                    request.abort();
                }
            };

            return deferred;
        };

        this.getVertexTitle = function(vertexId) {
            var deferredTitle = $.Deferred(),
                v;

            v = this.vertex(vertexId);
            if (v) {
                deferredTitle.resolve(F.vertex.title(v));
            } else {
                this.refresh(vertexId).done(function(vertex) {
                    deferredTitle.resolve(F.vertex.title(vertex));
                });
            }

            return deferredTitle;
        };

        this.updateCacheWithVertex = function(vertex, options) {
            var id = vertex.id,
                cache = this.cachedVertices[id] || (this.cachedVertices[id] = { id: id }),
                hasChanged = !_.isEqual(
                    _.pick.apply(_, [cache].concat(PROPERTIES_TO_INSPECT_FOR_CHANGES)),
                    _.pick.apply(_, [vertex].concat(PROPERTIES_TO_INSPECT_FOR_CHANGES))
                );

            if (_.isEqual(_.keys(vertex), 'id workspace'.split(' '))) {
                cache.workspace = $.extend(true, {}, cache.workspace, vertex.workspace || {});
                return cache;
            }

            if (!cache.properties) cache.properties = [];
            if (!cache.workspace) cache.workspace = {};

            verifyVisibility(vertex);

            cache.properties = _.isUndefined(vertex.properties) ? cache.properties : vertex.properties;
            cache.workspace = $.extend(true, {}, cache.workspace, vertex.workspace || {});

            $.extend(cache, _.pick(vertex, [
                'http://lumify.io#visibilityJson',
                'sandboxStatus']));

            cache.edgeLabels = vertex.edgeLabels;

            if (this.workspaceVertices[id]) {
                this.workspaceVertices[id] = cache.workspace;
            }

            var conceptType = F.vertex.prop(cache, 'conceptType', 'http://www.w3.org/2002/07/owl#Thing');
            cache.concept = this.cachedConcepts.byId[conceptType];
            if (cache.concept) {
                setPreviewsForVertex(cache, this.workspaceId);
            } else {
                console.error('Unable to attach concept to vertex', conceptType);
            }

            cache.edgeLabels = cache.edgeLabels || [];

            $.extend(true, vertex, cache);

            if (hasChanged) {
                this.trigger('vertexCacheUpdated', { vertex: cache, completeCache: this.cachedVertices });
            }

            return (options && options.returnNullIfNotChanged === true && !hasChanged) ? null : cache;
        };

        function verifyVisibility(vertex) {
            if (!vertex) return;<commit_msg>Set vertices that have unknown concept type to things<commit_after>
define([
    'service/vertex',
    'util/vertex/formatters'
], function(VertexService, F) {

    var ROOT_CONCEPT = 'http://www.w3.org/2002/07/owl#Thing',
        PROPERTIES_TO_INSPECT_FOR_CHANGES = [
            'visibilitySource',
            'properties',
            'sandboxStatus',
            'edgeLabels'
        ];

    return withVertexCache;

    function withVertexCache() {

        this.cachedVertices = {};
        this.workspaceVertices = {};
        if (!this.vertexService) {
            this.vertexService = new VertexService();
        }

        this.resemblesVertices = function(val) {
            return val && val.length && val[0].id && val[0].properties;
        };

        this.verticesInWorkspace = function() {
            return _.values(_.pick(this.cachedVertices, _.keys(this.workspaceVertices)));
        };

        this.copy = function(obj) {
            return JSON.parse(JSON.stringify(obj));
        };

        this.workspaceOnlyVertexCopy = function(vertex) {
            return {
                id: vertex.id,
                workspace: this.copy(vertex.workspace || this.workspaceVertices[vertex.id] || {})
            };
        };

        this.workspaceOnlyVertex = function(id) {
            return {
                id: id,
                workspace: this.workspaceVertices[id] || {}
            };
        };

        this.inWorkspace = function(vertex) {
            return !!this.workspaceVertices[_.isString(vertex) ? vertex : vertex.id];
        };

        this.vertex = function(id) {
            return this.cachedVertices['' + id];
        };

        this.vertices = function(ids) {
            if (ids.toArray) {
                ids = ids.toArray();
            }

            ids = ids.map(function(id) {
                return id.id ? id.id : '' + id;
            });

            return _.values(_.pick(this.cachedVertices, ids));
        };

        this.refresh = function(vertex, forceReload) {
            var self = this,
                deferred = $.Deferred(),
                request = null,
                cachedVertex;

            if (_.isArray(vertex)) {
                var vertices = [], toRequest = [];
                vertex.forEach(function(v) {
                    cachedVertex = self.vertex(v);
                    if (forceReload !== true && cachedVertex) {
                        vertices.push(cachedVertex);
                    } else {
                        toRequest.push(v);
                    }
                })

                if (toRequest.length) {
                    request = this.vertexService.getMultiple(toRequest)
                        .done(function(data) {
                            deferred.resolve(vertices.concat(data.vertices));
                        })
                } else {
                    deferred.resolve(vertices);
                }

            } else if (_.isString(vertex) || _.isNumber(vertex)) {
                cachedVertex = this.vertex(vertex);
                if (forceReload !== true && cachedVertex) {
                    deferred.resolve(cachedVertex);
                } else {
                    request = this.vertexService.getVertexProperties(vertex)
                        .done(function(data) {
                            deferred.resolve(data);
                        })
                }
            } else {
                cachedVertex = this.vertex(vertex.id);
                if (forceReload !== true && cachedVertex) {
                    deferred.resolve(cachedVertex);
                } else {
                    request = this.vertexService.getVertexProperties(vertex.id)
                        .done(function(data) {
                            deferred.resolve(data[0]);
                        })
                }
            }

            deferred.abort = function() {
                if (request) {
                    request.abort();
                }
            };

            return deferred;
        };

        this.getVertexTitle = function(vertexId) {
            var deferredTitle = $.Deferred(),
                v;

            v = this.vertex(vertexId);
            if (v) {
                deferredTitle.resolve(F.vertex.title(v));
            } else {
                this.refresh(vertexId).done(function(vertex) {
                    deferredTitle.resolve(F.vertex.title(vertex));
                });
            }

            return deferredTitle;
        };

        this.updateCacheWithVertex = function(vertex, options) {
            var id = vertex.id,
                cache = this.cachedVertices[id] || (this.cachedVertices[id] = { id: id }),
                hasChanged = !_.isEqual(
                    _.pick.apply(_, [cache].concat(PROPERTIES_TO_INSPECT_FOR_CHANGES)),
                    _.pick.apply(_, [vertex].concat(PROPERTIES_TO_INSPECT_FOR_CHANGES))
                );

            if (_.isEqual(_.keys(vertex), 'id workspace'.split(' '))) {
                cache.workspace = $.extend(true, {}, cache.workspace, vertex.workspace || {});
                return cache;
            }

            if (!cache.properties) cache.properties = [];
            if (!cache.workspace) cache.workspace = {};

            verifyVisibility(vertex);

            cache.properties = _.isUndefined(vertex.properties) ? cache.properties : vertex.properties;
            cache.workspace = $.extend(true, {}, cache.workspace, vertex.workspace || {});

            $.extend(cache, _.pick(vertex, [
                'http://lumify.io#visibilityJson',
                'sandboxStatus']));

            cache.edgeLabels = vertex.edgeLabels;

            if (this.workspaceVertices[id]) {
                this.workspaceVertices[id] = cache.workspace;
            }

            var conceptType = F.vertex.prop(cache, 'conceptType', ROOT_CONCEPT);
            if (conceptType === 'Unknown') {
                console.warn('Concept type is Unknown', cache);
                conceptType = ROOT_CONCEPT;
            }
            cache.concept = this.cachedConcepts.byId[conceptType];
            if (cache.concept) {
                setPreviewsForVertex(cache, this.workspaceId);
            } else {
                console.error('Unable to attach concept to vertex', conceptType);
            }

            cache.edgeLabels = cache.edgeLabels || [];

            $.extend(true, vertex, cache);

            if (hasChanged) {
                this.trigger('vertexCacheUpdated', { vertex: cache, completeCache: this.cachedVertices });
            }

            return (options && options.returnNullIfNotChanged === true && !hasChanged) ? null : cache;
        };

        function verifyVisibility(vertex) {
            if (!vertex) return;
{'fraction_non_alphanumeric': 0.11774436090225564, 'fraction_numerical': 0.0015037593984962407, 'mean_word_length': 1.3944194419441944, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4621139', 'n_tokens_mistral': 3502, 'n_tokens_neox': 3284, 'n_words': 541}