def update_licenses_relations_rec(new_neo_license, new_object_license, neo_license, object_license): # update precedings and followings of license recursively. grand_follower = False for neo_license_following in neo_license.followings: object_license_following = ObjectFactory.objectLicense( neo_license_following) if new_object_license.is_following(object_license_following): # new license is a follower of a following grand_follower = True if new_object_license.is_preceding(object_license_following): if Constraints.is_compatibility_viable(new_object_license, object_license_following): neo_license_following.precedings.connect(new_neo_license) if new_object_license.is_following(object_license): # new_license is between license and its following_license. if Constraints.is_compatibility_viable(object_license, new_object_license): neo_license.followings.connect(new_neo_license) neo_license.followings.disconnect(neo_license_following) else: update_licenses_relations_rec(new_neo_license, new_object_license, neo_license_following, object_license_following) if not grand_follower and new_object_license.is_following(object_license): # then its just the next follower of the current license if Constraints.is_compatibility_viable(object_license, new_object_license): neo_license.followings.connect(new_neo_license)
def add_license_to_db(object_license): neo_license = LicenseModel.nodes.get_or_none( hashed_sets=object_license.hash()) if neo_license: # update of labels list if needed neo_license.labels = list( set(object_license.get_labels()).union(neo_license.labels)) neo_license.save() else: # license does not exists in db license_leaves = get_leaf_licenses() neo_license = NeoFactory.NeoLicense(object_license) neo_license.save() for neo_license_leaf in license_leaves: object_license_leaf = ObjectFactory.objectLicense(neo_license_leaf) if object_license.is_preceding(object_license_leaf): if Constraints.is_compatibility_viable(object_license, object_license_leaf): neo_license_leaf.precedings.connect(neo_license) else: update_licenses_relations_rec(neo_license, object_license, neo_license_leaf, object_license_leaf) for dataset in object_license.get_datasets(): neo_dataset = DatasetModel.nodes.get_or_none(hashed_uri=dataset.hash()) if not neo_dataset: neo_dataset = NeoFactory.NeoDataset(dataset) neo_dataset.save() neo_license.datasets.connect(neo_dataset) object_license = ObjectFactory.objectLicense(neo_license) return object_license
def update_licenses_relations_supremum_rec(new_neo_license, new_object_license, neo_license, object_license, viability_check, nb_visit, tested_licenses): # update precedings and followings of license recursively. if object_license in tested_licenses: return nb_visit nb_visit += 1 tested_licenses.append(object_license) grand_preceder = False for neo_license_preceding in neo_license.precedings: object_license_preceding = ObjectFactory.objectLicense( neo_license_preceding) if already_preceder(object_license_preceding, new_neo_license): continue if new_object_license.is_following(object_license_preceding) and ( Constraints.is_compatibility_viable(object_license_preceding, new_object_license) or not viability_check): update_transitivity_preceder(new_neo_license, object_license_preceding) new_neo_license.precedings.connect(neo_license_preceding) if new_object_license.is_preceding(object_license) and ( Constraints.is_compatibility_viable( new_object_license, object_license) or not viability_check): new_neo_license.followings.connect(neo_license) neo_license.precedings.disconnect(neo_license_preceding) else: if new_object_license.is_preceding(object_license_preceding) and ( Constraints.is_compatibility_viable( new_object_license, object_license_preceding) or not viability_check): grand_preceder = True nb_visit = update_licenses_relations_supremum_rec( new_neo_license, new_object_license, neo_license_preceding, object_license_preceding, viability_check, nb_visit, tested_licenses) if not grand_preceder and ( new_object_license.is_preceding(object_license) and (Constraints.is_compatibility_viable( new_object_license, object_license) or not viability_check)): new_neo_license.followings.connect(neo_license) return nb_visit
def add_license(request, graph): json_licenses = json.loads(request.body) added_licenses = [] # random.shuffle(json_licenses) license_levels = [] # level_median = 0 ''' try: with open(LEVELS_FILE, 'r') as f: license_levels = json.load(f) except IOError: pass ''' for json_license in json_licenses: object_license = License() object_license.from_json(json_license) if object_license.contains_only_odrl_actions(): if Constraints.is_license_viable(object_license): object_license, nb_visit = add_license_to_db( object_license, method='infimum', license_levels=license_levels, graph=graph) ''' if license_levels: level_median = median(license_levels) if object_license.get_level() > level_median: object_license, nb_visit = add_license_to_db(object_license, method='supremum', license_levels=license_levels, graph=graph) else: object_license, nb_visit = add_license_to_db(object_license, method='infimum', license_levels=license_levels, graph=graph) ''' added_licenses.append(object_license.to_json()) else: added_licenses.append( "Not a valid license: License is non-viable") else: added_licenses.append("Not a valid license: Use only ODRL actions") ''' with open(LEVELS_FILE, 'w') as outfile: json.dump(license_levels, outfile) ''' response = HttpResponse( json.dumps(added_licenses), content_type='application/json', status=201, ) response['Access-Control-Allow-Origin'] = '*' return response
def update_licenses_relations_supremum(object_license, viability_check, nb_visit, graph='ld'): tested_licenses = [object_license] license_roots = get_root_licenses(graph) neo_license = NeoFactory.NeoLicense(object_license, graph) neo_license.save() for neo_license_root in license_roots: object_license_root = ObjectFactory.objectLicense(neo_license_root) if object_license.is_following(object_license_root) and ( Constraints.is_compatibility_viable(object_license_root, object_license) or not viability_check): update_transitivity_preceder(neo_license, object_license_root) neo_license_root.followings.connect(neo_license) else: nb_visit = update_licenses_relations_supremum_rec( neo_license, object_license, neo_license_root, object_license_root, viability_check, nb_visit, tested_licenses) return neo_license, nb_visit
def update_licenses_relations_infimum(object_license, viability_check, nb_visit, graph='ld'): tested_licenses = [object_license] license_leaves = get_leaf_licenses(graph) neo_license = NeoFactory.NeoLicense(object_license, graph) neo_license.save() for neo_license_leaf in license_leaves: object_license_leaf = ObjectFactory.objectLicense(neo_license_leaf) if object_license.is_preceding(object_license_leaf) and ( Constraints.is_compatibility_viable(object_license, object_license_leaf) or not viability_check): update_transitivity_follower(neo_license, object_license_leaf) neo_license_leaf.precedings.connect(neo_license) else: nb_visit = update_licenses_relations_infimum_rec( neo_license, object_license, neo_license_leaf, object_license_leaf, viability_check, nb_visit, tested_licenses) return neo_license, nb_visit
def add_license(request): json_licenses = json.loads(request.body) added_licenses = [] for json_license in json_licenses: object_license = License() object_license.from_json(json_license) if object_license.contains_only_odrl_actions(): if Constraints.is_license_viable(object_license): object_license = add_license_to_db(object_license) added_licenses.append(object_license.to_json()) else: added_licenses.append( "Not a valid license: License is non-viable") else: added_licenses.append("Not a valid license: Use only ODRL actions") response = HttpResponse( json.dumps(added_licenses), content_type='application/json', status=201, ) response['Access-Control-Allow-Origin'] = '*' return response