package MpmasQLConstraintAnalysis;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use MpmasInputfile;
use MpmasInputBlock;
our @ISA = qw(MpmasInputfile);

sub filename {
	return "ConstraintAnalysis";
}

sub required {
	return 1;
}

sub write {
	my $self = shift;
	
	my $outdir = $self->outdir;
	my $mname = $self->mname;
	print "\e[K \r";
	print "Writing Constraint Analysis file...\r";
	open(my $lysout, ">",  $outdir.$mname."_MatrixAnalysis.txt") or die "Can't open $outdir.$mname._MatrixAnalysis.txt $!";
	for my $c ($self->sorted_constraints) {
		my $k = $self->constraint($c);
		my $crow = $k->lp_row;
		my @AffectedActivities;
		my $supply = 0;
		ACT: for my $r ($self->sorted_activities) {
			my $i =$self->activity($r);
			my $ccol = $i->lp_column;
			if (defined $i->coeff($c) and $i->coeff($c) > 0) {
				push	@AffectedActivities, [($r,$ccol)]
			}
			elsif (defined $i->coeff($c) and  $i->coeff($c) < 0 ) {
				$supply = 1;
				last ACT;
			}
		}	
		if ($supply == 0) {
			print $lysout "Undelivered Constraint: $c ($crow)\n";
			print $lysout "\t-> Dependent Activities:\n";
			for my $ccc (@AffectedActivities) {
				print $lysout "\t\t${$ccc}[0] (${$ccc}[1])\n";
				
			}
			print $lysout "\n\n";
		}
	}
	close $lysout;

}


1;
