Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Copyright 2017 JanusGraph Authors
//
// Licensed 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.
package org.janusgraph.example;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.util.GraphFactory;
import org.janusgraph.core.attribute.Geoshape;
import org.janusgraph.util.system.ConfigurationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class GraphApp {
private static final Logger LOGGER = LoggerFactory.getLogger(GraphApp.class);
protected String propFileName;
protected Configuration conf;
protected Graph graph;
protected GraphTraversalSource g;
protected boolean supportsTransactions;
protected boolean supportsSchema;
protected boolean supportsGeoshape;
/**
* Constructs a graph app using the given properties.
* @param fileName location of the properties file
*/
public GraphApp(final String fileName) {
propFileName = fileName;
}
/**
* Opens the graph instance. If the graph instance does not exist, a new
* graph instance is initialized.
*/
public GraphTraversalSource openGraph() throws ConfigurationException, IOException {
LOGGER.info("opening graph");
conf = ConfigurationUtil.loadPropertiesConfig(propFileName);
graph = GraphFactory.open(conf);
g = graph.traversal();
return g;
}
/**
* Closes the graph instance.
*/
public void closeGraph() throws Exception {
LOGGER.info("closing graph");
try {
if (g != null) {
g.close();
}
if (graph != null) {
graph.close();
}
} finally {
g = null;
graph = null;
}
}
/**
* Drops the graph instance. The default implementation does nothing.
*/
public void dropGraph() throws Exception {
}
/**
* Creates the graph schema. The default implementation does nothing.
*/
public void createSchema() {
}
/**
* Adds the vertices, edges, and properties to the graph.
*/
public void createElements() {
try {
// naive check if the graph was previously created
if (g.V().has("name", "saturn").hasNext()) {
if (supportsTransactions) {
g.tx().rollback();
}
return;
}
LOGGER.info("creating elements");
// see GraphOfTheGodsFactory.java
final Vertex saturn = g.addV("titan").property("name", "saturn").property("age", 10000).next();
final Vertex sky = g.addV("location").property("name", "sky").next();
final Vertex sea = g.addV("location").property("name", "sea").next();
final Vertex jupiter = g.addV("god").property("name", "jupiter").property("age", 5000).next();
final Vertex neptune = g.addV("god").property("name", "neptune").property("age", 4500).next();
final Vertex hercules = g.addV("demigod").property("name", "hercules").property("age", 30).next();
final Vertex alcmene = g.addV("human").property("name", "alcmene").property("age", 45).next();
final Vertex pluto = g.addV("god").property("name", "pluto").property("age", 4000).next();
final Vertex nemean = g.addV("monster").property("name", "nemean").next();
final Vertex hydra = g.addV("monster").property("name", "hydra").next();
final Vertex cerberus = g.addV("monster").property("name", "cerberus").next();
final Vertex tartarus = g.addV("location").property("name", "tartarus").next();
g.V(jupiter).as("a").V(saturn).addE("father").from("a").next();
g.V(jupiter).as("a").V(sky).addE("lives").property("reason", "loves fresh breezes").from("a").next();
g.V(jupiter).as("a").V(neptune).addE("brother").from("a").next();
g.V(jupiter).as("a").V(pluto).addE("brother").from("a").next();
g.V(neptune).as("a").V(sea).addE("lives").property("reason", "loves waves").from("a").next();
g.V(neptune).as("a").V(jupiter).addE("brother").from("a").next();
g.V(neptune).as("a").V(pluto).addE("brother").from("a").next();
g.V(hercules).as("a").V(jupiter).addE("father").from("a").next();
g.V(hercules).as("a").V(alcmene).addE("mother").from("a").next();
if (supportsGeoshape) {
g.V(hercules).as("a").V(nemean).addE("battled").property("time", 1)
.property("place", Geoshape.point(38.1f, 23.7f)).from("a").next();
g.V(hercules).as("a").V(hydra).addE("battled").property("time", 2)
.property("place", Geoshape.point(37.7f, 23.9f)).from("a").next();
g.V(hercules).as("a").V(cerberus).addE("battled").property("time", 12)
.property("place", Geoshape.point(39f, 22f)).from("a").next();
} else {
g.V(hercules).as("a").V(nemean).addE("battled").property("time", 1)
.property("place", getGeoFloatArray(38.1f, 23.7f)).from("a").next();
g.V(hercules).as("a").V(hydra).addE("battled").property("time", 2)
.property("place", getGeoFloatArray(37.7f, 23.9f)).from("a").next();
g.V(hercules).as("a").V(cerberus).addE("battled").property("time", 12)
.property("place", getGeoFloatArray(39f, 22f)).from("a").next();
}
g.V(pluto).as("a").V(jupiter).addE("brother").from("a").next();
g.V(pluto).as("a").V(neptune).addE("brother").from("a").next();
g.V(pluto).as("a").V(tartarus).addE("lives").property("reason", "no fear of death").from("a").next();
g.V(pluto).as("a").V(cerberus).addE("pet").from("a").next();
g.V(cerberus).as("a").V(tartarus).addE("lives").from("a").next();
if (supportsTransactions) {
g.tx().commit();
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
if (supportsTransactions) {
g.tx().rollback();
}
}
}
/**
* Returns the geographical coordinates as a float array.
*/
protected float[] getGeoFloatArray(final float lat, final float lon) {
return new float[]{ lat, lon };
}
/**
* Runs some traversal queries to get data from the graph.
*/
public void readElements() {
try {
if (g == null) {
return;
}
LOGGER.info("reading elements");
// look up vertex by name can use a composite index in JanusGraph
final Optional