isac-java/src/java/isac/bridge/xml/JavaToIsa.scala
author Walther Neuper <wneuper@ist.tugraz.at>
Sun, 19 Jul 2015 14:42:05 +0200
changeset 4727 21335af0ba6a
parent 4725 c655f684eae3
child 4728 b936756c4a26
permissions -rw-r--r--
improve allocation of code Java -- Scala

Note: conversion from and to XML is easy with Scala's pattern matching.
wneuper@4723
     1
/* 
wneuper@4723
     2
 * @author Walther Neuper
wneuper@4723
     3
 * Copyright (c) due to license terms
wneuper@4723
     4
 * Created on Jul 14, 2015
wneuper@4723
     5
 * Institute for Softwaretechnology, Graz University of Technology, Austria.
wneuper@4723
     6
 * 
wneuper@4723
     7
 * From now on isac-java is a Scala Project.
wneuper@4723
     8
 * When ConvertXML.scala was not recognized switiching "Scala Nature" 
wneuper@4723
     9
 * off and on again helped.
wneuper@4723
    10
 */
wneuper@4723
    11
wneuper@4723
    12
package isac.bridge.xml
wneuper@4723
    13
wneuper@4725
    14
import isac.bridge.CChanged
wneuper@4723
    15
import isac.util.formulae.Position
wneuper@4725
    16
import isac.util.formulae.Specification
wneuper@4725
    17
import isac.util.Variant
wneuper@4725
    18
import isac.util.Formalization
wneuper@4723
    19
wneuper@4723
    20
import isabelle.XML
wneuper@4723
    21
import isabelle.Markup
wneuper@4725
    22
wneuper@4723
    23
import java.util.ArrayList
wneuper@4723
    24
import java.util.Vector
wneuper@4723
    25
import java.math.BigInteger
wneuper@4723
    26
import scala.math.BigInt
wneuper@4723
    27
import scala.collection.JavaConverters._
wneuper@4725
    28
import scala.collection.JavaConversions._
wneuper@4723
    29
wneuper@4723
    30
object JavaToIsa {
wneuper@4723
    31
wneuper@4725
    32
  //===== convert arguments of JSystem.invoke(Operations.*
wneuper@4723
    33
  // Conversions are all done in Scala (has specific methods), not in Java.
wneuper@4723
    34
  // Note: java.int-->scala.BigInt not done here, because "int" is unknown in Scala.
wneuper@4723
    35
wneuper@4723
    36
  // conversion java.lang.Integer --> scala.math.BigInt
wneuper@4723
    37
  def Integer_to_BigInt (i: java.lang.Integer): scala.math.BigInt = {
wneuper@4723
    38
    new scala.math.BigInt(new BigInteger(i.toString())) //TODO: improve conversion ?
wneuper@4723
    39
  }
wneuper@4723
    40
  // UNUSED conversion scala.math.BigInt --> java.lang.Integer
wneuper@4723
    41
  def BigInt_to_Integer (i: scala.math.BigInt): java.lang.Integer = {
wneuper@4723
    42
    new java.lang.Integer(i.toString()) //TODO: improve conversion ?
wneuper@4723
    43
  }
wneuper@4723
    44
wneuper@4723
    45
  //----- step 1 -----------------------
wneuper@4725
    46
  def xml_of_Specification(spec: Specification): XML.Tree = {
wneuper@4725
    47
    XML.Elem(Markup("SPECIFICATION", Nil), List(
wneuper@4725
    48
      XML.Elem(Markup("THEORYID", Nil), List(XML.Text(spec.getTheory().getID()))),
wneuper@4725
    49
      XML.Elem(Markup("PROBLEMID", Nil), List(
wneuper@4725
    50
        ConvertXML.xml_of_strs(spec.getProblem().getID().asScala.toList))),
wneuper@4725
    51
      XML.Elem(Markup("METHODID", Nil), List(
wneuper@4725
    52
        ConvertXML.xml_of_strs(spec.getMethod().getID().asScala.toList)))))
wneuper@4723
    53
  }
wneuper@4725
    54
  def xml_of_Variant(vat: isac.util.Variant): isabelle.XML.Tree = {
wneuper@4725
    55
    XML.Elem(Markup("VARIANT", Nil), List(
wneuper@4725
    56
      ConvertXML.xml_of_strs(vat.getIsaStrings().asScala.toList),
wneuper@4725
    57
      xml_of_Specification(vat.getSpecification())))
wneuper@4725
    58
  }
wneuper@4725
    59
  def xml_of_Formalization(fmz: Formalization): XML.Tree = {
wneuper@4725
    60
    XML.Elem(Markup("FORMALIZATION", Nil), fmz.getVariants().asScala.toList map xml_of_Variant)  
wneuper@4725
    61
  }
wneuper@4725
    62
  /*isabelle.XML.Tree = 
wneuper@4723
    63
      <FORMALIZATION>
wneuper@4725
    64
        <VARIANT>
wneuper@4725
    65
          <STRINGLIST>      
wneuper@4725
    66
            <STRING>equality (x+1=(2::real))</STRING>
wneuper@4725
    67
            <STRING>solveFor x</STRING>
wneuper@4725
    68
            <STRING>solutions L</STRING>
wneuper@4725
    69
          </STRINGLIST>
wneuper@4725
    70
          <SPECIFICATION>
wneuper@4725
    71
            <THEORYID>Test</THEORYID>
wneuper@4725
    72
            <PROBLEMID>
wneuper@4725
    73
              <STRINGLIST>
wneuper@4725
    74
                <STRING>sqroot-test</STRING>
wneuper@4725
    75
                <STRING>univariate</STRING>
wneuper@4725
    76
                <STRING>equation</STRING>
wneuper@4725
    77
                <STRING>test</STRING></STRINGLIST>
wneuper@4725
    78
            </PROBLEMID>
wneuper@4725
    79
            <METHODID>
wneuper@4725
    80
              <STRINGLIST>
wneuper@4725
    81
                <STRING>Test</STRING>
wneuper@4725
    82
                <STRING>squ-equ-test-subpbl1</STRING>
wneuper@4725
    83
              </STRINGLIST>
wneuper@4725
    84
            </METHODID>
wneuper@4725
    85
          </SPECIFICATION>
wneuper@4725
    86
        </VARIANT>
wneuper@4723
    87
      </FORMALIZATION>
wneuper@4725
    88
  */
wneuper@4723
    89
   
wneuper@4723
    90
  //----- step 4 -----------------------
wneuper@4727
    91
  def xml_of_Position(pos: Position): XML.Tree = {
wneuper@4727
    92
    val is: List[Integer] = pos.getIntList().asInstanceOf[Vector[Integer]].asScala.toList
wneuper@4727
    93
    ConvertXML.xml_of_pos(List(1: java.lang.Integer) map Integer_to_BigInt, pos.getKind())
wneuper@4727
    94
  }
wneuper@4727
    95
wneuper@4727
    96
  //def get_formulae(calcid: scala.math.BigInt, from: ICalcIterator,to: ICalcIterator,
wneuper@4727
    97
  def   get_formulae(calcid: scala.math.BigInt, from: Position,     to: Position,
wneuper@4723
    98
    level: scala.math.BigInt, rules/*?*/: String): XML.Tree =
wneuper@4723
    99
  { XML.Elem(Markup("GETFORMULAEFROMTO", Nil), List(
wneuper@4723
   100
      XML.Elem(Markup("CALCID", Nil), List(XML.Text(calcid.toString()))),
wneuper@4727
   101
      xml_of_Position(from),
wneuper@4727
   102
      xml_of_Position(to),
wneuper@4723
   103
      XML.Elem(Markup("INT", Nil), List(XML.Text(level.toString()))),
wneuper@4723
   104
      XML.Elem(Markup("BOOL", Nil), List(XML.Text(rules)))))
wneuper@4727
   105
  } 
wneuper@4723
   106
  
wneuper@4723
   107
  //----- step 6 -----------------------
wneuper@4723
   108
  //def ref_formula(calcid: scala.math.BigInt, pos: ICalcIterator,  //NOT RESOLVED BY sbt  
wneuper@4727
   109
  def   ref_formula(calcid: scala.math.BigInt, pos: Position): XML.Tree =
wneuper@4723
   110
  { XML.Elem(Markup("REFFORMULA", Nil), List(
wneuper@4723
   111
      XML.Elem(Markup("CALCID", Nil), List(XML.Text(calcid.toString()))),
wneuper@4727
   112
      xml_of_Position(pos)))
wneuper@4723
   113
  }  
wneuper@4723
   114
    
wneuper@4723
   115
  //----- step 7 -----------------------
wneuper@4723
   116
  def auto_calculate(calcid: scala.math.BigInt, auto: String): XML.Tree =
wneuper@4723
   117
  {    
wneuper@4723
   118
    XML.Elem(Markup("AUTOCALC", Nil), List(
wneuper@4723
   119
      XML.Elem(Markup("CALCID", Nil), List(XML.Text(calcid.toString()))),
wneuper@4723
   120
      XML.Elem(Markup("AUTO", Nil), List(XML.Text(auto)))))
wneuper@4723
   121
  }
wneuper@4723
   122
wneuper@4723
   123
}