Package org.openjgraph.model

Examples of org.openjgraph.model.Graph


            budget.addExpenditure(exp5);
            budget.addExpenditure(exp6);

            BudgetService bs = new BudgetService(new AdjacencyMatrixGraphFactory());

            Graph graph = bs.resolveBudget(budget);
            assertEquals(graph.getVertexSet().size(), 4);

            Set<String> personIdSet = new HashSet<String>(4);
            personIdSet.add(p1.getId());
            personIdSet.add(p2.getId());
            personIdSet.add(p3.getId());
            personIdSet.add(p4.getId());

            for(Vertex vertex : graph.getVertexSet()) {
                assertTrue(personIdSet.contains(vertex.getId()));
                if(vertex.getId().equals(p1.getId())){
                    assertEquals(vertex.getDegree(),7000);
                }
                if(vertex.getId().equals(p2.getId())){
View Full Code Here


            budget.addExpenditure(exp2);
            budget.addExpenditure(exp3);

            BudgetService bs = new BudgetService(new AdjacencyMatrixGraphFactory());

            Graph graph = bs.createBudgetGraph(budget);
            assertEquals(graph.getVertexSet().size(), 3);

            Set<String> personIdSet = new HashSet<String>(3);
            personIdSet.add(p1.getId());
            personIdSet.add(p2.getId());
            personIdSet.add(p3.getId());

            for(Vertex vertex : graph.getVertexSet()) {
                assertTrue(personIdSet.contains(vertex.getId()));
                if(vertex.getId().equals(p1.getId())){
                    assertEquals(vertex.getDegree(),40000);
                }
                if(vertex.getId().equals(p2.getId())){
View Full Code Here

    public BudgetService(GraphFactory graphFactory) {
        this.graphFactory = graphFactory;
    }

    public Graph resolveBudget(Budget budget) {
        Graph graph = createBudgetGraph(budget);
        List<Vertex> positives = new LinkedList<Vertex>();
        Map<Vertex, Integer> positivesDegree = new HashMap<Vertex, Integer>();
        List<Vertex> negatives = new LinkedList<Vertex>();
        Map<Vertex, Integer> negativesDegree = new HashMap<Vertex, Integer>();
        for (Vertex v : graph.getVertexSet()) {
            int degree = v.getDegree();
            if (degree > 0) {
                positives.add(v);
                positivesDegree.put(v,degree);
            } else {
                negatives.add(v);
                negativesDegree.put(v, degree);
            }
        }

        BudgetGraphComparator comparator = new BudgetGraphComparator();
        Graph solvedGraph = new AdjacencyMatrixDirectedGraph(graph.getVertexSet().size());
        for( Vertex v : graph.getVertexSet()) {
            solvedGraph.addVertex(v.getId());
        }

        while( (!positivesDegree.isEmpty()) && (!negativesDegree.isEmpty())) {

            // on tri les deux listes de vertex
            comparator.setTable(positivesDegree);
            Collections.sort(positives, comparator);

            comparator.setTable(negativesDegree);
            Collections.sort(negatives, comparator);
            Collections.reverse(negatives);

            Vertex maxPosV = positives.get(0);
            Vertex maxNegV = negatives.get(0);

            int posDegree = positivesDegree.get(maxPosV);
            int negDegree = negativesDegree.get(maxNegV);

            if (posDegree + negDegree == 0) {
                try {
                    solvedGraph.addEdge(maxPosV.getId(), maxNegV.getId(), posDegree);

                    positives.remove(maxPosV);
                    positivesDegree.remove(maxPosV);
                    negatives.remove(maxNegV);
                    negativesDegree.remove(maxNegV);
                } catch (VertexNotFoundException e) {
                    // on ajoute le sommet manquant... un tour pour rien
                    solvedGraph.addVertex(e.getVertex());
                }

            }
            else if (posDegree + negDegree > 0) {
                try {
                    solvedGraph.addEdge(maxPosV.getId(), maxNegV.getId(), -negDegree);
                    positivesDegree.remove(maxPosV);
                    positivesDegree.put(maxPosV, posDegree + negDegree);
                    negatives.remove(maxNegV);
                    negativesDegree.remove(maxNegV);
                } catch (VertexNotFoundException e) {
                    // on ajoute le sommet manquant... un tour pour rien
                    solvedGraph.addVertex(e.getVertex());
                }
            }
            else {
                try {
                    solvedGraph.addEdge(maxPosV.getId(), maxNegV.getId(), posDegree);
                    positivesDegree.remove(maxPosV);
                    positives.remove(maxPosV);
                    negativesDegree.remove(maxNegV);
                    negativesDegree.put(maxNegV, posDegree + negDegree);

                } catch ( VertexNotFoundException e) {
                    // on ajoute le sommet manquant... un tour pour rien
                    solvedGraph.addVertex(e.getVertex());
                }
            }
        }

View Full Code Here

     * Cette méthode permet de créer un graphe à partir d'un budget.
     * @param budget budget
     * @return
     */
    public Graph createBudgetGraph(Budget budget) {
        Graph graph = graphFactory.create(true);
        for (Person person : budget.getPersons()) {
            graph.addVertex(person.getId());
        }

        Map<String, Map<String, Float>> expenditureMatrix = new HashMap<String, Map<String, Float>>();
        for (Expenditure ex : budget.getExpenditures()) {
            float value = 0;
            if (ex.getCurrency() == null || budget.getCurrency().equals(ex.getCurrency())) {
                value = ex.getAmount() * 100;
            } else {
                value = ex.getAmount() * 100 * budget.getChange().get(ex.getCurrency());
            }

            int coef = 0;
            for (int v : ex.getRecipients().values()) {
                coef += v;
            }
            value = value / coef;

            for (Person recipient : ex.getRecipients().keySet()) {
                if (!recipient.equals(ex.getOwner())) {
                    if (!expenditureMatrix.containsKey(ex.getOwner().getId())) {
                        Map expenditureMap = new HashMap<String, Float>();
                        expenditureMap.put(recipient.getId(), value * ex.getRecipients().get(recipient));
                        expenditureMatrix.put(ex.getOwner().getId(), expenditureMap);
                    } else {
                        Map<String, Float> expenditureMap = expenditureMatrix.get(ex.getOwner().getId());
                        if (!expenditureMap.containsKey(recipient.getId())) {
                            expenditureMap.put(recipient.getId(), value * ex.getRecipients().get(recipient));
                        } else {
                            float oldValue = expenditureMap.get(recipient.getId());
                            expenditureMap.put(recipient.getId(), value * ex.getRecipients().get(recipient) + oldValue);
                        }
                    }
                }
            }
        }

        for (String ownerId : expenditureMatrix.keySet()) {
            for (String recipientId : expenditureMatrix.get(ownerId).keySet()) {
                try {
                    graph.addEdge(ownerId, recipientId, (expenditureMatrix.get(ownerId).get(recipientId)).intValue());
                } catch (VertexNotFoundException e) {
                    // on fait rien, ce cas ne peut pas arriver.
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.openjgraph.model.Graph

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.