Skip to content

Commit

Permalink
Merge pull request #12 from koosvary/feature/definition
Browse files Browse the repository at this point in the history
Feature/definition
  • Loading branch information
lachiesainsbury authored Aug 16, 2018
2 parents e733027 + e3b58e8 commit 6aeb763
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 21 deletions.
3 changes: 3 additions & 0 deletions client/src/main/java/org/iconic/config/InMemoryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.inject.AbstractModule;
import org.iconic.project.ProjectService;
import org.iconic.project.TransientProjectService;
import org.iconic.project.definition.DefineSearchController;
import org.iconic.project.definition.DefineSearchService;
import org.iconic.project.search.SearchService;
import org.iconic.project.search.TransientSearchService;
import org.iconic.workspace.DefaultWorkspaceService;
Expand All @@ -24,5 +26,6 @@ protected void configure() {
bind(ProjectService.class).to(TransientProjectService.class);
bind(SearchService.class).to(TransientSearchService.class);
bind(WorkspaceService.class).to(DefaultWorkspaceService.class);
bind(DefineSearchService.class).to(DefineSearchController.class);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.iconic.project.definition;

import com.google.inject.Inject;
import javafx.beans.InvalidationListener;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import lombok.NonNull;
import lombok.extern.log4j.Log4j2;

import java.net.URL;
import java.util.List;
import java.util.HashMap;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import org.iconic.ea.data.DataManager;
import org.iconic.project.Displayable;
import org.iconic.project.ProjectService;
import org.iconic.project.dataset.DatasetModel;
import org.iconic.workspace.WorkspaceService;

@Log4j2
public class DefineSearchController implements Initializable, DefineSearchService {

private final ProjectService projectService;
private final WorkspaceService workspaceService;

private HashMap<String, String> functionDefinitions;

@FXML
private TextField tfTargetExpression;

@Inject
public DefineSearchController(final ProjectService projectService, final WorkspaceService workspaceService)
{
this.projectService = projectService;
this.workspaceService = workspaceService;
this.functionDefinitions = new HashMap<String, String>();

InvalidationListener selectionChangedListener = observable -> loadFunction();
workspaceService.activeWorkspaceItemProperty().addListener(selectionChangedListener);
}

@Override
public void initialize(URL location, ResourceBundle resources) {}

@Override
public String getFunction()
{
String functionStr = null;

Optional<DataManager<Double>> dataset = getDataManager();

if(dataset.isPresent()) {
// Get the ID of the dataset
String[] splitString = dataset.toString().split("@");
String datasetID = splitString[splitString.length - 1].replace("]", ""); // There's a trailing ']' from the toString

// Get the dataset, if exists
functionStr = functionDefinitions.get(datasetID);
}

return functionStr;
}

private void loadFunction()
{
Optional<DataManager<Double>> dataset = getDataManager();

if(dataset.isPresent())
{
// Get the ID of the dataset
String[] splitString = dataset.toString().split("@");
String datasetID = splitString[splitString.length - 1].replace("]", ""); // There's a trailing ']' from the toString

// No need to redefine the function if one already exists, just insert instead
String functionStr = functionDefinitions.get(datasetID);
if(functionStr == null)
{
List<String> headers = dataset.get().getSampleHeaders();

if(!headers.isEmpty())
{
functionStr = generateDefaultFunction(headers);

// Save the function defined in the hashmap of all the functions definitions
functionDefinitions.put(datasetID, functionStr);
}
else
{
log.error("No headers found in this dataset");
}
}

// NOTE(Meyer): Must check if not null otherwise injection will cause an NPE (it's dumb, I know)
if(tfTargetExpression != null)
{
tfTargetExpression.setText(functionStr);
}
}
}

private String generateDefaultFunction(List<String> headers)
{
// Get the last value in arraylist to get the target variable
String functionResultStr = "(" + headers.get(headers.size() - 1) + ")";
// Get the first value in list to start the function going
String functionDefinitionStr = "(" + headers.get(0) + ")";

// Get all the values bar the first and last column, which we already have
for(int i = 1; i < headers.size() - 1; i++)
{
functionDefinitionStr += ", (" + headers.get(i) + ")";
}

return functionResultStr + " = f(" + functionDefinitionStr + ")";
}

private Optional<DataManager<Double>> getDataManager() {
Displayable item = workspaceService.getActiveWorkspaceItem();

if (item instanceof DatasetModel) {
DatasetModel dataset = (DatasetModel) item;
return Optional.of(dataset.getDataManager());
} else {
return Optional.empty();
}
}
}
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.iconic.project.definition;

public interface DefineSearchService {
/**
* <p>
* Returns a list of projects owned by this service.
* </p>
*
* @return The function defined by the user
*/
String getFunction();
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ public SearchModel(@NonNull final DatasetModel datasetModel) {

// Add in the objectives the algorithm should aim for
ea.addObjective(
new DefaultObjective<>(
new MeanSquaredError(), datasetModel.getDataManager())
new DefaultObjective<>(
new MeanSquaredError(), datasetModel.getDataManager()
)
);
}

Expand Down
14 changes: 11 additions & 3 deletions client/src/main/java/org/iconic/workspace/WorkspaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.iconic.ea.data.DataManager;
import org.iconic.project.Displayable;
import org.iconic.project.dataset.DatasetModel;
import org.iconic.project.definition.DefineSearchService;
import org.iconic.project.search.SearchModel;
import org.iconic.project.search.SearchService;
import org.iconic.ea.data.preprocessing.Normalise;
Expand All @@ -42,8 +43,9 @@
*/
@Log4j2
public class WorkspaceController implements Initializable {
private final WorkspaceService workspaceService;
private final SearchService searchService;
private final WorkspaceService workspaceService;
private final DefineSearchService defineSearchService;

@FXML
private Button btnSearch;
Expand Down Expand Up @@ -79,6 +81,8 @@ public class WorkspaceController implements Initializable {
private TextField tfNormaliseMin;
@FXML
private TextField tfNormaliseMax;
@FXML
private TextField tfTargetExpression;


@Getter(AccessLevel.PRIVATE)
Expand All @@ -94,9 +98,10 @@ public class WorkspaceController implements Initializable {
* </p>
*/
@Inject
public WorkspaceController(final WorkspaceService workspaceService, final SearchService searchService) {
this.workspaceService = workspaceService;
public WorkspaceController(final WorkspaceService workspaceService, final SearchService searchService, final DefineSearchService defineSearchService) {
this.defineSearchService = defineSearchService;
this.searchService = searchService;
this.workspaceService = workspaceService;
this.defaultName = "";
this.defaultWelcomeMessage = "Select a dataset on the left to get started.";

Expand Down Expand Up @@ -147,6 +152,9 @@ public void startSearch(ActionEvent actionEvent) {

// Check that there's an active dataset before starting the search
if (item instanceof DatasetModel) {
// TODO(Meyer): Use the function defined to determine what data is used, and what to calculate to
log.info("Function for use: " + defineSearchService.getFunction());

DatasetModel dataset = (DatasetModel) item;
SearchModel search = getSearchService().searchesProperty().get(dataset.getId());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<?import org.iconic.project.BlockDisplay?>

<Tab text=" Define Search" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="org.iconic.workspace.WorkspaceController">
<Tab text=" Define Search" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="org.iconic.project.definition.DefineSearchController">
<graphic>
<ImageView fitWidth="24" fitHeight="24">
<image>
Expand All @@ -31,7 +31,7 @@
</font>
</Label>

<TextField text="y = f(x)"/>
<TextField fx:id="tfTargetExpression" text="y = f(x)"/>
</HBox>

<HBox>
Expand Down

0 comments on commit 6aeb763

Please sign in to comment.