Skip to main content
summaryrefslogtreecommitdiffstats
blob: a532398758dfb1142183eba4dbbf6324cd36d023 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.core.internal.graph.impl;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Conditions;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.core.ds.ArtifactData;
import org.eclipse.osee.orcs.core.ds.AttributeData;
import org.eclipse.osee.orcs.core.ds.LoadDataHandlerAdapter;
import org.eclipse.osee.orcs.core.ds.LoadDescription;
import org.eclipse.osee.orcs.core.ds.RelationData;
import org.eclipse.osee.orcs.core.internal.artifact.Artifact;
import org.eclipse.osee.orcs.core.internal.artifact.ArtifactFactory;
import org.eclipse.osee.orcs.core.internal.attribute.AttributeFactory;
import org.eclipse.osee.orcs.core.internal.attribute.AttributeManager;
import org.eclipse.osee.orcs.core.internal.graph.GraphBuilder;
import org.eclipse.osee.orcs.core.internal.graph.GraphData;
import org.eclipse.osee.orcs.core.internal.graph.GraphProvider;
import org.eclipse.osee.orcs.core.internal.relation.Relation;
import org.eclipse.osee.orcs.core.internal.relation.RelationFactory;
import org.eclipse.osee.orcs.core.internal.relation.impl.RelationNodeAdjacencies;

/**
 * @author Roberto E. Escobar
 */
public class GraphBuilderImpl extends LoadDataHandlerAdapter implements GraphBuilder {

   private final Log logger;
   private final ArtifactFactory artifactFactory;
   private final AttributeFactory attributeFactory;
   private final RelationFactory relationFactory;
   private final GraphProvider graphProvider;

   private final Set<Artifact> updated = new LinkedHashSet<Artifact>();
   private GraphData graph;

   public GraphBuilderImpl(Log logger, ArtifactFactory artifactFactory, AttributeFactory attributeFactory, RelationFactory relationFactory, GraphProvider graphProvider) {
      super();
      this.logger = logger;
      this.graphProvider = graphProvider;
      this.artifactFactory = artifactFactory;
      this.attributeFactory = attributeFactory;
      this.relationFactory = relationFactory;
   }

   @Override
   public void onLoadStart() {
      graph = null;
   }

   private GraphData getGraph() throws OseeCoreException {
      Conditions.checkNotNull(graph, "graph");
      return graph;
   }

   @Override
   public void onLoadDescription(LoadDescription data) throws OseeCoreException {
      graph = graphProvider.getGraph(data.getSession(), data.getBranch(), data.getTransaction());
      Conditions.checkNotNull(graph, "graph");
   };

   @Override
   public void onData(ArtifactData data) throws OseeCoreException {
      GraphData graph = getGraph();
      Artifact artifact = graph.getNode(data);
      if (artifact == null) {
         artifact = artifactFactory.createArtifact(data);
         graph.addNode(artifact);

         RelationNodeAdjacencies adjacencies = relationFactory.createRelationContainer();
         graph.addAdjacencies(data.getLocalId(), adjacencies);
      }
      updated.add(artifact);
   }

   @Override
   public void onData(AttributeData data) throws OseeCoreException {
      GraphData graph = getGraph();
      AttributeManager container = graph.getNode(data.getArtifactId());
      if (container == null) {
         logger.warn("Orphaned attribute detected - data[%s]", data);
      } else {
         attributeFactory.createAttribute(container, data);
      }
   }

   @Override
   public void onData(RelationData data) throws OseeCoreException {
      GraphData graph = getGraph();

      RelationNodeAdjacencies aAdjacencies = getAdjacencies(graph, data.getArtIdA());
      RelationNodeAdjacencies bAdjacencies = getAdjacencies(graph, data.getArtIdB());
      Relation relation = findRelation(aAdjacencies, data);
      if (relation == null) {
         relation = findRelation(bAdjacencies, data);
      }
      if (relation == null) {
         relation = relationFactory.createRelation(data);
      }
      aAdjacencies.add(data.getTypeUuid(), relation);
      bAdjacencies.add(data.getTypeUuid(), relation);
   }

   private Relation findRelation(RelationNodeAdjacencies adjacencies, RelationData data) throws OseeCoreException {
      return adjacencies.getRelation(data.getArtIdA(), data.getTypeUuid(), data.getArtIdB());
   }

   private RelationNodeAdjacencies getAdjacencies(GraphData graph, int id) {
      RelationNodeAdjacencies adjacencies = graph.getAdjacencies(id);
      if (adjacencies == null) {
         adjacencies = relationFactory.createRelationContainer();
         graph.addAdjacencies(id, adjacencies);
      }
      return adjacencies;
   }

   @Override
   public void onLoadEnd() {
      //
   }

   @Override
   public Iterable<Artifact> getArtifacts() {
      return updated;
   }

   @Override
   public Iterable<GraphData> getGraphs() {
      return graph != null ? Collections.<GraphData> singleton(graph) : Collections.<GraphData> emptyList();
   }
}

Back to the top