예제 #1
0
 def _find_used_data_input_for_constraints(self, template,
                                           name_of_constraint):
     dataType = None
     temporalResolution = None
     spatialResolution = None
     for o in template.objects(name_of_constraint, self.PRIVVULN.feeds):
         if o in self.domain_supported_data_streams:
             dataType = o
             break
     if dataType is None:
         print(name_of_constraint,
               " uses not support data stream or no stream defined")
     else:
         try:
             #Only one transformation or PrivacyAttacks per model.
             temporalResolution = template.value(
                 predicate=self.PRIVVULNV2.TemporalResolution,
                 subject=name_of_constraint,
                 any=False)
         except rdflib.exceptions.UniquenessError:
             return
     spatialResolutions = []
     for spatialResolution in template.objects(
             name_of_constraint, self.PRIVVULNV2.spatialRequirement):
         spatialResolutions.append(spatialResolution)
     temporalResolution = float(
         temporalResolution.value
     ) if temporalResolution is not None else None
     return Data(dataType,
                 temporalResolution,
                 spatial_resolutions=spatialResolutions)
예제 #2
0
    def find_all_input_data_old(self, inputModel):
        inputModel_temp = copy.deepcopy(inputModel)
        inputModel_temp.parse(self.domain_url)
        inputModel_temp.parse(self.extention_ontology_url)

        inputModel_temp.parse(self.base_ontology_url)

        q = rdflib.plugins.sparql.prepareQuery(
            """
                SELECT DISTINCT ?context ?inputName ?individualData ?resolution ?templateCount ?context_type
                WHERE {
                    ?individualData rdf:type owl:NamedIndividual .
                    ?inputName rdf:type ?individualData .

                    ?dataTypes rdfs:subClassOf pv:Data .
                    ?inputName rdf:type ?dataTypes .

                    ?context  rdf:type/rdfs:subClassOf* pv2:Context .
                    ?context  pv2:has* ?directDataInputName .
                    ?context  rdf:type ?context_type .

                    #Finds datatypes supported by domain
                    ?directDataInputName  pv:feeds* ?inputName .

                    #Find transformation count for datainput if it is set
                    OPTIONAL {
                         ?inputName pv2:TemplateCount ?templateCount .
                    }.
                    OPTIONAL {
                        ?inputName pv2:TemporalResolution ?resolution .
                    }
                    FILTER ( !bound(?templateCount) || ?templateCount < ?max_count  )
                }
                """,
            initNs=NSUtil.get_binding_namespaces()
            # ,
            # initNs = {}
        )
        ro = inputModel_temp.query(
            q, initBindings={'?max_count': self.max_transformation_count})

        supported_data_types = {}

        for row in ro:
            if not row[0] in supported_data_types:
                supported_data_types[row[0]] = []
            template_count = 0 if row[4] is None else int(row[4])
            resolution = None if row[3] is None else float(row[3])
            supported_data_types[row[0]].append(
                Data(row[2],
                     resolution,
                     subject_name=row[1],
                     template_count=template_count,
                     context=row[5],
                     context_subject=row[0]))
        return supported_data_types
예제 #3
0
    def find_all_domain_types_from_input_data_old(self, inputModel):
        inputModel_temp = copy.deepcopy(inputModel)
        inputModel_temp.parse(self.domain_url)
        inputModel_temp.parse(self.extention_ontology_url)

        inputModel_temp.parse(self.base_ontology_url)

        q = rdflib.plugins.sparql.prepareQuery(
            """
                # SELECT DISTINCT  ?inputName ?individualData ?inputName_subData ?individualData_subData ?templateCount ?resolution ?context ?contextType
                SELECT DISTINCT  ?inputName ?individualData ?inputName_subData ?individualData_subData ?templateCount ?resolution ?context ?contextType
                WHERE {
                    ?individualData rdf:type owl:NamedIndividual .
                    ?inputName rdf:type ?individualData .

                    ?inputName rdf:type/rdfs:subClassOf pv:Data .
                    # ?inputName rdf:type ?dataTypes .

                    ?individualData_subData rdf:type owl:NamedIndividual .
                    ?inputName_subData rdf:type ?individualData_subData .

                    ?dataTypes_subData rdfs:subClassOf pv:Data .
                    ?inputName_subData rdf:type ?dataTypes_subData .

                    ?inputName pv:feeds* ?inputName_subData .

                    ?context pv2:has ?inputName .
                    ?context  rdf:type/rdfs:subClassOf* pv2:Context .
                    ?context  rdf:type ?contextType  .

                    #Find transformation count for datainput if it is set
                    OPTIONAL {
                         ?inputName_subData pv2:TemplateCount ?templateCount .
                    }.
                    OPTIONAL {
                        ?inputName_subData pv2:TemporalResolution ?resolution .
                    }
                    FILTER ( !bound(?templateCount) || ?templateCount < ?max_count  )

                }
                """,
            initNs=NSUtil.get_binding_namespaces())

        seconds = time.time()

        ro = inputModel_temp.query(
            q, initBindings={'?max_count': self.max_transformation_count})

        domain_data_found_using_data = {}

        for row in ro:
            print("Loop:", time.time() - seconds)
            if not row[6] in domain_data_found_using_data:
                domain_data_found_using_data[row[6]] = {}
            if not row[1] in domain_data_found_using_data[row[6]]:
                domain_data_found_using_data[row[6]][row[1]] = []
            template_count = 0 if row[4] is None else int(row[4])
            resolution = None if row[5] is None else float(row[5])
            domain_data_found_using_data[row[6]][row[1]].append(
                Data(row[3],
                     resolution,
                     subject_name=row[2],
                     template_count=template_count,
                     base_subject_name=row[0],
                     spatial_resolutions=row[7]))
            # domain_data_found_using_data[row[1]].append(row[2])
        return domain_data_found_using_data
    def can_template_be_used(self, inputModel, template):
        stored_privacy_attack = self.get_loaded_privacy_attack(template)
        template_needed_data_types = stored_privacy_attack.get_model_requeued_data_types(
        )
        template_needed_data_dict = DataUtil.find_domain_data_in_data_list_dict(
            template_needed_data_types)

        context_data_types = self._get_context_data_types(inputModel)

        #import pdb; pdb.set_trace()

        context_structure = self._get_input_context_structure(inputModel)

        output_type = stored_privacy_attack.get_output_data_type()

        template_name = stored_privacy_attack.get_template_name()

        usered_data_subjects = []

        usefull_data_input = {}
        for context in context_data_types.keys():
            context_data_type = context_data_types[context]
            template_can_be_used_in_context = False
            # template_can_be_used_in_data_context = True
            temp_data_objects = {}
            for template_data_type in template_needed_data_types:
                context_data_objects = []
                for elem in context_data_type:
                    if elem.domain_data_type == template_data_type.domain_data_type:
                        context_data_objects.append(elem)
                if len(context_data_objects) > 0:
                    for context_data_object in context_data_objects:
                        spatial_resolution_match = False
                        if len(template_data_type.spatial_resolutions) > 0:
                            for spatial_resolutions in template_data_type.spatial_resolutions:
                                if self._context_match(
                                        spatial_resolutions,
                                        context_data_object.context):
                                    spatial_resolution_match = True
                                    break
                        else:
                            spatial_resolution_match = True
                        if spatial_resolution_match:
                            if template_data_type.temporal_resolutions is None or context_data_object.temporal_resolutions <= template_data_type.temporal_resolutions:
                                if template_data_type.domain_data_type not in temp_data_objects:
                                    temp_data_objects[template_data_type.
                                                      domain_data_type] = []
                                temp_data_objects[
                                    template_data_type.
                                    domain_data_type].append(
                                        Data(
                                            template_data_type.
                                            domain_data_type,
                                            context_data_object.
                                            temporal_resolutions,
                                            template_name=template_name,
                                            subject_name=context_data_object.
                                            subject_name,
                                            template_count=context_data_object.
                                            template_count,
                                            base_subject_name=context_data_object
                                            .base_subject_name,
                                            description=template_data_type.
                                            description,
                                            spatial_resolutions=
                                            template_data_type.context))
            if len(temp_data_objects.keys()) == len(
                    template_needed_data_types):
                usefull_data_input[context] = temp_data_objects
            elif len(temp_data_objects.keys()) > 0:
                #look for data stream at a higher level in the context strucktor to find missing sources
                missing_streams = template_needed_data_dict.keys(
                ) - temp_data_objects.keys()
                if context not in context_structure:
                    continue
                context_has_base = True
                context_objet = context_structure[context]
                context_name = context_objet.super_subject
                while context_has_base:
                    for missing_stream in missing_streams:
                        stream = template_needed_data_dict[missing_stream]
                        if len(stream.spatial_resolutions) > 0:
                            spatial_resolution_match = False
                            for spatial_resolutions in stream.spatial_resolutions:
                                if self._context_match(
                                        spatial_resolutions,
                                        context_objet.super_domain_class_type):
                                    spatial_resolution_match = True
                                    break
                        else:
                            spatial_resolution_match = True
                        if spatial_resolution_match and context_name in context_data_types:
                            for data_type in context_data_types[context_name]:
                                if data_type.domain_data_type == missing_stream:
                                    if stream.temporal_resolutions is None or data_type.temporal_resolutions <= stream.temporal_resolutions:
                                        data_type.spatial_resolutions = data_type.context
                                        data_type.template_name = template_name
                                        if stream.temporal_resolutions is None or stream.temporal_resolutions >= stream.temporal_resolutions:
                                            if stream.domain_data_type not in temp_data_objects:
                                                temp_data_objects[
                                                    stream.
                                                    domain_data_type] = []
                                            temp_data_objects[
                                                stream.
                                                domain_data_type].append(
                                                    data_type)
                                        # elif stream.temporal_resolutions >= stream.temporal_resolutions:
                                        #     if stream.domain_data_type not in temp_data_objects: temp_data_objects[stream.domain_data_type] = []
                                        #     temp_data_objects[stream.domain_data_type].append(data_type)
                    if context_name not in context_structure:
                        context_has_base = False
                        break
                    context_objet = context_structure[context_name]
                    context_name = context_objet.super_subject
                if len(temp_data_objects) == len(template_needed_data_types):
                    # temp_subject_names = [x.subject_name for x in temp_data_objects]
                    # if len(set(temp_subject_names) & set(usered_data_subjects)) == 0:
                    # usered_data_subjects.extend(temp_subject_names)
                    usefull_data_input[context] = temp_data_objects
        return usefull_data_input
    def combind_using_template_context(self,
                                       inputModel,
                                       used_data_inputs,
                                       stored_transformation,
                                       template,
                                       context_url=None):
        template_rand_nr = uuid.uuid4().__str__()

        output_domain_data_type = stored_transformation.get_template_output_domain_data_type(
        )

        for data_obj in used_data_inputs:
            inputModel.add((data_obj.subject_name, self.PRIVVULN.feeds,
                            data_obj.template_name + template_rand_nr))
            inputModel.add((data_obj.domain_data_type, self.PRIVVULN.feeds,
                            data_obj.template_name + template_rand_nr))
            self._add_element_data_creates_from_data_types(
                data_obj.subject_name, output_domain_data_type, inputModel)

        inputModel.add((data_obj.template_name + template_rand_nr,
                        self.RDF.type, self.PRIVVULN.Transformation))
        inputModel.add((data_obj.template_name + template_rand_nr,
                        self.PRIVVULNV2.name, data_obj.template_name))

        output_subjet = stored_transformation.get_template_output_subject()
        output_data_type = stored_transformation.get_template_template_output_data_type(
        )

        self._add_element_data_creates_from_data_types(
            output_subjet, output_domain_data_type, inputModel)

        # output_subjet, output_data_type, output_domain_data_type = self._find_output_for_template(template)

        inputModel.add((output_subjet + template_rand_nr, self.RDF.type,
                        output_domain_data_type))
        inputModel.add((output_subjet + template_rand_nr, self.RDF.type,
                        output_data_type))
        inputModel.add((output_subjet + template_rand_nr, self.PRIVVULNV2.name,
                        output_subjet))

        inputModel.add((data_obj.template_name + template_rand_nr,
                        self.PRIVVULN.feeds, output_subjet + template_rand_nr))

        time_resolutions = stored_transformation.get_time_relations_for_transformation(
        )
        time_resultion = self._find_output_time_resultion_for_transformation(
            time_resolutions, used_data_inputs)

        if time_resultion is not None:
            inputModel.add((output_subjet + template_rand_nr,
                            self.PRIVVULNV2.TemporalResolution,
                            rdflib.Literal(time_resultion)))

        template_count = self._find_template_count(used_data_inputs)
        inputModel.add(
            (output_subjet + template_rand_nr, self.PRIVVULNV2.TemplateCount,
             rdflib.Literal(template_count)))

        context_types = self._get_context_types(inputModel)

        self._get_context_data_types(inputModel)[context_url].append(
            Data(
                output_domain_data_type,
                time_resultion,
                subject_name=output_subjet + template_rand_nr,
                template_count=template_count,
                context_subject=context_url,
                # spatial_resolutions=rdflib.term.URIRef('https://ontology.hviidnet.com/2020/01/03/smartbuildingprivacyvunl.ttl#Room'),
                context=context_types[context_url]))
        # domain_data_type, temporal_resolutions,subject_name, template_count, context, context_subject

        return inputModel
    def combind_using_template_to_super_context(self,
                                                inputModel,
                                                used_data_inputs,
                                                stored_transformation,
                                                template,
                                                spatial_resultion,
                                                context_url=None):
        super_class_name_url = self.find_context_super_class(
            used_data_inputs, spatial_resultion, inputModel)
        if super_class_name_url is None:
            return inputModel

        super_class_name = super_class_name_url.split('#')[-1]

        output_domain_data_type = stored_transformation.get_template_output_domain_data_type(
        )

        for data_obj in used_data_inputs:
            inputModel.add((data_obj.subject_name, self.PRIVVULN.feeds,
                            data_obj.template_name + super_class_name))
            inputModel.add((data_obj.domain_data_type, self.PRIVVULN.feeds,
                            data_obj.template_name + super_class_name))
            self._add_element_data_creates_from_data_types(
                data_obj.subject_name, output_domain_data_type, inputModel)

        inputModel.add((data_obj.template_name + super_class_name,
                        self.RDF.type, self.PRIVVULN.Transformation))
        inputModel.add((data_obj.template_name + super_class_name,
                        self.PRIVVULNV2.name, data_obj.template_name))

        output_subjet = stored_transformation.get_template_output_subject()
        output_data_type = stored_transformation.get_template_template_output_data_type(
        )
        self._add_element_data_creates_from_data_types(
            output_subjet, output_domain_data_type, inputModel)

        self._add_element_data_creates_from_data_types(
            super_class_name_url, output_domain_data_type, inputModel)

        inputModel.add((output_subjet + super_class_name, self.RDF.type,
                        output_domain_data_type))
        inputModel.add((output_subjet + super_class_name, self.RDF.type,
                        output_data_type))
        inputModel.add((output_subjet + super_class_name, self.PRIVVULNV2.name,
                        output_subjet))

        inputModel.add(
            (data_obj.template_name + super_class_name,
             self.PRIVVULNV2.creates, output_subjet + super_class_name))

        inputModel.add((super_class_name_url, self.PRIVVULNV2.has,
                        output_subjet + super_class_name))

        time_resolutions = stored_transformation.get_time_relations_for_transformation(
        )
        time_resultion = self._find_output_time_resultion_for_transformation(
            time_resolutions, used_data_inputs)

        if time_resultion is not None:
            if (output_subjet + super_class_name,
                    self.PRIVVULNV2.TemporalResolution, None) in inputModel:
                temporalResolution = inputModel.value(
                    subject=output_subjet + super_class_name,
                    predicate=self.PRIVVULNV2.TemporalResolution)
                if temporalResolution.value < time_resultion:
                    inputModel.remove((output_subjet + super_class_name,
                                       self.PRIVVULNV2.TemporalResolution,
                                       temporalResolution))
                    inputModel.add((output_subjet + super_class_name,
                                    self.PRIVVULNV2.TemporalResolution,
                                    rdflib.Literal(time_resultion)))
            else:
                inputModel.add((output_subjet + super_class_name,
                                self.PRIVVULNV2.TemporalResolution,
                                rdflib.Literal(time_resultion)))

        template_count = self._find_template_count(used_data_inputs)
        inputModel.add(
            (output_subjet + super_class_name, self.PRIVVULNV2.TemplateCount,
             rdflib.Literal(template_count)))

        context_types = self._get_context_types(inputModel)

        self._get_context_data_types(inputModel)[context_url].append(
            Data(
                output_data_type,
                time_resultion,
                subject_name=output_subjet + super_class_name,
                template_count=template_count,
                context_subject=context_url,
                # spatial_resolutions=rdflib.term.URIRef('https://ontology.hviidnet.com/2020/01/03/smartbuildingprivacyvunl.ttl#Room'),
                context=context_types[context_url]))
        # domain_data_type, temporal_resolutions,subject_name, template_count, context, context_subject

        return inputModel