java - java 的 gremlin 图形查询

标签 java gremlin

这是我正在尝试图形查询的代码。

package com;  
    import java.util.ArrayList;
    import java.util.List;

import org.apache.commons.configuration.BaseConfiguration;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;

public class SampleTitanMatchability {

    @SuppressWarnings("rawtypes")
    public static double matchability(GraphTraversalSource g, Vertex a, Vertex b) {
        int matchedQues = 0;
        int matchedAns = 0;

        List list1 = new ArrayList();
        List list2 = new ArrayList();

        list1 = g.V(a).outE().label().toList();
        System.out.println("total answered questions :" + list1);

        list2 = g.V(b).outE().label().toList();
        System.out.println("total answered questions :" + list2);

        if (list1.size() > list2.size())
        {   
            for (int i = 0; i < list2.size(); i++)
            {
                if (list1.contains(list2.get(i))) 
                {
                    matchedQues++;

                    if(g.V(a).outE(list2.get(i).toString()).next().inVertex().property("ans").toString().equals(g.V(b).outE(list2.get(i).toString()).next().inVertex().property("ans").toString()))
                    {
                        matchedAns++;
                    }

                }
            }

            System.out.println("matched questions :"+matchedQues);

            System.out.println("matched ans :"+matchedAns);

        }       
        else
        {
            for (int i = 0; i < list1.size(); i++)
            {
                if (list2.contains(list1.get(i))) 
                {
                    matchedQues++;

                    if(g.V(a).outE(list1.get(i).toString()).next().inVertex().property("ans").toString().equals(g.V(b).outE(list1.get(i).toString()).next().inVertex().property("ans").toString()))
                    {
                        matchedAns++;
                    }

                }
            }

            System.out.println("matched questions :"+matchedQues);

            System.out.println("matched ans :"+matchedAns);
        }

        double per = (matchedAns*100)/matchedQues;
        return per;
    }

    @SuppressWarnings({})
    public static void main(String[] args) throws Exception {

        BaseConfiguration conf = new BaseConfiguration();

        conf.setProperty("storage.backend", "com.amazon.titan.diskstorage.dynamodb.DynamoDBStoreManager");
        conf.setProperty("storage.dynamodb.client.endpoint", "http://localhost:4567");
        conf.setProperty("index.search.backend", "elasticsearch");
        conf.setProperty("index.search.directory", "/tmp/searchindex");
        conf.setProperty("index.search.elasticsearch.client-only", "false");
        conf.setProperty("index.search.elasticsearch.local-mode", "true");

        TitanGraph graph = TitanFactory.open(conf);

        GraphTraversalSource g = graph.traversal();

        Vertex mayank = graph.addVertex("name", "mayank");
        Vertex mque1 = graph.addVertex("ans", "yes");
        mayank.addEdge("que1", mque1);
        Vertex mque2 = graph.addVertex("ans", "yes");
        mayank.addEdge("que2", mque2);
        Vertex mque3 = graph.addVertex("ans", "yes");
        mayank.addEdge("que3", mque3);
        //Vertex mque4 = graph.addVertex("ans", "yes");
        //mayank.addEdge("que4", mque4);
        Vertex mque5 = graph.addVertex("ans", "no");
        mayank.addEdge("que5", mque5);
        Vertex mque6 = graph.addVertex("ans", "yes");
        mayank.addEdge("que6", mque6);
        Vertex mque7 = graph.addVertex("ans", "yes");
        mayank.addEdge("que7", mque7);
        Vertex mque8 = graph.addVertex("ans", "yes");
        mayank.addEdge("que8", mque8);
        Vertex mque9 = graph.addVertex("ans", "yes");
        mayank.addEdge("que9", mque9);
        Vertex mque10 = graph.addVertex("ans", "no");
        mayank.addEdge("que10", mque10);
        //Vertex mque11 = graph.addVertex("ans", "yes");
        //mayank.addEdge("que11", mque11);
        Vertex mque12 = graph.addVertex("ans", "yes");
        mayank.addEdge("que12", mque12);
        Vertex mque13 = graph.addVertex("ans", "yes");
        mayank.addEdge("que13", mque13);
        Vertex mque14 = graph.addVertex("ans", "no");
        mayank.addEdge("que14", mque14);
        Vertex mque15 = graph.addVertex("ans", "yes");
        mayank.addEdge("que15", mque15);

        Vertex poonam = graph.addVertex("name", "poonam");
        Vertex pque1 = graph.addVertex("ans", "yes");
        poonam.addEdge("que1", pque1);
        Vertex pque2 = graph.addVertex("ans", "yes");
        poonam.addEdge("que2", pque2);
        Vertex pque3 = graph.addVertex("ans", "yes");
        poonam.addEdge("que3", pque3);
        Vertex pque4 = graph.addVertex("ans", "no");
        poonam.addEdge("que4", pque4);
        Vertex pque5 = graph.addVertex("ans", "yes");
        poonam.addEdge("que5", pque5);
        Vertex pque6 = graph.addVertex("ans", "yes");
        poonam.addEdge("que6", pque6);
        Vertex pque7 = graph.addVertex("ans", "yes");
        poonam.addEdge("que7", pque7);
        Vertex pque8 = graph.addVertex("ans", "no");
        poonam.addEdge("que8", pque8);
        //Vertex pque9 = graph.addVertex("ans", "yes");
        //poonam.addEdge("que9", pque9);
        Vertex pque10 = graph.addVertex("ans", "no");
        poonam.addEdge("que10", pque10);
        Vertex pque11 = graph.addVertex("ans", "yes");
        poonam.addEdge("que11", pque11);
        Vertex pque12 = graph.addVertex("ans", "yes");
        poonam.addEdge("que12", pque12);
        Vertex pque13 = graph.addVertex("ans", "yes");
        poonam.addEdge("que13", pque13);
        // Vertex pque14 = graph.addVertex("ans", "no");
        // poonam.addEdge("que14", pque14);
        // Vertex pque15 = graph.addVertex("ans", "yes");
        // poonam.addEdge("que15", pque15);

        Vertex bhoomi = graph.addVertex("name", "bhoomi");
        //Vertex bque1 = graph.addVertex("ans", "yes");
        //bhoomi.addEdge("que1", bque1);
        Vertex bque2 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que2", bque2);
        Vertex bque3 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que3", bque3);
        Vertex bque4 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que4", bque4);
        Vertex bque5 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que5", bque5);
        Vertex bque6 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que6", bque6);
        Vertex bque7 = graph.addVertex("ans", "yes");
        bhoomi.addEdge("que7", bque7);
        Vertex bque8 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que8", bque8);
        Vertex bque9 = graph.addVertex("ans", "no");
        bhoomi.addEdge("que9", bque9);
        // Vertex bque10 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que10", bque10);
        // Vertex bque11 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que11", bque11);
        // Vertex bque12 = graph.addVertex("ans", "yes");
        // bhoomi.addEdge("que12", bque12);
        // Vertex bque13 = graph.addVertex("ans", "yes");
        // bhoomi.addEdge("que13", bque13);
        // Vertex bque14 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que14", bque14);
        // Vertex bque15 = graph.addVertex("ans", "no");
        // bhoomi.addEdge("que15", bque15);

        System.out.println("graph is " + graph);
        System.out.println("Graph created.....");
        System.out.println();

        double i = matchability(g, mayank, poonam);
        System.out.println("matchability between mayank and poonam is :" + i + "%");
        System.out.println();

        double j = matchability(g, mayank, bhoomi);
        System.out.println("matchability between mayank and bhoomi is :" + j + "%");
        System.out.println();


        double k = matchability(g, bhoomi, poonam);
        System.out.println("matchability between bhoomi and poonam is :" + k + "%");
        System.out.println();

        graph.close();
        System.exit(0);

        System.out.println("graph is " + graph);

        graph.close();
        System.exit(0);

    }

}

这是我的代码,如何减少 matchability 方法中的代码? 我希望它减少为 gremlin 查询。 我不知道如何将其简化为 gremlin 查询。 请为我的代码建议适当的查询。

最佳答案

这与打包到单个 Gremlin 遍历中的代码几乎相同:

public static double matchability(GraphTraversalSource g, Vertex a, Vertex b) {
    return g.withSack(0).V(a).outE().aggregate("x").by(label).limit(1)
            .V(b).outE().as("e").label().as("l").where(select("x").unfold().as("l"))
            .store("matchedQues").by(constant(1))
            .V(a).outE().where(label().as("l")).inV().as("a1")
            .select("e").inV().as("a2").where("a1", eq("a2")).by("ans")
            .aggregate("matchedAns").limit(1)
            .sack(assign).by(select("matchedAns").count(Scope.local))
            .sack(mult).by(constant(100))
            .sack(div).by(select("matchedQues").count(Scope.local)).<Long>sack()
            .tryNext().orElse(0L);
}

更新

对于 TP 3.0.1-incubating,您必须做更多的手动工作。这对我有用:

final Set<String> x = g.V(a).outE().label().toSet();
final Map<String, Long> m = g.withSideEffect("x", x).V(b).outE().as("e").label().as("l")
        .where(select("x").unfold().as("l"))
        .store("matchedQues").by(constant(1))
        .constant(a).outE().where(label().as("l")).inV().as("a1")
        .select("e").inV().as("a2").filter(select("a1", "a2").by("ans").where("a1", eq("a2")))
        .aggregate("matchedAns").by(constant(1)).cap("matchedAns", "matchedQues")
        .<Long>select("matchedAns", "matchedQues").by(count(local)).next();
return (m.get("matchedAns") * 100) / m.get("matchedQues");

关于java - java 的 gremlin 图形查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41300808/

相关文章:

java - 我可以对嵌入的 JanusGraph 实例执行 Gremlin 查询吗?

python - 使用 pythongremlin 删除 AWS Neptune 上的所有边

java - 如何使用 lambda 表达式调试 stream().map(...)?

java - 未触发 Firebase 异步上传到 Cloud Firestore

java - 将标识符与字符串、数字和 _ 进行匹配

gremlin - AWS Neptune 日期时间()

java - Vertex.value() 属性未找到 Gremlin Neptune Java

java - 对数组进行排序后获取元素的排名

java - 递归谢尔宾斯基三角形不递归

Javascript 如何从以 EnumValue 作为键的 Map 中检索值