Jess bind this instance of java object - jess

I want to do something like that in Jess:
(bind ?instance (this))
The only way I get it working is by using "new Object" instead of "this".
How can I get it working?

Actually there's three ways you can access a Pojo from the Jess environment, and a fourth if it is a singleton.
public class Main {
public static Main theInstance;
public static Main getInstance(){ return theInstance; }
private String user = "Joe";
public String getUser(){ return user; }
public static void main( String[] args ) throws Exception {
Main main = new Main();
Main.theInstance = main;
Rete rete = new Rete();
// as a global
Defglobal glob = new Defglobal( "*main*", new Value( main ) );
glob.reset( rete );
// as a store member
rete.store( "main", main );
// as a fact
rete.add( main );
// execute demo clp
rete.batch( "two.clp" );
}
}
And the .clp is:
(printout t "?*main* = " ?*main* crlf)
(printout t "?*main*.user = " (?*main* getUser) crlf)
(printout t "main = " (fetch main) crlf)
(printout t "main.user = " ((fetch main) getUser) crlf)
(defrule get-main-user
(Main (user ?user))
=>
(printout t "a Main(slot user) = " ?user crlf)
)
(run)
(printout t "Main.theInstance = " ((call Main getInstance) getUser) crlf)
Output:
?*main* = <Java-Object:Main>
?*main*.user = Joe
main = <Java-Object:Main>
main.user = Joe
a Main(slot user) = Joe
Main.theInstance = Joe

Presumably you are passing some Jess code to the Rete.exec() method, and want a reference to the object that's making the call. You just have to inject it into Jess yourself. Either use Rete.store() to store the object in Jess, then use (fetch) to retrieve it from storage in the Jess code, or use the methods of the global context object -- see Rete.getGlobalContext() -- to set a variable to refer to the Java object, then use the variable in the Jess code.

Related

Pass information to new web-browser window/tab being opened with BrowserWindowOpener in Vaadin 8

In Vaadin 8, we can let the user open a new tab/window within their web browser by clicking a button that has been associated with a BrowserWindowOpener. As discussed in the manual, a new UI is instantiated on our behalf. All we pass is a .class object, the class of our UI subclass to be instantiated for display in the new window/tab. For example:
BrowserWindowOpener opener = new BrowserWindowOpener( PersonDetailUI.class );
That works for me. My question is: How do I pass some information to that new UI object in that new window/tab?
For example, I might need to pass:
The ID number or UUID of a record to be looked up in a database.
A JavaBean object ready for display in a layout.
I see I asked about this same issue for Vaadin 7. Consider this an updated version of the Question for Vaadin 8. The only Answer there speculated about adding parameters to the URI of the new window. But that limits me to a small piece of text. I prefer to pass a smart object rather than a dumb string.
There are basically three approaches you can use, and combinations of these
Use URI parameters. As you mentioned, this is limited to String type data.
You can read the URI fragment in UI with e.g. String uriFragment = Page.getCurrent().getUriFragment();
You can read URI parameter using VaadinRequest.getParameter(), VaadinRequest is given as parameter in init(...) of main UI
UI's in different browser tabs share the same Vaadin session. That gives some tools, namely
You can use session attributes, i.e. VaadinSession.getCurrent().getAttribute(…) and VaadinSession.getCurrent().setAttribute(…)
If you use CDI or Spring, you can Inject / Autowire #VaadinSessionScoped bean. The instance is then bound to Session and hence shared between the tabs.
Read data from database (possibly using 1. and/or 2. as help for keys)
The Answer by Tatu Lund mentioned passing a URI parameter to the new window being opened.
Example app, passing URI parameter to new window
Here is a simple little demonstration app in Vaadin 8.5.0 to show that technique.
We start with three cats that we pretend to retrieve from a database. Each time the user selects a cat, we get the cat’s identifier, a UUID object. We generate a canonical 32-character hex string representation of that UUID. And we specify that as the parameter value to be passed with the parameter key cat_id. We specify that parameter by calling BrowserWindowOpener::setParameter.
Note that we do this on the selection of an item in the Grid listing cats. Because of browser restrictions in opening windows, the BrowserWindowOpener must be configured before the user clicks its button. We cannot run code in reaction to the button click, as far as I know.
The new browser window is populated with an automatically-instantiated subclass of UI. In this case CatUI is what we wrote. In CatUI, we get the URI parameter, extract the string representing the UUID, recreate the UUID object, and then pass that to our pretend database-service to fetch the cat in question. Then a layout’s fields are populated with the Cat object values. We don’t bother with data-binding the cat-to-layout as that is not the point of this demo.
Caveat: This is just a demo, and ignores issues that are not directly related to issue of passing information via URI parameter to a new window. For example, crucial issues of concurrency are ignored here but would not be in real work.
This demo consists of four class files, all pasted below.
MainUI (opened by default when app launches)
CatUI (opened when user clicks button)
Cat (business object, with name and id properties)
DatabaseService (pretend storehouse of cat records)
MainUI
package com.basilbourque.example;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.BrowserWindowOpener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import javax.servlet.annotation.WebServlet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
/**
* This UI is the application entry point. A UI may either represent a browser window
* (or tab) or some part of an HTML page where a Vaadin application is embedded.
* <p>
* The UI is initialized using {#link #init(VaadinRequest)}. This method is intended to be
* overridden to add component to the user interface and initialize non-component functionality.
*/
#Theme ( "mytheme" )
public class MainUI extends UI {
private Grid< Cat > grid;
private Button button;
#Override
protected void init ( VaadinRequest vaadinRequest ) {
// Configure button to open now browser window/tab.
this.button = new Button( "Open in new window" );
BrowserWindowOpener opener = new BrowserWindowOpener( CatUI.class );
opener.setFeatures( "height=300,width=500,resizable" );
opener.extend( this.button );
opener.setParameter( "cat_id" , new UUID(0,0).toString()); // Send nil UUID (all zeros) if nothing selected.
System.out.println( "BWO URL: " + opener.getUrl() );
this.button.setEnabled( false );
this.grid = new Grid<>( Cat.class );
this.grid.setCaption( "Cats" );
List<Cat> cats = new DatabaseService().fetchAllCats() ;
this.grid.setItems( cats );
// Every time the user selects a cat in the Grid, assign that cat’s ID to our `BrowserWindowOpener`. This way our button is always prepared to open a window for the selected cat.
this.grid.addSelectionListener( event -> {
Set< Cat > selectedCats = event.getAllSelectedItems();
this.button.setEnabled( selectedCats.size() > 0 );
if ( selectedCats.size() > 0 ) { // If the user selected an item.
Cat cat = selectedCats.stream().findFirst().get();
opener.setParameter( "cat_id" , cat.getId().toString() ); // A UUID’s canonical presentation is as a 36-character hexadecimal string in five groups with HYPHEN-MINUS as delimiter.
} else {
opener.setParameter( "cat_id" , new UUID(0,0).toString()); // Send nil UUID (all zeros) if nothing selected.
}
System.out.println( "BWO URL: " + opener.getUrl() );
} );
this.grid.select( cats.stream().findFirst().get() ); // Select first item arbitrarily, to provoke the grid’s selection-listener above to fire.
button.addClickListener( e -> {
System.out.println( "BASIL opening now window" );
} );
final VerticalLayout layout = new VerticalLayout();
layout.addComponents( this.grid , button );
setContent( layout );
}
#WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
#VaadinServletConfiguration ( ui = MainUI.class, productionMode = false )
public static class MyUIServlet extends VaadinServlet {
}
}
CatUI
package com.basilbourque.example;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.*;
import java.util.Optional;
import java.util.UUID;
public class CatUI extends UI {
private Cat cat = null;
#Override
protected void init ( VaadinRequest vaadinRequest ) {
// Retrieve a parameter from the URI of this UI/window.
String catUuidString = vaadinRequest.getParameter( "cat_id" ); // In the URI key-value parameters, "cat_id" is our key, and a UUID’s hex string is the expected value.
if ( null == catUuidString ) { // If we did not receive the UUID-string parameter we expected.
this.setContent( this.buildLayoutForNoCat( null ) );
return;
}
UUID uuid = UUID.fromString( catUuidString ); // Rehydrate the `UUID` from our passed hex string representing the UUID’s value.
Optional< Cat > cat = new DatabaseService().fetchCat( uuid );
if ( cat.isPresent() ) { // NULL check.
System.out.println( "uuidString: " + uuid + " and cat: " + cat.get() );
this.setContent( this.buildLayoutForCat( cat.get() ) ); // Retrieve the `Cat` object from our `Optional< Cat >` object by calling `get()` only after checking for NULL.
return;
} else { // Failed to find cat.
this.setContent( this.buildLayoutForNoCat( uuid ) );
return;
}
}
private Layout buildLayoutForCat ( Cat cat ) {
this.cat = cat ;
this.getPage().setTitle( "Cat details" );
// Have some content for it
TextField name = new TextField( "Name: " );
name.setWidth( 100 , Unit.PERCENTAGE );
name.setValue( this.cat.getName() );
TextField id = new TextField( "Id: " );
id.setWidth( 100 , Unit.PERCENTAGE );
id.setValue( this.cat.getId().toString() );
VerticalLayout layout = new VerticalLayout();
layout.addComponent( name );
layout.addComponent( id );
return layout;
}
private Layout buildLayoutForNoCat ( UUID uuid ) {
VerticalLayout layout = new VerticalLayout();
String message = "No cat found for the id: " + uuid;
Label label = new Label( message );
layout.addComponentsAndExpand( label );
return layout;
}
}
Cat
package com.basilbourque.example;
import java.util.UUID;
public class Cat {
private UUID id;
private String name;
public Cat ( UUID id , String name ) {
this.id = id;
this.name = name;
}
public UUID getId () {
return id;
}
public void setId ( UUID id ) {
this.id = id;
}
public String getName () {
return name;
}
public void setName ( String name ) {
this.name = name;
}
// Override `Object`.
#Override
public String toString () {
return "Cat{ " +
"id=" + id +
", name='" + name + '\'' +
" }";
}
}
DatabaseService
package com.basilbourque.example;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
// Pretending to be our gateway to a database.
public class DatabaseService {
static private List< Cat > cats;
{
DatabaseService.cats = List.of( // Produces an unmodifiable list. (A new feature in Java 9 and later.)
new Cat( UUID.fromString( "adf5c1a0-912e-11e8-9eb6-529269fb1459" ) , "Fluffy" ) ,
new Cat( UUID.fromString( "d37401c6-912e-11e8-9eb6-529269fb1459" ) , "Spot" ) ,
new Cat( UUID.fromString( "de29b6d8-912e-11e8-9eb6-529269fb1459" ) , "Lilly Mae" )
);
}
public List< Cat > fetchAllCats () {
return new ArrayList<>( DatabaseService.cats ); // Copy the list, then return.
}
public Optional< Cat > fetchCat ( UUID uuid ) {
return DatabaseService.cats.stream().filter( cat -> cat.getId().equals( uuid ) ).findFirst();
}
public static void main ( String[] args ) {
Optional< Cat > cat = new DatabaseService().fetchCat( UUID.fromString( "de29b6d8-912e-11e8-9eb6-529269fb1459" ) );
if ( cat.isPresent() ) {
System.out.println( "cat: " + cat.get() );
} else {
System.out.println( "No cat found." );
}
}
}

Apache isis v1.15 repositoryService allInstances return incorrect size

I'm quite new to Apache ISIS, and I want to get a list via the dataNucleus with a legacy database(MYSQL), There are 300,000 of the data, But when I'm trying to use repositoryService.allInstances() method to get a List, returns the size of list is 2. I have other domain objects and those works fine.
here is the code and debug infos.
#PersistenceCapable(
identityType = IdentityType.DATASTORE,
schema = "public",
table = "tinstruction_parameter_value"
)
#DatastoreIdentity(
strategy = IdGeneratorStrategy.IDENTITY,
column = "id")
#Queries({
#Query(
name = "find", language = "JDOQL",
value = "SELECT "
+ "FROM domainapp.modules.simple.dom.impl.xfs.parameter.InstructionParameterValueTest "),
#Query(
name = "findByValueContains", language = "JDOQL",
value = "SELECT "
+ "FROM domainapp.modules.simple.dom.impl.xfs.parameter.InstructionParameterValueTest "
+ "WHERE value.indexOf(:value) >= 0 "),
#Query(
name = "findByValue", language = "JDOQL",
value = "SELECT "
+ "FROM domainapp.modules.simple.dom.impl.xfs.parameter.InstructionParameterValueTest "
+ "WHERE value == :value ")
})
#DomainObject(
editing = Editing.DISABLED
)
#DomainObjectLayout(
bookmarking = BookmarkPolicy.AS_ROOT
)
public class InstructionParameterValueTest implements Comparable<InstructionParameterValueTest> {
#Column(allowsNull = "true",jdbcType = "CLOB")
#Property()
#MemberOrder(sequence = "10")
#Getter #Setter
private String value;
//region > compareTo, toString
#Override
public int compareTo(final InstructionParameterValueTest other) {
return org.apache.isis.applib.util.ObjectContracts.compare(this, other, "value");
}
#Override
public String toString() {
return org.apache.isis.applib.util.ObjectContracts.toString(this, "value");
}
//endregion
}
public class InstructionParameterValueTestRepository {
#Programmatic
public java.util.List<InstructionParameterValueTest> listAll() {
return repositoryService.allInstances(InstructionParameterValueTest.class);
}
}
dataNucleus debug log
I donot know why the size of the list is 2, not all datas, the debug sql can execute and get all datas.
dataNucleus sql execute
can anyone tell me what I should do,

Java Function chaining

I'm trying to work out why the following code fails to compile:
Function<Employee, String> getLastName = (Employee employee) -> {
return employee.getName().substring(employee.getName().indexOf(" ") + 1);
};
Function<Employee, String> getFirstName = (Employee employee) -> {
return employee.getName().substring(0, employee.getName().indexOf(" "));
};
Function chained = getFirstName.apply(employees.get(2).andThen(getFirstName.apply(employees.get(2))));
Can't all Functions be cahined in java 8?
Exactly, andThen is applied to the result from that Function, for example:
Function<Employee, String> chained = getFirstName.andThen(x -> x.toUpperCase());
x -> x.toUpperCase() (or this can be replaced with a method reference String::toUpperCase) is applied to the String result from getFirstName Function.
How do you imagine chaining them? One Function returns String, so that makes it impossible to chain. But you can return both those fields via a single Function:
Function<Employee, String[]> bothFunction = (Employee employee) -> {
String[] both = new String[2];
both[0] = employee.getName().substring(employee.getName().indexOf(" ") + 1);
both[1] = employee.getName().substring(0, employee.getName().indexOf(" "));
return both;
};

for_each bind2nd mem_fun vs 2005

Having problems with STL under VS 2005. I am restricted to VS 2005.
class SpeedTest
{
public:
void AddDataPair(const std::pair<std::string, double> & datum, const std::string & insertName = "");
private:
std::map <std::string, double> _Data;
}
void SpeedTest::AddDataPair(const pair<string, double> & datum, const string & insertName)
{
string key = insertName + '_' + datum.first;
_Data[key] += datum.second;
}
void SpeedTest::Insert(SpeedTest * toCopy, const string & insertName)
{
map<string, double>::iterator dataIter;
map<string, double>::iterator beginIter = toCopy->_Data.begin();
map<string, double>::iterator endIter = toCopy->_Data.end();
for_each
(
beginIter,
endIter,
bind2nd(mem_fun(&INGSpeedTest::AddDataPair)(), insertName)
);
}
I get error C2784:
'std::const_mem_fun1_t<_Result,_Ty,_Arg> std::mem_fun(Result (_thiscall _Ty::* )(_Arg) const)' :
could not deduce template argument for 'Result (_thiscall _Ty::* )(_Arg) const'
from 'void (__thiscall INGSpeedTest::* )(const std::pair<_Ty1,_Ty2> &,const std::string &)'
with
[
_Ty1=std::string,
_Ty2=double
]
If I replace the for_each with a for loop
for (dataIter = beginIter;
dataIter != endIter;
++dataIter)
{
AddDataPair(*dataIter, insertName);
}
It compiles nicely.
But I would still like to know what is failing with the for_each.
Thank you very much for any time, knowledge and effort you can spare me,
Robert
std::mem_fun can only wrap a member function taking zero or one parameter. Yours takes two.

Accessing property values in F# constructors

Sorry if this is basic, but I'm still wrapping my head around F#.
I want to do this:
type Person(user:User) as this =
member val Name = "" with get, set
user.UserId <- this.Name
But that gives me the error Unexpected identifier in member definition.
This compiles:
type Person(user:User) as this =
do
user.UserId <- this.Name
member val Name = "" with get, set
But gives me an InvalidOperationException at runtime.
I've settled for this:
type Person(user:User) =
member val Name = "" with get, set
member this.Setup() =
user.UserId <- this.Name
But I don't like having to call person.Setup() each time.
What's the best F-Sharpy way to do this?
You can do this with an explicit constructor to force the code for initializing the member functions to be run before your other code as follows
type User() =
member val UserId = "" with get,set
type Person private () =
member val Name = "" with get, set
new (user:User) as this =
Person()
then user.UserId <- this.Name
This is due to the fact that the initializer code is run in the constructor, so you must force it to run to completion before acccessing the properties