Example #1
0
def add_ind_to_col(request, username):
    global message
    if request.method == 'POST':

        if request.user.is_authenticated():

            u1 = User.objects.get(username=username)
            u = User.objects.get(id=u1.id)

            json_data = json.loads(request.body)

            col = json_data['userCollection']
            ind = json_data['id']

            c = Collection.objects.get(id=col)
            collection_name = c.name
            #print collection_name

            itc = Collection_Individual(collection=c,
                                        individual_id=ind,
                                        added_from=c,
                                        from_user=u,
                                        date_added=datetime.datetime.now())

            itc.save()

            #Agregar activity stream
            activity_stream = Activity_stream()
            usr = request.user.username
            activity_stream.activity("person", "save", "individual to collection", usr)

            #Agregar relacion entre individuo y coleccion en la red de grafos
            collection = GraphCollection()
            collection_result = collection.get_collection(collection_name)
            individual = Graph_Individual()
            individual_result = individual.get_node(ind)
            nodo1 = node(collection_result[0][0])
            nodo2 = node(individual_result[0][0])
            relation = Relations()
            relation.has(nodo1, nodo2)





            message = "Individual is now added to this collection!"
        else:
            message = "No username in evoart!"

        print "YYYYYYYYYYYYYYY"
        print col
        print ind
        print message

        data = ({'collection': col, 'individual': ind, 'message': message})
        datar = json.dumps(data)

    return HttpResponse(datar, content_type='application/json')
Example #2
0
def add_ind_to_col(request, username):
    global message
    if request.method == 'POST':

        if request.user.is_authenticated():

            u1 = User.objects.get(username=username)
            u = User.objects.get(id=u1.id)

            json_data = json.loads(request.body)

            col = json_data['userCollection']
            ind = json_data['id']

            c = Collection.objects.get(id=col)
            collection_name = c.name
            #print collection_name

            itc = Collection_Individual(collection=c,
                                        individual_id=ind,
                                        added_from=c,
                                        from_user=u,
                                        date_added=datetime.datetime.now())

            itc.save()

            #Agregar activity stream
            activity_stream = Activity_stream()
            usr = request.user.username
            activity_stream.activity("person", "save",
                                     "individual to collection", usr)

            #Agregar relacion entre individuo y coleccion en la red de grafos
            collection = GraphCollection()
            collection_result = collection.get_collection(collection_name)
            individual = Graph_Individual()
            individual_result = individual.get_node(ind)
            nodo1 = node(collection_result[0][0])
            nodo2 = node(individual_result[0][0])
            relation = Relations()
            relation.has(nodo1, nodo2)

            message = "Individual is now added to this collection!"
        else:
            message = "No username in evoart!"

        print "YYYYYYYYYYYYYYY"
        print col
        print ind
        print message

        data = ({'collection': col, 'individual': ind, 'message': message})
        datar = json.dumps(data)

    return HttpResponse(datar, content_type='application/json')
def evospace(request):
    if request.method == 'POST':
        population = Population(popName)
        #print 'Raw Data___: "%s"' % request.body
        #print type(request.body)
        json_data = json.loads(request.body)
        method = json_data["method"]
        params = json_data["params"]
        id = json_data["id"]



        if method == "initialize":
            result = population.initialize()
            data = json.dumps({"result": result, "error": None, "id": id})
            print data
            return HttpResponse(data, mimetype='application/javascript')
        elif method == "getSample":
            #Auto ReInsert
            if population.read_sample_queue_len() >= REINSERT_THRESHOLD:
                population.respawn(5)
            result = population.get_sample(params[0])
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({"result": None, "error":
                    {"code": -32601, "message": "EvoSpace empty"}, "id": id})
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_pop_keys":
            result = population.read_pop_keys()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({"result": None, "error":
                    {"code": -32601, "message": "EvoSpace empty"}, "id": id})
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_sample_queue":
            result = population.read_sample_queue()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({"result": None, "error":
                    {"code": -32601, "message": "EvoSpace empty"}, "id": id})
            return HttpResponse(data, mimetype='application/json')

        elif method == "putSample":
            #Cada EVOLUTION_INTERVAL evoluciona
            print "##################"
            if not population.get_returned_counter() % EVOLUTION_INTERVAL:
                try:
                    print "Evolucionando"
                    evolve_Tournament()
                except Exception as e:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    print e.message
                    traceback.print_tb(exc_traceback, limit=20, file=sys.stdout)

                pass
            population.put_sample(params[0])

            #Aqui se construye el grafo con el individuo
            if request.user.is_authenticated():
                usr = request.user.username
                first_name = request.user.first_name
                #last_name = request.user.last_name
                name = first_name
                nodo = Nodo()
                person = Person()
                person_result = person.get_person(name)
                activity_stream = Activity_stream()

                #print u
                print "=========Parametros==========="
                print params[0]
                usrLastDate=str(usr_last_date(params[0]['sample'][0]['fitness']))
                print usrLastDate
                usr_date_key = usr + ":" + usrLastDate
                print usr_date_key
                print params[0]['sample'][0]['fitness'][usr_date_key]
                rate = params[0]['sample'][0]['fitness'][usr_date_key]

                if params[0]["individuals_with_like"]:
                    for item in params[0]["individuals_with_like"]:
                        id = item
                        print id
                        individual_node = Graph_Individual()

                        print "prueba ", params[0]["individuals_with_like"]

                        # Verificar si el nodo individual existe con status last
                        individual_node_exist = individual_node.get_node(id)

                        if person_result:
                            nodo1 = node(person_result[0][0])

                        if individual_node_exist: # si la lista esta vacia quiere decir que no existe
                            nodo2 = node(individual_node_exist[0][0])

                        relation = Relations()
                        relation.likes(nodo1,nodo2,rate)

                        #request.user.username in k

                        #Agreagando Activity stream para el verbo like
                        activity_stream.activity("person", "like", "evaluate", usr)

                print "=========Parametros==========="
            else:
                print "Usuario anonimo"


            return HttpResponse(json.dumps("Success"), mimetype='application/json')
        elif method == "init_pop":
            data = init_pop(populationSize=params[0])
            return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
        elif method == "respawn":
            data = population.respawn(n=params[0])
            return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
        elif method == "put_individual":
            print  "params", params[0]
            population.put_individual(**params[0])
            data = json.dumps({"result": None, "error": None, "id": id})
            return HttpResponse(data, mimetype='application/json')


    else:
        return HttpResponse("ajax & post please", mimetype='text')
Example #4
0
def evospace(request):
    if request.method == 'POST':
        population = Population(popName)
        #print 'Raw Data___: "%s"' % request.body
        #print type(request.body)
        json_data = json.loads(request.body)
        method = json_data["method"]
        params = json_data["params"]
        id = json_data["id"]

        if method == "initialize":
            result = population.initialize()
            data = json.dumps({"result": result, "error": None, "id": id})
            print data
            return HttpResponse(data, mimetype='application/javascript')
        elif method == "getSample":
            #Auto ReInsert
            if population.read_sample_queue_len() >= REINSERT_THRESHOLD:
                population.respawn(5)
            result = population.get_sample(params[0])
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({
                    "result": None,
                    "error": {
                        "code": -32601,
                        "message": "EvoSpace empty"
                    },
                    "id": id
                })
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_pop_keys":
            result = population.read_pop_keys()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({
                    "result": None,
                    "error": {
                        "code": -32601,
                        "message": "EvoSpace empty"
                    },
                    "id": id
                })
            return HttpResponse(data, mimetype='application/json')
        elif method == "read_sample_queue":
            result = population.read_sample_queue()
            if result:
                data = json.dumps({"result": result, "error": None, "id": id})
            else:
                data = json.dumps({
                    "result": None,
                    "error": {
                        "code": -32601,
                        "message": "EvoSpace empty"
                    },
                    "id": id
                })
            return HttpResponse(data, mimetype='application/json')

        elif method == "putSample":
            #Cada EVOLUTION_INTERVAL evoluciona
            print "##################"
            if not population.get_returned_counter() % EVOLUTION_INTERVAL:
                try:
                    print "Evolucionando"
                    evolve_Tournament()
                except Exception as e:
                    exc_type, exc_value, exc_traceback = sys.exc_info()
                    print e.message
                    traceback.print_tb(exc_traceback,
                                       limit=20,
                                       file=sys.stdout)

                pass
            population.put_sample(params[0])

            #Aqui se va armar la machaca del individuo
            if request.user.is_authenticated():
                usr = request.user.username
                first_name = request.user.first_name
                last_name = request.user.last_name
                name = first_name + " " + last_name
                nodo = Nodo()
                person = Person()
                person_result = person.get_person(name)
                activity_stream = Activity_stream()

                #print u
                print "=========Parametros==========="
                print params[0]

                if params[0]["individuals_with_like"]:
                    for item in params[0]["individuals_with_like"]:
                        id = item
                        print id
                        individual_node = Graph_Individual()

                        print "prueba ", params[0]["individuals_with_like"]

                        # Verificar si el nodo individual existe con status last
                        individual_node_exist = individual_node.get_node(id)

                        if person_result:
                            nodo1 = node(person_result[0][0])

                        if individual_node_exist:  # si la lista esta vacia quiere decir que no existe
                            nodo2 = node(individual_node_exist[0][0])

                        relation = Relations()
                        relation.likes(nodo1, nodo2)

                        #request.user.username in k

                        #Agreagando Activity stream para el verbo like
                        activity_stream.activity("person", "like", "evaluate",
                                                 usr)

                        #Curret experience calculation
                        current_experience(request)

                print "=========Parametros==========="
            else:
                print "Usuario anonimo"

            return HttpResponse(json.dumps("Success"),
                                mimetype='application/json')
        elif method == "init_pop":
            data = init_pop(populationSize=params[0])
            return HttpResponse(json.dumps("Success"),
                                mimetype='application/javascript')
        elif method == "respawn":
            data = population.respawn(n=params[0])
            return HttpResponse(json.dumps("Success"),
                                mimetype='application/javascript')
        elif method == "put_individual":
            print "params", params[0]
            population.put_individual(**params[0])
            data = json.dumps({"result": None, "error": None, "id": id})
            return HttpResponse(data, mimetype='application/json')

    else:
        return HttpResponse("ajax & post please", mimetype='text')