/*
Copyright (c) 2013, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package galileo.graph;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * Represents a simple graph path.  A path contains a number of vertices and
 * edges that are connected to form a chain that traverses through a graph.
 *
 * @author malensek
 */
public class Path<L extends Comparable<L>, V>
implements Iterable<Vertex<L, V>> {

    private List<Vertex<L, V>> vertices = new ArrayList<>();
    protected Collection<V> payload = new HashSet<V>();

    /**
     * Create a Path with a number of vertices pre-populated.
     */
    @SafeVarargs
    public Path(Vertex<L, V>... vertices) {
        for (Vertex<L, V> vertex : vertices) {
            this.vertices.add(vertex);
        }
    }

    /**
     * Create a Path with a single payload and number of vertices pre-populated.
     */
    @SafeVarargs
    public Path(V payload, Vertex<L, V>... vertices) {
        this(vertices);
        addPayload(payload);
    }

    /**
     * Create a Path with payload(s) and number of vertices pre-populated.
     */
    @SafeVarargs
    public Path(Collection<V> payload, Vertex<L, V>... vertices) {
        this(vertices);
        setPayload(payload);
    }

    /**
     * Creates a path by copying the vertices from an existing path.
     */
    public Path(Path<L, V> p) {
        /* New Vertices must be created if this path will be used anywhere but
         * its source graph; the type hierarchy is embedded in the vertices. */
        for (Vertex<L, V> v : p.getVertices()) {
            this.vertices.add(new Vertex<L, V>(v));
        }
    }

    public int size() {
        return vertices.size();
    }

    public void add(Vertex<L, V> vertex) {
        vertices.add(vertex);
    }

    public void add(L label, V value) {
        vertices.add(new Vertex<>(label, value));
    }

    public void remove(int index) {
        vertices.remove(index);
    }

    public boolean remove(Vertex<L, V> vertex) {
        return vertices.remove(vertex);
    }

    public Vertex<L, V> get(int index) {
        return vertices.get(index);
    }

    public Vertex<L, V> getTail() {
        return vertices.get(vertices.size() - 1);
    }

    public List<Vertex<L, V>> getVertices() {
        return vertices;
    }

    public Collection<V> getPayload() {
        return payload;
    }

    public void setPayload(Collection<V> payload) {
        this.payload = payload;
    }

    public void addPayload(V payload) {
        this.payload.add(payload);
    }

    public boolean hasPayload() {
        return payload.size() > 0;
    }

    /**
     * Retrieve a list of the {@link Vertex} labels in this Path.
     */
    public List<L> getLabels() {
        List<L> labels = new ArrayList<>();
        for (Vertex<L, V> vertex : vertices) {
            labels.add(vertex.getLabel());
        }

        return labels;
    }

    public void sort(Comparator<? super Vertex<L, V>> c) {
        Collections.sort(vertices, c);
    }

    @Override
    public Iterator<Vertex<L, V>> iterator() {
        return vertices.iterator();
    }

    @Override
    public String toString() {
        String str = "";
        for (int i = 0; i < vertices.size(); ++i) {
            str += vertices.get(i).getLabel();

            if (i < vertices.size() - 1) {
                str += " -> ";
            } else {
                /* Include the path 'payload' */
                str += " " + payload.toString();
            }
        }

        return str;
    }
}

@@ -1336,16 +1336,110 @@
 graph;%0A%0A
+import galileo.query.Expression;%0Aimport galileo.query.Operation;%0Aimport galileo.query.Query;%0A%0A
 import j
@@ -4481,32 +4481,1245 @@
 ces, c);%0A    %7D%0A%0A
+    /**%0A     * Evaluates this path against a provided %7B@link Query%7D instance.  If any of%0A     * the %7B@link Operations%7D in the Query are satisfied by this Path instance,%0A     * this method will return true.%0A     *%0A     * @param query Query to evaluate against this Path instance%0A     *%0A     * @return true if the Query is satisfied by this path, or false otherwise.%0A     */%0A    public boolean satisfiesQuery(Query query) %7B%0A        for (Operation operation : query.getOperations()) %7B%0A            if (this.satisfiesOperation(operation)) %7B%0A                return true;%0A            %7D%0A        %7D%0A%0A        /* None of the query operations were satisfied by this path */%0A        return false;%0A    %7D%0A%0A    private boolean satisfiesOperation(Operation operation) %7B%0A        for (Expression expression : operation.getExpressions()) %7B%0A            if (this.satisfiesExpression(expression) == false) %7B%0A                /* All expressions within an operation must be satisfied. */%0A                return false;%0A            %7D%0A        %7D%0A%0A        /* All Expressions in the Operation were satisfied by this path */%0A        return true;%0A    %7D%0A%0A    private boolean satisfiesExpression(Expression expression) %7B%0A        return false;%0A    %7D%0A%0A
     @Override%0A  

Add path query evaluation functionality