<commit_before>package petrinet.snet;

import java.util.HashSet;
import java.util.Set;

import misc.SetUtils;

import petrinet.cpn.abstr.AbstractCPN;
import validate.ParameterException;
import exception.PNValidationException;

public class DeclassificationTransition extends AbstractSNetTransition{
	
	protected SNet sNet = null;
	
	public DeclassificationTransition(SNet sNet, String name, String label, boolean isEmpty) throws ParameterException {
		super(name, label, isEmpty);
		this.sNet = sNet;
	}

	public DeclassificationTransition(SNet sNet, String name, boolean isEmpty) throws ParameterException {
		this(sNet, name, name, isEmpty);
	}

	public DeclassificationTransition(SNet sNet, String name, String label) throws ParameterException {
		this(sNet, name, label, false);
	}

	public DeclassificationTransition(SNet sNet, String name) throws ParameterException {
		this(sNet, name, name, false);
	}
	
	@Override
	public boolean isDeclassificator() {
		return true;
	}
	
	@Override
	public void checkValidity() throws PNValidationException{
		super.checkValidity();
		
		// Property 1: Only one input and output place
		if(getIncomingRelations().size() > 1)
			throw new PNValidationException("Declassificators can only have one input place.");
		if(getOutgoingRelations().size() > 1)
			throw new PNValidationException("Declassificators can only have one output place.");
		
		// Property 2: The declassification transition must be effective, i.e. 
		//             consume at least one colored token (which is not the control flow token).
		Set<String> consumedAttributes = getConsumedAttributes();
		if(consumedAttributes.isEmpty())
			throw new PNValidationException("Declassificators must consume at least one colored token (which is not the control flow token).");
		
		// Property 3: The sets of consumed colors and produced colors have no elements in common
		
		if(!SetUtils.intersection(getConsumedAttributes(), getProducedAttributes()).isEmpty())
			throw new PNValidationException("The sets of consumed and produced token colors must not have common elements.");
		
		// Property 4: No other net transition creates a token with the same color than any of the produced colors of this transition.
		//      (Either as regular transition with CREATE mode or as declassification transition)
		Set<AbstractSNetTransition> otherNetTransitions = new HashSet<AbstractSNetTransition>();
		otherNetTransitions.addAll(sNet.getTransitions());
		otherNetTransitions.remove(this);
		for(AbstractSNetTransition otherTransition: otherNetTransitions){
			if(otherTransition.isDeclassificator()){
				for(String color: getProducedAttributes()){
					if(otherTransition.producesColor(color))
						throw new PNValidationException("There is another declassification transition which produces color \""+color+"\"");
				}
			} else {
				try{
				for(String color: getProducedAttributes()){
					if(otherTransition.producesColor(color) && ((RegularSNetTransition) otherTransition).getAccessModes(color).contains(AccessMode.CREATE))
						throw new PNValidationException("There is another net transition which creates tokens of color \""+color+"\"");
				}
				}catch(ParameterException e){
					e.printStackTrace();
				}
			}
		}
		
		// Property 5: Token color constraints
		consumedAttributes = getConsumedAttributes();
		Set<String> producedColors = getProducedAttributes();
		// 4 a) For each input token color there is one output token color.
		if(consumedAttributes.size() != producedColors.size())
			throw new PNValidationException("The number of consumed token colors does not match the number of produced token colors.");
		// 4 b) The number of consumed tokens for each color matches the number of produced tokens for another color.
		Set<String> assignedAttributes = new HashSet<String>();
		for(String consumedAttribute: consumedAttributes){
			for(String producedColor: producedColors){
				if(!assignedAttributes.contains(producedColor) && getConsumedTokens(consumedAttribute) == getProducedTokens(producedColor)){
					assignedAttributes.add(consumedAttribute);
					break;
				}
			}
		}
		if(!assignedAttributes.containsAll(consumedAttributes))
			throw new PNValidationException("For at least one input token color, there is no output token color where the number of produced/consumed tokens equals.");
		
	}  

	@Override
	public String toPNML() {
		// TODO Auto-generated method stub
		return null;
	}

}<commit_msg>Variable renaming in class DeclassificationTransition<commit_after>package petrinet.snet;

import java.util.HashSet;
import java.util.Set;

import misc.SetUtils;

import petrinet.cpn.abstr.AbstractCPN;
import validate.ParameterException;
import exception.PNValidationException;

public class DeclassificationTransition extends AbstractSNetTransition{
	
	protected SNet sNet = null;
	
	public DeclassificationTransition(SNet sNet, String name, String label, boolean isEmpty) throws ParameterException {
		super(name, label, isEmpty);
		this.sNet = sNet;
	}

	public DeclassificationTransition(SNet sNet, String name, boolean isEmpty) throws ParameterException {
		this(sNet, name, name, isEmpty);
	}

	public DeclassificationTransition(SNet sNet, String name, String label) throws ParameterException {
		this(sNet, name, label, false);
	}

	public DeclassificationTransition(SNet sNet, String name) throws ParameterException {
		this(sNet, name, name, false);
	}
	
	@Override
	public boolean isDeclassificator() {
		return true;
	}
	
	@Override
	public void checkValidity() throws PNValidationException{
		super.checkValidity();
		
		// Property 1: Only one input and output place
		if(getIncomingRelations().size() > 1)
			throw new PNValidationException("Declassificators can only have one input place.");
		if(getOutgoingRelations().size() > 1)
			throw new PNValidationException("Declassificators can only have one output place.");
		
		// Property 2: The declassification transition must be effective, i.e. 
		//             consume at least one colored token (which is not the control flow token).
		Set<String> consumedAttributes = getConsumedAttributes();
		if(consumedAttributes.isEmpty())
			throw new PNValidationException("Declassificators must consume at least one colored token (which is not the control flow token).");
		
		// Property 3: The sets of consumed colors and produced colors have no elements in common
		
		if(!SetUtils.intersection(getConsumedAttributes(), getProducedAttributes()).isEmpty())
			throw new PNValidationException("The sets of consumed and produced token colors must not have common elements.");
		
		// Property 4: No other net transition creates a token with the same color than any of the produced colors of this transition.
		//      (Either as regular transition with CREATE mode or as declassification transition)
		Set<AbstractSNetTransition> otherNetTransitions = new HashSet<AbstractSNetTransition>();
		otherNetTransitions.addAll(sNet.getTransitions());
		otherNetTransitions.remove(this);
		for(AbstractSNetTransition otherTransition: otherNetTransitions){
			if(otherTransition.isDeclassificator()){
				for(String color: getProducedAttributes()){
					if(otherTransition.producesColor(color))
						throw new PNValidationException("There is another declassification transition which produces color \""+color+"\"");
				}
			} else {
				try{
				for(String color: getProducedAttributes()){
					if(otherTransition.producesColor(color) && ((RegularSNetTransition) otherTransition).getAccessModes(color).contains(AccessMode.CREATE))
						throw new PNValidationException("There is another net transition which creates tokens of color \""+color+"\"");
				}
				}catch(ParameterException e){
					e.printStackTrace();
				}
			}
		}
		
		// Property 5: Token color constraints
		consumedAttributes = getConsumedAttributes();
		Set<String> producedAttributes = getProducedAttributes();
		// 4 a) For each input token color there is one output token color.
		if(consumedAttributes.size() != producedAttributes.size())
			throw new PNValidationException("The number of consumed token colors does not match the number of produced token colors.");
		// 4 b) The number of consumed tokens for each color matches the number of produced tokens for another color.
		Set<String> assignedAttributes = new HashSet<String>();
		for(String consumedAttribute: consumedAttributes){
			for(String producedAttribute: producedAttributes){
				if(!assignedAttributes.contains(producedAttribute) && getConsumedTokens(consumedAttribute) == getProducedTokens(producedAttribute)){
					assignedAttributes.add(consumedAttribute);
					break;
				}
			}
		}
		if(!assignedAttributes.containsAll(consumedAttributes))
			throw new PNValidationException("For at least one input token color, there is no output token color where the number of produced/consumed tokens equals.");
		
	}  

	@Override
	public String toPNML() {
		// TODO Auto-generated method stub
		return null;
	}

}
{'fraction_non_alphanumeric': 0.08655781704316708, 'fraction_numerical': 0.0020129724893759786, 'mean_word_length': 4.910773298083278, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '4933104', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2267, 'n_words': 774}