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

The following examples show how to use org.neo4j.graphdb.Node#getId() . 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: Traversals.java    From EvilCoder with MIT License 6 votes vote down vote up
public static List<Node> getChildrenConnectedBy(Node node, String edgeType)
{
	List<Node> retval = new LinkedList<Node>();

	long nodeId = node.getId();

	Iterable<Relationship> rels = node.getRelationships();
	for (Relationship rel : rels)
	{
		if (!rel.getType().name().equals(edgeType))
			continue;
		Node childNode = rel.getEndNode();
		if (childNode.getId() == nodeId)
			continue;

		retval.add(childNode);
	}
	return retval;
}
 
Example 2
Source File: MoveIndicesToIsLatestVertexMigration.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
private void updateQuicksearchIndex(Vres vres, IndexManager indexManager, ObjectMapper mapper, Node node,
                                    Collection wwpersonCollection, GraphTraversalSource traversalSource) {
  for (String type : getEntityTypes(node, mapper)) {
    if (!type.equals("relationtype")) {
      Optional<Collection> collection = vres.getCollectionForType(type);
      long id = node.getId();
      if (collection.isPresent()) {
        String displayName;
        if (type.equals("wwdocument")) {
          displayName = getWwDocumentsQuickSearchValue(collection.get(), wwpersonCollection,
            id, traversalSource
          );
        } else {
          displayName = getGenericQuickSearchValue(collection.get(), id, traversalSource);
        }
        indexManager.forNodes(collection.get().getCollectionName()).add(node, "quickSearch", displayName);
      } else {
        LOG.error("Could not find collection for " + type + " at vertex " + id);
      }
    }
  }
}
 
Example 3
Source File: ReachabilityIndex.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
/**
 * @return The hop coverage for each node sorted in descending order.
 */
SortedSet<Entry<Long, Integer>> getHopCoverages(Predicate<Node> nodePredicate) {
  SortedSet<Entry<Long, Integer>> nodeSet = new TreeSet<Entry<Long, Integer>>(
      new Comparator<Entry<Long, Integer>>() {
        @Override
        public int compare(Entry<Long, Integer> a, Entry<Long, Integer> b) {
          int difference = b.getValue() - a.getValue();
          return (0 != difference) ? difference : (int) (a.getKey() - b.getKey());
        }
      });

  try (Transaction tx = graphDb.beginTx()) {
    for (Node n : graphDb.getAllNodes()) {
      if (n.getId() > 0) {
        int relationshipCount = nodePredicate.apply(n) ? size(n.getRelationships()) : -1;
        nodeSet.add(new AbstractMap.SimpleEntry<Long, Integer>(n.getId(), relationshipCount));
      }
    }
  }

  return nodeSet;
}
 
Example 4
Source File: GraphTransactionalImpl.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@Override
public long createNode(String id) {
  if (idMap.containsKey(id)) {
    return idMap.get(id);
  } else {
    synchronized (graphLock) {
      Node node;
      try (Transaction tx = graphDb.beginTx()) {
        node = graphDb.createNode();
        tx.success();
      }
      idMap.put(id, node.getId());
      return node.getId();
    }
  }
}
 
Example 5
Source File: Find_data_paths.java    From EvilCoder with MIT License 6 votes vote down vote up
public static HashSet<Long> find_inflow_nodes_with_path_length(Joern_db joern_db, Long node_id, Long path_length, HashSet<Long> nodes_in_step_before)
{
ArrayList<Long> start_nodes = new ArrayList(nodes_in_step_before);
  if(!nodes_in_step_before.contains(node_id))
   {
    start_nodes.add(node_id);
   }
List<Node> nodes = Pipeline.v(start_nodes).in("CFG_EDGE").to_list();

HashSet<Long> new_node_ids = new HashSet();
  for(Node n : nodes)
   {
   Long id = n.getId();
     if(!nodes_in_step_before.contains(id))
      {
       new_node_ids.add(id);
      }
   }
 return new_node_ids;
}
 
Example 6
Source File: Traversals.java    From EvilCoder with MIT License 6 votes vote down vote up
public static List<Node> getParentsConnectedBy(Node node, String edgeType)
{
	List<Node> retval = new LinkedList<Node>();

	long nodeId = node.getId();

	Iterable<Relationship> rels = node.getRelationships();
	for (Relationship rel : rels)
	{
		if (!rel.getType().name().equals(edgeType))
			continue;
		Node parentNode = rel.getStartNode();
		if (parentNode.getId() == nodeId)
			continue;

		retval.add(parentNode);
	}
	return retval;
}
 
Example 7
Source File: Traversals.java    From EvilCoder with MIT License 6 votes vote down vote up
public static List<Pair<Long, String>> getIdAndCodeOfChildrenConnectedBy(
		Node node, String edgeType)
{
	List<Pair<Long, String>> retval = new LinkedList<Pair<Long, String>>();
	List<Node> children = getChildrenConnectedBy(node, edgeType);

	for (Node childNode : children)
	{
		String childCode = childNode.getProperty(NodeKeys.CODE).toString();
		Pair<Long, String> pair = new Pair<Long, String>(childNode.getId(),
				childCode);
		retval.add(pair);
	}

	return retval;
}
 
Example 8
Source File: DefUseCFGPatcher.java    From EvilCoder with MIT License 5 votes vote down vote up
public void writeChangesToDatabase()
{
	if (defUseCFG == null)
	{
		return;
	}

	for (DefUseLink link : newlyAddedLinks)
	{
		Long fromId = link.statement;
		Long toId = (Long) defUseCFG.getIdForSymbol(link.symbol);

		if (toId == null)
		{
			Map<String, Object> properties = new HashMap<String, Object>();
			Node statementNode = Neo4JDBInterface.getNodeById(link.statement);
			
			properties.put("functionId", statementNode.getProperty("functionId"));
			properties.put("type", "Symbol");
			properties.put("code", link.symbol);
			
			Node symbolNode = Neo4JDBInterface.addNode(properties);
			toId = (Long) symbolNode.getId();
		}

		RelationshipType relType = DynamicRelationshipType
				.withName(EdgeTypes.DEF);
		Neo4JDBInterface.addRelationship(fromId, toId, relType, null, true); //JANNIK: added flag
	}
	
	
}
 
Example 9
Source File: Traversals.java    From EvilCoder with MIT License 5 votes vote down vote up
public static String getCalleeFromCall(Long nodeId)
{
	Node node = Neo4JDBInterface.getNodeById(nodeId);
	Iterable<Relationship> rels = node.getRelationships();
	for (Relationship rel : rels)
	{
		if (!rel.getType().name().equals(EdgeTypes.IS_AST_PARENT))
			continue;

		Node endNode = rel.getEndNode();

		if (endNode.getId() == node.getId())
			continue;

		try
		{
			String childNumStr = (String) endNode
					.getProperty(NodeKeys.CHILD_NUMBER);
			if (childNumStr.equals("0"))
				return endNode.getProperty(NodeKeys.CODE).toString();
		}
		catch (RuntimeException ex)
		{
			return endNode.getProperty(NodeKeys.CODE).toString();
		}
	}
	return "";
}
 
Example 10
Source File: Neo4jHelper.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
private static String dumpNode(Node node) {
  try {
    ArrayList<String> labels = new ArrayList<>();
    for (Label label : node.getLabels()) {
      if (!"vertex".equals(label.name())) {
        labels.add(label.name());
      }
    }
    return String.format("(%d) [%s] %s ", node.getId(), String.join(", ", labels), dumpProps(node));
  } catch (Exception e) {
    return "(" + node.getId() + ") " + e.getMessage();
  }
}
 
Example 11
Source File: NodeTransformer.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Override
public Concept apply(Node n) {
  try (Transaction tx = n.getGraphDatabase().beginTx()) {
    Concept concept = new Concept(n.getId());
    concept.setIri((String) n.getProperty(Concept.IRI, null));
    concept.setAnonymous(n.hasLabel(OwlLabels.OWL_ANONYMOUS));
    concept.setDeprecated(isDeprecated(n));

    for (String definition : GraphUtil.getProperties(n, Concept.DEFINITION, String.class)) {
      concept.addDefinition(definition);
    }
    for (String abbreviation : GraphUtil.getProperties(n, Concept.ABREVIATION, String.class)) {
      concept.addAbbreviation(abbreviation);
    }
    for (String acronym : GraphUtil.getProperties(n, Concept.ACRONYM, String.class)) {
      concept.addAcronym(acronym);
    }
    for (String category : GraphUtil.getProperties(n, Concept.CATEGORY, String.class)) {
      concept.addCategory(category);
    }
    for (String label : GraphUtil.getProperties(n, Concept.LABEL, String.class)) {
      concept.addLabel(label);
    }
    for (String synonym : GraphUtil.getProperties(n, Concept.SYNONYM, String.class)) {
      concept.addSynonym(synonym);
    }
    for (Label type : n.getLabels()) {
      concept.addType(type.name());
    }

    for (Relationship r: n.getRelationships(OwlRelationships.OWL_EQUIVALENT_CLASS)) {
      Node equivalence = r.getStartNode().equals(n) ? r.getEndNode() : r.getStartNode();
      concept.getEquivalentClasses().add((String)equivalence.getProperty(CommonProperties.IRI));
    }

    tx.success();
    return concept;
  }
}
 
Example 12
Source File: Clique.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
private void moveEdgesToLeader(Node leader, List<Node> clique, Transaction tx) {
  for (Node n : clique) {
    logger.fine("Processing underNode - " + n.getProperty(NodeProperties.IRI));
    // int edgesMoved = 0;
    Iterable<Relationship> rels = n.getRelationships();
    for (Relationship rel : rels) {
      if ((isOneOfType(rel, relationships)) && (rel.getStartNode().getId() == leader.getId()
          || rel.getEndNode().getId() == leader.getId())) {
        logger.fine("equivalence relation which is already attached to the leader, do nothing");
      } else {
        if ((rel.getEndNode().getId() == n.getId())) {
          logger.fine("MOVE TARGET " + rel.getId() + " FROM " + n.getProperty(NodeProperties.IRI)
              + " TO " + leader.getProperty(NodeProperties.IRI));

          moveRelationship(n, leader, rel, ORIGINAL_REFERENCE_KEY_TARGET);
        } else if ((rel.getStartNode().getId() == n.getId())) {
          logger.fine("MOVE SOURCE " + rel.getId() + " FROM " + n.getProperty(NodeProperties.IRI)
              + " TO " + leader.getProperty(NodeProperties.IRI));

          moveRelationship(n, leader, rel, ORIGINAL_REFERENCE_KEY_SOURCE);
        }
      }
      // edgesMoved += 1;
      //
      // if (edgesMoved >= 100) { // Commit for nodes with many edges
      // logger.fine("rel batch commit for leader " + leader.getProperty(NodeProperties.IRI) +
      // " and peasant " + n.getProperty(NodeProperties.IRI));
      // tx.success();
      // tx.close();
      // tx = graphDb.beginTx();
      // edgesMoved = 0;
      // }
    }
    deleteEdges(n, tx);
  }
}
 
Example 13
Source File: VectorUtilTest.java    From graphify with Apache License 2.0 5 votes vote down vote up
@Ignore
public void sentimentAnalysisTest() throws IOException {
    GraphDatabaseService db = setUpDb();
    GraphManager graphManager = new GraphManager("Pattern");

    // Invalidate all caches
    NodeManager.globalNodeCache.invalidateAll();
    DataNodeManager.dataCache.invalidateAll();
    ClassNodeManager.classCache.invalidateAll();
    GraphManager.edgeCache.invalidateAll();
    GraphManager.inversePatternCache.invalidateAll();
    GraphManager.patternCache.invalidateAll();
    DataRelationshipManager.relationshipCache.invalidateAll();
    ClassRelationshipCache.relationshipCache.invalidateAll();
    PatternRelationshipCache.relationshipCache.invalidateAll();
    VectorUtil.vectorSpaceModelCache.invalidateAll();

    Node rootNode = getRootPatternNode(db, graphManager);
    DecisionTree decisionTree = new DecisionTree<>(rootNode.getId(), new scala.collection.mutable.HashMap<>(), db, graphManager);

    // Train the model on examples each of positive and negative reviews
    train(db, graphManager, decisionTree);

    // Test the model on the next examples of positive and negative reviews
    Map<String, Double> errorMap = test(db, graphManager, TEST_COUNT);

    // To ensure the validity of the classifier, assert success ratio is greater than 50%
    Assert.assertTrue(errorMap.get("positive") > .5 && errorMap.get("negative") > .5);

    System.out.println(errorMap);

    System.out.println(VectorUtil.getPhrasesForClass(db, "negative"));
    System.out.println(VectorUtil.getPhrasesForClass(db, "positive"));
}
 
Example 14
Source File: EmbeddedNode.java    From extended-objects with Apache License 2.0 5 votes vote down vote up
public EmbeddedNode(Node delegate) {
    super(delegate.getId(), delegate);
    this.labels = new HashSet<>();
    for (Label label : delegate.getLabels()) {
        labels.add(new EmbeddedLabel(label));
    }
}
 
Example 15
Source File: Find_all_function_pointers.java    From EvilCoder with MIT License 4 votes vote down vote up
public static HashSet<String> all_used_func_names(Joern_db joern_db) throws Exception
   {
   HashSet<String> all_funcs = find_all_func_names(joern_db);

   HashSet<String> used = new HashSet<>();

//    uses = joern_db.runGremlinQuery("g.V.uses()")
   List<Node> uses = Joern_db.queryNodeIndex("type:Symbol");
   HashSet<Long> checked_already = new HashSet<>();
    for(Node u : uses)
     {
     Long id = (Long)u.getId();
        if(checked_already.contains(id))
         {
          continue;
         }

      checked_already.add(id);
     String code = (String)u.getProperty("code");
        if(all_funcs.contains(code))
         {
         List<Node> context_list = Pipeline.v(id).in("USE").to_list();
           if(context_list.isEmpty())
            {
             continue;
            }
         String context = (String)context_list.get(0).getProperty("code");
            if(context.contains(code + " ("))
             {
              used.add(code);
             }
         }
     }
    return used;

//    assigned = set()
//    assign_codes = joern_db.runGremlinQuery("g.V.filter{it.type == 'AssignmentExpr'}.code")
//    for a in assign_codes:
//        a = a.replace(")", "") # func-ptr may be casted
//        a = a.split(" ")[-1] # saves endswith-operations, allows "in"
//        if(a in all_funcs):
//            assigned.add(a)
//
//    return assigned
  }
 
Example 16
Source File: Get_call_graph.java    From EvilCoder with MIT License 4 votes vote down vote up
public static HashMap<Pair<String, Long>, Pair<String, HashSet<Pair<String, Long>>>> get_call_graph(Joern_db joern_db)
   {
   HashMap<Pair<String, Long>, Pair<String, HashSet<Pair<String, Long>>>> cg = new HashMap<>();
//   funcs = joern_db.runGremlinQuery("g.V.filter{it.type == 'Function'}")
   List<Node> funcs = Joern_db.queryNodeIndex("type:Function");
     for(Long i = new Long(0), i_end = new Long(funcs.size()); i<i_end; ++i)
      {
      Node f = funcs.get(i.intValue());
      Long f_id = (Long)f.getId();
       System.out.println("handling func " + (String)f.getProperty("name") + "\t" + i.toString() + " of " + i_end.toString());

      List<Node> it = Pipeline.v(f_id).functionToAST().to_list();
      String func_sig = (String)(it.get(0).getProperty("code"));
      String func_name = (String)(f.getProperty("name"));
   
       it = Pipeline.v(f_id).functionToAST().children().has("type", "ParameterList").children().to_list();
      HashSet<Node> as_set = new HashSet<>(it);
      Long nof_params = new Long(as_set.size());
   
      HashSet<Pair<String, Long>> calls = new HashSet<>();
      List<Node> callees = Pipeline.v(f_id).functionToStatements().has("type", "CallExpression").callToCallee().to_list();
        for(Node callee : callees)
         {
         String callee_name = (String)(callee.getProperty("code"));
          it = Pipeline.v((Long)(callee.getId())).calleeToCall().callToArguments().to_list();
          as_set = new HashSet<>(it);
         Long nof_args = new Long(it.size());
   
          calls.add(new Pair<String, Long>(callee_name, nof_args));
         }
   
      Pair<String, Long> key = new Pair<>(func_name, nof_params);
        if(cg.containsKey(key))
         {
          System.out.println("func-collision for " + func_sig + " and " + cg.get(key).first);

         String now_func_sig = cg.get(key).first;
         HashSet<Pair<String, Long>> now_calls = cg.get(key).second;
         String[] names = now_func_sig.split("\n", -1);
         List<String> names_as_list = new ArrayList<>(Arrays.asList(names));
           if(!names_as_list.contains(func_sig))
            {
             names_as_list.add(func_sig);
             now_func_sig = join(names_as_list, "\n");
            }

           for(Pair<String, Long> c : calls)
            {
             now_calls.add(c);
            }
          cg.put(key, new Pair<String, HashSet<Pair<String, Long>>>(now_func_sig, now_calls));
         }
        else
         {
          cg.put(key, new Pair<String, HashSet<Pair<String, Long>>>(func_sig, calls));
         }
      }
    return cg;
   }
 
Example 17
Source File: PatternRecognitionResource.java    From graphify with Apache License 2.0 4 votes vote down vote up
/**
 * A REST API method that trains a natural language parse tree with a supplied text input and a
 * label that describes that text input.
 * @param body The JSON model that binds to the LabeledText class model.
 * @param db The Neo4j GraphDatabaseService that is the persistent data store for the natural language parsing model.
 * @return Returns a JSON response with a probability distribution of inferred classes that may describe the supplied text input based on the current training model represented as a hierarchical pattern recognition tree.
 * @throws IOException
 */
@POST
@Path("/training")
@Produces(MediaType.APPLICATION_JSON)
public Response training(String body, @Context GraphDatabaseService db) throws IOException {
    HashMap<String, Object> input;
    try {
        input = objectMapper.readValue(body, HashMap.class);
    } catch (Exception e) {
        return Response.status(200).entity("{\"error\":\"" + Arrays.toString(e.getStackTrace()) + "\", \"message\":\"" + e.getMessage() + "\"}").build();
    }
    LabeledText labeledText = new LabeledText();
    ArrayList labels = (ArrayList) input.get("label");
    ArrayList texts = new ArrayList();
    if (input.get("text").getClass() == ArrayList.class) {
        texts = (ArrayList) input.get("text");
    } else {
        texts.add(input.get("text"));
    }

    for (int i = 0; i < texts.size(); i++) {
        texts.set(i, cleanText((String) texts.get(i)));
    }

    labeledText.setLabel((String[]) labels.toArray(new String[labels.size()]));
    labeledText.setText((String[]) texts.toArray(new String[texts.size()]));

    if (input.containsKey("focus")) {
        labeledText.setFocus((int) input.get("focus"));
    } else {
        labeledText.setFocus(1);
    }

    // Create tree
    Node rootNode = getRootPatternNode(db);
    DecisionTree<Long> tree = new DecisionTree<>(rootNode.getId(), new scala.collection.mutable.HashMap<>(), db, GRAPH_MANAGER);

    // Add first matcher
    for (int i = 0; i < labeledText.getFocus(); i++) {
        LearningManager.trainInput(Arrays.asList(labeledText.getText()), Arrays.asList(labeledText.getLabel()), GRAPH_MANAGER, db, tree);
    }

    return Response.ok()
            .entity("{\"success\":\"true\"}")
            .type(MediaType.APPLICATION_JSON)
            .build();
}
 
Example 18
Source File: AbstractEmbeddedDBAccess.java    From jcypher with Apache License 2.0 4 votes vote down vote up
private NodeHolder(Node node) {
	super();
	this.id = node.getId();
}
 
Example 19
Source File: DefUseCFGPatcher.java    From EvilCoder with MIT License 3 votes vote down vote up
public void patchDefUseCFG(DefUseCFG defUseCFG,
		Collection<Node> statementsToPatch)
{
	
	this.defUseCFG = defUseCFG;
	newlyAddedLinks.clear();

	for (Node statement : statementsToPatch)
	{
		
		if(statement == null) continue;
		
		long statementId = statement.getId();

		Node node = Traversals.getASTForStatement(statement);

		ReadWriteDbASTProvider astProvider = new ReadWriteDbASTProvider();
		astProvider.setNodeId(node.getId());

		Collection<UseOrDef> newDefs = astDefUseAnalyzer
				.analyzeAST(astProvider);

		Collection<Object> oldDefs = defUseCFG
				.getSymbolsDefinedBy(statementId);
		updateDefsToAdd(oldDefs, newDefs, statementId);

	}

}
 
Example 20
Source File: WriterTest.java    From neo4j-mazerunner with Apache License 2.0 2 votes vote down vote up
@Test
public void testParallelUpdate() throws Exception {

    GraphDatabaseService db = setUpDb();

    Transaction tx = db.beginTx();


    // Use test configurations
    ConfigurationLoader.testPropertyAccess = true;

    Node nodePartition = db.createNode();
    nodePartition.addLabel(DynamicLabel.label("Category"));

    PartitionDescription partitionDescription = new PartitionDescription(nodePartition.getId(), "Category");

    // Create sample PageRank result
    String nodeList = "";

    for(int i = 0; i < 100; i++)
    {
        db.createNode();
        nodeList += i + " .001\n";
    }

    tx.success();
    tx.close();

    // Create test path
    String path = ConfigurationLoader.getInstance().getHadoopHdfsUri() + "/test/propertyNodeList.txt";

    writeListFile(path, nodeList);

    ProcessorMessage processorMessage = new ProcessorMessage(path, "pagerank", ProcessorMode.Partitioned);
    processorMessage.setPartitionDescription(partitionDescription);

    BufferedReader br = FileUtil.readGraphAdjacencyList(processorMessage);
    BufferedReader br2 = FileUtil.readGraphAdjacencyList(processorMessage);

    // Test parallel update
    PartitionedAnalysis.updatePartition(processorMessage, br, db);
    PartitionedAnalysis.updatePartition(processorMessage, br2, db);
}