org.biopax.validator.rules.DanglingElementRule.java Source code

Java tutorial

Introduction

Here is the source code for org.biopax.validator.rules.DanglingElementRule.java

Source

package org.biopax.validator.rules;

/*
 * #%L
 * BioPAX Validator
 * %%
 * Copyright (C) 2008 - 2013 University of Toronto (baderlab.org) and Memorial Sloan-Kettering Cancer Center (cbio.mskcc.org)
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.util.Collection;
import java.util.HashSet;

import org.biopax.paxtools.controller.AbstractTraverser;
import org.biopax.paxtools.controller.PropertyEditor;
import org.biopax.paxtools.model.BioPAXElement;
import org.biopax.paxtools.model.Model;
import org.biopax.paxtools.model.level3.Pathway;
import org.biopax.validator.api.AbstractRule;
import org.biopax.validator.api.beans.Validation;
import org.biopax.paxtools.controller.SimpleEditorMap;
import org.springframework.stereotype.Component;

/**
 * Checks whether the element is referenced from others.
 * 
 */
@Component
public class DanglingElementRule extends AbstractRule<Model> {

    public boolean canCheck(Object thing) {
        return thing instanceof Model;
    }

    public void check(final Validation validation, Model model) {

        // get all the root elements
        final Collection<BioPAXElement> rootElements = new HashSet<BioPAXElement>(model.getObjects());

        // extends traverser ;)
        AbstractTraverser checker = new AbstractTraverser(SimpleEditorMap.get(model.getLevel())) {
            @Override
            protected void visit(Object value, BioPAXElement parent, Model model, PropertyEditor editor) {
                if (value instanceof BioPAXElement)
                    rootElements.remove(value); // found, i.e., it is used by another element.
            }
        };

        // this removes those elements that are referenced from others
        for (BioPAXElement e : model.getObjects()) {
            checker.traverse(e, model);
        }

        // those left are in fact dangling!
        for (BioPAXElement thing : rootElements) {
            if (!(thing instanceof Pathway))
                error(validation, thing, "dangling.element", false);
        }

    }

}