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