Skip to content

Commit

Permalink
Add parsing of relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
Mats-SX committed Feb 23, 2016
1 parent 4f38378 commit c534fc3
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 174 deletions.
Expand Up @@ -26,18 +26,18 @@
import java.util.Deque; import java.util.Deque;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;


import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.RelationshipType;


class CypherValuesCreator extends FeatureResultsBaseListener class CypherValuesCreator extends FeatureResultsBaseListener
{ {
private Stack<Object> workload; private Stack<Object> workload;
private Stack<Integer> listCounters; private Stack<Integer> listCounters;
private Stack<Integer> mapCounters; private Stack<Integer> mapCounters;
private List<Label> labels; private Stack<String> names;


private static final String INFINITY = "Inf"; private static final String INFINITY = "Inf";


Expand All @@ -46,7 +46,7 @@ class CypherValuesCreator extends FeatureResultsBaseListener
this.workload = new Stack<>(); this.workload = new Stack<>();
this.listCounters = new Stack<>(); this.listCounters = new Stack<>();
this.mapCounters = new Stack<>(); this.mapCounters = new Stack<>();
this.labels = new ArrayList<>(); this.names = new Stack<>();
} }


Object parsed() Object parsed()
Expand Down Expand Up @@ -155,23 +155,18 @@ public void enterPropertyKey( FeatureResultsParser.PropertyKeyContext ctx )
@Override @Override
public void enterLabelName( FeatureResultsParser.LabelNameContext ctx ) public void enterLabelName( FeatureResultsParser.LabelNameContext ctx )
{ {
labels.add( Label.label( ctx.getText() ) ); names.push( ctx.getText() );
} }


@Override @Override
public void exitNode( FeatureResultsParser.NodeContext ctx ) public void exitNode( FeatureResultsParser.NodeContext ctx )
{ {
final Map<String,Object> properties; final Map<String,Object> properties = getMapOrEmpty();
if ( workload.empty() ) final ArrayList<Label> nodeLabels = new ArrayList<>();
{ while ( !names.empty() )
properties = new HashMap<>();
}
else
{ {
properties = (Map<String,Object>) workload.pop(); nodeLabels.add( Label.label( names.pop() ) );
} }
final ArrayList<Label> nodeLabels = new ArrayList<>( labels );
labels.clear();
workload.push( new ParsedNode() workload.push( new ParsedNode()
{ {
@Override @Override
Expand All @@ -187,4 +182,43 @@ public Iterable<Label> getLabels()
} }
} ); } );
} }

private Map<String,Object> getMapOrEmpty()
{
if ( workload.empty() )
{
return new HashMap<>();
}
else
{
return (Map<String,Object>) workload.pop();
}
}

@Override
public void enterRelationshipTypeName( FeatureResultsParser.RelationshipTypeNameContext ctx )
{
names.push( ctx.getText() );
}

@Override
public void exitRelationship( FeatureResultsParser.RelationshipContext ctx )
{
final Map<String,Object> properties = getMapOrEmpty();
final RelationshipType type = RelationshipType.withName( names.pop() );
workload.push( new ParsedRelationship()
{
@Override
public RelationshipType getType()
{
return type;
}

@Override
public Map<String,Object> getAllProperties()
{
return properties;
}
} );
}
} }
Expand Up @@ -245,4 +245,20 @@ else if ( obj instanceof ParsedNode )
} }
return false; return false;
} }

public static Node parsedNode(final Iterable<Label> labelNames, final Map<String, Object> properties) {
return new ParsedNode() {
@Override
public Map<String,Object> getAllProperties()
{
return properties;
}

@Override
public Iterable<Label> getLabels()
{
return labelNames;
}
};
}
} }
@@ -0,0 +1,169 @@
/*
* Copyright (c) 2002-2016 "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 <http://www.gnu.org/licenses/>.
*/
package cypher.feature.parser;

import java.util.Map;

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

public class ParsedRelationship implements Relationship
{
@Override
public long getId()
{
throw new UnsupportedOperationException();
}

@Override
public void delete()
{
throw new UnsupportedOperationException();
}

@Override
public Node getStartNode()
{
throw new UnsupportedOperationException();
}

@Override
public Node getEndNode()
{
throw new UnsupportedOperationException();
}

@Override
public Node getOtherNode( Node node )
{
throw new UnsupportedOperationException();
}

@Override
public Node[] getNodes()
{
throw new UnsupportedOperationException();
}

@Override
public RelationshipType getType()
{
throw new UnsupportedOperationException();
}

@Override
public boolean isType( RelationshipType type )
{
throw new UnsupportedOperationException();
}

@Override
public GraphDatabaseService getGraphDatabase()
{
throw new UnsupportedOperationException();
}

@Override
public boolean hasProperty( String key )
{
throw new UnsupportedOperationException();
}

@Override
public Object getProperty( String key )
{
throw new UnsupportedOperationException();
}

@Override
public Object getProperty( String key, Object defaultValue )
{
throw new UnsupportedOperationException();
}

@Override
public void setProperty( String key, Object value )
{
throw new UnsupportedOperationException();
}

@Override
public Object removeProperty( String key )
{
throw new UnsupportedOperationException();
}

@Override
public Iterable<String> getPropertyKeys()
{
throw new UnsupportedOperationException();
}

@Override
public Map<String,Object> getProperties( String... keys )
{
throw new UnsupportedOperationException();
}

@Override
public Map<String,Object> getAllProperties()
{
throw new UnsupportedOperationException();
}

@Override
public boolean equals( Object obj )
{
if ( obj == null )
{
return false;
}
else if ( obj instanceof ParsedRelationship )
{
ParsedRelationship other = (ParsedRelationship) obj;

boolean typeEquality = this.getType().equals( other.getType() );
boolean propEquality = getAllProperties().equals( other.getAllProperties() );
return typeEquality && propEquality;
}
return false;
}

public static Relationship parsedRelationship( final RelationshipType type, final Map<String,Object> properties )
{
return new ParsedRelationship()
{
@Override
public Map<String,Object> getAllProperties()
{
return properties;
}

@Override
public RelationshipType getType()
{
return type;
}
};
}

}
Expand Up @@ -258,6 +258,18 @@ public class FeatureResultsBaseListener implements FeatureResultsListener {
* <p>The default implementation does nothing.</p> * <p>The default implementation does nothing.</p>
*/ */
@Override public void exitRelationshipType(FeatureResultsParser.RelationshipTypeContext ctx) { } @Override public void exitRelationshipType(FeatureResultsParser.RelationshipTypeContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void enterRelationshipTypeName(FeatureResultsParser.RelationshipTypeNameContext ctx) { }
/**
* {@inheritDoc}
*
* <p>The default implementation does nothing.</p>
*/
@Override public void exitRelationshipTypeName(FeatureResultsParser.RelationshipTypeNameContext ctx) { }
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
Expand Down
Expand Up @@ -216,6 +216,16 @@ public interface FeatureResultsListener extends ParseTreeListener {
* @param ctx the parse tree * @param ctx the parse tree
*/ */
void exitRelationshipType(FeatureResultsParser.RelationshipTypeContext ctx); void exitRelationshipType(FeatureResultsParser.RelationshipTypeContext ctx);
/**
* Enter a parse tree produced by {@link FeatureResultsParser#relationshipTypeName}.
* @param ctx the parse tree
*/
void enterRelationshipTypeName(FeatureResultsParser.RelationshipTypeNameContext ctx);
/**
* Exit a parse tree produced by {@link FeatureResultsParser#relationshipTypeName}.
* @param ctx the parse tree
*/
void exitRelationshipTypeName(FeatureResultsParser.RelationshipTypeNameContext ctx);
/** /**
* Enter a parse tree produced by {@link FeatureResultsParser#label}. * Enter a parse tree produced by {@link FeatureResultsParser#label}.
* @param ctx the parse tree * @param ctx the parse tree
Expand Down

0 comments on commit c534fc3

Please sign in to comment.