All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.graphmatching.PatternMatch Maven / Gradle / Ivy

There is a newer version: 3.2.0-alpha02
Show newest version
/*
 * Copyright (c) 2002-2017 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.graphmatching;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;

/**
 * Represents one match found by the {@link PatternMatcher}. The match is
 * itself a graph which looks like the pattern fed to the {@link PatternMatcher}
 */
@Deprecated
public class PatternMatch
{
	private Map elements =
		new HashMap();
	private Map relElements =
        new HashMap();

	PatternMatch( Map elements,
        Map relElements )
	{
		this.elements = elements;
        this.relElements = relElements;
	}

	/**
	 * @param node the {@link PatternNode} to get the {@link Node} for.
	 * @return the actual {@link Node} for this particular match, represented
     * by {@code node} in the pattern
	 */
	public Node getNodeFor( PatternNode node )
	{
		return elements.containsKey( node ) ?
			elements.get( node ).getNode() : null;
	}

    /**
     * @param rel the {@link PatternRelationship} to get the
     * {@link Relationship} for.
     * @return the actual {@link Relationship} for this particular match,
     * represented by {@code rel} in the pattern
     */
    public Relationship getRelationshipFor( PatternRelationship rel )
    {
        return relElements.containsKey( rel ) ?
            relElements.get( rel ) : null;
    }

    /**
     * Get the matched elements in this match.
     *
     * @return an iterable over the matched elements in this match instance.
     */
	public Iterable getElements()
	{
		return elements.values();
	}

	/**
	 * Used to merge two matches. An example is to merge in an "optional"
	 * subgraph match into a match.
	 * @param matches the matches to merge together.
	 * @return the merged matches as one match.
	 */
	public static PatternMatch merge( Iterable matches )
	{
		Map matchMap =
			new HashMap();
        Map relElements =
            new HashMap();
		for ( PatternMatch match : matches )
		{
			for ( PatternNode node : match.elements.keySet() )
			{
				boolean exists = false;
				for ( PatternNode existingNode : matchMap.keySet() )
				{
					if ( node.getLabel().equals( existingNode.getLabel() ) )
					{
						exists = true;
						break;
					}
				}
				if ( !exists )
				{
					matchMap.put( node, match.elements.get( node ) );
                    relElements.put(
                        match.elements.get( node ).getFromPatternRelationship(),
                        match.elements.get( node ).getFromRelationship() );
				}
			}
		}
		PatternMatch mergedMatch = new PatternMatch( matchMap, relElements );
		return mergedMatch;
	}

    /**
     * Used to merge matches. An example is to merge in an "optional" subgraph
     * match into a match.
     * 
     * @param matches the matches to merge together.
     * @return the merged matches as one match.
     */
	public static PatternMatch merge( PatternMatch... matches )
	{
		return merge( Arrays.asList( matches ) );
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy