org.jgrapht.graph.ClassBasedEdgeFactory Java Examples

The following examples show how to use org.jgrapht.graph.ClassBasedEdgeFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RefreshOperationTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute() throws Exception {
    // GIVEN
    final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 1l).put("value", "A").build());
    final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 2l).build());
    final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
            ImmutableMap.<String, Object>builder().put("id", 3l).put("value", "C").build());

    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    graph.addVertex(n1);
    graph.addVertex(n2);
    graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);

    // WHEN
    operation.execute(connection, graph);

    // THEN
    final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
    verify(operation, times(3)).executeQuery(eq(connection), queryCaptor.capture());
    final List<String> queries = queryCaptor.getAllValues();
    assertThat(queries.get(0), containsString("MERGE (n1:A {id:1}) SET n1.value=\"A\""));
    assertThat(queries.get(1), containsString("MERGE (n2:A {id:2})"));
    assertThat(queries.get(2), containsString("MATCH (n1:A {id:1}),(n2:A {id:2}) MERGE (n1)-[e1:E]->(n2) SET e1.id=3,e1.value=\"C\""));
}
 
Example #2
Source File: UpdateOperationTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute() throws Exception {
    // GIVEN
    final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 1l).put("value", "A").build());
    final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 2l).build());
    final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
            ImmutableMap.<String, Object>builder().put("id", 3l).put("value", "C").build());

    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    graph.addVertex(n1);
    graph.addVertex(n2);
    graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);

    // WHEN
    operation.execute(connection, graph);

    // THEN
    final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
    verify(operation, times(2)).executeQuery(eq(connection), queryCaptor.capture());
    final List<String> queries = queryCaptor.getAllValues();
    assertThat(queries.get(0), containsString("MATCH (n1:A {id:1}) SET n1.value=\"A\""));
    assertThat(queries.get(1), containsString("MATCH (n1:A {id:1}),(n2:A {id:2}) MERGE (n1)-[e1:E]->(n2) SET e1.id=3,e1.value=\"C\""));
}
 
Example #3
Source File: DeleteAllOperationTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute() throws Exception {
    // GIVEN
    final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 1l).build());
    final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 2l).build());
    final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"), Collections.emptyMap());

    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    graph.addVertex(n1);
    graph.addVertex(n2);
    graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);

    // WHEN
    operation.execute(connection, graph);

    // THEN
    final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
    verify(operation, times(2)).executeQuery(eq(connection), queryCaptor.capture());
    final String query = queryCaptor.getValue();
    assertThat(query, containsString("MATCH (n:A) DETACH DELETE n"));
}
 
Example #4
Source File: InsertOperationTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute() throws Exception {
    // GIVEN
    final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 1l).build());
    final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 2l).build());
    final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
            ImmutableMap.<String, Object>builder().put("id", 3l).build());

    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    graph.addVertex(n1);
    graph.addVertex(n2);
    graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);

    // WHEN
    operation.execute(connection, graph);

    // THEN
    final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
    verify(operation).executeQuery(eq(connection), queryCaptor.capture());
    final String query = queryCaptor.getValue();
    assertThat(query, containsString("CREATE (n1:A {id:1}),(n2:A {id:2}),(n1)-[e1:E {id:3}]->(n2)"));
}
 
Example #5
Source File: CompositeOperationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testOperationExecution() throws SQLException {
    // GIVEN
    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    final CompositeOperation operation = new CompositeOperation(operation1, operation2, operation3);

    // WHEN
    operation.execute(connection, graph);

    // THEN
    final InOrder order = inOrder(operation1, operation2, operation3);
    order.verify(operation1).execute(eq(connection), eq(graph));
    order.verify(operation2).execute(eq(connection), eq(graph));
    order.verify(operation3).execute(eq(connection), eq(graph));
}
 
Example #6
Source File: DeleteOperationTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecute() throws Exception {
    // GIVEN
    final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 1l).build());
    final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
            ImmutableMap.<String, Object>builder().put("id", 2l).build());
    final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
            ImmutableMap.<String, Object>builder().put("id", 3l).build());

    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    graph.addVertex(n1);
    graph.addVertex(n2);
    graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);

    // WHEN
    operation.execute(connection, graph);

    // THEN
    final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
    verify(operation, times(2)).executeQuery(eq(connection), queryCaptor.capture());
    final List<String> queries = queryCaptor.getAllValues();
    final String query1 = queries.get(0);
    final String query2 = queries.get(1);
    assertThat(query1, containsString("MATCH (n1:A {id:1})-[e1:E {id:3}]->(`n2:A {id:2}`) DELETE e1"));
    assertThat(query2, containsString("MATCH (n1:A {id:1}),(n2:A {id:2}) DELETE n1,n2"));
}
 
Example #7
Source File: DatabaseReader.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
public Graph<Node, Edge> readGraph(final Connection connection) throws SQLException {
    final List<Node> nodes = new ArrayList<>();
    final List<Edge> edges = new ArrayList<>();

    readGraphElements(connection, edges, nodes);

    final DefaultDirectedGraph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    nodes.forEach(graph::addVertex);
    edges.forEach(e -> graph.addEdge(e.getSourceNode(), e.getTargetNode(), e));

    return graph;
}
 
Example #8
Source File: DataSetLoaderProvider.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Override
public DataSetLoader<Graph<Node, Edge>> xmlLoader() {
    return (final File path) -> {
        try {
            final DefaultDirectedGraph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
            final GraphMLReader<Node, Edge> importer = new GraphMLReader<>(graphElementFactory, graphElementFactory);
            importer.importGraph(graph, path);
            return graph;
        } catch (final ImportException e) {
            throw new IOException(e);
        }
    };

}
 
Example #9
Source File: Neo4JDbFeatureExecutor.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Graph<Node, Edge> mergeGraphs(final List<Graph<Node, Edge>> graphs) {
    final Graph<Node, Edge> mergedGraph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));

    for (final Graph<Node, Edge> graph : graphs) {
        Graphs.addGraph(mergedGraph, graph);
    }

    return mergedGraph;
}
 
Example #10
Source File: CleanupStrategyProvider.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private Graph<Node, Edge> computeGraphToBeDeleted(final Graph<Node, Edge> graph, final String... nodeTypesToRetain) {
    final DirectedGraph<Node, Edge> toDelete = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));

    // copy graph to a destination, which we are going to modify
    Graphs.addGraph(toDelete, graph);

    // remove the nodes, we have to retain from the graph
    Graphs.removeVerticesAndPreserveConnectivity(toDelete, v -> shouldRetainNode(v, nodeTypesToRetain));

    return toDelete;
}
 
Example #11
Source File: CleanupStrategyProviderIT.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private static List<Graph<Node, Edge>> loadDataSet(final String path) {
    try {
        final URL resource = Thread.currentThread().getContextClassLoader().getResource(path);
        final File file = new File(resource.toURI());

        final DefaultDirectedGraph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
        final GraphMLReader<Node, Edge> importer = new GraphMLReader<>(factory, factory);
        importer.importGraph(graph, file);

        return Arrays.asList(graph);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #12
Source File: Neo4JDbFeatureExecutorTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testCleanupFeatureExecution() throws DbFeatureException {
    // GIVEN
    when(cleanupStrategy.provide(any(CleanupStrategy.StrategyProvider.class))).thenReturn(cleanupStrategyExecutor);
    final List<Graph<Node, Edge>> initialDataSets = Arrays.asList(new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class)));

    // WHEN
    final DbFeature<Connection> feature = featureExecutor.createCleanupFeature(cleanupStrategy, initialDataSets);
    assertThat(feature, notNullValue());
    feature.execute(connection);

    // THEN
    verify(cleanupStrategyExecutor).execute(eq(connection), eq(initialDataSets));
}
 
Example #13
Source File: AutomationEngine.java    From JTAF-XCore with Apache License 2.0 4 votes vote down vote up
private AutomationEngine() {
	try {
		InputStream fi = getFrameworkFile();
		GenericApplicationContext ctx = new GenericApplicationContext();

		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
		xmlReader
				.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);

		xmlReader.loadBeanDefinitions(new InputSource(fi));

		ctx.refresh();

		this.pluginManager = (PluginManager) ctx.getBean("PluginManager");

		digraph = new TestDigraph(
				new ClassBasedEdgeFactory<DiNode, DiEdge>(DiEdge.class));
		commandlibParser = new CommandLibraryParser();
		scriptParser = new ScriptParser();
		scriptParser.setDigraph(digraph);
		commandlibParser
				.setAutomationClassLoader(new DefaultAutomationClassLoader());
		testStrategyParser = new TestStrategyParser();
		testStrategyParser.setDigraph(digraph);
		initPostParseStrategyElementPlugins();
		testRoot = null;

		this.interpreter = (Interpreter) ctx.getBean("Interpreter");

		this.interpreter.setCommandRunnerPlugins(pluginManager
				.getCommandRunnerPlugins());
		this.interpreter.setTestRunnerPlugins(pluginManager
				.getTestRunnerPlugins());
		this.interpreter.setTearDownPlugins(pluginManager.getTearDownPlugins());

		initPostParseAllPlugins();
		initPostParseSuitePlugins();
		initPostParseTestPlugins();

	} catch (Exception e) {
		// If something goes wrong here, we have a serious issue
		logger.fatal(e);
		throw new RuntimeException(e);
	}
}
 
Example #14
Source File: AssociationAnalyzer.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public AssociationAnalyzer(Collection<Association> associations) {
	this.associations = associations;
	this.datasetToAssociationToColumnMap = new HashMap<String, Map<String, String>>();
	this.graph = new Pseudograph<String, LabeledEdge<String>>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
}
 
Example #15
Source File: GraphComparatorTest.java    From jpa-unit with Apache License 2.0 4 votes vote down vote up
private Graph<Node, Edge> createGraph(final List<Node> nodes, final List<Edge> edges) {
    final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
    nodes.forEach(graph::addVertex);
    edges.forEach(e -> graph.addEdge(e.getSourceNode(), e.getTargetNode(), e));
    return graph;
}
 
Example #16
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneComplexAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	LabeledEdge<String> labeledEdgeStoreCustomer1 = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer1);
	LabeledEdge<String> labeledEdgeStoreCustomer2 = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A2");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer2);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_COUNTRY);
	associationToColumnStore.put("A2", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_COUNTRY);
	associationToColumnCustomer.put("A2", COLUMN_CITY);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);

	Map<String, String> selections = new HashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "gender = 'F'");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(1, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();
	labeledEdges.add(labeledEdgeStoreCustomer1);
	labeledEdges.add(labeledEdgeStoreCustomer2);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(109, values.size());
}
 
Example #17
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessFourDatasetsThreeSimpleAssociations() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	graph.addVertex(DATASET_FOODMART_PRODUCT);
	graph.addVertex(DATASET_FOODMART_PRODUCTCLASS);
	LabeledEdge<String> labeledEdgeStoreCustomer = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer);
	LabeledEdge<String> labeledEdgeCustomerProduct = new LabeledEdge<String>(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, "A2");
	graph.addEdge(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, labeledEdgeCustomerProduct);
	LabeledEdge<String> labeledEdgeProductProductClass = new LabeledEdge<String>(DATASET_FOODMART_PRODUCT, DATASET_FOODMART_PRODUCTCLASS, "A3");
	graph.addEdge(DATASET_FOODMART_PRODUCT, DATASET_FOODMART_PRODUCTCLASS, labeledEdgeProductProductClass);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnProductClass = new HashMap<String, String>();
	associationToColumnProductClass.put("A3", COLUMN_PRODUCT_CLASS_ID);
	Map<String, String> associationToColumnProduct = new HashMap<String, String>();
	associationToColumnProduct.put("A2", COLUMN_PRODUCT_ID);
	associationToColumnProduct.put("A3", COLUMN_PRODUCT_CLASS_ID);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_CITY);
	associationToColumnCustomer.put("A2", COLUMN_CUSTOMER_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCTCLASS, associationToColumnProductClass);

	Map<String, String> selections = new HashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "account_num = '17993524670'");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(3, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();

	labeledEdges.add(labeledEdgeCustomerProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(1, values.size());
	assertTrue(values.contains("('2163')"));
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeProductProductClass);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.isEmpty());
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeStoreCustomer);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.contains("('Burnaby')"));
	labeledEdges.clear();
}
 
Example #18
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessThreeDatasetsTwoSimpleAssociationsTwoSelections() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	graph.addVertex(DATASET_FOODMART_PRODUCT);
	LabeledEdge<String> labeledEdgeStoreCustomer = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer);
	LabeledEdge<String> labeledEdgeCustomerProduct = new LabeledEdge<String>(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, "A2");
	graph.addEdge(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, labeledEdgeCustomerProduct);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnProduct = new HashMap<String, String>();
	associationToColumnProduct.put("A2", COLUMN_PRODUCT_ID);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_CITY);
	associationToColumnCustomer.put("A2", COLUMN_CUSTOMER_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);

	Map<String, String> selections = new LinkedHashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "account_num IN ('87500482201','17993524670')");
	selections.put(DATASET_FOODMART_PRODUCT, "product_id = '4'");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(2, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();

	labeledEdges.add(labeledEdgeCustomerProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(1, values.size());
	assertTrue(values.contains("('4')"));
	assertTrue(!values.contains("('2163')"));
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeStoreCustomer);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.contains("('Burnaby')"));
}
 
Example #19
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessThreeDatasetsTwoSimpleAssociations() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	graph.addVertex(DATASET_FOODMART_PRODUCT);
	LabeledEdge<String> labeledEdgeStoreCustomer = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdgeStoreCustomer);
	LabeledEdge<String> labeledEdgeCustomerProduct = new LabeledEdge<String>(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, "A2");
	graph.addEdge(DATASET_FOODMART_CUSTOMER, DATASET_FOODMART_PRODUCT, labeledEdgeCustomerProduct);

	Map<String, String> associationToColumnStore = new HashMap<String, String>();
	associationToColumnStore.put("A1", COLUMN_STORE_CITY);
	Map<String, String> associationToColumnProduct = new HashMap<String, String>();
	associationToColumnProduct.put("A2", COLUMN_PRODUCT_ID);
	Map<String, String> associationToColumnCustomer = new HashMap<String, String>();
	associationToColumnCustomer.put("A1", COLUMN_CITY);
	associationToColumnCustomer.put("A2", COLUMN_CUSTOMER_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumnStore);
	datasetToAssociations.put(DATASET_FOODMART_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumnCustomer);

	Map<String, String> selections = new HashMap<String, String>();
	selections.put(DATASET_FOODMART_CUSTOMER, "account_num IN ('87500482201','17993524670')");

	Set<String> realtimeDatasets = new HashSet<String>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>();

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(2, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();

	labeledEdges.add(labeledEdgeCustomerProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(2, values.size());
	assertTrue(values.contains("('4')"));
	assertTrue(values.contains("('2163')"));
	labeledEdges.clear();

	labeledEdges.add(labeledEdgeStoreCustomer);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));
	values = edgeGroupToValues.get(edgeGroup);
	assertTrue(values.contains("('Burnaby')"));
}
 
Example #20
Source File: AssociativeLogicManagerTests.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneSimpleAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<String, LabeledEdge<String>>(new ClassBasedEdgeFactory<String, LabeledEdge<String>>(
			(Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DATASET_FOODMART_STORE);
	graph.addVertex(DATASET_FOODMART_CUSTOMER);
	LabeledEdge<String> labeledEdge = new LabeledEdge<String>(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, "A1");
	graph.addEdge(DATASET_FOODMART_STORE, DATASET_FOODMART_CUSTOMER, labeledEdge);

	Map<String, Map<String, String>> datasetToAssociations = new HashMap<String, Map<String, String>>();
	Map<String, String> associationToColumns = new HashMap<String, String>(1);
	associationToColumns.put("A1", COLUMN_STORE_CITY);
	datasetToAssociations.put(DATASET_FOODMART_STORE, associationToColumns);
	associationToColumns = new HashMap<String, String>(1);
	associationToColumns.put("A1", COLUMN_CITY);
	datasetToAssociations.put(DATASET_FOODMART_CUSTOMER, associationToColumns);

	Map<String, String> selections = new HashMap<String, String>(1);
	selections.put(DATASET_FOODMART_CUSTOMER, "customer_id = '4'");

	Set<String> realtimeDatasets = new HashSet<String>(0);
	Map<String, Map<String, String>> datasetParameters = new HashMap<String, Map<String, String>>();
	Set<String> documents = new HashSet<String>(0);

	Map<EdgeGroup, Set<String>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections,
			realtimeDatasets, datasetParameters, documents);
	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		edgeGroupToValues = manager.process().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);

	Set<LabeledEdge<String>> labeledEdges = new HashSet<LabeledEdge<String>>();
	labeledEdges.add(labeledEdge);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<String> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(1, values.size());
	assertTrue(values.contains("('Burnaby')"));
}
 
Example #21
Source File: AssociativeLogicManagerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneComplexAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DS_STORE);
	graph.addVertex(DS_CUSTOMER);
	LabeledEdge<String> labeledEdgeStoreCustomer1 = new LabeledEdge<>(DS_STORE, DS_CUSTOMER, COL_CITY);
	graph.addEdge(DS_STORE, DS_CUSTOMER, labeledEdgeStoreCustomer1);
	LabeledEdge<String> labeledEdgeStoreCustomer2 = new LabeledEdge<>(DS_STORE, DS_CUSTOMER, COL_COUNTRY);
	graph.addEdge(DS_STORE, DS_CUSTOMER, labeledEdgeStoreCustomer2);

	Map<String, String> associationToColumnStore = new HashMap<>();
	associationToColumnStore.put(COL_CITY, COL_STORE_CITY);
	associationToColumnStore.put(COL_COUNTRY, COL_STORE_COUNTRY);
	Map<String, String> associationToColumnCustomer = new HashMap<>();
	associationToColumnCustomer.put(COL_CITY, COL_CITY);
	associationToColumnCustomer.put(COL_COUNTRY, COL_COUNTRY);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<>();
	datasetToAssociations.put(DS_STORE, associationToColumnStore);
	datasetToAssociations.put(DS_CUSTOMER, associationToColumnCustomer);

	List<SimpleFilter> selections = new ArrayList<>(1);
	IDataSet dataSet = dataSetDAO.loadDataSetByLabel(DS_CUSTOMER);
	selections.add(new InFilter(new Projection(dataSet, "gender"), "F"));

	Set<String> realtimeDatasets = new HashSet<>();
	Map<String, Map<String, String>> datasetParameters = new HashMap<>();
	Set<String> documents = new HashSet<>();

	Map<EdgeGroup, Set<Tuple>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections, realtimeDatasets,
			datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		manager.process();
		edgeGroupToValues = manager.getResult().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(1, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<>();
	labeledEdges.add(labeledEdgeStoreCustomer1);
	labeledEdges.add(labeledEdgeStoreCustomer2);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<Tuple> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(23, values.size());
}
 
Example #22
Source File: AssociativeLogicManagerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessThreeDatasetsTwoSimpleAssociations() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DS_STORE);
	graph.addVertex(DS_SALES_FACT_1997);
	graph.addVertex(DS_PRODUCT);
	LabeledEdge<String> labeledEdgeStoreSales = new LabeledEdge<>(DS_STORE, DS_SALES_FACT_1997, COL_STORE_ID);
	graph.addEdge(DS_STORE, DS_SALES_FACT_1997, labeledEdgeStoreSales);
	LabeledEdge<String> labeledEdgeSalesProduct = new LabeledEdge<>(DS_SALES_FACT_1997, DS_PRODUCT, COL_PRODUCT_ID);
	graph.addEdge(DS_SALES_FACT_1997, DS_PRODUCT, labeledEdgeSalesProduct);

	Map<String, String> associationToColumnStore = new HashMap<>();
	associationToColumnStore.put(COL_STORE_ID, COL_STORE_ID);
	Map<String, String> associationToColumnProduct = new HashMap<>();
	associationToColumnProduct.put(COL_PRODUCT_ID, COL_PRODUCT_ID);
	Map<String, String> associationToColumnSales = new HashMap<>();
	associationToColumnSales.put(COL_STORE_ID, COL_STORE_ID);
	associationToColumnSales.put(COL_PRODUCT_ID, COL_PRODUCT_ID);
	Map<String, Map<String, String>> datasetToAssociations = new HashMap<>();
	datasetToAssociations.put(DS_STORE, associationToColumnStore);
	datasetToAssociations.put(DS_PRODUCT, associationToColumnProduct);
	datasetToAssociations.put(DS_SALES_FACT_1997, associationToColumnSales);

	List<SimpleFilter> selections = new ArrayList<>(1);
	IDataSet dataSet = dataSetDAO.loadDataSetByLabel(DS_PRODUCT);
	selections.add(new InFilter(new Projection(dataSet, "brand_name"), "Queen"));

	Set<String> realtimeDatasets = new HashSet<>();
	realtimeDatasets.add(DS_STORE);
	realtimeDatasets.add(DS_SALES_FACT_1997);
	realtimeDatasets.add(DS_PRODUCT);

	Map<String, Map<String, String>> datasetParameters = new HashMap<>();
	HashMap<String, String> storeParameters = new HashMap<>();
	storeParameters.put("store_id", "100");
	datasetParameters.put(DS_STORE, storeParameters);

	Set<String> documents = new HashSet<>();

	Map<EdgeGroup, Set<Tuple>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections, realtimeDatasets,
			datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		manager.process();
		edgeGroupToValues = manager.getResult().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);
	assertEquals(2, edgeGroupToValues.size());

	Set<LabeledEdge<String>> labeledEdges = new HashSet<>();
	labeledEdges.add(labeledEdgeSalesProduct);
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<Tuple> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(2, values.size());
	assertTrue(values.contains("('41')"));
	assertTrue(values.contains("('42')"));

	labeledEdges.clear();
	labeledEdges.add(labeledEdgeStoreSales);
	edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	values = edgeGroupToValues.get(edgeGroup);
	assertEquals(22, values.size());
	assertTrue(!values.contains("('0')"));
	assertTrue(!values.contains("('2')"));
	assertTrue(!values.contains("('22')"));
}
 
Example #23
Source File: AssociativeLogicManagerTest.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void testProcessTwoDatasetsOneSimpleAssociation() {
	Pseudograph<String, LabeledEdge<String>> graph = new Pseudograph<>(
			new ClassBasedEdgeFactory<String, LabeledEdge<String>>((Class<LabeledEdge<String>>) (Object) LabeledEdge.class));
	graph.addVertex(DS_STORE);
	graph.addVertex(DS_SALES_FACT_1998);
	LabeledEdge<String> labeledEdge = new LabeledEdge<>(DS_STORE, DS_SALES_FACT_1998, COL_STORE_ID);
	graph.addEdge(DS_STORE, DS_SALES_FACT_1998, labeledEdge);

	Map<String, Map<String, String>> datasetToAssociations = new HashMap<>();
	Map<String, String> associationToColumns = new HashMap<>();
	associationToColumns.put(COL_STORE_ID, COL_STORE_ID);
	datasetToAssociations.put(DS_STORE, associationToColumns);
	datasetToAssociations.put(DS_SALES_FACT_1998, associationToColumns);

	List<SimpleFilter> selections = new ArrayList<>(1);
	IDataSet dataSet = dataSetDAO.loadDataSetByLabel(DS_STORE);
	selections.add(new InFilter(new Projection(dataSet, "store_type"), Arrays.asList(new String[]{"Small Grocery"})));

	// realtimes means that they are not cached
	Set<String> realtimeDatasets = new HashSet<>();
	realtimeDatasets.add(DS_STORE);
	realtimeDatasets.add(DS_SALES_FACT_1998);

	Map<String, Map<String, String>> datasetParameters = new HashMap<>();
	HashMap<String, String> storeParameters = new HashMap<>();
	storeParameters.put("store_id", "100");
	datasetParameters.put(DS_STORE, storeParameters);

	Set<String> documents = new HashSet<>();

	Map<EdgeGroup, Set<Tuple>> edgeGroupToValues = null;

	Config config = AssociativeLogicUtils.buildConfig(AssociativeStrategyFactory.OUTER_STRATEGY, graph, datasetToAssociations, selections, realtimeDatasets,
			datasetParameters, documents);

	try {
		OuterAssociativityManager manager = new OuterAssociativityManager(config, UserProfileManager.getProfile());
		manager.process();
		edgeGroupToValues = manager.getResult().getEdgeGroupValues();
	} catch (Exception e) {
		e.printStackTrace();
		fail(e.toString());
	}

	assertNotNull(edgeGroupToValues);

	Set<LabeledEdge<String>> labeledEdges = new HashSet<>();
	labeledEdges.add(labeledEdge);
	// edge group is the collection if relations (associations) between 2 datasets
	EdgeGroup edgeGroup = new EdgeGroup(labeledEdges);
	assertTrue(edgeGroupToValues.containsKey(edgeGroup));

	Set<Tuple> values = edgeGroupToValues.get(edgeGroup);
	assertEquals(4, values.size());
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{2}))));
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{5}))));
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{14}))));
	assertTrue(values.contains(new Tuple(Arrays.asList(new Integer[]{22}))));
}