<commit_before>package at.ac.tuwien.kr.alpha.common.depgraph;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.antlr.v4.runtime.CharStreams;
import org.junit.Assert;
import org.junit.Test;

import at.ac.tuwien.kr.alpha.Alpha;
import at.ac.tuwien.kr.alpha.common.Predicate;
import at.ac.tuwien.kr.alpha.common.Program;
import at.ac.tuwien.kr.alpha.common.depgraph.io.DependencyGraphWriter;
import at.ac.tuwien.kr.alpha.grounder.parser.ProgramParser;
import at.ac.tuwien.kr.alpha.grounder.structure.ProgramAnalysis;

public class DependencyGraphTest {

	@SuppressWarnings("unused")
	private static String generateRandomProgram(int numRules, int numPredicates, int maxRuleBodyLiterals) {
		String[] predicates = new String[numPredicates];
		for (int i = 0; i < predicates.length; i++) {
			predicates[i] = "p" + Integer.toString(i + 1);
		}

		StringBuilder prgBuilder = new StringBuilder();
		String tmpAtom;
		int tmpBodyLiterals;
		for (int i = 0; i < numRules; i++) {
			tmpBodyLiterals = 1 + ((int) (Math.random() * maxRuleBodyLiterals));
			tmpAtom = predicates[(int) (Math.random() * predicates.length)];
			prgBuilder.append(tmpAtom).append(" :- ");
			for (int j = 0; j < tmpBodyLiterals; j++) {
				tmpAtom = predicates[(int) (Math.random() * predicates.length)];
				prgBuilder.append(tmpAtom);
				if (j < (tmpBodyLiterals - 1)) {
					prgBuilder.append(", ");
				}
			}
			prgBuilder.append(".");
			prgBuilder.append("\n");
		}
		return prgBuilder.toString();
	}

	@Test
	@Ignore("Not a real test, rather a playground for local testing while changing stuff")
	public void dependencyGraphSmokeTest() throws IOException {
		InputStream is = DependencyGraphTest.class.getResourceAsStream("/components-test.asp");
		Program p = new ProgramParser().parse(CharStreams.fromStream(is));
		ProgramAnalysis pa = new ProgramAnalysis(p);
		DependencyGraph dg = DependencyGraph.buildDependencyGraph(pa.getNonGroundRules());
		DependencyGraphWriter dgw = new DependencyGraphWriter();
		dgw.writeAsDotfile(dg, "/tmp/components-test.asp.dg.dot", true);
	}

	/**
	 * make sure that all internal data structures in dependency graph use the same node instances and no useless copies are created
	 * 
	 * @throws IOException
	 */
	@Test
	public void noNodesCopiedTest() throws IOException {
		Program prog = new ProgramParser().parse(CharStreams.fromStream(DependencyGraphTest.class.getResourceAsStream("/components-test.asp")));
		ProgramAnalysis pa = new ProgramAnalysis(prog);
		DependencyGraph dg = pa.getDependencyGraph();
		for (Node refNode : dg.getNodes().keySet()) {
			Assert.assertEquals(refNode, dg.getNodesByPredicate().get(refNode.getPredicate()));
			Assert.assertEquals(true, refNode == dg.getNodesByPredicate().get(refNode.getPredicate()));
		}
		for (Node refNode : dg.getTransposedNodes().keySet()) {
			Assert.assertEquals(refNode, dg.getNodesByPredicate().get(refNode.getPredicate()));
			Assert.assertEquals(true, refNode == dg.getNodesByPredicate().get(refNode.getPredicate()));
		}
		for(Entry<Integer, List<Node>> componentEntry : dg.getStronglyConnectedComponents().entrySet()) {
			for(Node refNode : componentEntry.getValue()) {
				Assert.assertEquals(refNode, dg.getNodesByPredicate().get(refNode.getPredicate()));
				Assert.assertEquals(true, refNode == dg.getNodesByPredicate().get(refNode.getPredicate()));
			}
		}
	}

	@Test
	public void edgesEqualTest() {
		Predicate testPredicate = Predicate.getInstance("test", 2, false, false);
		Edge e1 = new Edge(new Node(testPredicate, testPredicate.toString()), true, null);
		Edge e2 = new Edge(new Node(testPredicate, testPredicate.toString()), true, null);
		Assert.assertEquals(e1, e2);
	}

	@Test
	public void dependencyGraphTransposeSimpleTest() {
		Alpha system = new Alpha();
		Program prog = system.readProgramString("b :- a.", null);
		ProgramAnalysis analysis = new ProgramAnalysis(prog);
		DependencyGraph dg = analysis.getDependencyGraph();<commit_msg>re-ignored dependency graph smoke test<commit_after>package at.ac.tuwien.kr.alpha.common.depgraph;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.antlr.v4.runtime.CharStreams;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import at.ac.tuwien.kr.alpha.Alpha;
import at.ac.tuwien.kr.alpha.common.Predicate;
import at.ac.tuwien.kr.alpha.common.Program;
import at.ac.tuwien.kr.alpha.common.depgraph.io.DependencyGraphWriter;
import at.ac.tuwien.kr.alpha.grounder.parser.ProgramParser;
import at.ac.tuwien.kr.alpha.grounder.structure.ProgramAnalysis;

public class DependencyGraphTest {

	@SuppressWarnings("unused")
	private static String generateRandomProgram(int numRules, int numPredicates, int maxRuleBodyLiterals) {
		String[] predicates = new String[numPredicates];
		for (int i = 0; i < predicates.length; i++) {
			predicates[i] = "p" + Integer.toString(i + 1);
		}

		StringBuilder prgBuilder = new StringBuilder();
		String tmpAtom;
		int tmpBodyLiterals;
		for (int i = 0; i < numRules; i++) {
			tmpBodyLiterals = 1 + ((int) (Math.random() * maxRuleBodyLiterals));
			tmpAtom = predicates[(int) (Math.random() * predicates.length)];
			prgBuilder.append(tmpAtom).append(" :- ");
			for (int j = 0; j < tmpBodyLiterals; j++) {
				tmpAtom = predicates[(int) (Math.random() * predicates.length)];
				prgBuilder.append(tmpAtom);
				if (j < (tmpBodyLiterals - 1)) {
					prgBuilder.append(", ");
				}
			}
			prgBuilder.append(".");
			prgBuilder.append("\n");
		}
		return prgBuilder.toString();
	}

	@Test
	@Ignore("Not a real test, rather a playground for local testing while changing stuff")
	public void dependencyGraphSmokeTest() throws IOException {
		InputStream is = DependencyGraphTest.class.getResourceAsStream("/components-test.asp");
		Program p = new ProgramParser().parse(CharStreams.fromStream(is));
		ProgramAnalysis pa = new ProgramAnalysis(p);
		DependencyGraph dg = DependencyGraph.buildDependencyGraph(pa.getNonGroundRules());
		DependencyGraphWriter dgw = new DependencyGraphWriter();
		dgw.writeAsDotfile(dg, "/tmp/components-test.asp.dg.dot", true);
	}

	/**
	 * make sure that all internal data structures in dependency graph use the same node instances and no useless copies are created
	 * 
	 * @throws IOException
	 */
	@Test
	public void noNodesCopiedTest() throws IOException {
		Program prog = new ProgramParser().parse(CharStreams.fromStream(DependencyGraphTest.class.getResourceAsStream("/components-test.asp")));
		ProgramAnalysis pa = new ProgramAnalysis(prog);
		DependencyGraph dg = pa.getDependencyGraph();
		for (Node refNode : dg.getNodes().keySet()) {
			Assert.assertEquals(refNode, dg.getNodesByPredicate().get(refNode.getPredicate()));
			Assert.assertEquals(true, refNode == dg.getNodesByPredicate().get(refNode.getPredicate()));
		}
		for (Node refNode : dg.getTransposedNodes().keySet()) {
			Assert.assertEquals(refNode, dg.getNodesByPredicate().get(refNode.getPredicate()));
			Assert.assertEquals(true, refNode == dg.getNodesByPredicate().get(refNode.getPredicate()));
		}
		for (Entry<Integer, List<Node>> componentEntry : dg.getStronglyConnectedComponents().entrySet()) {
			for (Node refNode : componentEntry.getValue()) {
				Assert.assertEquals(refNode, dg.getNodesByPredicate().get(refNode.getPredicate()));
				Assert.assertEquals(true, refNode == dg.getNodesByPredicate().get(refNode.getPredicate()));
			}
		}
	}

	@Test
	public void edgesEqualTest() {
		Predicate testPredicate = Predicate.getInstance("test", 2, false, false);
		Edge e1 = new Edge(new Node(testPredicate, testPredicate.toString()), true, null);
		Edge e2 = new Edge(new Node(testPredicate, testPredicate.toString()), true, null);
		Assert.assertEquals(e1, e2);
	}

	@Test
	public void dependencyGraphTransposeSimpleTest() {
		Alpha system = new Alpha();
		Program prog = system.readProgramString("b :- a.", null);
		ProgramAnalysis analysis = new ProgramAnalysis(prog);
		DependencyGraph dg = analysis.getDependencyGraph();
{'fraction_non_alphanumeric': 0.1351947097722263, 'fraction_numerical': 0.0029390154298310064, 'mean_word_length': 5.140601503759399, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '378506', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2539, 'n_words': 535}