From a42de79292d9541db7865b54e93be2d0b6e6a094 Mon Sep 17 00:00:00 2001 From: "serena.spinoso" Date: Thu, 7 Sep 2017 10:22:39 +0200 Subject: update verigraph JIRA: PARSER-154 code optimizations about graph manipulation and formula generation. Change-Id: Idebef19b128281aa2bc40d1aeab6e208c7ddd93d Signed-off-by: serena.spinoso --- .../escape/verify/service/NeighbourService.java | 182 --------------------- 1 file changed, 182 deletions(-) delete mode 100644 verigraph/src/main/java/it/polito/escape/verify/service/NeighbourService.java (limited to 'verigraph/src/main/java/it/polito/escape/verify/service/NeighbourService.java') diff --git a/verigraph/src/main/java/it/polito/escape/verify/service/NeighbourService.java b/verigraph/src/main/java/it/polito/escape/verify/service/NeighbourService.java deleted file mode 100644 index ecf4e69..0000000 --- a/verigraph/src/main/java/it/polito/escape/verify/service/NeighbourService.java +++ /dev/null @@ -1,182 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2017 Politecnico di Torino and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Apache License, Version 2.0 - * which accompanies this distribution, and is available at - * http://www.apache.org/licenses/LICENSE-2.0 - *******************************************************************************/ - -package it.polito.escape.verify.service; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import it.polito.escape.verify.database.DatabaseClass; -import it.polito.escape.verify.exception.BadRequestException; -import it.polito.escape.verify.exception.DataNotFoundException; -import it.polito.escape.verify.exception.ForbiddenException; -import it.polito.escape.verify.model.Graph; -import it.polito.escape.verify.model.Neighbour; -import it.polito.escape.verify.model.Node; - -public class NeighbourService { - - private Map graphs = DatabaseClass.getInstance().getGraphs(); - - public List getAllNeighbours(long graphId, long nodeId) { - if (graphId <= 0) { - throw new ForbiddenException("Illegal graph id: " + graphId); - } - if (nodeId <= 0) { - throw new ForbiddenException("Illegal node id: " + nodeId); - } - Graph graph = graphs.get(graphId); - if (graph == null) - throw new DataNotFoundException("Graph with id " + graphId + " not found"); - Map nodes = graph.getNodes(); - Node node = nodes.get(nodeId); - if (node == null) - throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId); - Map neighbours = node.getNeighbours(); - return new ArrayList(neighbours.values()); - } - - public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) { - if (graphId <= 0) { - throw new ForbiddenException("Illegal graph id: " + graphId); - } - if (nodeId <= 0) { - throw new ForbiddenException("Illegal node id: " + nodeId); - } - if (neighbourId <= 0) { - throw new ForbiddenException("Illegal neighbour id: " + neighbourId); - } - Graph graph = graphs.get(graphId); - if (graph == null) - throw new DataNotFoundException("Graph with id " + graphId + " not found"); - Map nodes = graph.getNodes(); - Node node = nodes.get(nodeId); - if (node == null) { - throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId); - } - Map neighbours = node.getNeighbours(); - Neighbour neighbour = neighbours.get(neighbourId); - if (neighbour == null) { - throw new DataNotFoundException("Neighbour with id " + neighbourId + " not found for node with id " + nodeId - + " in graph with id " + graphId); - } - return neighbour; - } - - public Neighbour addNeighbour(long graphId, long nodeId, Neighbour neighbour) { - if (graphId <= 0) { - throw new ForbiddenException("Illegal graph id: " + graphId); - } - if (nodeId <= 0) { - throw new ForbiddenException("Illegal node id: " + nodeId); - } - Graph graph = graphs.get(graphId); - if (graph == null) - throw new DataNotFoundException("Graph with id " + graphId + " not found"); - Map nodes = graph.getNodes(); - Node node = nodes.get(nodeId); - if (node == null) { - throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId); - } - Map neighbours = node.getNeighbours(); - - validateNeighbour(graph, node, neighbour); - - synchronized (this) { - neighbour.setId(neighbours.size() + 1); - neighbours.put(neighbour.getId(), neighbour); - DatabaseClass.persistDatabase(); - return neighbour; - } - } - - public Neighbour updateNeighbour(long graphId, long nodeId, Neighbour neighbour) { - if (graphId <= 0) { - throw new ForbiddenException("Illegal graph id: " + graphId); - } - if (nodeId <= 0) { - throw new ForbiddenException("Illegal node id: " + nodeId); - } - if (neighbour.getId() <= 0) { - throw new ForbiddenException("Illegal neighbour id: " + nodeId); - } - Graph graph = graphs.get(graphId); - if (graph == null) - throw new DataNotFoundException("Graph with id " + graphId + " not found"); - Map nodes = graph.getNodes(); - Node node = nodes.get(nodeId); - if (node == null) { - throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId); - } - Map neighbours = node.getNeighbours(); - Neighbour currentNeighbour = neighbours.get(neighbour.getId()); - if (currentNeighbour == null) { - throw new DataNotFoundException("Neighbour with id " + neighbour.getId() + " not found for node with id " - + nodeId + " in graph with id " + graphId); - } - - validateNeighbour(graph, node, neighbour); - - synchronized (this) { - neighbours.put(neighbour.getId(), neighbour); - DatabaseClass.persistDatabase(); - return neighbour; - } - } - - public Neighbour removeNeighbour(long graphId, long nodeId, long neighbourId) { - if (graphId <= 0) { - throw new ForbiddenException("Illegal graph id: " + graphId); - } - if (nodeId <= 0) { - throw new ForbiddenException("Illegal node id: " + nodeId); - } - if (neighbourId <= 0) { - throw new ForbiddenException("Illegal neighbour id: " + nodeId); - } - Graph graph = graphs.get(graphId); - if (graph == null) - throw new DataNotFoundException("Graph with id " + graphId + " not found"); - Map nodes = graph.getNodes(); - Node node = nodes.get(nodeId); - if (node == null) { - throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId); - } - Map neighbours = node.getNeighbours(); - - synchronized(this){ - return neighbours.remove(neighbourId); - } - } - - public static void validateNeighbour(Graph graph, Node node, Neighbour neighbour) { - if (graph == null) - throw new BadRequestException("Neighbour validation failed: cannot validate null graph"); - if (node == null) - throw new BadRequestException("Neighbour validation failed: cannot validate null node"); - if (neighbour == null) - throw new BadRequestException("Neighbour validation failed: cannot validate null neighbour"); - - if (neighbour.getName() == null) - throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be null"); - if (neighbour.getName().equals("")) - throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be an empty string"); - - Node nodeFound = graph.searchNodeByName(neighbour.getName()); - if ((nodeFound == null) || (nodeFound.getName().equals(node.getName()))) - throw new BadRequestException("Neighbour validation failed: '" + neighbour.getName() - + "' is not a valid name for a neighbour of node '" + node.getName() + "'"); - - Neighbour neighbourFound = node.searchNeighbourByName(neighbour.getName()); - if ((neighbourFound != null) && (neighbourFound.equals(neighbour) == false)) - throw new BadRequestException("Neighbour validation failed: node '" + node.getName() - + "' already has a neighbour named '" + neighbour.getName() + "'"); - } -} -- cgit 1.2.3-korg