All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.idml.utils.DocumentClassifier.scala Maven / Gradle / Ivy

There is a newer version: 63488a16f994d8c0416bab30ec9ef2b0304a03b5
Show newest version
package io.idml.utils

import io.idml.utils.visitor.{ExecNodeVisitor, StructureAgnosticVisitationStyle}
import io.idml.ast.Document

trait DocumentType
object SchemaDocumentType                  extends DocumentType
object MappingDocumentType                 extends DocumentType
object MixedDocumentType                   extends DocumentType
class ClassificationException(msg: String) extends RuntimeException(msg)

/** Classifies documents */
object DocumentClassifier {

  class DocumentClassifierVisitor extends ExecNodeVisitor with StructureAgnosticVisitationStyle {
    var assignments   = 0
    var reassignments = 0

    override def visitAssign(ctx: ExecAssignContext): Unit = {
      assignments += 1
    }

    override def visitReassign(ctx: ExecReassignContext): Unit = {
      reassignments += 1
    }

    def classifyDocument(): DocumentType = {
      (assignments, reassignments) match {
        case (a, r) if (r > 0 && a == 0) => SchemaDocumentType
        case (a, r) if (r > 0 && a > 0)  => MixedDocumentType
        case (a, r) if (r == 0 && a > 0) => MappingDocumentType
        case (a, r) if (r == 0 && a == 0) =>
          throw new ClassificationException("Document contained no assignments or reassignments")
        case _ =>
          throw new ClassificationException("Unable to classify document")
      }
    }
  }

  /**
    * Classify an Document tree as a Mapping, Schema or Mixed document
    * @param doc parsed Document tree
    * @return any subclass of DocumentType
    */
  def classify(doc: Document): DocumentType = {
    val visitor = new DocumentClassifierVisitor()
    visitor.visit(doc)
    visitor.classifyDocument()
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy