isac-java/src/java-tests/isac/bridge/TestPIDE.java
author Walther Neuper <walther.neuper@jku.at>
Fri, 26 Mar 2021 10:45:05 +0100
changeset 5239 b4e3883d7b66
parent 5229 6bf0e95981e3
permissions -rw-r--r--
reset mathematics-engine to Isabelle2015

note: for this version libisabelle was available,
which connects front-end (Java) and back-end (Isabelle/ML)
     1 package isac.bridge;
     2 
     3 import isac.bridge.xml.TestsDATA; // DataTypes.scala
     4 import isac.bridge.IsacOperations;
     5 import isac.bridge.xml.IntCEvent;
     6 import isac.bridge.xml.IntIntCompound;
     7 import isac.bridge.xml.IntFormHead;
     8 import isac.bridge.xml.IntFormulas;
     9 import isac.bridge.xml.IntPosition;
    10 import isac.bridge.xml.DataTypes;  //.scala
    11 import isac.bridge.xml.JavaToIsa;  //.scala
    12 import isac.bridge.xml.IsaToJava;  //.scala
    13 import isac.util.formulae.CalcFormula;
    14 import isac.util.formulae.CalcHead;
    15 import isac.util.formulae.Formula;
    16 import isac.util.formulae.Model;
    17 import isac.util.formulae.ModelItem;
    18 import isac.util.formulae.Position;
    19 import isac.util.formulae.Specification;
    20 import isac.util.Formalization;
    21 
    22 import edu.tum.cs.isabelle.api.*;
    23 import edu.tum.cs.isabelle.japi.*;
    24 //import edu.tum.cs.isabelle.*;    // OVERWRITES System.out.println
    25 import edu.tum.cs.isabelle.pure.*; // DEFINES type Term
    26 import edu.tum.cs.isabelle.setup.Setup;
    27 
    28 import java.io.FileNotFoundException;
    29 import java.io.IOException;
    30 import java.io.InputStream;
    31 import java.nio.file.Paths;
    32 import java.math.BigInteger;
    33 import java.util.Properties;
    34 import java.util.Vector;
    35 
    36 import junit.framework.TestCase;
    37 
    38 public class TestPIDE extends TestCase {
    39 	String isabelle_home_;
    40 
    41 	public void setUp() {	          
    42 	  InputStream inputStream = null;
    43 	  try {
    44 	    Properties prop = new Properties();
    45 	    String propFileName = "./properties/BridgeMain.properties";
    46 	    inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
    47 	    if (inputStream != null) {
    48 	      prop.load(inputStream);
    49 	    } else {
    50 	      throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
    51 	    }
    52 	    // get the property value and print it out
    53 	    isabelle_home_ = prop.getProperty("ISABELLE_HOME");
    54         } catch (Exception e) {
    55 	      System.out.println("Exception: " + e);
    56 	    } finally {
    57 	      try { inputStream.close();
    58 			} catch (IOException e) { e.printStackTrace(); }
    59 	    }
    60       }
    61 
    62 	/* 
    63 	 * ISABELLE_HOME is set by "isabelle.Isabelle_System.init",
    64 	 * the connection to Isabelle/Isac by "JSystem.instance". 
    65 	 */
    66     public void testConnection() {
    67         System.out.println("/--BEGIN isac.bridge.TestPIDE#testConnection");
    68 
    69 		System.out.println("---1 isac.bridge.TestPIDE#testConnection");        
    70 		Setup setup = new Setup(Paths.get(isabelle_home_), JPlatform.guess(),
    71           new Version("2015"), Setup.defaultPackageName());
    72         Environment env = JSetup.makeEnvironment(setup); // ohne Duration
    73 
    74         System.out.println("---2 isac.bridge.TestPIDE#testConnection");        
    75 /*Error*/Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
    76 	    JSystem sys = JSystem.create(env, config);
    77 
    78 		System.out.println("---3 isac.bridge.TestPIDE#testConnection");        
    79 		String hello_out = sys.invoke(Operations.HELLO, "should-be-returned");
    80 
    81 		System.out.println("---4 isac.bridge.TestPIDE#testConnection");        
    82 		assertEquals(hello_out,"Hello should-be-returned");
    83 
    84 		System.out.println("---5 isac.bridge.TestPIDE#testConnection");        
    85 	    sys.dispose();
    86 
    87 	    System.out.println("\\--END isac.bridge.TestPIDE#testConnection");        
    88 	}
    89 
    90     /*
    91      * transfer the minimal test from libisabelle to isac-java:
    92      * https://github.com/wneuper/libisabelle/blob/master/examples/src/main/java/Mini_Test.java
    93      * which has been derived from:
    94      * https://github.com/wneuper/libisabelle/blob/master/doc/test--isac-Java--isac-kernel.txt
    95      */
    96 	public void testMini_Test() {
    97 		System.out.println("/--BEGIN isac.bridge.TestPIDE#testMini_Test");
    98 
    99 		Setup setup = new Setup(Paths.get(isabelle_home_), JPlatform.guess(),
   100           new Version("2015"), Setup.defaultPackageName());
   101         Environment env = JSetup.makeEnvironment(setup); // ohne Duration
   102 	    Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
   103 	    JSystem sys = JSystem.create(env, config);
   104 
   105 	    //----- step 1 ----------------------------------------------------------------
   106 	    //build Formalization in Java and then convert to XML
   107 	    Formalization fmz = isac.TESTUTIL.Formalization.solve_x_plus_1_equ_2();
   108 	    XML.Tree CALC_TREE_out = sys.invoke(IsacOperations.CALC_TREE,
   109 	      DataTypes.xml_of_Formalization(fmz));
   110 	    int calcid = (IsaToJava.calc_tree_out(CALC_TREE_out)).intValue();
   111 		assertEquals(calcid, 1);
   112 	    System.out.println("# 1  # " + CALC_TREE_out);
   113 	    
   114 	    
   115 	    //----- step 2 ----------------------------------------------------------------
   116 	    XML.Tree ITERATOR_out = sys.invoke(IsacOperations.ITERATOR,
   117 	      new scala.math.BigInt(BigInteger.valueOf(calcid)));
   118 	    IntIntCompound int_int = IsaToJava.iterator_out(ITERATOR_out);
   119 	    calcid = int_int.getCalcId().intValue();
   120 	    int userid = int_int.getUserId().intValue();
   121 		assertEquals(calcid, 1);
   122 		assertEquals(userid, 1);
   123 	    System.out.println("# 2  # " + ITERATOR_out);
   124 
   125 	    //----- step 3 ----------------------------------------------------------------
   126 	    XML.Tree MOVE_ACTIVE_ROOT_out = sys.invoke(IsacOperations.MOVE_ACTIVE_ROOT,
   127 	      new scala.math.BigInt(BigInteger.valueOf(calcid)));
   128 	    IntPosition calcid_pos = IsaToJava.move_active_root_out(MOVE_ACTIVE_ROOT_out);
   129 	    calcid = calcid_pos.getCalcId();
   130 	    Position pos = calcid_pos.getPosition();
   131 		assertEquals(pos.toSMLString(), "([],Pbl)");
   132 	    System.out.println("# 3  # " + MOVE_ACTIVE_ROOT_out);
   133 
   134         //----- step 4 ----------------------------------------------------------------
   135         Position from = new Position();
   136         from.setKind("Pbl");
   137         Position to = new Position();
   138         to.setKind("Pbl");
   139         int level = 0;
   140         boolean rules = false;
   141         XML.Tree GET_FORMULAE_out = sys.invoke(IsacOperations.GET_FORMULAE,
   142           JavaToIsa.get_formulae(new scala.math.BigInt(BigInteger.valueOf(calcid)),
   143           from, to, new scala.math.BigInt(BigInteger.valueOf(level)), rules));
   144         IntFormulas calcid_forms = IsaToJava.get_formulae_out(GET_FORMULAE_out);
   145 	    calcid = calcid_forms.getCalcId();
   146         Vector<CalcFormula> forms = calcid_forms.getCalcFormulas();
   147 		assertEquals(forms.size(), 1);
   148 		assertEquals(((CalcFormula)forms.elementAt(0)).getFormula().toString(), "solve (x + 1 = 2, x)");
   149         System.out.println("# 4  # " + GET_FORMULAE_out);
   150 
   151 	    //----- step 6 ----------------------------------------------------------------
   152         pos = new Position();
   153         pos.setKind("Pbl");
   154 	    XML.Tree REF_FORMULA_out = sys.invoke(IsacOperations.REF_FORMULA,
   155 	      JavaToIsa.ref_formula(new scala.math.BigInt(BigInteger.valueOf(calcid)), pos));
   156 	    System.out.println("# 6 before IsaToJava.ref_formula_out");
   157         IntFormHead calcid_formhead = IsaToJava.ref_formula_out(REF_FORMULA_out);
   158         calcid = calcid_formhead.getCalcId();
   159         assertEquals(calcid, 1);
   160 		
   161         CalcHead ch = (CalcHead)(calcid_formhead.getFormHead());
   162         assertEquals(ch.getPosition().toSMLString(), "([],Pbl)");
   163         assertEquals(ch.getHeadLine().toSMLString(), "solve (x + 1 = 2, x)");
   164 
   165         Model model = ch.getModel();
   166         assertEquals(model.getGiven().getItems().size(), 0);
   167         assertEquals(model.getWhere().getItems().size(), 1);
   168         assertEquals(((ModelItem)model.getWhere().getItems().elementAt(0)).getItemStatus(), "false");
   169         assertEquals(((ModelItem)model.getWhere().getItems().elementAt(0)).toSMLString(), "precond_rootpbl v_v");
   170 
   171         assertEquals(ch.getBelongsTo(), "Pbl");
   172 
   173         Specification spec = ch.getSpecification();
   174         assertEquals(spec.getTheory().toString(), "e_domID\n");
   175         assertEquals(spec.getProblem().toSMLString(), "[\"e_pblID\"]");
   176         assertEquals(spec.getMethod().toSMLString(), "[\"e_metID\"]");
   177 	    
   178 	    System.out.println("# 6  # " + REF_FORMULA_out);
   179 
   180 	    //----- step 7 ----------------------------------------------------------------
   181 	    String scope = "CompleteCalc";
   182 	    XML.Tree AUTO_CALC_out = sys.invoke(IsacOperations.AUTO_CALC,
   183 	      JavaToIsa.auto_calculate(new scala.math.BigInt(BigInteger.valueOf(calcid)), scope, 
   184 	    	new scala.math.BigInt(BigInteger.valueOf(0))));
   185 	    IntCEvent calcid_cc = IsaToJava.auto_calc_out(AUTO_CALC_out);
   186 	    calcid = calcid_cc.getCalcId();
   187 	    CChanged cc = (CChanged)calcid_cc.getCEvent();
   188 		assertEquals(cc.getLastUnchanged().toSMLString(), "([],Pbl)");
   189 		assertEquals(cc.getLastDeleted().toSMLString(), "([],Pbl)");
   190 		assertEquals(cc.getLastGenerated().toSMLString(), "([],Res)");
   191 	    System.out.println("# 7  # " + AUTO_CALC_out);
   192 
   193 	    //----- step 10 ---------------------------------------------------------------
   194         pos = new Position();
   195         pos.setKind("Res");
   196 	    REF_FORMULA_out = sys.invoke(IsacOperations.REF_FORMULA,
   197 	      JavaToIsa.ref_formula(new scala.math.BigInt(BigInteger.valueOf(calcid)), pos));
   198 	    
   199 	    calcid_formhead = IsaToJava.ref_formula_out(REF_FORMULA_out);
   200 	    calcid = calcid_formhead.getCalcId().intValue();
   201 	    // later here we shall check for CalcFormula OR CalcHead
   202 	    CalcFormula form = (CalcFormula)calcid_formhead.getFormHead();
   203 	    form.getFormula();
   204 	    assertEquals(form.getPosition().toSMLString(), "([],Res)");
   205 	    assertEquals(form.toString(), "[x = 1]");
   206 	    System.out.println("# 10 # " + REF_FORMULA_out);
   207 
   208 	    //----- step 13 ---------------------------------------------------------------
   209 	    XML.Tree DEL_CALC_out = sys.invoke(IsacOperations.DEL_CALC,
   210 	      new scala.math.BigInt(BigInteger.valueOf(calcid)));
   211 	    calcid = IsaToJava.del_calc_out(DEL_CALC_out).intValue();
   212 		assertEquals(calcid, 1);
   213 	    System.out.println("# 13 # " + DEL_CALC_out);
   214 					    
   215 	    sys.dispose();
   216 
   217 		System.out.println("\\--END isac.bridge.TestPIDE##testMini_Test");
   218 	}
   219 	
   220     /*
   221      * transfer a term to Isabelle/Isac, let the term process there
   222      * and transfer the processed term back to here.
   223      * 
   224      * Note: this test probably goes beyond what is required by Isac:
   225      * 
   226      * Terms isac-java --> Isabelle/Isac are mostly input by user, 
   227      * thus may contain different kinds of errors;
   228      * their format is still to be determined (String ?).
   229      * 
   230      * Terms isac-java <-- Isabelle/Isac the other way round pack two formats,
   231      * edu.tum.cs.isabelle.pure.Term and String.
   232      */
   233 	public void testTermTransfer() {
   234 		System.out.println("/--BEGIN isac.bridge.TestPIDE#testTermTransfer");
   235 
   236 		Setup setup = new Setup(Paths.get(isabelle_home_), JPlatform.guess(),
   237            new Version("2015"), Setup.defaultPackageName());
   238         Environment env = JSetup.makeEnvironment(setup); // ohne Duration
   239 	    Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
   240 	    JSystem sys = JSystem.create(env, config);
   241 
   242 	    //----- create test_term ------------------------------------------------------
   243 	    Term test_term = TestsDATA.test_term();
   244 	    XML.Tree wrapped_term = DataTypes.xml_of_Term(test_term);
   245 	    
   246 	    //----- transfer test_term to Isabelle/Isac and back again --------------------
   247 	    /*XML.Tree processed_test_term =*/ sys.invoke(IsacOperations.TEST_TERM_TRANSFER, wrapped_term);
   248 
   249 	    //----- check result ----------------------------------------------------------
   250 	    // TODO
   251 	    
   252 	    sys.dispose();
   253 	    System.out.println("\\--END isac.bridge.TestPIDE##testTermTransfer");
   254 	}
   255 	
   256     /*
   257      * transfer a String to Isabelle/Isac, let it parse there
   258      * and transfer the processed term back to here.
   259      * 
   260      * Terms isac-java --> Isabelle/Isac are mostly input by user, 
   261      * their format is String most likely during the next phase of devel.
   262      * 
   263      * Terms isac-java <-- Isabelle/Isac the other way round pack two formats,
   264      * edu.tum.cs.isabelle.pure.Term and String.
   265      */
   266 	public void testTermOneWay() {
   267 		System.out.println("/--BEGIN isac.bridge.TestPIDE#testTermOneWay");
   268 
   269 		Setup setup = new Setup(Paths.get(isabelle_home_), JPlatform.guess(),
   270            new Version("2015"), Setup.defaultPackageName());
   271         Environment env = JSetup.makeEnvironment(setup); // ohne Duration
   272 	    Configuration config = Configuration.fromBuiltin("libisabelle_Isac");
   273 	    JSystem sys = JSystem.create(env, config);
   274 
   275 	    //----- create test_term ------------------------------------------------------
   276 	    String term_str = "aaa + bbb::real";
   277 	    
   278 	    //----- transfer test_term to Isabelle/Isac and back again --------------------
   279 	    XML.Tree processed_test_term = sys.invoke(IsacOperations.TEST_TERM_ONE_WAY, term_str);
   280 	    Formula form = DataTypes.xml_to_Formula_NEW(processed_test_term);
   281 
   282 	    //----- check result ----------------------------------------------------------
   283 	    assertEquals(form.toString(), "aaa + bbb = ??.processed_by_Isabelle_Isac");
   284 	    //TODO Util#string_of(Term)
   285 	    //assertEquals(Util.string_of(form.getTerm()), "aaa + bbb = ??.processed_by_Isabelle_Isac");
   286 	    
   287 	    sys.dispose();
   288 	    System.out.println("\\--END isac.bridge.TestPIDE##testTermOneWay");
   289 	}
   290 	
   291 }