<commit_before>/*******************************************************************************
 * Copyright 2012 University of Southern California
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This code was developed by the Information Integration Group as part 
 * of the Karma project at the Information Sciences Institute of the 
 * University of Southern California.  For more information, publications, 
 * and related projects, please see: http://www.isi.edu/integration
 ******************************************************************************/
package edu.isi.karma.controller.command.alignment;

import java.util.Set;

import org.jgrapht.graph.DirectedWeightedMultigraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.isi.karma.controller.command.Command;
import edu.isi.karma.controller.command.CommandException;
import edu.isi.karma.controller.update.ErrorUpdate;
import edu.isi.karma.controller.update.SVGAlignmentUpdate_ForceKarmaLayout;
import edu.isi.karma.controller.update.SemanticTypesUpdate;
import edu.isi.karma.controller.update.UpdateContainer;
import edu.isi.karma.modeling.alignment.Alignment;
import edu.isi.karma.modeling.alignment.AlignmentManager;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.alignment.Link;
import edu.isi.karma.rep.alignment.LinkStatus;
import edu.isi.karma.rep.alignment.Node;
import edu.isi.karma.view.VWorkspace;

public class AddUserLinkToAlignmentCommand extends Command {

	private final String vWorksheetId;
	private final String edgeId;
	private final String alignmentId;
	private Alignment 	 oldAlignment;
	private DirectedWeightedMultigraph<Node, Link> oldGraph;
	private String edgeLabel;
	
	// private String edgeLabel;
	private static Logger logger = LoggerFactory.getLogger(AddUserLinkToAlignmentCommand.class);

	public AddUserLinkToAlignmentCommand(String id, String edgeId,
			String alignmentId, String vWorksheetId) {
		super(id);
		this.edgeId = edgeId;
		this.alignmentId = alignmentId;
		this.vWorksheetId = vWorksheetId;
		addTag(CommandTag.Modeling);
	}

	@Override
	public String getCommandName() {
		return this.getClass().getSimpleName();
	}

	@Override
	public String getTitle() {
		return "Add User Link";
	}

	@Override
	public String getDescription() {
		return edgeLabel;
	}

	@Override
	public CommandType getCommandType() {
		return CommandType.undoable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UpdateContainer doIt(VWorkspace vWorkspace) throws CommandException {
		Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
		
		Worksheet worksheet = vWorkspace.getViewFactory().getVWorksheet(vWorksheetId).getWorksheet();
		if(alignment == null || alignment.getGraphLinks().size() == 0) {
			logger.error("Alignment cannot be null before calling this command since the alignment is created while " +
					"setting the semantic types.");
			return new UpdateContainer(new ErrorUpdate("Error occured while generating the model for the source."));
		}
		// Save the original alignment for undo
		oldAlignment = alignment.getAlignmentClone();
		oldGraph = (DirectedWeightedMultigraph<Node, Link>)alignment.getGraph().clone();
		
		// Set the other links to the target node to normal
		Link newLink = alignment.getLinkById(edgeId);
		edgeLabel = newLink.getLabel().getDisplayName();
		Set<Link> currentLinks = alignment.getCurrentLinksToNode(newLink.getTarget().getId());
		if (currentLinks != null && !currentLinks.isEmpty()) {
			for (Link currentLink: currentLinks) {
				alignment.changeLinkStatus(currentLink.getId(), LinkStatus.Normal);
			}
		}
		
		// Change the status of the user selected edge
		alignment.changeLinkStatus(edgeId, LinkStatus.ForcedByUser);
		alignment.align();
		
		return getAlignmentUpdateContainer(alignment, worksheet, vWorkspace);
	}

	@Override
	public UpdateContainer undoIt(VWorkspace vWorkspace) {
		Worksheet worksheet = vWorkspace.getViewFactory()
				.getVWorksheet(vWorksheetId).getWorksheet();

		// Revert to the old alignment
		AlignmentManager.Instance().addAlignmentToMap(alignmentId, oldAlignment);
		oldAlignment.setGraph(oldGraph);
		
		// Get the alignment update
		return getAlignmentUpdateContainer(oldAlignment, worksheet, vWorkspace);
	}
<commit_msg>Small bug fix for multiple parents<commit_after>/*******************************************************************************
 * Copyright 2012 University of Southern California
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 	http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * This code was developed by the Information Integration Group as part 
 * of the Karma project at the Information Sciences Institute of the 
 * University of Southern California.  For more information, publications, 
 * and related projects, please see: http://www.isi.edu/integration
 ******************************************************************************/
package edu.isi.karma.controller.command.alignment;

import java.util.Set;

import org.jgrapht.graph.DirectedWeightedMultigraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.isi.karma.controller.command.Command;
import edu.isi.karma.controller.command.CommandException;
import edu.isi.karma.controller.update.ErrorUpdate;
import edu.isi.karma.controller.update.SVGAlignmentUpdate_ForceKarmaLayout;
import edu.isi.karma.controller.update.SemanticTypesUpdate;
import edu.isi.karma.controller.update.UpdateContainer;
import edu.isi.karma.modeling.alignment.Alignment;
import edu.isi.karma.modeling.alignment.AlignmentManager;
import edu.isi.karma.rep.Worksheet;
import edu.isi.karma.rep.alignment.Link;
import edu.isi.karma.rep.alignment.LinkStatus;
import edu.isi.karma.rep.alignment.Node;
import edu.isi.karma.view.VWorkspace;

public class AddUserLinkToAlignmentCommand extends Command {

	private final String vWorksheetId;
	private final String edgeId;
	private final String alignmentId;
	private Alignment 	 oldAlignment;
	private DirectedWeightedMultigraph<Node, Link> oldGraph;
	private String edgeLabel;
	
	// private String edgeLabel;
	private static Logger logger = LoggerFactory.getLogger(AddUserLinkToAlignmentCommand.class);

	public AddUserLinkToAlignmentCommand(String id, String edgeId,
			String alignmentId, String vWorksheetId) {
		super(id);
		this.edgeId = edgeId;
		this.alignmentId = alignmentId;
		this.vWorksheetId = vWorksheetId;
		addTag(CommandTag.Modeling);
	}

	@Override
	public String getCommandName() {
		return this.getClass().getSimpleName();
	}

	@Override
	public String getTitle() {
		return "Add User Link";
	}

	@Override
	public String getDescription() {
		return edgeLabel;
	}

	@Override
	public CommandType getCommandType() {
		return CommandType.undoable;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UpdateContainer doIt(VWorkspace vWorkspace) throws CommandException {
		Alignment alignment = AlignmentManager.Instance().getAlignment(alignmentId);
		
		Worksheet worksheet = vWorkspace.getViewFactory().getVWorksheet(vWorksheetId).getWorksheet();
		if(alignment == null || alignment.getGraphLinks().size() == 0) {
			logger.error("Alignment cannot be null before calling this command since the alignment is created while " +
					"setting the semantic types.");
			return new UpdateContainer(new ErrorUpdate("Error occured while generating the model for the source."));
		}
		// Save the original alignment for undo
		oldAlignment = alignment.getAlignmentClone();
		oldGraph = (DirectedWeightedMultigraph<Node, Link>)alignment.getGraph().clone();
		
		// Set the other links to the target node to normal
		Link newLink = alignment.getLinkById(edgeId);
		edgeLabel = newLink.getLabel().getDisplayName();
		Set<Link> currentLinks = alignment.getCurrentLinksToNode(newLink.getTarget().getId());
		if (currentLinks != null && !currentLinks.isEmpty()) {
			for (Link currentLink: currentLinks) {
				if (currentLink.getSource().getId().equals(newLink.getSource().getId()))
					alignment.changeLinkStatus(currentLink.getId(), LinkStatus.Normal);
			}
		}
		
		// Change the status of the user selected edge
		alignment.changeLinkStatus(edgeId, LinkStatus.ForcedByUser);
		alignment.align();
		
		return getAlignmentUpdateContainer(alignment, worksheet, vWorkspace);
	}

	@Override
	public UpdateContainer undoIt(VWorkspace vWorkspace) {
		Worksheet worksheet = vWorkspace.getViewFactory()
				.getVWorksheet(vWorksheetId).getWorksheet();

		// Revert to the old alignment
		AlignmentManager.Instance().addAlignmentToMap(alignmentId, oldAlignment);
		oldAlignment.setGraph(oldGraph);
		
		// Get the alignment update
		return getAlignmentUpdateContainer(oldAlignment, worksheet, vWorkspace);
	}

{'fraction_non_alphanumeric': 0.12102702143078993, 'fraction_numerical': 0.002277668495703489, 'mean_word_length': 5.297262059973924, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3961508', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2613, 'n_words': 733}