Skip to content

Commit

Permalink
implement 8 olap algorithms (#4)
Browse files Browse the repository at this point in the history
* add olap algo api
* improve source filter
* fix louvain shaking with limit degree
* catch exception for lpa and louvain
* add 3 params for lpa: label,source_label,percision
* improve louvain node store
* remove vertices from class Community
* move showCommunity to AbstractCommAlgorithm
* add some parameters to AbstractAlgorithm
* improve louvain log
* improve clearPass and communities check
* split louvain cache
* fix degreeCentrality bug: degree is always < 500

Change-Id: I2341b981dab44f43ac50ae0f8fa5e51b7acc1b5a
  • Loading branch information
javeme authored and imbajin committed Nov 7, 2022
1 parent 2fc8869 commit 6ff96f9
Show file tree
Hide file tree
Showing 18 changed files with 2,862 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* 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.
*/

package com.baidu.hugegraph.api.job;

import java.util.Map;

import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.slf4j.Logger;

import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.api.API;
import com.baidu.hugegraph.api.filter.StatusFilter.Status;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.job.AlgorithmJob;
import com.baidu.hugegraph.job.JobBuilder;
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.JsonUtil;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableMap;

@Path("graphs/{graph}/jobs/algorithm")
@Singleton
public class AlgorithmAPI extends API {

private static final Logger LOG = Log.logger(RestServer.class);

@POST
@Timed
@Path("/{name}")
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public Map<String, Id> post(@Context GraphManager manager,
@PathParam("graph") String graph,
@PathParam("name") String algorithm,
Map<String, Object> parameters) {
LOG.debug("Graph [{}] schedule algorithm job: {}", graph, parameters);
E.checkArgument(algorithm != null && !algorithm.isEmpty(),
"The algorithm name can't be empty");
if (parameters == null) {
parameters = ImmutableMap.of();
}
if (!AlgorithmJob.check(algorithm, parameters)) {
throw new NotFoundException("Not found algorithm: " + algorithm);
}

HugeGraph g = graph(manager, graph);
Map<String, Object> input = ImmutableMap.of("algorithm", algorithm,
"parameters", parameters);
JobBuilder<Object> builder = JobBuilder.of(g);
builder.name("algorithm:" + algorithm)
.input(JsonUtil.toJson(input))
.job(new AlgorithmJob());
return ImmutableMap.of("task_id", builder.schedule().id());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* 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.
*/

package com.baidu.hugegraph.job;

import java.util.Map;

import com.baidu.hugegraph.job.algorithm.Algorithm;
import com.baidu.hugegraph.job.algorithm.AlgorithmPool;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.JsonUtil;

public class AlgorithmJob extends Job<Object> {

public static final String TASK_TYPE = "algorithm";

public static boolean check(String name, Map<String, Object> parameters) {
Algorithm algorithm = AlgorithmPool.instance().find(name);
if (algorithm == null) {
return false;
}
algorithm.checkParameters(parameters);
return true;
}

@Override
public String type() {
return TASK_TYPE;
}

@Override
public Object execute() throws Exception {
String input = this.task().input();
E.checkArgumentNotNull(input, "The input can't be null");
@SuppressWarnings("unchecked")
Map<String, Object> map = JsonUtil.fromJson(input, Map.class);

Object value = map.get("algorithm");
E.checkArgument(value instanceof String,
"Invalid algorithm name '%s'", value);
String name = (String) value;

value = map.get("parameters");
E.checkArgument(value instanceof Map,
"Invalid algorithm parameters '%s'", value);
@SuppressWarnings("unchecked")
Map<String, Object> parameters = (Map<String, Object>) value;

AlgorithmPool pool = AlgorithmPool.instance();
Algorithm algorithm = pool.find(name);
E.checkArgument(algorithm != null,
"There is no algorithm named '%s'", name);
return algorithm.call(this, parameters);
}
}
Loading

0 comments on commit 6ff96f9

Please sign in to comment.