Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with CCM metric addition v2 #523

Open
User123363 opened this issue May 13, 2021 · 4 comments
Open

Problem with CCM metric addition v2 #523

User123363 opened this issue May 13, 2021 · 4 comments

Comments

@User123363
Copy link

To fix the CCM metric, the FindConnectedComponents class was implemented to find the connected components in a graph:

package com.company;

import java.util.ArrayList;

class FindConnectedComponents
{
    int V;
    ArrayList<ArrayList<Integer>> graph;

    FindConnectedComponents(int V)
    {
        this.V = V;
        graph = new ArrayList<>();

        for (int i = 0; i < V; i++) {
            graph.add(i, new ArrayList<>());
        }
    }

    void DFS(int v, boolean[] visited)
    {
        visited[v] = true;
        System.out.print(v + " ");
        for (int x : graph.get(v)) {
            if (!visited[x])
                DFS(x, visited);
        }
    }

    int connectedComponents()
    {
        int countConnectedComponents = 0;
        boolean[] visited = new boolean[V];
        for (int v = 0; v < V; ++v) {
            if (!visited[v]) {
                DFS(v, visited);
                System.out.println();
                countConnectedComponents++;
            }
        }
        return countConnectedComponents;
    }


    void addEdge(int src, int dest)
    {
        graph.get(src).add(dest);
        graph.get(dest).add(src);
    }

    public static void main(String[] args)
    {
        FindConnectedComponents f = new FindConnectedComponents(7);

        f.addEdge(1, 0);
        f.addEdge(2, 3);
        f.addEdge(3, 4);
        f.addEdge(4, 5);
        f.addEdge(5, 6);

        System.out.println("Following are connected components");
        int countConnectedComponents = f.connectedComponents();

        System.out.print("Count Connected Components: ");
        System.out.print(countConnectedComponents);

    }
}

This class needs to be implemented in jpeek , it needs to input the graph found in the file "org/jpeek/metrics/CCM.xsl". As a result of the FindConnectedComponents class we get the number of connectivity components of the graph, this data must be placed in the ncc variable in line 65 in the file "org/jpeek/metrics/CCM.xsl".

After that the problem with CCM metrics will be solved.

@iMaks99
Copy link

iMaks99 commented May 14, 2021

We tried to realize component connection algorithm in XSL, and got problem with variables reassign for visit checking.

    <xsl:variable name="visited"/>

    <xsl:function name="dfs" as="xs:integer">
      <xsl:param name="v" as="xs:element"/>

      <xsl:if test="$visited[. = $v/edge[0]/name()]">
        <xsl:for-each select="$visited">
          <visit>
            <xsl:value-of select="$v/edge[0]/name()"/>
          </visit>
        </xsl:for-each>
      </xsl:if>

      <xsl:for-each select="$edges">
        <xsl:variable name="e" select="."/>
        <xsl:for-each select="$visited">
          <xsl:if test=".[. = $v/edge[1]/name()]">
                 ...
          </xsl:if>
        </xsl:for-each>
      </xsl:for-each>

      <xsl:sequence select="0"/>
    </xsl:function>

    <xsl:variable name="components">

      <xsl:for-each select="$visited">
        <xsl:variable name="v" select="."/>

        <xsl:if test="$v[. = false]">
          <component>
            <xsl:value-of select="increment"/>
          </component>
        </xsl:if>

      </xsl:for-each>
    </xsl:variable>

@yegor256
Copy link
Member

@iMaks99 Let's say you have this XML:

<root>
  <a id="1">
    <ref>2</ref>
    <ref>4</ref>
  </a>
  <a id="2">
    <ref>1</ref>
    <ref>4</ref>
  </a>
  <a id="5">
    <ref>3</ref>
  </a>
  <a id="3">
    <ref>5</ref>
  </a>
</root>

Then, you need to find combinations of <a>, which don't have any contacts with any other <a>.

First, you create a function that recursively can turn element <a> into a line of its dependencies:

  <xsl:template name="group">
    <xsl:param name="x" as="element()"/>
    <xsl:param name="seen"/>
    <xsl:element name="group">
      <xsl:attribute name="id">
        <xsl:value-of select="$x/@id"/>
      </xsl:attribute>
      <xsl:for-each select="$x/ref">
        <xsl:variable name="r" select="."/>
        <xsl:if test="not($seen[@id=$r])">
          <xsl:element name="e">
            <xsl:value-of select="$r"/>
          </xsl:element>
          <xsl:call-template name="group">
            <xsl:with-param name="x" select="/root/a[@id=$r]"/>
            <xsl:with-param name="seen">
              <xsl:copy-of select="$seen"/>
              <xsl:copy-of select="$r"/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:if>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

This code will produce something like this (unsorted!):

<group id="1">
  <e>2</e>
  <e>1</e>
  <e>4</e>
</group>

Then, you need to collect all groups in a package, making sure each group is sorted inside:

  <xsl:template name="groups">
    <xsl:param name="root"/>
    <xsl:element name="groups">
      <xsl:for-each select="$root/a">
        <xsl:variable name="g">
          <xsl:call-template name="group">
            <xsl:with-param name="x" select="."/>
            <xsl:with-param name="seen"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:element name="group">
          <xsl:for-each select="$g/e">
            <xsl:sort select="."/>
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

This will produce something like this (elements <e> inside each group are sorted!):

<groups>
  <group id="1">
    <e>1</e>
    <e>2</e>
    <e>4</e>
  </group>
  <group id="3">
    <e>3</e>
    <e>5</e>
  </group>
</groups>

The rest is easy, I believe. You just select unique groups.

@iMaks99
Copy link

iMaks99 commented May 23, 2021

@yegor256 we made a transformation of data in skeleton:

<graph>
  <v name="one">
    <edge>
      <method>one</method>
      <method>two</method>
    </edge>
    <edge>
      <method>one</method>
      <method>four</method>
    </edge>
    <edge>
      <method>two</method>
      <method>one</method>
    </edge>
    <edge>
      <method>four</method>
      <method>one</method>
    </edge>
  </v>
  <v name="two">
    <edge>
      <method>one</method>
      <method>two</method>
    </edge>
    <edge>
      <method>two</method>
      <method>four</method>
    </edge>
    <edge>
      <method>two</method>
      <method>one</method>
    </edge>
    <edge>
      <method>four</method>
      <method>two</method>
    </edge>
  </v>
  <v name="four">
    <edge>
      <method>one</method>
      <method>four</method>
    </edge>
    <edge>
      <method>two</method>
      <method>four</method>
    </edge>
    <edge>
      <method>four</method>
      <method>one</method>
    </edge>
    <edge>
      <method>four</method>
      <method>two</method>
    </edge>
  </v>
  <v name="three">
    <edge>
      <method>three</method>
      <method>five</method>
    </edge>
    <edge>
      <method>five</method>
      <method>three</method>
    </edge>
  </v>
  <v name="five">
    <edge>
      <method>three</method>
      <method>five</method>
    </edge>
    <edge>
      <method>five</method>
      <method>three</method>
    </edge>
  </v>
</graph>

and this data is storing as xsl:variable, how can we call templates above?

@yegor256
Copy link
Member

@iMaks99 I'm not sure I understand the question (strongly recommend to use Stackoverflow for such questions), but I would do:

<xsl:call-template>
  <xsl:with-param name="x">
    <!-- here goes the variable -->
  </xsl:with-param>
</xsl:call-template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants