001package org.biopax.paxtools.pattern.constraint;
002
003import org.biopax.paxtools.pattern.Match;
004import org.biopax.paxtools.controller.PathAccessor;
005import org.biopax.paxtools.model.BioPAXElement;
006import org.biopax.paxtools.model.BioPAXLevel;
007
008import java.util.ArrayList;
009import java.util.Collection;
010import java.util.List;
011import java.util.Set;
012
013/**
014 * PathConstraint encapsulates PathAccessor of Paxtools.
015 *
016 * @author Ozgun Babur
017 */
018public class PathConstraint extends ConstraintAdapter
019{
020        /**
021         * Encapsulated PathAccessor.
022         */
023        PathAccessor pa;
024
025        /**
026         * Constructor with the constructor String of PathAccessor.
027         * @param path constructor String of PathAccessor
028         */
029        public PathConstraint(String path)
030        {
031                super(2);
032                this.pa = new PathAccessor(path, BioPAXLevel.L3);
033        }
034
035        /**
036         * Checks if the PathAccessor is generating the second mapped element.
037         * @param match current pattern match
038         * @param ind mapped indices
039         * @return true if second element is generated by PathAccessor
040         */
041        @Override
042        public boolean satisfies(Match match, int... ind)
043        {
044                BioPAXElement ele0 = match.get(ind[0]);
045                BioPAXElement ele1 = match.get(ind[1]);
046
047                if (ele1 == null) return false;
048
049                Set vals = pa.getValueFromBean(ele0);
050                return vals.contains(ele1);
051        }
052
053        /**
054         * This is a generative constraint.
055         * @return true
056         */
057        @Override
058        public boolean canGenerate()
059        {
060                return true;
061        }
062
063        /**
064         * Uses the encapsulated PAthAccessor to generate satisfying elements.
065         * @param match current pattern match
066         * @param ind mapped indices
067         * @return generated elements
068         */
069        @Override
070        public Collection<BioPAXElement> generate(Match match, int ... ind)
071        {
072                BioPAXElement ele0 = match.get(ind[0]);
073
074                if (ele0 == null)
075                        throw new RuntimeException("Constraint cannot generate based on null value");
076
077                Set vals = pa.getValueFromBean(ele0);
078                List<BioPAXElement> list = new ArrayList<BioPAXElement>(vals.size());
079
080                for (Object o : vals)
081                {
082                        assert o instanceof BioPAXElement;
083                        list.add((BioPAXElement) o);
084                }
085                return list;
086        }
087}