Java tutorial
package version1.Neo4jTest; /** * Licensed to Neo Technology under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Neo Technology licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import java.awt.Label; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; //import org.neo4j.cypher.ExecutionEngine; import org.neo4j.cypher.javacompat.ExecutionEngine; import org.neo4j.cypher.javacompat.ExecutionResult; //import org.neo4j.cypher.ExecutionResult; //import org.neo4j.cypher.internal.compiler.v2_1.planDescription.PlanDescription.Arguments$; import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.DynamicLabel; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.GraphDatabaseFactory; import org.neo4j.graphdb.index.Index; import org.neo4j.graphdb.index.IndexHits; import org.neo4j.graphdb.index.IndexManager; import org.neo4j.graphdb.index.RelationshipIndex; import org.neo4j.graphdb.schema.IndexDefinition; import org.neo4j.graphdb.schema.Schema; import org.neo4j.helpers.collection.IteratorUtil; public class Neo4jQuery { private static final String DB_PATH = "C://Users//frisch//Desktop//Neo4j//test65"; public String greeting; // START SNIPPET: vars GraphDatabaseService graphDb; Node firstNode; Node secondNode; Node thirdNode; String version = "Version 1.2"; TreeMap<String, Node> string_node = new TreeMap(); String[] mc = { "sc", "MetaCube" }; String[] sc = { "sc", "name" }; String[] sca = { "sca", "name" }; String[] bca = { "bca", "name" }; Relationship relationship; // END SNIPPET: vars // START SNIPPET: createReltype private static enum RelTypes implements RelationshipType { MEMBER_OF, BEAVER, TEST//, HAS_ROLE, HAS_PERMISSION } // END SNIPPET: createReltype public static void main(final String[] args) { Neo4jQuery query = new Neo4jQuery(); query.query2(); // Kredit_Verwendungszweck_CL // // Kredituebertragung_An_Verkauf_CL // // Kredituebertragung_Art_CL // // Verzinsungsart_CL // // Kredithoehenklasse_CL // // Anlage_Umlauf_Vermoegens_CL // // Wertpapierklassifikation_CL ArrayList<String[]> X = query.getCLAuspraegungen("Kredithoehenklasse_CL"); for (int i = 0; i < X.size(); i++) { String[] dummy = X.get(i); System.out.println(dummy[0] + " / " + dummy[1]); } System.out.println("Ende"); } void addAttribute_to_Treemap(String key, String element, TreeMap<String, HashSet> y) { HashSet dummy = y.get(key); if (dummy == null) { dummy = new HashSet(); dummy.add(element); y.put(key, dummy); } else { dummy.add(element); } } public void part_tree(TreeMap<String, HashSet> treemap_hashset, TreeMap<String, JSONObject> treemap_previous_jsonobject, TreeMap<String, JSONObject> treemap_next_jsonobject, boolean atbottom) { try { for (String head : treemap_hashset.keySet()) { JSONObject json_head = new JSONObject(); json_head.append("name", head.toString()); HashSet hashset = treemap_hashset.get(head); Iterator it = hashset.iterator(); while (it.hasNext()) { String leave = (String) it.next(); JSONObject json_leave = new JSONObject(); if (atbottom) { json_leave.append("name", leave); json_head.append("children", json_leave); } else { JSONObject jsononject_leave = treemap_previous_jsonobject.get(leave); if (jsononject_leave != null) { json_head.append("children", jsononject_leave); } else { JSONObject dummy = new JSONObject(); dummy.append("name", leave); json_head.append("children", dummy); } } } treemap_next_jsonobject.put(head, json_head); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //TreeMap<String,JSONObject> getSCAttributDependency(){ // // TreeMap<String,JSONObject> SCDependency = new TreeMap(); // // graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); // registerShutdownHook(graphDb); // ExecutionEngine engine = new ExecutionEngine(graphDb); // ExecutionResult result; // // try ( Transaction ignored = graphDb.beginTx() ) // { // // result = (ExecutionResult) engine.execute("MATCH (sc:SmartCube)<-[:MEMBER_OF]-(sca:SCAttribut) RETURN sc,sca"); // // TreeMap<String,HashSet> SmartCube_SC_Attribut = new TreeMap(); // TreeMap<String,HashSet> MetaCube_SmartCube = new TreeMap(); // //TreeMap<String,HashSet> SCAttribut_SCAlgo = new TreeMap(); // //TreeMap<String,HashSet> SCAlgo_BCAttribut = new TreeMap(); // //ArrayList<TreeMap<String,HashSet>> treemaps = new ArrayList(); // // for ( Map<String, Object> row : result ) // { // String SmartCube = null;String SC_Attribut = null; // String SC_Algorithumus = null;String BC_Attribut = null; // String MetaCube = null; // // for ( Entry<String, Object> column : row.entrySet()){ // Node x = (Node) column.getValue(); // if (column.getKey().equals("sca")) SC_Attribut = (String) x.getProperty("name"); // //if (column.getKey().equals("c")) SC_Algorithumus = (String) x.getProperty("name"); // if (column.getKey().equals("sc")) SmartCube = (String) x.getProperty("name"); // //for (String y :x.getPropertyKeys()) System.out.println(y); // if (column.getKey().equals("sc")) MetaCube = (String) x.getProperty("MetaCube"); // } // // System.out.println("------------------------"); // // addAttribute_to_Treemap(MetaCube,SmartCube,MetaCube_SmartCube); // // addAttribute_to_Treemap(SmartCube,SC_Attribut,SmartCube_SC_Attribut); // // // }//ende quer // // // return null; // //} TreeMap<String, JSONObject> getDepTree(String query, ArrayList<String[]> hirarchy) { //staring on top graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); registerShutdownHook(graphDb); ExecutionEngine engine = new ExecutionEngine(graphDb); ExecutionResult result; TreeMap<String, JSONObject> name_json = new TreeMap(); try (Transaction ignored = graphDb.beginTx()) { result = (ExecutionResult) engine.execute(query);//"MATCH (sc:SmartCube)<-[:MEMBER_OF]-(sca:SCAttribut) RETURN sc,sca" ArrayList<TreeMap<String, HashSet>> head_tail = new ArrayList(); for (int j = 0; j < hirarchy.size() - 1; j++) { TreeMap<String, HashSet> dummy = new TreeMap(); head_tail.add(dummy); } //TreeMap<String,HashSet> O2_O3 = new TreeMap(); //TreeMap<String,HashSet> O1_O2 = new TreeMap(); for (Map<String, Object> row : result) { ArrayList<String> objects = new ArrayList(); for (int i = 0; i < hirarchy.size(); i++) objects.add(""); for (Entry<String, Object> column : row.entrySet()) { Node x = (Node) column.getValue(); for (int i = 0; i < hirarchy.size(); i++) { String[] object_property = hirarchy.get(i); if (column.getKey().equals(object_property[0])) { System.out.println(x.getProperty(object_property[1])); objects.set(i, (String) x.getProperty(object_property[1])); } } } for (int j = 0; j < hirarchy.size() - 1; j++) addAttribute_to_Treemap(objects.get(j), objects.get(j + 1), head_tail.get(j)); // addAttribute_to_Treemap(objects.get(0),objects.get(1),O1_O2); // addAttribute_to_Treemap(objects.get(1),objects.get(2),O2_O3); } //ende query for (int j = 0; j < hirarchy.size() - 1; j++) { if (j == hirarchy.size() - 1) { part_tree(head_tail.get(j), name_json, name_json, false); } else { part_tree(head_tail.get(j), null, name_json, true); } } ignored.success(); } graphDb.shutdown(); return name_json; } // working TreeMap<String, JSONObject> getDepTree_old(String query, ArrayList<String[]> hirarchy) { //staring on top graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); registerShutdownHook(graphDb); ExecutionEngine engine = new ExecutionEngine(graphDb); ExecutionResult result; TreeMap<String, JSONObject> name_json1 = new TreeMap(); TreeMap<String, JSONObject> name_json2 = new TreeMap(); try (Transaction ignored = graphDb.beginTx()) { result = (ExecutionResult) engine.execute(query);//"MATCH (sc:SmartCube)<-[:MEMBER_OF]-(sca:SCAttribut) RETURN sc,sca" TreeMap<String, HashSet> O2_O3 = new TreeMap(); TreeMap<String, HashSet> O1_O2 = new TreeMap(); for (Map<String, Object> row : result) { ArrayList<String> objects = new ArrayList(); for (int i = 0; i < hirarchy.size(); i++) objects.add(""); for (Entry<String, Object> column : row.entrySet()) { Node x = (Node) column.getValue(); for (int i = 0; i < hirarchy.size(); i++) { String[] object_property = hirarchy.get(i); if (column.getKey().equals(object_property[0])) { System.out.println(x.getProperty(object_property[1])); objects.set(i, (String) x.getProperty(object_property[1])); } } } addAttribute_to_Treemap(objects.get(0), objects.get(1), O1_O2); addAttribute_to_Treemap(objects.get(1), objects.get(2), O2_O3); } //ende query part_tree(O2_O3, null, name_json1, true); part_tree(O1_O2, name_json1, name_json2, false); ignored.success(); } graphDb.shutdown(); return name_json2; } void query3() { ArrayList<String[]> hirarchy = new ArrayList<String[]>(); String[] mc = { "sc", "MetaCube" }; hirarchy.add(mc); String[] sc = { "sc", "name" }; hirarchy.add(sc); String[] sca = { "sca", "name" }; hirarchy.add(sca); String[] bca = { "bca", "name" }; hirarchy.add(sca); TreeMap<String, JSONObject> SmartCube_SCAttribut = getDepTree( "MATCH (sc:SmartCube)<-[:MEMBER_OF]-(sca:SCAttribut) RETURN sc,sca", hirarchy); hirarchy.clear(); //ArrayList<String[]> hirarchy = new ArrayList<String[]>(); hirarchy.add(sc); hirarchy.add(sca); hirarchy.add(bca); TreeMap<String, JSONObject> SCAttribut_SCAlgo_BCAttribut = getDepTree_old( "MATCH (sc:SCAttribut) -[:DERIVED_BY]->(sca:SCAlgorithmen)-[:USES] ->(bca:BCAttribut) RETURN sc,sca,bca", hirarchy); hirarchy.clear(); hirarchy.add(sc); hirarchy.add(bca); TreeMap<String, JSONObject> SCAttribut_BCAttribut = getDepTree( "MATCH (sc:SCAttribut) -[:TRANSITION]->(bca:BCAttribut) RETURN sc,bca", hirarchy); JSONObject MetaCube = new JSONObject(); try { MetaCube.append("name", "MetaCube"); for (String key : SmartCube_SCAttribut.keySet()) { JSONObject cube = SmartCube_SCAttribut.get(key); MetaCube.append("children", cube); } } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String content = MetaCube.toString(); try { //String content = json_smartcube.toString(); File file = new File("C://Users//frisch//Desktop//d3//flare.json"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } void query2() { graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); registerShutdownHook(graphDb); ExecutionEngine engine = new ExecutionEngine(graphDb); ExecutionResult result; TreeMap<String, HashSet> SmartCube_SC_Attribut = new TreeMap(); TreeMap<String, HashSet> MetaCube_SmartCube = new TreeMap(); TreeMap<String, JSONObject> name_json1 = new TreeMap(); TreeMap<String, JSONObject> name_json2 = new TreeMap(); try (Transaction ignored = graphDb.beginTx()) { result = (ExecutionResult) engine .execute("MATCH (sc:SmartCube)<-[:MEMBER_OF]-(sca:SCAttribut) RETURN sc,sca"); //TreeMap<String,HashSet> SCAttribut_SCAlgo = new TreeMap(); //TreeMap<String,HashSet> SCAlgo_BCAttribut = new TreeMap(); //ArrayList<TreeMap<String,HashSet>> treemaps = new ArrayList(); for (Map<String, Object> row : result) { String SmartCube = null; String SC_Attribut = null; String SC_Algorithumus = null; String BC_Attribut = null; String MetaCube = null; for (Entry<String, Object> column : row.entrySet()) { Node x = (Node) column.getValue(); if (column.getKey().equals("sca")) SC_Attribut = (String) x.getProperty("name"); //if (column.getKey().equals("c")) SC_Algorithumus = (String) x.getProperty("name"); if (column.getKey().equals("sc")) SmartCube = (String) x.getProperty("name"); //for (String y :x.getPropertyKeys()) System.out.println(y); if (column.getKey().equals("sc")) MetaCube = (String) x.getProperty("MetaCube"); } System.out.println("------------------------"); addAttribute_to_Treemap(MetaCube, SmartCube, MetaCube_SmartCube); addAttribute_to_Treemap(SmartCube, SC_Attribut, SmartCube_SC_Attribut); } //ende query } graphDb.shutdown(); ArrayList<String[]> hirarchy = new ArrayList<String[]>(); hirarchy.add(sc); hirarchy.add(sca); hirarchy.add(bca); TreeMap<String, JSONObject> SCAttribut_SCAlgo_BCAttribut = getDepTree_old( "MATCH (sc:SCAttribut) -[:DERIVED_BY]->(sca:SCAlgorithmen)-[:USES] ->(bca:BCAttribut) RETURN sc,sca,bca", hirarchy); hirarchy.clear(); hirarchy.add(sc); hirarchy.add(bca); TreeMap<String, JSONObject> SCAttribut_BCAttribut = getDepTree( "MATCH (sc:SCAttribut) -[:TRANSITION]->(bca:BCAttribut) RETURN sc,bca", hirarchy); SCAttribut_SCAlgo_BCAttribut.putAll(SCAttribut_BCAttribut); part_tree(SmartCube_SC_Attribut, SCAttribut_SCAlgo_BCAttribut, name_json1, false); part_tree(MetaCube_SmartCube, name_json1, name_json2, false); //old and working // part_tree(SmartCube_SC_Attribut, null,name_json1, true); // part_tree(MetaCube_SmartCube, name_json1,name_json2, false); //part_tree(SmartCube_SCAttribut, name_json1, name_json2, false); JSONObject MetaCube = new JSONObject(); try { MetaCube.append("name", "MetaCube"); for (String key : name_json2.keySet()) { JSONObject cube = name_json2.get(key); MetaCube.append("children", cube); } } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String content = MetaCube.toString(); try { //String content = json_smartcube.toString(); File file = new File("C://Users//frisch//Desktop//d3//flare.json"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } public ArrayList<String[]> getCLAuspraegungen(String clname) { ArrayList<String[]> dummy = new ArrayList(); graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); registerShutdownHook(graphDb); ExecutionEngine engine = new ExecutionEngine(graphDb); ExecutionResult result; try (Transaction ignored = graphDb.beginTx()) { result = (ExecutionResult) engine.execute( "MATCH (c:Codeliste)<-[:MEMBER_OF]-(cd:Code) WHERE c.Name = '" + clname + "' RETURN c,cd"); for (Map<String, Object> row : result) { String Code = null; String Code_Bezeichnung = null; for (Entry<String, Object> column : row.entrySet()) { Node x = (Node) column.getValue(); //in Zukunft in Nodes in Objekt laden if (column.getKey().equals("cd")) Code = (String) x.getProperty("Code"); if (column.getKey().equals("cd")) Code_Bezeichnung = (String) x.getProperty("Codebezeichnung"); //if (column.getKey().equals("c")) SC_Algorithumus = (String) x.getProperty("name"); //if (column.getKey().equals("sc")) SmartCube = (String) x.getProperty("name"); //if (column.getKey().equals("bca")) BC_Attribut =(String) x.getProperty("name"); // if (column.getKey().equals("b")) b.append("name", x.getProperty("name")); // if (column.getKey().equals("c")) c.append("name", x.getProperty("name")); // if (column.getKey().equals("sc")) sc.append("name", x.getProperty("name")); // if (column.getKey().equals("bca")) bca.append("name", x.getProperty("name")); if (Code != null && Code_Bezeichnung != null) { String[] data = new String[2]; data[0] = Code; data[1] = Code_Bezeichnung; dummy.add(data); } //System.out.println(Code+ " : " + Code); } } } return dummy; } void query() { graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); registerShutdownHook(graphDb); ExecutionEngine engine = new ExecutionEngine(graphDb); ExecutionResult result; try (Transaction ignored = graphDb.beginTx()) { result = (ExecutionResult) engine.execute( "MATCH (sc:SmartCube)<-[:MEMBER_OF]-(b:SCAttribut) -[:DERIVED_BY]->(c:SCAlgorithmen)-[:USES] ->(bca:BCAttribut) WHERE sc.name='Kredit-Cube V1' RETURN sc,b,c,bca LIMIT 2500"); TreeMap<String, HashSet> SmartCube_SCAttribut = new TreeMap(); TreeMap<String, HashSet> SCAttribut_SCAlgo = new TreeMap(); TreeMap<String, HashSet> SCAlgo_BCAttribut = new TreeMap(); ArrayList<TreeMap<String, HashSet>> treemaps = new ArrayList(); for (Map<String, Object> row : result) { // JSONObjectX b = new JSONObjectX();//sc Attribut // JSONObjectX c = new JSONObjectX();// sc algorithmus // JSONObjectX sc = new JSONObjectX();// smart Cube // JSONObjectX bca = new JSONObjectX();//basic cube attribut String SmartCube = null; String SC_Attribut = null; String SC_Algorithumus = null; String BC_Attribut = null; for (Entry<String, Object> column : row.entrySet()) { Node x = (Node) column.getValue(); System.out.println(column.getKey() + ": " + column.getValue() + x.getProperty("name")); //in Zukunft in Nodes in Objekt laden if (column.getKey().equals("b")) SC_Attribut = (String) x.getProperty("name"); if (column.getKey().equals("c")) SC_Algorithumus = (String) x.getProperty("name"); if (column.getKey().equals("sc")) SmartCube = (String) x.getProperty("name"); if (column.getKey().equals("bca")) BC_Attribut = (String) x.getProperty("name"); // if (column.getKey().equals("b")) b.append("name", x.getProperty("name")); // if (column.getKey().equals("c")) c.append("name", x.getProperty("name")); // if (column.getKey().equals("sc")) sc.append("name", x.getProperty("name")); // if (column.getKey().equals("bca")) bca.append("name", x.getProperty("name")); } System.out.println("------------------------"); addAttribute_to_Treemap(SmartCube, SC_Attribut, SmartCube_SCAttribut); addAttribute_to_Treemap(SC_Attribut, SC_Algorithumus, SCAttribut_SCAlgo); addAttribute_to_Treemap(SC_Algorithumus, BC_Attribut, SCAlgo_BCAttribut); } //ende query //Show dependency structure //top down structure JSONObject json_smartcube = new JSONObject();// smart Cube TreeMap<String, JSONObject> name_json1 = new TreeMap(); TreeMap<String, JSONObject> name_json2 = new TreeMap(); TreeMap<String, JSONObject> name_json3 = new TreeMap(); part_tree(SCAlgo_BCAttribut, null, name_json1, true); part_tree(SCAttribut_SCAlgo, name_json1, name_json2, false); part_tree(SmartCube_SCAttribut, name_json2, name_json3, false); String content = (name_json3.get("Kredit-Cube V1")).toString(); try { //String content = json_smartcube.toString(); File file = new File("C://Users//frisch//Desktop//d3//flare.json"); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } } void removeData() { try (Transaction tx = graphDb.beginTx()) { // START SNIPPET: removingData // let's remove the data //firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete(); //firstNode.delete(); //secondNode.delete(); // END SNIPPET: removingData tx.success(); } } public void shutDown() { System.out.println(); System.out.println("Shutting down database ..."); // START SNIPPET: shutdownServer graphDb.shutdown(); // END SNIPPET: shutdownServer } // START SNIPPET: shutdownHook private static void registerShutdownHook(final GraphDatabaseService graphDb) { // Registers a shutdown hook for the Neo4j instance so that it // shuts down nicely when the VM exits (even if you "Ctrl-C" the // running application). Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { graphDb.shutdown(); } }); } // END SNIPPET: shutdownHook private static void deleteFileOrDirectory(File file) { if (file.exists()) { if (file.isDirectory()) { for (File child : file.listFiles()) { deleteFileOrDirectory(child); } } file.delete(); } } }