Java Code Examples for org.neo4j.graphdb.Node#hasLabel()

The following examples show how to use org.neo4j.graphdb.Node#hasLabel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReferenceExtractor.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
public void run(GraphDatabaseService db) {
    this.db = db;
    codeIndexes = new CodeIndexes(db);
    try (Transaction tx=db.beginTx()){
    	for (Node node:db.getAllNodes()){
    		if (!node.hasProperty(TextExtractor.IS_TEXT)||!(boolean)node.getProperty(TextExtractor.IS_TEXT))
                continue;
    		if (node.hasLabel(Label.label(JavaCodeExtractor.CLASS)))
    			continue;
    		if (node.hasLabel(Label.label(JavaCodeExtractor.METHOD)))
    			continue;
    		if (node.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)))
    			continue;
    		if (node.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
    			continue;
    		textNodes.add(node);
    	}
    	fromHtmlToCodeElement();
    	fromTextToJira();
    	fromDiffToCodeElement();
    	tx.success();
    }
}
 
Example 2
Source File: ReferenceExtractor.java    From SnowGraph with Apache License 2.0 6 votes vote down vote up
private void fromTextToJira(){
	Map<String, Node> jiraMap=new HashMap<>();
	try (Transaction tx = db.beginTx()) {
		for (Node node:db.getAllNodes()){
			if (node.hasLabel(Label.label(JiraExtractor.ISSUE))){
				String name=(String) node.getProperty(JiraExtractor.ISSUE_NAME);
				jiraMap.put(name, node);
			}
		}
		tx.success();
	}
	try (Transaction tx = db.beginTx()) {
        for (Node srcNode : textNodes) {
            String content = text(srcNode);
            Set<String> tokenSet=new HashSet<>();
            for (String e:content.split("[^A-Za-z0-9\\-_]+"))
            	tokenSet.add(e);
            for (String jiraName:jiraMap.keySet()){
            	if (tokenSet.contains(jiraName))
            		srcNode.createRelationshipTo(jiraMap.get(jiraName), RelationshipType.withName(REFERENCE));
            }
        }
        tx.success();
	}
}
 
Example 3
Source File: Neo4jUtil.java    From trainbenchmark with Eclipse Public License 1.0 6 votes vote down vote up
public static Iterable<Node> getAdjacentNodes(final Node sourceNode, final RelationshipType relationshipType, final Direction direction, final Label targetNodeLabel) {
	final Collection<Node> nodes = new ArrayList<>();
	
	final Iterable<Relationship> relationships = sourceNode.getRelationships(relationshipType, direction);
	for (final Relationship relationship : relationships) {
		final Node candidate;
		switch (direction) {
		case INCOMING:
			candidate = relationship.getStartNode();
			break;
		case OUTGOING:
			candidate = relationship.getEndNode();			
			break;
		default:
			throw new UnsupportedOperationException("Direction: " + direction + " not supported.");
		}
		if (!candidate.hasLabel(targetNodeLabel)) {
			continue;
		}
		nodes.add(candidate);
	}
	return nodes;
}
 
Example 4
Source File: Neo4jIndexer.java    From elasticsearch-river-neo4j with Apache License 2.0 6 votes vote down vote up
public void index() {
    version = getVersion();

    logger.debug("Awake and about to poll...");
    Iterable<Node> r = db.getAllNodes();
    for (Node node : r) {
        // If labels exists, filter nodes by label
        if(labels.size() > 0) {
            for (Label label : labels) {
                if(node.hasLabel(label)) {
                    worker.queue(new IndexOperation(indexingStrategy, index, type, node, version));
                }
            }
        } else {
            worker.queue(new IndexOperation(indexingStrategy, index, type, node, version));
        }
    }
    logger.debug("...polling completed");

    logger.debug("Deleting all nodes with version < {}", version);
    worker.queue(new ExpungeOperation(deletingStategy, index, type, version));
}
 
Example 5
Source File: TransExtractor.java    From SnowGraph with Apache License 2.0 5 votes vote down vote up
private void prepare() {
    List<String> entities = new ArrayList<>();
    List<String> relations = new ArrayList<>();
    List<Triple<String, String, String>> triples = new ArrayList<>();
    try (Transaction tx = db.beginTx()) {
        for (Node node : db.getAllNodes()) {
            if (!node.hasLabel(Label.label(JavaCodeExtractor.CLASS)) &&
                    !node.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) &&
                    !node.hasLabel(Label.label(JavaCodeExtractor.METHOD)) &&
                    !node.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
                continue;
            entities.add("" + node.getId());
        }

        for (Relationship rel : db.getAllRelationships()) {
            Node node1 = rel.getStartNode();
            if (!node1.hasLabel(Label.label(JavaCodeExtractor.CLASS)) &&
                    !node1.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) &&
                    !node1.hasLabel(Label.label(JavaCodeExtractor.METHOD)) &&
                    !node1.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
                continue;
            Node node2 = rel.getEndNode();
            if (!node2.hasLabel(Label.label(JavaCodeExtractor.CLASS)) &&
                    !node2.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) &&
                    !node2.hasLabel(Label.label(JavaCodeExtractor.METHOD)) &&
                    !node2.hasLabel(Label.label(JavaCodeExtractor.FIELD)))
                continue;
            triples.add(new ImmutableTriple<>("" + node1.getId(), "" + node2.getId(), rel.getType().name()));
            if (!relations.contains(rel.getType().name()))
                relations.add(rel.getType().name());
        }
        tx.success();
    }
    transE.prepare(entities, relations, triples);
}
 
Example 6
Source File: Neo4JApiQuerySwitchMonitored.java    From trainbenchmark with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Collection<Neo4jSwitchMonitoredMatch> evaluate() {
	final Collection<Neo4jSwitchMonitoredMatch> matches = new ArrayList<>();

	final GraphDatabaseService graphDb = driver.getGraphDb();
	try (Transaction tx = graphDb.beginTx()) {
		final Iterable<Node> sws = () -> graphDb.findNodes(Neo4jConstants.labelSwitch);
		// (sw:Switch)
		for (final Node sw : sws) {
			// (sw)-[:sensor]->(Sensor) NAC
			final Iterable<Relationship> relationshipSensors = sw.getRelationships(Direction.OUTGOING, Neo4jConstants.relationshipTypeMonitoredBy);

			boolean hasSensor = false;
			for (final Relationship relationshipSensor : relationshipSensors) {
				final Node sensor = relationshipSensor.getEndNode();
				if (sensor.hasLabel(Neo4jConstants.labelSensor)) {
					hasSensor = true;
					break;
				}
			}

			if (!hasSensor) {
				final Map<String, Object> match = new HashMap<>();
				match.put(VAR_SW, sw);
				matches.add(new Neo4jSwitchMonitoredMatch(match));
			}
		}
	}

	return matches;
}
 
Example 7
Source File: Clique.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
private boolean containsOneLabel(Node n, Set<Label> labels) {
  for (Label l : labels) {
    if (n.hasLabel(l)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: CodeIndexes.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
public CodeIndexes(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        ResourceIterator<Node> nodes = db.getAllNodes().iterator();
        Set<Node> codeNodes = new HashSet<>();
        while (nodes.hasNext()) {
            Node node = nodes.next();
            if (node.hasLabel(Label.label(JavaCodeExtractor.CLASS)) || node.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)) || node.hasLabel(Label.label(JavaCodeExtractor.METHOD))) {
                codeNodes.add(node);
            }
        }

        for (Node codeNode : codeNodes) {
            String name = "";
            boolean type = true;
            if (codeNode.hasLabel(Label.label(JavaCodeExtractor.CLASS)))
                name = (String) codeNode.getProperty(JavaCodeExtractor.CLASS_FULLNAME);
            if (codeNode.hasLabel(Label.label(JavaCodeExtractor.INTERFACE)))
                name = (String) codeNode.getProperty(JavaCodeExtractor.INTERFACE_FULLNAME);
            if (codeNode.hasLabel(Label.label(JavaCodeExtractor.METHOD))) {
                name = codeNode.getProperty(JavaCodeExtractor.METHOD_BELONGTO) + "." + codeNode.getProperty(JavaCodeExtractor.METHOD_NAME);
                type = false;
            }
            if (name.contains("$"))
                continue;
            if (type) {
                typeMap.put(name, codeNode.getId());
                idToTypeNameMap.put(codeNode.getId(), name);
                String shortName = name;
                int p = shortName.lastIndexOf('.');
                if (p > 0)
                    shortName = shortName.substring(p + 1, shortName.length());
                if (!typeShortNameMap.containsKey(shortName))
                    typeShortNameMap.put(shortName, new HashSet<>());
                typeShortNameMap.get(shortName).add(codeNode.getId());
            } else {
                if (!methodMap.containsKey(name))
                    methodMap.put(name, new HashSet<>());
                methodMap.get(name).add(codeNode.getId());
                idToMethodNameMap.put(codeNode.getId(), name);
                int p1 = name.lastIndexOf('.');
                int p2 = name.lastIndexOf('.', p1 - 1);
                String midName, shortName;
                if (p2 > 0) {
                    midName = name.substring(p2 + 1);
                    shortName = name.substring(p1 + 1);
                } else {
                    midName = name;
                    shortName = name.substring(p1 + 1);
                }
                if (!methodMidNameMap.containsKey(midName))
                    methodMidNameMap.put(midName, new HashSet<>());
                methodMidNameMap.get(midName).add(codeNode.getId());
                if (!methodShortNameMap.containsKey(shortName))
                    methodShortNameMap.put(shortName, new HashSet<>());
                methodShortNameMap.get(shortName).add(codeNode.getId());
            }
        }

        tx.success();
    }
}
 
Example 9
Source File: Clique.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
private void markAsCliqueLeader(Node n) {
  if (!n.hasLabel(CLIQUE_LEADER_LABEL)) {
    n.addLabel(CLIQUE_LEADER_LABEL);
  }
}