Skip to content

Commit

Permalink
Merge pull request #71 from feup-infolab/lazaroDevelop
Browse files Browse the repository at this point in the history
Lazaro develop
  • Loading branch information
lazarocosta committed May 5, 2020
2 parents 23efadd + d3b1968 commit 4a419fb
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 205 deletions.
2 changes: 1 addition & 1 deletion src/Models/CRM/v5_0_2/NodeEntities/E12_Production.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class E12_Production(E11_Modification):

def __init__(self, schema=None, *args, **kwargs):
if schema is None:
schema = E11_ModificationSchema()
schema = E12_ProductionSchema()

super().__init__(schema, *args, **kwargs)
6 changes: 6 additions & 0 deletions src/Models/CRM/v5_0_2/NodeEntities/E29_Design_or_Procedure.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ class E29_Design_or_Procedure(E73_Information_Object):
foresees_use_of = RelationshipTo(
".E57_Material.E57_Material", "P68_foresees_use_of", model=P68_foresees_use_of,
)

def __init__(self, schema=None, *args, **kwargs):
if schema is None:
schema = E29_Design_or_ProcedureSchema()

super().__init__(schema, *args, **kwargs)
1 change: 1 addition & 0 deletions src/Models/CRM/v5_0_2/NodeEntities/E52_Time_Span.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.Models.CRM.v5_0_2.NodeProperties.P86_falls_within import P86_falls_within
from src.Models.CRM.v5_0_2.NodeProperties.P191_had_duration import P191_had_duration


#todo
class E52_Time_SpanSchema(E1_CRM_EntitySchema):
date = fields.Date(required=True)
Expand Down
102 changes: 38 additions & 64 deletions src/Models/DataObject/v0_0_2/SuperClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from marshmallow_jsonschema import JSONSchema


class SuperClass:
def __init__(self, schema):
self.schema = schema
Expand Down Expand Up @@ -53,13 +54,13 @@ def __get_schema_with_template_aux(self, definitions, json_template, new_json_sc
current_entity = entity_name + "Schema"
entity = {}
get_entity = new_json_schema["definitions"].get(current_entity, None)
if get_entity is not None:
if get_entity is not None:
entity = new_json_schema["definitions"][current_entity]
else:
entity = {
'type': definitions[current_entity]['type'],
'properties': {},
'additionalProperties': definitions[current_entity]['additionalProperties'],
'type': definitions[current_entity]['type'],
'properties': {},
'additionalProperties': definitions[current_entity]['additionalProperties'],
}
if 'required' in definitions[current_entity].keys():
entity['required'] = definitions[current_entity]['required']
Expand All @@ -74,7 +75,6 @@ def __get_schema_with_template_aux(self, definitions, json_template, new_json_sc
return

for property_name in json_template[entity_name]:

next_entity = json_template[entity_name][property_name]

entity['properties'][property_name] = properties[property_name]
Expand Down Expand Up @@ -112,8 +112,6 @@ def merge_node(self, updated_node):
except BaseException:
return None



# array_uid = read_relationships(node_name, relationship_name)
#
# for uid in array_uid:
Expand All @@ -122,8 +120,8 @@ def merge_node(self, updated_node):
return True

def node_self_build(self, updated_node):
object = {'self_node':{},
'relationships':{}}
object = {'self_node': {},
'relationships': {}}
for property in updated_node.keys():
if isinstance(updated_node[property], str):
object['self_node'][property] = updated_node[property]
Expand All @@ -147,59 +145,35 @@ def __get_field_of_type_date(self):
result.append(field["title"])
return result

def generate_template(self):
schema = self.getSchema()
class_name = self.__class__.__name__
schema_class_name = class_name + "Schema"
properties_of_entity = schema["definitions"][schema_class_name]["properties"]

template_aux = {class_name: {}}
template = {
"class_name": class_name,
"template": template_aux,
"schema": json.dumps(schema)
}
for property_name in properties_of_entity:
property = properties_of_entity[property_name]
if property["type"] == "string":
continue

range = ""
title = ""
if property["type"] == "array":
range = property["items"]["$ref"]
title = property["title"]
elif property["type"] == "object":
title = property_name
range = property["$ref"]

range_schema_class_name = range.split("/")[2]
range_class_name = range_schema_class_name.split("Schema")[0]
template_aux[class_name][title] = range_class_name

return template

var = {'P1_is_identified_by': {'type': 'object',
'$ref': '#/definitions/E55_TypeSchema'},
'P2_has_type': {'title': 'P2_has_type',
'type': 'array',
'items': {
'type': 'object',
'$ref': '#/definitions/E55_TypeSchema'}}}

var3 = {'P48_has_preferred_identifier': {
'type': 'object',
'$ref': '#/definitions/E42_IdentifierSchema'}, }

var1 = {'DataObjectSchema': {'properties': {
'name': {'title': 'name',
'type': 'string'},
'uid': {'title': 'uid',
'type': 'string'}},
'type': 'object',
'required': ['name'],
'additionalProperties': False}}

var2 = {
"E18_Physical_Thing": [
{"P46_is_composed_of": "E18_Physical_Thing"}
]
}

var = {'$schema': 'http://json-schema.org/draft-07/schema#', 'definitions': {
'E24_Physical_Human_Made_ThingSchema': {
'type': 'object', 'properties': {
'P130_shows_features_of': {
'title': 'P130_shows_features_of',
'type': 'array',
'items': {'type': 'object',
'$ref': '#/definitions/E70_ThingSchema'}},
'P62_depicts': {
'title': 'P62_depicts',
'type': 'array',
'items': {'type': 'object',
'$ref': '#/definitions/E1_CRM_EntitySchema'}}},
'additionalProperties': False},
'E18_Physical_ThingSchema': {
'type': 'object', 'properties': {
'P46_is_composed_of': {
'title': 'P46_is_composed_of',
'type': 'array',
'items': {'type': 'object',
'$ref': '#/definitions/E18_Physical_ThingSchema'}},
'P59_has_section': {
'title': 'P59_has_section',
'type': 'array',
'items': {'type': 'object',
'$ref': '#/definitions/E53_PlaceSchema'}}},
'additionalProperties': False}},
'$ref': '#/definitions/E18_Physical_ThingSchema'}
72 changes: 25 additions & 47 deletions src/Routes/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,11 @@
date_now = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")


# def insert_into_mongo(content):
# result = {}
# if isinstance(content, type([])):
# db.reviews.insert_many(content)
# print("inserted many documents")
#
# elif isinstance(content, type({})):
# result = db.reviews.insert_one(content)
# print(result.inserted_id)
#
# cursor = db.reviews.find({})
# for document in cursor:
# print(document)
def populate_template_collection(object_json):
for object in object_json:
class_name = object["className"]

class_in_project = find_name_of_class_schema_in_project(class_name)
if class_in_project is None:
print(class_name + " did't found")
template = object["template"]
schema = class_in_project().getSchema()
schema_str = json.dumps(schema)
db.defaultTemplate.insert_one({"class_name": class_name, "template": template, "schema": schema_str})
def insert_default_templates(templates):
for template in templates:
class_name = template["class_name"]
schema = template["schema"]
db.defaultTemplate.insert_one({"class_name": class_name, "template": template["template"], "schema": schema})
return


Expand Down Expand Up @@ -71,34 +52,32 @@ def get_schema_from_mongo(template):
return records[0]


def get_schema_from_mongo_by_classes_name(classes_name):
def get_templates_from_mongo_by_classes_name(classes_name):
records = db.createdTemplate.find({'classes_name': {'$in': classes_name}})
if records.count() == 0:
default_template = get_default_templates_from_mongo(classes_name)
result = []

if records.count() == 0 and default_template is None:
return None
else:
result = []
if default_template is not None:
for template in default_template:
result.append(template)
for record in records:
result.append(record["template"])
return result

# def get_schema_from_mongo(classes_name):
# if db.createdTemplate.count_documents({'classes_name': {'$in': classes_name}}) == 0:
# return None
# else:
# result = db.createdTemplate.find({'classes_name': {'$in': classes_name}})
#
# elements_match_records = {
# '_id': "",
# 'match': 0,
# 'schema': {}
# }
# for document in result:
# merged = set(classes_name) & set(document['classes_name'])
# if len(merged) > elements_match_records["match"]:
# elements_match_records['match'] = len(merged)
# elements_match_records['schema'] = document
# elements_match_records['_id'] = document['_id']
# return elements_match_records

def get_default_templates_from_mongo(classes_name):
if db.defaultTemplate.count_documents({'classes_name': {'$in': classes_name}}) == 0:
return None
else:
result = db.defaultTemplate.find({'classes_name': {'$in': classes_name}})
templates = []

for document in result:
templates.append(document["template"])
return templates


def get_record_from_collection(uid, collection):
Expand All @@ -124,8 +103,6 @@ def delete_collection(collection):
db[collection].delete_many({})


delete_collection("data")
delete_collection("template")

# unique = datetime.now()
# #update_data(5552,"aaa")
Expand All @@ -142,4 +119,5 @@ def delete_collection(collection):
# print("get all records")
# get_all_records_from_collection("createdTemplate")
# delete_collection("createdTemplate")
# delete_collection("defaultTemplate")
# get_all_records_from_collection("data")
16 changes: 13 additions & 3 deletions src/Routes/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from src.Utils.Utils import read_file

from src.Routes.mongo import insert_template_in_mongo, get_all_records_from_collection, update_data_in_mongo, \
get_record_from_collection, add_record_to_collection, get_schema_from_mongo, populate_template_collection, \
delete_collection, get_schema_from_mongo_by_classes_name
get_record_from_collection, add_record_to_collection, get_schema_from_mongo, \
delete_collection, get_templates_from_mongo_by_classes_name

parser = argparse.ArgumentParser(description="Starts the archgraph server.")

Expand Down Expand Up @@ -67,6 +67,16 @@ def response_get_node(uid):
return make_response(jsonify(message="Node doesn't exists"), 404)


@app.route("/template/<uid>", methods=["GET"])
@cross_origin()
def default_template(uid):
node = get_node_by_uid(uid)
if node is not None:
return make_response(jsonify(node.generate_template()["template"]), 201)
else:
return make_response(jsonify(message="Node doesn't exists"), 404)


@app.route("/withtemplate/<uid>", methods=["GET"])
@cross_origin()
def get_record(uid):
Expand Down Expand Up @@ -160,7 +170,7 @@ def get_templates_from_entity(uid):
if node is not None:
get_all_records_from_collection("createdTemplate")
classes_name = node.get_superclasses_name()
templates = get_schema_from_mongo_by_classes_name(classes_name)
templates = get_templates_from_mongo_by_classes_name(classes_name)
if templates is None:
return make_response(jsonify(message="Don't have templates for this entity"), 200)
else:
Expand Down
5 changes: 3 additions & 2 deletions src/Utils/defaultTemplates.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
{
"className": "E52_Time_Span",
"template": {
"E52_Time_Span": {
"P86_falls_within": "E52_Time_Span"}
"E52_Time_Span": {
"P86_falls_within": "E52_Time_Span"
}
}
}
]
Loading

0 comments on commit 4a419fb

Please sign in to comment.