Including code in Java activecode

31 views
Skip to first unread message

J David Eisenberg

unread,
Nov 8, 2023, 1:18:47 PM11/8/23
to PreTeXt support
The scenario: I have a class Stack<T> which I want to use in several activecode programs on several different pages, and I would like to include it in all of them.

On the first page where this occurs, I have code like this:

<program language="java" interactive="activecode" xml:id="stackcode">
class Stack<T> {...}
</program>

And later:

<program language="java" interactive="activecode" include ="stackcode">
public class StackTest {
   // Uses a Stack<String>
}
</program>

This all works great. I don’t want users to run the first code, but if I remove @interactive on the stack code, it stops working (StackTest.java:1: error: class, interface, enum, or record expected)

Now, I want to use the "stackcode"  on a different page:

<program language="java" interactive="activecode" include="stackcode">
public class ReverseString {
  // uses a Stack<String>
}
</program>

This does not work, because "stackcode" is not on the same page (error: class, interface, enum, or record expected)

I tried pasting the stack code into the activecode, preceded by a ====

<program language="java" interactive="activecode" include="stackcode">
public class ReverseString {
  // uses a Stack<String>
}
====
class Stack<T> {...}
</program>

But that generates a “server error” when I run the code.

What I would like is some way to do cross-page includes, or some way to insert the code on a page so that it is include-able but  not visible to the student.



J David Eisenberg

unread,
Nov 8, 2023, 4:52:18 PM11/8/23
to PreTeXt support
OK, I figured out what is going on. The @include to the non-existent (in that page) code put a string "null"  -- the string, not the value -- into the string being handed off to jobe.
This code works fine without an @include:

  <program xml:id="testing" label="testing" line-numbers="yes" language="java"
    interactive="activecode">
    <input>
class Helper {
  public static String help() {
    return "this is the helper class in action.";
  }
}
^^^^
public class Test {

    public static void main(String[] args) {
        System.out.println("test class");
        System.out.println(Helper.help());
    }
}
        </input>
  </program>

I was able to use an <xi:include> to include the Stack class code above the ^^^^, and it now works.

While looking for the problem, I saw this code in bases/rsptx/interactives/runestone/activecode/activecode.py, line 470:

    if "====" in self.content:
        idx = self.content.index("====")
        source = "\n".join(self.content[:idx])
        suffix = "\n".join(self.content[idx + 1 :])


I am curious why the suffix starts at idx + 1; wouldn’t that include some of the equal signs?

Andrew Scholer

unread,
Nov 8, 2023, 9:11:54 PM11/8/23
to pretext...@googlegroups.com
If you are authoring in PreTeXt you should probably use <tests> to contain your unit tests instead of using the magic delimeters. It is not well (at all?) documented, but you can see a sample here:

As for idx + 1, self.content is an array, not a string - you can see some of the setup for it here in the parent class:
https://github.com/RunestoneInteractive/rs/blob/cde24bd96276502d1a7c6abfe8895dbc5acd78d2/bases/rsptx/interactives/runestone/common/runestonedirective.py#L315

It sounds like you have figured out a solution that works for you?

Andrew Scholer (he/him/his)
Computer Science Instructor/Program Chair
Chemeketa Community College


--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pretext-support/81a6d159-1afe-406a-ae56-1d0c196da27dn%40googlegroups.com.

J David Eisenberg

unread,
Nov 8, 2023, 9:39:40 PM11/8/23
to PreTeXt support
These aren’t unit tests; I have a Java class that is needed in many examples, and I want to include the source code (so it compiles and runs properly) without the student having to see the code over and over again.
The delimiters do that.

Thanks for the info about idx + 1.

Rob Beezer

unread,
Nov 8, 2023, 11:42:07 PM11/8/23
to pretext...@googlegroups.com
I'm not following every detail here, but have comments.

1. PreTeXt rarely expects magic delimiters and there is zero guarantee they will be perform in the future.

2. xi:including repeated code from one file for multiple pages is a technique. You might be doing this already.

3. I think this suggests we have a hiding option for the repeated code.

4. You might direct Brad to this, he does not always see this forum and he can perhaps say much more.

Rob

On November 8, 2023 6:39:39 PM PST, J David Eisenberg <jdavid.e...@gmail.com> wrote:
>These aren’t unit tests; I have a Java class that is needed in many
>examples, and I want to include the source code (so it compiles and runs
>properly) without the student having to see the code over and over again.
>The delimiters do that.
>
>Thanks for the info about idx + 1.
>
>On Wednesday, November 8, 2023 at 6:11:54 PM UTC-8 asch...@chemeketa.edu
>wrote:
>
>> If you are authoring in PreTeXt you should probably use <tests> to contain
>> your unit tests instead of using the magic delimeters. It is not well (at
>> all?) documented, but you can see a sample here:
>>
>> https://github.com/PreTeXtBook/pretext/blob/71d6141f60de844eaf3a24595728feb2e8a34c13/examples/sample-book/rune.xml#L2100
>>
>> As for idx + 1, self.content is an array, not a string - you can see some
>> of the setup for it here in the parent class:
>>
>> https://github.com/RunestoneInteractive/rs/blob/cde24bd96276502d1a7c6abfe8895dbc5acd78d2/bases/rsptx/interactives/runestone/common/runestonedirective.py#L315
>>
>> It sounds like you have figured out a solution that works for you?
>>
>> Andrew Scholer (he/him/his)
>> Computer Science Instructor/Program Chair
>> Chemeketa Community College
>> 503.589.7649 <(503)%20589-7649>
>>> <https://groups.google.com/d/msgid/pretext-support/81a6d159-1afe-406a-ae56-1d0c196da27dn%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>
>
Reply all
Reply to author
Forward
0 new messages