Beispiel #1
0
        def startElement(self, element_name, attributes):
            """ Overrides startElement in class ContentHandler

            :param element_name: Contains the raw XML 1.0 name of the element type.
            :type element_name: str
            :param attributes: An instance of the Attributes class containing
              the attributes of the element
            :type attributes: xml.sax.xmlreader.AttributesImpl
            """
            tag_name = element_name
            if tag_name != "WorkflowLog":
                if tag_name == "Source":
                    self.__sourceOpen = True
                    description_string = attributes.get("program")
                    self.__sourceAttribute = XMxmlParser(
                    ).factory.create_attribute_literal("source",
                                                       description_string,
                                                       None)
                    self.__add_model_reference__(attributes,
                                                 self.__sourceAttribute)
                elif tag_name == "Process":
                    description_string = attributes.get("id")
                    description = attributes.get("description")

                    self.__currentProcess = XMxmlParser().factory.create_log()
                    self.__currentProcess.get_extensions().add(
                        XConceptExtension())
                    self.__currentProcess.get_extensions().add(
                        XOrganizationalExtension())
                    self.__currentProcess.get_extensions().add(
                        XLifecycleExtension())
                    self.__currentProcess.get_extensions().add(
                        XSemanticExtension())
                    self.__currentProcess.get_extensions().add(
                        XTimeExtension())
                    if self.__sourceAttribute:
                        self.__currentProcess.get_attributes()[
                            self.__sourceAttribute.get_key(
                            )] = self.__sourceAttribute

                    XConceptExtension().assign_name(self.__currentProcess,
                                                    description_string)
                    XLifecycleExtension().assign_model(self.__currentProcess,
                                                       "standard")

                    if description and len(description.lower()) > 0:

                        description1 = XMxmlParser(
                        ).factory.create_attribute_literal(
                            "description", description, None)
                        self.__currentProcess.get_attributes()[
                            description1.get_key()] = description1

                    self.__add_model_reference__(attributes,
                                                 self.__currentProcess)

                elif tag_name == "ProcessInstance":
                    self.__currentInstance = XMxmlParser(
                    ).factory.create_trace()
                    name = attributes.get("id")
                    if name is None:
                        name = "None"

                    XConceptExtension().assign_name(self.__currentInstance,
                                                    name)
                    description_string = attributes.get("description")

                    if description_string and len(
                            description_string.strip()) > 0:
                        description2 = XMxmlParser(
                        ).factory.create_attribute_literal(
                            "description", description_string, None)
                        self.__currentInstance.get_attributes()[
                            description2.get_key()] = description2

                    self.__add_model_reference__(attributes,
                                                 self.__currentInstance)

                elif tag_name == "AuditTrailEntry":
                    self.__entry = XMxmlParser().factory.create_event()

                elif tag_name == "Attribute":
                    self.__genericAttribute = XMxmlParser(
                    ).factory.create_attribute_literal(
                        attributes.get("name").strip(), "DEFAULT_VALUE", None)
                    self.__add_model_reference__(attributes,
                                                 self.__genericAttribute)

                elif tag_name == "EventType":
                    self.__eventTypeAttribute = XLifecycleExtension(
                    ).ATTR_TRANSITION.clone()
                    unknown_type = attributes.get("unknowntype")
                    if unknown_type:
                        self.__eventTypeAttribute.set_value(unknown_type)
                    else:
                        self.__eventTypeAttribute.set_value("UNKNOWN")

                    self.__add_model_reference__(attributes,
                                                 self.__eventTypeAttribute)

                elif tag_name == "WorkflowModelElement":
                    self.__add_model_reference__(attributes, self.__entry)

                elif tag_name == "Originator":
                    self.__originatorAttribute = XOrganizationalExtension(
                    ).ATTR_RESOURCE.clone()
                    self.__add_model_reference__(attributes,
                                                 self.__originatorAttribute)
Beispiel #2
0
        def endElement(self, local_name):
            """ Overrides endElement in class ContentHandler

            :param local_name: The name of the element type, just as with the startElement event
            :type local_name: str
            """
            tag_name = local_name

            if tag_name == "WorkflowLog":
                if self.__numUnorderedEntries > 0:
                    XLogging().log(
                        "LogData: Log contains " +
                        str(self.__numUnorderedEntries) +
                        " audit trail entries in non-natural order!",
                        XLogging.Importance.ERROR)
                    XLogging().log(
                        "LogData: The log file you have loaded is not MXML compliant! (error compensated transparently)",
                        XLogging.Importance.ERROR)

            elif tag_name == "Process":
                self.__currentProcess.get_classifiers().extend(
                    XMxmlParser.MXML_CLASSIFIERS)
                self.__currentProcess.get_global_trace_attributes().append(
                    XConceptExtension().ATTR_NAME.clone())
                self.__currentProcess.get_global_event_attributes().append(
                    XConceptExtension().ATTR_NAME.clone())
                self.__currentProcess.get_global_event_attributes().append(
                    XLifecycleExtension().ATTR_TRANSITION.clone())
                self.__logs.append(self.__currentProcess)
                self.__currentProcess = None

            elif tag_name == "Process":
                self.__sourceOpen = False

            elif tag_name == "ProcessInstance":
                if len(self.__currentInstance) > 0:
                    self.__currentProcess.append(self.__currentInstance)

                self.__currentInstance = None
                self.__lastTimestamp = None

            elif tag_name == "AuditTrailEntry":
                if self.__timestamp is None:
                    self.__currentInstance.append(self.__entry)
                elif self.__lastTimestamp is None:
                    self.__currentInstance.append(self.__entry)
                    self.__lastTimestamp = self.__timestamp
                elif self.__timestamp > self.__lastTimestamp:
                    self.__currentInstance.append(self.__entry)
                    self.__lastTimestamp = self.__timestamp
                else:
                    self.__currentInstance.append(self.__entry)

                self.__entry = None

            else:
                if tag_name == "Attribute":
                    originator = "".join(self.__buffer).strip()
                    if len(originator) > 0:
                        self.__genericAttribute.set_value("".join(
                            self.__buffer).strip())
                        if self.__entry:
                            self.__entry.get_attributes()[
                                self.__genericAttribute.get_key(
                                )] = self.__genericAttribute
                        elif self.__currentInstance:
                            self.__currentInstance.get_attributes()[
                                self.__genericAttribute.get_key(
                                )] = self.__genericAttribute
                        elif self.__currentProcess:
                            self.__currentProcess.get_attributes()[
                                self.__genericAttribute.get_key(
                                )] = self.__genericAttribute
                        elif self.__sourceOpen:
                            self.__sourceAttribute.get_attributes()[
                                self.__genericAttribute.get_key(
                                )] = self.__genericAttribute
                    self.__genericAttribute = None

                elif tag_name == "EventType":
                    if self.__eventTypeAttribute.get_value() == "UNKNOWN":
                        originator = "".join(self.__buffer).strip()
                        if len(originator) > 0:
                            self.__eventTypeAttribute.set_value(originator)
                            self.__entry.get_attributes()[
                                self.__eventTypeAttribute.get_key(
                                )] = self.__eventTypeAttribute
                    else:
                        self.__entry.get_attributes()[
                            self.__eventTypeAttribute.get_key(
                            )] = self.__eventTypeAttribute
                    self.__eventTypeAttribute = None

                elif tag_name == "WorkflowModelElement":
                    XConceptExtension().assign_name(
                        self.__entry, "".join(self.__buffer).strip())

                elif tag_name == "Timestamp":
                    originator = "".join(self.__buffer).strip()
                    self.__timestamp = parse_date_time(originator)
                    if self.__timestamp:
                        timestamp_attribute = XTimeExtension(
                        ).ATTR_TIMESTAMP.clone()
                        timestamp_attribute.set_value(self.__timestamp)
                        self.__entry.get_attributes(
                        )[timestamp_attribute.get_key()] = timestamp_attribute

                elif tag_name == "Originator":
                    originator = "".join(self.__buffer).strip()
                    if len(originator) > 0:
                        self.__originatorAttribute.set_value(originator)

                    self.__entry.get_attributes()[
                        self.__originatorAttribute.get_key(
                        )] = self.__originatorAttribute
                    self.__originatorAttribute = None

            self.__buffer.clear()
Beispiel #3
0
    def serialize(self, log, out, in_bytes=False):
        """Serializes a given log to the given output stream.

        :param log: Log to be serialized.
        :type log: `XLog`
        :param out:  TextIOWrapper for serialization.
        :type out: _io.TextIOWrapper
        :param in_bytes: Private argument to decide if serialized in bytes or in string
        :type in_bytes: bool
        """
        XLogging().log("start serializing log to MXML",
                       XLogging.Importance.DEBUG)
        start = time.time() * 1000

        root = Et.Element("WorkflowLog")
        source = Et.SubElement(root, "Source")
        source.set("program", "XES MXML serialization")
        source.set("openxes.version", "1.0RC7")
        process = Et.SubElement(root, "Process")
        identity = XConceptExtension().extract_name(log)
        if identity is None:
            process.set("id", "none")
        else:
            process.set("id", identity)
        name = XConceptExtension().extract_name(log)
        if name is None:
            name = "None"
        process.set("description", "process with id " + name)
        self.add_model_reference(log, process)
        self.add_attribute(process, log.get_attributes().values())

        for trace in log:
            instance = Et.SubElement(process, "ProcessInstance")
            identity = XConceptExtension().extract_name(trace)
            if identity is None:
                identity = "None"
            instance.set("id", identity)
            name = XConceptExtension().extract_name(trace)
            if name is None:
                name = "None"
            instance.set("description", "instance with id " + name)
            self.add_model_reference(trace, instance)
            self.add_attribute(instance, trace.get_attributes().values())

            for event in trace:
                ate = Et.SubElement(instance, "AuditTrailEntry")
                self.add_attribute(ate, event.get_attributes().values())
                workflow_mode = Et.SubElement(ate, "WorkflowModelElement")
                self.add_model_reference(event, workflow_mode)
                workflow_mode.text = XConceptExtension().extract_name(event)
                type_ = Et.SubElement(ate, "EventType")
                type_attr = event.get_attributes().get("lifecycle:transition")

                if type_attr:
                    self.add_model_reference(type_attr, type_)
                    originator_attr = type_attr.get_value().strip().lower()
                    if originator_attr in self.__known_types:
                        type_.text = originator_attr
                    else:
                        type_.set("unknownType", type_attr.get_value())
                        type_.text = "unknown"

                else:
                    type_.text = "complete"

                originator_attr_1 = event.get_attributes().get("org:resource")
                if originator_attr_1 is None:
                    originator_attr_1 = event.get_attributes().get("org:role")

                if originator_attr_1 is None:
                    originator_attr_1 = event.get_attributes().get("org:group")

                if originator_attr_1 is not None:
                    timestamp_attr = Et.SubElement(ate, "originator")
                    self.add_model_reference(originator_attr_1, timestamp_attr)
                    timestamp_attr.text = (originator_attr_1.get_value())

                timestamp_attr_1 = event.get_attributes().get("time:timestamp")
                if timestamp_attr_1:
                    timestamp = Et.SubElement(ate, "timestamp")
                    self.add_model_reference(timestamp_attr_1, timestamp)
                    date = timestamp_attr_1.get_value()
                    timestamp.text = str(date)

        text = minidom.parseString(Et.tostring(root, "utf-8"))
        c1 = text.createComment(
            "This file has been generated with the OpenXES library. It conforms"
        )
        c2 = text.createComment(
            "to the legacy MXML standard for log storage and management.")
        c3 = text.createComment("OpenXES library version: 1.0RC7")
        c4 = text.createComment(
            "OpenXES is available from http://www.xes-standard.org/")
        text.insertBefore(c4, text.childNodes[0])
        text.insertBefore(c3, text.childNodes[0])
        text.insertBefore(c2, text.childNodes[0])
        text.insertBefore(c1, text.childNodes[0])
        if in_bytes:
            out.write(text.toprettyxml("\t").encode())
        else:
            out.write(text.toprettyxml("\t"))

        duration1 = " (" + str(time.time() * 1000 - start) + " msec.)"
        XLogging().log("finished serializing log" + duration1,
                       XLogging.Importance.DEBUG)
Beispiel #4
0
def test_create_extension():
    extension = XConceptExtension()
    assert extension is not None